Send a phone call from AI agent, in an API call. Or, directly call the bot from the configured phone number!
Python
perf: Fix OpenAI and Content Safety memory leak with async calls
Clémence Lesnédb93d3b8edfd
diff --git a/helpers/llm.py b/helpers/llm.pyindex 83307d4..00be314 100644--- a/helpers/llm.py+++ b/helpers/llm.py@@ -9,6 +9,7 @@ from azure.core.credentials import AzureKeyCredentialfrom azure.core.exceptions import HttpResponseErrorfrom azure.identity import DefaultAzureCredential, get_bearer_token_providerfrom helpers.config import CONFIG+from contextlib import asynccontextmanagerfrom helpers.logging import build_loggerfrom openai import (AsyncAzureOpenAI,@@ -35,29 +36,8 @@ import asyncio_logger = build_logger(__name__)-_logger.info(f"Using OpenAI GPT model {CONFIG.openai.gpt_model}")-_oai = AsyncAzureOpenAI(- api_version="2023-12-01-preview",- azure_deployment=CONFIG.openai.gpt_deployment,- azure_endpoint=CONFIG.openai.endpoint,- # Authentication, either RBAC or API key- api_key=CONFIG.openai.api_key.get_secret_value() if CONFIG.openai.api_key else None,- azure_ad_token_provider=(- get_bearer_token_provider(- DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"- )- if not CONFIG.openai.api_key- else None- ),-)-_logger.info(f"Using Content Safety {CONFIG.content_safety.endpoint}")-_contentsafety = ContentSafetyClient(- credential=AzureKeyCredential(CONFIG.content_safety.access_key.get_secret_value()),- endpoint=CONFIG.content_safety.endpoint,-)-ModelType = TypeVar("ModelType", bound=BaseModel)@@ -97,17 +77,18 @@ async def completion_stream(if tools:extra["tools"] = tools- stream: AsyncStream[ChatCompletionChunk] = await _oai.chat.completions.create(- max_tokens=max_tokens,- messages=messages,- model=CONFIG.openai.gpt_model,- stream=True,- temperature=0, # Most focused and deterministic- **extra,- )- async for chunck in stream:- if chunck.choices: # Skip empty chunks, happens with GPT-4 Turbo- yield chunck.choices[0].delta+ async with _use_oai() as client:+ stream: AsyncStream[ChatCompletionChunk] = await client.chat.completions.create(+ max_tokens=max_tokens,+ messages=messages,+ model=CONFIG.openai.gpt_model,+ stream=True,+ temperature=0, # Most focused and deterministic+ **extra,+ )+ async for chunck in stream:+ if chunck.choices: # Skip empty chunks, happens with GPT-4 Turbo+ yield chunck.choices[0].delta@retry(@@ -125,7 +106,7 @@ async def completion_sync(messages: List[ChatCompletionMessageParam],max_tokens: int,json_output: bool = False,-) -> str:+) -> Optional[str]:"""Returns a completion result.@@ -136,18 +117,22 @@ async def completion_sync(if json_output:extra["response_format"] = {"type": "json_object"}- res = await _oai.chat.completions.create(- max_tokens=max_tokens,- messages=messages,- model=CONFIG.openai.gpt_model,- temperature=0, # Most focused and deterministic- **extra,- )- content = res.choices[0].message.content+ content = None+ async with _use_oai() as client:+ res = await client.chat.completions.create(+ max_tokens=max_tokens,+ messages=messages,+ model=CONFIG.openai.gpt_model,+ temperature=0, # Most focused and deterministic+ **extra,+ )+ content = res.choices[0].message.content++ if not content:+ return Noneif not json_output:await safety_check(content)-return content@@ -161,13 +146,15 @@ async def completion_model_sync(messages: List[ChatCompletionMessageParam],max_tokens: int,model: Type[ModelType],-) -> ModelType:+) -> Optional[ModelType]:"""Returns an object validated against a given model, from a completion result.Catch errors for a maximum of 3 times, but not `SafetyCheckError`."""res = await completion_sync(messages, max_tokens, json_output=True)+ if not res:+ return Nonereturn model.model_validate_json(res)@@ -225,13 +212,6 @@ async def safety_check(text: str) -> None:)-async def close() -> None:- """- Safely close the OpenAI and Content Safety clients.- """- await asyncio.gather(_oai.close(), _contentsafety.close())--@retry(reraise=True,retry=retry_if_exception_type(HttpResponseError),@@ -239,14 +219,15 @@ async def close() -> None:wait=wait_random_exponential(multiplier=0.5, max=30),)async def _contentsafety_analysis(text: str) -> AnalyzeTextResult:- return await _contentsafety.analyze_text(- AnalyzeTextOptions(- blocklist_names=CONFIG.content_safety.blocklists,- halt_on_blocklist_hit=False,- output_type="EightSeverityLevels",- text=text,+ async with _use_contentsafety() as client:+ return await client.analyze_text(+ AnalyzeTextOptions(+ blocklist_names=CONFIG.content_safety.blocklists,+ halt_on_blocklist_hit=False,+ output_type="EightSeverityLevels",+ text=text,+ ))- )def _contentsafety_category_test(@@ -266,3 +247,37 @@ def _contentsafety_category_test(_logger.debug(f"Matched {category} with severity {detection.severity}")return Falsereturn True+++@asynccontextmanager+async def _use_oai() -> AsyncGenerator[AsyncAzureOpenAI, None]:+ client = AsyncAzureOpenAI(+ api_version="2023-12-01-preview",+ azure_deployment=CONFIG.openai.gpt_deployment,+ azure_endpoint=CONFIG.openai.endpoint,+ # Authentication, either RBAC or API key+ api_key=(+ CONFIG.openai.api_key.get_secret_value() if CONFIG.openai.api_key else None+ ),+ azure_ad_token_provider=(+ get_bearer_token_provider(+ DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"+ )+ if not CONFIG.openai.api_key+ else None+ ),+ )+ yield client+ await client.close()+++@asynccontextmanager+async def _use_contentsafety() -> AsyncGenerator[ContentSafetyClient, None]:+ client = ContentSafetyClient(+ credential=AzureKeyCredential(+ CONFIG.content_safety.access_key.get_secret_value()+ ),+ endpoint=CONFIG.content_safety.endpoint,+ )+ yield client+ await client.close()diff --git a/main.py b/main.pyindex 3190083..3e44641 100644--- a/main.py+++ b/main.py@@ -59,8 +59,14 @@ from models.message import (StyleEnum as MessageStyle,ToolModel as MessageToolModel,)-from contextlib import asynccontextmanager-from helpers.llm import completion_stream, completion_sync, safety_check, close as llm_close, completion_model_sync, ModelType, SafetyCheckError+from helpers.llm import (+ completion_stream,+ completion_sync,+ safety_check,+ completion_model_sync,+ ModelType,+ SafetyCheckError,+)from models.claim import ClaimModelfrom pydantic import ValidationErrorfrom uuid import UUID@@ -100,13 +106,6 @@ 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(@@ -118,7 +117,6 @@ 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,