made querying async

This commit is contained in:
bolade
2025-10-28 21:09:47 +01:00
parent bb03f6ade4
commit 02c8bb816f
7 changed files with 27 additions and 14 deletions
+12 -4
View File
@@ -1,3 +1,4 @@
import asyncio
import hashlib
import logging
import os
@@ -116,11 +117,18 @@ Return ONLY the SQL query, no explanations or markdown.""",
"""Generate cache key from normalized question."""
return hashlib.md5(question.lower().strip().encode()).hexdigest()
def process_query(self, question: str) -> PaginatedResponse[CompanyData]:
"""Process a query by generating and executing SQL directly.
# synchronous helper is provided below as `_process_query_sync` and an
# async wrapper `process_query` runs it in a thread. This keeps the
# FastAPI event loop non-blocking while reusing the existing sync code.
async def process_query(self, question: str) -> PaginatedResponse[CompanyData]:
"""Async wrapper for process_query. Runs blocking work in a thread to avoid
blocking the event loop.
"""
return await asyncio.to_thread(self._process_query_sync, question)
Args:
question: The natural language query to process
def _process_query_sync(self, question: str) -> PaginatedResponse[CompanyData]:
"""Synchronous implementation of process_query. This is run in a thread by
the async wrapper above.
"""
cache_key = self._get_cache_key(question)