feat: backend embedding configurabile — EMBED_API=ollama|llamacpp
- llama.cpp: /v1/embeddings OpenAI-compatible con Bearer key (EMBED_API_KEY) - ollama (default): /api/embed invariato - EMBED_URL sostituisce OLLAMA_URL (alias retrocompatibile)
This commit is contained in:
+18
-2
@@ -43,8 +43,11 @@ from qdrant_client.http import models as qm
|
||||
# ---------------------------------------------------------------------------
|
||||
QDRANT_URL = os.environ.get("QDRANT_URL", "http://127.0.0.1:6333")
|
||||
QDRANT_API_KEY = os.environ.get("QDRANT_API_KEY", "")
|
||||
OLLAMA_URL = os.environ.get("OLLAMA_URL", "http://127.0.0.1:11434")
|
||||
# Backend embedding: ollama (default) | llamacpp (OpenAI-compatible /v1/embeddings)
|
||||
EMBED_API = os.environ.get("EMBED_API", "ollama")
|
||||
EMBED_URL = os.environ.get("EMBED_URL", os.environ.get("OLLAMA_URL", "http://127.0.0.1:11434"))
|
||||
EMBED_MODEL = os.environ.get("EMBED_MODEL", "bge-m3")
|
||||
EMBED_API_KEY = os.environ.get("EMBED_API_KEY", "")
|
||||
EMBED_DIM = int(os.environ.get("EMBED_DIM", "1024"))
|
||||
COLLECTION = os.environ.get("COLLECTION", "memories")
|
||||
# Chiavi condivise (separate da virgola): accesso completo in lettura/scrittura
|
||||
@@ -392,8 +395,21 @@ def _get_http() -> httpx.AsyncClient:
|
||||
|
||||
|
||||
async def embed(text: str) -> list[float]:
|
||||
if EMBED_API == "llamacpp":
|
||||
# llama.cpp: OpenAI-compatible /v1/embeddings
|
||||
headers = {"Content-Type": "application/json"}
|
||||
if EMBED_API_KEY:
|
||||
headers["Authorization"] = f"Bearer {EMBED_API_KEY}"
|
||||
r = await _get_http().post(
|
||||
f"{EMBED_URL}/v1/embeddings",
|
||||
json={"model": EMBED_MODEL, "input": text},
|
||||
headers=headers,
|
||||
)
|
||||
r.raise_for_status()
|
||||
return r.json()["data"][0]["embedding"]
|
||||
# Ollama (default)
|
||||
r = await _get_http().post(
|
||||
f"{OLLAMA_URL}/api/embed",
|
||||
f"{EMBED_URL}/api/embed",
|
||||
json={"model": EMBED_MODEL, "input": text},
|
||||
)
|
||||
r.raise_for_status()
|
||||
|
||||
Reference in New Issue
Block a user