feat: add OpenAI-compatible and Gemini API embedding support#427
Open
feat: add OpenAI-compatible and Gemini API embedding support#427
Conversation
Adds external embedding API support via three environment variables: - QMD_EMBED_API_URL: base URL (presence activates API mode) - QMD_EMBED_API_KEY: API key - QMD_EMBED_API_MODEL: model name API type is auto-detected from URL: - Contains "googleapis.com" → Gemini (batchEmbedContents) - Otherwise → OpenAI-compatible format (Ollama, LM Studio, OpenAI, etc.) In API mode, formatQueryForEmbedding/formatDocForEmbedding return raw text without task prefixes, since cloud embedding models don't use them. The model URI is stored as "api:<model-name>" in the vectors table. Also adds scripts/benchmark-embed.ts comparing embeddinggemma-300M (local/Metal), text-embedding-3-small/large, gemini-embedding-001, and gemini-embedding-2-preview on 63 Korean+English chunks.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds external embedding API support as an alternative to local GGUF models. Three environment variables activate API mode:
API type is auto-detected from the URL — no extra config needed:
Usage
```bash
OpenAI
export QMD_EMBED_API_URL="https://api.openai.com/v1"
export QMD_EMBED_API_KEY="sk-..."
export QMD_EMBED_API_MODEL="text-embedding-3-small"
qmd embed && qmd vsearch "test query"
Gemini (free tier)
export QMD_EMBED_API_URL="https://generativelanguage.googleapis.com/v1beta"
export QMD_EMBED_API_KEY="AIza..."
export QMD_EMBED_API_MODEL="gemini-embedding-001"
qmd embed && qmd vsearch "test query"
Ollama (OpenAI-compatible)
export QMD_EMBED_API_URL="http://localhost:11434/v1"
export QMD_EMBED_API_MODEL="nomic-embed-text"
qmd embed && qmd vsearch "test query"
```
Why
Local GGUF embedding is great for privacy and offline use, but has tradeoffs:
API mode is useful when:
Implementation notes
Benchmark (63 chunks, Korean+English, M3 Max)
Full benchmark results + script in `docs/embedding-benchmark.md` and `scripts/benchmark-embed.ts`.
Relation to other PRs
#406 and #415 also address remote embedding — worth noting the differences:
Happy to collaborate with the other authors or adjust the approach based on feedback.