Skip to content

Commit f2738e2

Browse files
committed
Add TTL cache to prevent redundant API calls
1 parent 46c1a9d commit f2738e2

3 files changed

Lines changed: 44 additions & 19 deletions

File tree

poetry.lock

Lines changed: 13 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ sentence-transformers = "^4.1.0"
3838
peft = "^0.15.2"
3939
flagembedding = "^1.3.5"
4040
logfmt = "^0.4"
41+
cachetools = "^6.0.0"
4142

4243
[tool.poetry.group.dev.dependencies]
4344
pytest = "^8.3.4"
Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,41 @@
11
import httpx
22
from fastapi import HTTPException
3+
from cachetools import TTLCache
34
from ...config import get_settings
45
from ...logger import log
56

7+
_system_prompts_cache = TTLCache(maxsize=100, ttl=300) # 5 minutes TTL
8+
69
async def get_system_prompt(model: str, usage: str) -> str:
710
"""
811
Get the system prompt for the model and usage.
912
"""
10-
base_url = get_settings().PANDA_APP_SERVER_URL
11-
api_key = get_settings().PANDA_APP_SERVER_TOKEN
12-
client = httpx.AsyncClient()
13-
response = await client.get(
14-
f"{base_url}/system-prompt?model={model}&usage={usage}",
15-
headers={"X-API-Key": f"{api_key}"}
16-
)
13+
try:
14+
cached_prompts = _system_prompts_cache.get(f"prompt-{model}-{usage}")
15+
if cached_prompts is not None:
16+
log.info("Retrieved system prompts from cache")
17+
return cached_prompts
1718

18-
if response.status_code != 200:
19-
if response.status_code == 401:
20-
log.error(f"Invalid API key for system prompt")
21-
raise HTTPException(status_code=401, detail="Invalid API key")
22-
if response.status_code == 404:
23-
log.warning(f"No system prompt found for model {model} and usage {usage}, proceeding without it.")
24-
return None
25-
else:
26-
log.error(f"Failed to get system prompt for model {model} and usage {usage}", response.text)
27-
raise HTTPException(status_code=500, detail="Failed to get system prompt")
19+
base_url = get_settings().PANDA_APP_SERVER_URL
20+
api_key = get_settings().PANDA_APP_SERVER_TOKEN
21+
client = httpx.AsyncClient()
22+
response = await client.get(
23+
f"{base_url}/system-prompt?model={model}&usage={usage}",
24+
headers={"X-API-Key": f"{api_key}"}
25+
)
2826

29-
return response.json()["system_prompt"]
27+
if response.status_code != 200:
28+
if response.status_code == 401:
29+
log.error(f"Invalid API key for system prompt")
30+
raise HTTPException(status_code=401, detail="Invalid API key")
31+
if response.status_code == 404:
32+
log.warning(f"No system prompt found for model {model} and usage {usage}, proceeding without it.")
33+
return None
34+
else:
35+
log.error(f"Failed to get system prompt for model {model} and usage {usage}", response.text)
36+
raise HTTPException(status_code=500, detail="Failed to get system prompt")
37+
_system_prompts_cache[f"prompt-{model}-{usage}"] = response.json()["system_prompt"]
38+
return response.json()["system_prompt"]
39+
except Exception as e:
40+
log.error(f"Error getting system prompt for model {model} and usage {usage}: {str(e)}")
41+
raise HTTPException(status_code=500, detail=f"Error getting system prompt for model {model} and usage {usage}: {str(e)}")

0 commit comments

Comments
 (0)