perf: client httpx riusato per gli embedding (lazy, chiuso a shutdown)
- _get_http(): creazione lazy, keep-alive riusato tra le chiamate - lifespan shutdown: aclose del client - verificato sul server: status + search OK (istanza di test)
This commit is contained in:
+21
-8
@@ -82,6 +82,10 @@ async def lifespan(_app: FastAPI):
|
|||||||
await cleanup_task
|
await cleanup_task
|
||||||
except asyncio.CancelledError:
|
except asyncio.CancelledError:
|
||||||
pass
|
pass
|
||||||
|
global _http
|
||||||
|
if _http is not None:
|
||||||
|
await _http.aclose()
|
||||||
|
_http = None
|
||||||
|
|
||||||
|
|
||||||
app = FastAPI(title="Memory Gateway", version="2.5.0", lifespan=lifespan)
|
app = FastAPI(title="Memory Gateway", version="2.5.0", lifespan=lifespan)
|
||||||
@@ -184,16 +188,25 @@ def _parse_ts(value: Optional[str]) -> Optional[float]:
|
|||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
# Embedding via Ollama (BGE-M3)
|
# Embedding via Ollama (BGE-M3) — client httpx riusato (creato lazy, chiuso a shutdown)
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
|
_http: Optional[httpx.AsyncClient] = None
|
||||||
|
|
||||||
|
|
||||||
|
def _get_http() -> httpx.AsyncClient:
|
||||||
|
global _http
|
||||||
|
if _http is None:
|
||||||
|
_http = httpx.AsyncClient(timeout=30)
|
||||||
|
return _http
|
||||||
|
|
||||||
|
|
||||||
async def embed(text: str) -> list[float]:
|
async def embed(text: str) -> list[float]:
|
||||||
async with httpx.AsyncClient(timeout=30) as client:
|
r = await _get_http().post(
|
||||||
r = await client.post(
|
f"{OLLAMA_URL}/api/embed",
|
||||||
f"{OLLAMA_URL}/api/embed",
|
json={"model": EMBED_MODEL, "input": text},
|
||||||
json={"model": EMBED_MODEL, "input": text},
|
)
|
||||||
)
|
r.raise_for_status()
|
||||||
r.raise_for_status()
|
return r.json()["embeddings"][0]
|
||||||
return r.json()["embeddings"][0]
|
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
|
|||||||
Reference in New Issue
Block a user