Skip to content

Send a phone call from AI agent, in an API call. Or, directly call the bot from the configured phone number!

Python

quality: Make sure caching func doesn't stay forever

Clémence Lesné584b626b62fb
diff --git a/app/helpers/config_models/ai_search.py b/app/helpers/config_models/ai_search.py
index 86f3833..4b72b59 100644
--- a/app/helpers/config_models/ai_search.py
+++ b/app/helpers/config_models/ai_search.py
@@ -1,4 +1,4 @@
-from functools import cache
+from functools import lru_cache
from pydantic import BaseModel, Field
@@ -17,7 +17,7 @@ class AiSearchModel(BaseModel, frozen=True):
strictness: float = Field(default=2, ge=0, le=5)
top_n_documents: int = Field(default=5, ge=1)
- @cache
+ @lru_cache
def instance(self) -> ISearch:
from app.helpers.config import CONFIG
from app.persistence.ai_search import (
diff --git a/app/helpers/config_models/cache.py b/app/helpers/config_models/cache.py
index b8b212d..fdc3097 100644
--- a/app/helpers/config_models/cache.py
+++ b/app/helpers/config_models/cache.py
@@ -1,5 +1,5 @@
from enum import Enum
-from functools import cache
+from functools import lru_cache
from pydantic import BaseModel, Field, SecretStr, ValidationInfo, field_validator
@@ -16,7 +16,7 @@ class ModeEnum(str, Enum):
class MemoryModel(BaseModel, frozen=True):
max_size: int = Field(default=128, ge=10)
- @cache
+ @lru_cache
def instance(self) -> ICache:
from app.persistence.memory import (
MemoryCache,
@@ -32,7 +32,7 @@ class RedisModel(BaseModel, frozen=True):
port: int = 6379
ssl: bool = True
- @cache
+ @lru_cache
def instance(self) -> ICache:
from app.persistence.redis import (
RedisCache,
diff --git a/app/helpers/config_models/database.py b/app/helpers/config_models/database.py
index 26e3f95..8b7bbf3 100644
--- a/app/helpers/config_models/database.py
+++ b/app/helpers/config_models/database.py
@@ -1,4 +1,4 @@
-from functools import cache
+from functools import lru_cache
from pydantic import BaseModel
@@ -10,7 +10,7 @@ class CosmosDbModel(BaseModel, frozen=True):
database: str
endpoint: str
- @cache
+ @lru_cache
def instance(self) -> IStore:
from app.helpers.config import CONFIG
from app.persistence.cosmos_db import (
diff --git a/app/helpers/config_models/queue.py b/app/helpers/config_models/queue.py
index dcbd071..48e9f4c 100644
--- a/app/helpers/config_models/queue.py
+++ b/app/helpers/config_models/queue.py
@@ -1,4 +1,4 @@
-from functools import cache
+from functools import lru_cache
from pydantic import BaseModel
@@ -10,7 +10,7 @@ class QueueModel(BaseModel, frozen=True):
sms_name: str
training_name: str
- @cache
+ @lru_cache
def call(self):
from app.persistence.azure_queue_storage import AzureQueueStorage
@@ -19,7 +19,7 @@ class QueueModel(BaseModel, frozen=True):
name=self.call_name,
)
- @cache
+ @lru_cache
def post(self):
from app.persistence.azure_queue_storage import AzureQueueStorage
@@ -28,7 +28,7 @@ class QueueModel(BaseModel, frozen=True):
name=self.post_name,
)
- @cache
+ @lru_cache
def sms(self):
from app.persistence.azure_queue_storage import AzureQueueStorage
@@ -37,7 +37,7 @@ class QueueModel(BaseModel, frozen=True):
name=self.sms_name,
)
- @cache
+ @lru_cache
def training(self):
from app.persistence.azure_queue_storage import AzureQueueStorage
diff --git a/app/helpers/config_models/sms.py b/app/helpers/config_models/sms.py
index c8ba335..013dde5 100644
--- a/app/helpers/config_models/sms.py
+++ b/app/helpers/config_models/sms.py
@@ -1,5 +1,5 @@
from enum import Enum
-from functools import cache
+from functools import lru_cache
from pydantic import BaseModel, SecretStr, ValidationInfo, field_validator
@@ -21,7 +21,7 @@ class CommunicationServiceModel(BaseModel, frozen=True):
Model is purely empty to fit to the `ISms` interface and the "mode" enum code organization. As the Communication Services is also used as the only call interface, it is not necessary to duplicate the models.
"""
- @cache
+ @lru_cache
def instance(self) -> ISms:
from app.helpers.config import CONFIG
from app.persistence.communication_services import (
@@ -36,7 +36,7 @@ class TwilioModel(BaseModel, frozen=True):
auth_token: SecretStr
phone_number: PhoneNumber
- @cache
+ @lru_cache
def instance(self) -> ISms:
from app.persistence.twilio import (
TwilioSms,
diff --git a/app/helpers/llm_utils.py b/app/helpers/llm_utils.py
index 2a21551..807c35b 100644
--- a/app/helpers/llm_utils.py
+++ b/app/helpers/llm_utils.py
@@ -7,7 +7,7 @@ import asyncio
import inspect
import json
from collections.abc import Awaitable, Callable
-from functools import cache, wraps
+from functools import lru_cache, wraps
from inspect import getmembers, isfunction
from textwrap import dedent
from types import FunctionType
@@ -162,7 +162,7 @@ class AbstractPlugin:
# Enrich span
SpanAttributeEnum.TOOL_RESULT.attribute(tool.content)
- @cache
+ @lru_cache
def _available_functions(
self,
blacklist: frozenset[str],