Send a phone call from AI agent, in an API call. Or, directly call the bot from the configured phone number!
Python
fix: Properly close async connexions
Clémence Lesné6c8ea0574fd8
diff --git a/helpers/llm.py b/helpers/llm.pyindex 4c989ae..f6ad95a 100644--- a/helpers/llm.py+++ b/helpers/llm.py@@ -18,6 +18,7 @@ from openai.types.chat import (ChatCompletionMessageParam,ChatCompletionToolParam,)+import asynciofrom openai.types.chat.chat_completion_chunk import ChoiceDeltafrom tenacity import retry, stop_after_attempt, wait_random_exponentialfrom typing import Any, AsyncGenerator, List, Optional@@ -123,6 +124,13 @@ async def safety_check(text: str) -> bool:return safety+async def close() -> None:+ """+ Safely close the OpenAI and Content Safety clients.+ """+ await asyncio.gather(_oai.close(), _contentsafety.close())++@retry(stop=stop_after_attempt(3), wait=wait_random_exponential(multiplier=0.5, max=30))async def _contentsafety_analysis(text: str) -> AnalyzeTextResult:return await _contentsafety.analyze_text(diff --git a/main.py b/main.pyindex 1f65727..7f97427 100644--- a/main.py+++ b/main.py@@ -55,6 +55,8 @@ from models.message import (Persona as MessagePersona,ToolModel as MessageToolModel,)+from contextlib import asynccontextmanager+from helpers.llm import completion_stream, completion_sync, safety_check, close as llm_closefrom models.claim import ClaimModelfrom pydantic import ValidationErrorfrom uuid import UUID@@ -94,6 +96,13 @@ db = ()search = AiSearchSearch(CONFIG.ai_search)++@asynccontextmanager+async def lifespan(_: FastAPI):+ yield+ await llm_close()++# FastAPI_logger.info(f'Using root path "{CONFIG.api.root_path}"')api = FastAPI(@@ -105,6 +114,7 @@ api = FastAPI("name": "Apache-2.0","url": "https://github.com/clemlesne/claim-ai-phone-bot/blob/master/LICENCE",},+ lifespan=lifespan,root_path=CONFIG.api.root_path,title="claim-ai-phone-bot",version=CONFIG.version,diff --git a/persistence/ai_search.py b/persistence/ai_search.pyindex ec39a9c..c67118f 100644--- a/persistence/ai_search.py+++ b/persistence/ai_search.py@@ -67,9 +67,10 @@ class AiSearchSearch(ISearch):@asynccontextmanagerasync def _use_db(self) -> AsyncGenerator[SearchClient, None]:- async with SearchClient(+ db = SearchClient(credential=AzureKeyCredential(self._config.access_key.get_secret_value()),endpoint=self._config.endpoint,index_name=self._config.index,- ) as db:- yield db+ )+ yield db+ await db.close()diff --git a/persistence/cosmos.py b/persistence/cosmos.pyindex a8d7b7f..7e573fb 100644--- a/persistence/cosmos.py+++ b/persistence/cosmos.py@@ -113,10 +113,11 @@ class CosmosStore(IStore):@asynccontextmanagerasync def _use_db(self) -> AsyncGenerator[ContainerProxy, None]:- async with CosmosClient(+ client = CosmosClient(connection_timeout=5,credential=self._config.access_key.get_secret_value(),url=self._config.endpoint,- ) as client:- database = client.get_database_client(self._config.database)- yield database.get_container_client(self._config.container)+ )+ database = client.get_database_client(self._config.database)+ yield database.get_container_client(self._config.container)+ await client.close()