Send a phone call from AI agent, in an API call. Or, directly call the bot from the configured phone number!
Python
perf: Use Redis cache for translations
Clémence Lesné84ddf82e39f3
diff --git a/helpers/translation.py b/helpers/translation.pyindex d070149..04066e6 100644--- a/helpers/translation.py+++ b/helpers/translation.py@@ -1,10 +1,10 @@from azure.ai.translation.text.aio import TextTranslationClient-from azure.ai.translation.text.models import InputTextItem+from azure.ai.translation.text.models import InputTextItem, TranslatedTextItemfrom azure.core.credentials import AzureKeyCredentialfrom contextlib import asynccontextmanagerfrom helpers.config import CONFIGfrom helpers.logging import build_logger-from typing import AsyncGenerator, Optional+from typing import AsyncGenerator, List, Optionalfrom azure.core.exceptions import HttpResponseErrorfrom tenacity import (retry,@@ -16,7 +16,7 @@ from tenacity import (_logger = build_logger(__name__)_logger.info(f"Using Translation {CONFIG.ai_translation.endpoint}")-_cache = {} # Local cache for translations, TODO: Use Redis+_cache = CONFIG.cache.instance()@retry(@@ -36,20 +36,28 @@ async def translate_text(if source_lang == target_lang: # No need to translatereturn text- cache_key = (text, source_lang, target_lang)- if cache_key in _cache: # Search in cache- return _cache[cache_key]+ # Try cache+ cache_key = f"{__name__}:translate_text:{text}:{source_lang}:{target_lang}"+ cached = await _cache.aget(cache_key)+ if cached:+ return cached.decode("utf-8")+ # Try live+ translation: Optional[str] = Noneasync with _use_client() as client: # Perform translation- res = await client.translate(+ res: List[TranslatedTextItem] = await client.translate(content=[InputTextItem(text=text)], # type: ignorefrom_parameter=source_lang,to=[target_lang],)- translation = res[0] if res else None- translation = translation.translations[0].text if translation else None- _cache[cache_key] = translation # Add to cache- return translation+ translation = (+ res[0].translations[0].text if res and res[0].translations else None+ )++ # Update cache+ await _cache.aset(cache_key, translation)++ return translation@asynccontextmanager