Skip to content

Commit e3b2b62

Browse files
thread and cache
1 parent 6f5044c commit e3b2b62

2 files changed

Lines changed: 18 additions & 18 deletions

File tree

‎src/app/core/lifespan.py‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
from contextlib import asynccontextmanager
22

33
from fastapi import FastAPI
4+
from qdrant_client import AsyncQdrantClient
45

5-
from src.app.services.search import close_qdrant, init_qdrant
6+
from src.app.api.dependencies import get_settings
67

78

89
@asynccontextmanager
910
async def lifespan(app: FastAPI):
10-
await init_qdrant()
11+
settings = get_settings()
12+
app.state.qdrant = AsyncQdrantClient(
13+
url=settings.QDRANT_HOST,
14+
port=settings.QDRANT_PORT,
15+
timeout=100,
16+
)
1117
yield
12-
await close_qdrant()
18+
await app.state.qdrant.close()

‎src/app/services/search.py‎

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import time
2-
from functools import cache, lru_cache
2+
from functools import cache
33
from typing import Tuple, cast
44

55
import numpy as np
6-
from fastapi import Depends
6+
from fastapi import Depends, Request
7+
from fastapi.concurrency import run_in_threadpool
78
from numpy import ndarray
8-
from psycopg import Error
99
from qdrant_client import AsyncQdrantClient
1010
from qdrant_client import models as qdrant_models
11-
from qdrant_client import qdrant_client
1211
from qdrant_client.http import exceptions as qdrant_exceptions
1312
from qdrant_client.http import models as http_models
1413
from sentence_transformers import SentenceTransformer
@@ -49,12 +48,8 @@ async def close_qdrant():
4948
await _qdrant_client.close()
5049

5150

52-
@lru_cache(maxsize=1)
53-
async def get_qdrant() -> AsyncQdrantClient | None:
54-
if qdrant_client is None:
55-
raise Error()
56-
57-
return _qdrant_client
51+
async def get_qdrant(request: Request) -> AsyncQdrantClient:
52+
return request.app.state.qdrant
5853

5954

6055
class SearchService:
@@ -150,7 +145,7 @@ def get_query_embed(
150145

151146
return embedding
152147

153-
@lru_cache(maxsize=1)
148+
@cache
154149
@log_time_and_error_sync
155150
def _get_model(self, curr_model: str) -> tuple[int | None, SentenceTransformer]:
156151
try:
@@ -168,7 +163,6 @@ def _get_model(self, curr_model: str) -> tuple[int | None, SentenceTransformer]:
168163
raise ModelNotFoundError()
169164
return (model.get_max_seq_length(), model)
170165

171-
@cache
172166
@log_time_and_error_sync
173167
def _split_input_seq_len(self, seq_len: int, input: str) -> list[str]:
174168
if not seq_len:
@@ -192,7 +186,6 @@ def _split_input_seq_len(self, seq_len: int, input: str) -> list[str]:
192186

193187
return inputs
194188

195-
@cache
196189
@log_time_and_error_sync
197190
def _embed_query(self, search_input: str, curr_model: str) -> np.ndarray:
198191
logger.debug("Creating embeddings model=%s", curr_model)
@@ -223,10 +216,11 @@ async def search_handler(
223216

224217
collection = await self.get_collection_by_language(lang="mul")
225218
subject_vector = get_subject_vector(qp.subject)
226-
embedding = self.get_query_embed(
219+
embedding = await run_in_threadpool(
220+
self.get_query_embed,
227221
model=collection.model,
228-
subject_vector=subject_vector,
229222
query=qp.query,
223+
subject_vector=subject_vector,
230224
subject_influence_factor=qp.influence_factor,
231225
)
232226

0 commit comments

Comments
 (0)