feat(semantic_search): support local OpenAI-compatible embedding servers (#22)

Adds a LocalEmbedder that targets any OpenAI-compatible embedding
endpoint (Ollama, llama.cpp, vLLM, LM Studio, ...). Zero new
Python dependencies — reuses the existing openai SDK with a
custom base_url. Defaults to Ollama at http://localhost:11434/v1
with nomic-embed-text @ 768 dims; override via env vars for other
servers/models (e.g. bge-m3 @ 1024 dims for better Turkish).

Refactors the shared encode/similarity logic into a private base
class so OpenRouterEmbedder and LocalEmbedder don't duplicate ~50
lines. OpenRouter keeps its ranking headers; local sends none.

Adds get_embedder() factory selecting the provider based on
EMBEDDING_PROVIDER (local) or OPENROUTER_API_KEY presence, and
is_semantic_search_available() that returns True for either path.
mcp_server_main now uses these so the semantic_search tool is
exposed when only a local server is configured.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
saidsurucu
2026-05-03 01:50:47 +03:00
co-authored by Claude Opus 4.7
parent 42731a2c03
commit fb29146755
4 changed files with 227 additions and 95 deletions
+17 -4
View File
@@ -74,18 +74,31 @@ JWT_SECRET_KEY=your_jwt_secret_key_here
# SEMANTIC SEARCH SETTINGS (Optional)
# =============================================================================
# OpenRouter API Key for semantic search functionality
# Embedding provider for the semantic_search tool.
# Pick exactly one of: OpenRouter (hosted) or Local (your own server).
# --- Option A: OpenRouter (hosted, default) -----------------------------------
# Get your API key from: https://openrouter.ai/keys
# If not set, semantic search tool will be disabled
# If neither this nor EMBEDDING_PROVIDER=local is set, semantic search is off.
OPENROUTER_API_KEY=sk-or-v1-your_openrouter_api_key_here
# Optional: override the embedding model and dimension.
# Optional: override the OpenRouter embedding model and dimension.
# Defaults: google/gemini-embedding-001 at 3072 dims (paid on OpenRouter).
# Pick any embedding model from https://openrouter.ai/models?modality=embedding
# Pick any model from https://openrouter.ai/models?modality=embedding
# and set the dimension to that model's output size — they must match.
# OPENROUTER_EMBEDDING_MODEL=google/gemini-embedding-001
# OPENROUTER_EMBEDDING_DIMENSION=3072
# --- Option B: Local OpenAI-compatible server (Ollama / llama.cpp / vLLM) -----
# Uncomment to use your own server instead of OpenRouter (no API key required).
# Defaults target Ollama with nomic-embed-text. For Turkish, bge-m3 (1024 dims)
# tends to work better — pull it with: `ollama pull bge-m3`
# EMBEDDING_PROVIDER=local
# LOCAL_EMBEDDING_BASE_URL=http://localhost:11434/v1
# LOCAL_EMBEDDING_MODEL=nomic-embed-text
# LOCAL_EMBEDDING_DIMENSION=768
# LOCAL_EMBEDDING_API_KEY= # most local servers ignore this
# =============================================================================
# USAGE INSTRUCTIONS
# =============================================================================