diff --git a/index/main.py b/index/main.py index 3609ee8..df4575a 100644 --- a/index/main.py +++ b/index/main.py @@ -1,6 +1,7 @@ import asyncio import logging import os +from contextlib import asynccontextmanager from functools import lru_cache from typing import Any @@ -189,7 +190,13 @@ def build_chunks( 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") diff --git a/search/main.py b/search/main.py index 763658d..a55a466 100644 --- a/search/main.py +++ b/search/main.py @@ -148,11 +148,10 @@ def get_sparse_model() -> SparseTextEmbedding: @asynccontextmanager async def lifespan(app: FastAPI): - app.state.http = httpx.AsyncClient() - app.state.qdrant = AsyncQdrantClient( - url=QDRANT_URL, - api_key=API_KEY, - ) + await asyncio.to_thread(get_sparse_model) + limits = httpx.Limits(max_connections=100, max_keepalive_connections=20) + app.state.http = httpx.AsyncClient(timeout=30.0, limits=limits) + app.state.qdrant = AsyncQdrantClient(url=QDRANT_URL, api_key=API_KEY) try: yield finally: @@ -354,7 +353,7 @@ async def search(payload: SearchAPIRequest) -> SearchAPIResponse: sparse_query = build_sparse_query(question) 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_vectors = [dense_vector]