Speed up: preload BM25 at startup, httpx timeout+limits, fix lambda in to_thread
- index: lifespan preloads BM25 model so first /sparse_embedding request doesn't pay cold-start cost (~1-2s per worker) - search: same BM25 preload + httpx timeout=30s + connection limits to avoid hanging on slow external APIs - search: asyncio.to_thread(fn, arg) instead of lambda wrapper Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
775c874399
commit
57a5229c95
2 changed files with 13 additions and 7 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
from contextlib import asynccontextmanager
|
||||||
from functools import lru_cache
|
from functools import lru_cache
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
|
|
@ -189,7 +190,13 @@ def build_chunks(
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
app = FastAPI(title="Index Service", version="0.1.0")
|
@asynccontextmanager
|
||||||
|
async def lifespan(app: FastAPI):
|
||||||
|
await asyncio.to_thread(get_sparse_model)
|
||||||
|
yield
|
||||||
|
|
||||||
|
|
||||||
|
app = FastAPI(title="Index Service", version="0.1.0", lifespan=lifespan)
|
||||||
|
|
||||||
|
|
||||||
@app.get("/health")
|
@app.get("/health")
|
||||||
|
|
|
||||||
|
|
@ -148,11 +148,10 @@ def get_sparse_model() -> SparseTextEmbedding:
|
||||||
|
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def lifespan(app: FastAPI):
|
async def lifespan(app: FastAPI):
|
||||||
app.state.http = httpx.AsyncClient()
|
await asyncio.to_thread(get_sparse_model)
|
||||||
app.state.qdrant = AsyncQdrantClient(
|
limits = httpx.Limits(max_connections=100, max_keepalive_connections=20)
|
||||||
url=QDRANT_URL,
|
app.state.http = httpx.AsyncClient(timeout=30.0, limits=limits)
|
||||||
api_key=API_KEY,
|
app.state.qdrant = AsyncQdrantClient(url=QDRANT_URL, api_key=API_KEY)
|
||||||
)
|
|
||||||
try:
|
try:
|
||||||
yield
|
yield
|
||||||
finally:
|
finally:
|
||||||
|
|
@ -354,7 +353,7 @@ async def search(payload: SearchAPIRequest) -> SearchAPIResponse:
|
||||||
sparse_query = build_sparse_query(question)
|
sparse_query = build_sparse_query(question)
|
||||||
|
|
||||||
dense_task = embed_dense(client, dense_query)
|
dense_task = embed_dense(client, dense_query)
|
||||||
sparse_task = asyncio.to_thread(lambda: embed_sparse_sync(sparse_query))
|
sparse_task = asyncio.to_thread(embed_sparse_sync, sparse_query)
|
||||||
dense_vector, sparse_vector = await asyncio.gather(dense_task, sparse_task)
|
dense_vector, sparse_vector = await asyncio.gather(dense_task, sparse_task)
|
||||||
|
|
||||||
dense_vectors = [dense_vector]
|
dense_vectors = [dense_vector]
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue