Send a phone call from AI agent, in an API call. Or, directly call the bot from the configured phone number!
Python
fix: Catch transitive Azure errors
Clémence Lesné8d1508d2d2e7
diff --git a/persistence/ai_search.py b/persistence/ai_search.pyindex 6fa9cd1..58990cf 100644--- a/persistence/ai_search.py+++ b/persistence/ai_search.py@@ -1,5 +1,9 @@from azure.core.credentials import AzureKeyCredential-from azure.core.exceptions import HttpResponseError, ServiceRequestError+from azure.core.exceptions import (+ HttpResponseError,+ ServiceRequestError,+ ServiceResponseError,+)from azure.search.documents.aio import SearchClientfrom azure.search.documents.models import VectorizableTextQueryfrom contextlib import asynccontextmanager@@ -13,6 +17,12 @@ from persistence.isearch import ISearchfrom pydantic import TypeAdapterfrom pydantic import ValidationErrorfrom typing import AsyncGenerator, List, Optional+from tenacity import (+ retry,+ stop_after_attempt,+ wait_random_exponential,+ retry_if_exception_type,+)_logger = build_logger(__name__)@@ -29,6 +39,12 @@ class AiSearchSearch(ISearch):self._config = configsuper().__init__(cache)+ @retry(+ reraise=True,+ retry=retry_if_exception_type(ServiceResponseError),+ stop=stop_after_attempt(3),+ wait=wait_random_exponential(multiplier=0.5, max=30),+ )async def training_asearch_all(self, text: str, call: CallModel) -> Optional[List[TrainingModel]]:diff --git a/persistence/cosmos.py b/persistence/cosmos.pyindex 52abdd8..c030245 100644--- a/persistence/cosmos.py+++ b/persistence/cosmos.py@@ -1,3 +1,4 @@+from azure.core.exceptions import ServiceResponseErrorfrom azure.cosmos.aio import CosmosClient, ContainerProxyfrom azure.cosmos.exceptions import CosmosHttpResponseErrorfrom contextlib import asynccontextmanager@@ -11,6 +12,12 @@ from persistence.istore import IStorefrom pydantic import ValidationErrorfrom typing import AsyncGenerator, List, Optionalfrom uuid import UUID+from tenacity import (+ retry,+ stop_after_attempt,+ wait_random_exponential,+ retry_if_exception_type,+)_logger = build_logger(__name__)@@ -23,6 +30,12 @@ class CosmosStore(IStore):_logger.info(f"Using Cosmos DB {config.database}/{config.container}")self._config = config+ @retry(+ reraise=True,+ retry=retry_if_exception_type(ServiceResponseError),+ stop=stop_after_attempt(3),+ wait=wait_random_exponential(multiplier=0.5, max=30),+ )async def call_aget(self, call_id: UUID) -> Optional[CallModel]:_logger.debug(f"Loading call {call_id}")res = None@@ -43,6 +56,12 @@ class CosmosStore(IStore):_logger.error(f"Error accessing CosmosDB, {e}")return res+ @retry(+ reraise=True,+ retry=retry_if_exception_type(ServiceResponseError),+ stop=stop_after_attempt(3),+ wait=wait_random_exponential(multiplier=0.5, max=30),+ )async def call_aset(self, call: CallModel) -> bool:data = jsonable_encoder(call.model_dump(), exclude_none=True)data["id"] = str(call.call_id) # CosmosDB requires an id field@@ -55,6 +74,12 @@ class CosmosStore(IStore):_logger.error(f"Error accessing CosmosDB: {e}")return False+ @retry(+ reraise=True,+ retry=retry_if_exception_type(ServiceResponseError),+ stop=stop_after_attempt(3),+ wait=wait_random_exponential(multiplier=0.5, max=30),+ )async def call_asearch_one(self, phone_number: str) -> Optional[CallModel]:_logger.debug(f"Loading last call for {phone_number}")res = None@@ -90,6 +115,12 @@ class CosmosStore(IStore):_logger.error(f"Error accessing CosmosDB: {e}")return res+ @retry(+ reraise=True,+ retry=retry_if_exception_type(ServiceResponseError),+ stop=stop_after_attempt(3),+ wait=wait_random_exponential(multiplier=0.5, max=30),+ )async def call_asearch_all(self, phone_number: str) -> Optional[List[CallModel]]:_logger.debug(f"Loading all calls for {phone_number}")calls = []