|
1 | 1 | import httpx |
2 | 2 | from fastapi import HTTPException |
| 3 | +from cachetools import TTLCache |
3 | 4 | from ...config import get_settings |
4 | 5 | from ...logger import log |
5 | 6 |
|
| 7 | +_system_prompts_cache = TTLCache(maxsize=100, ttl=300) # 5 minutes TTL |
| 8 | + |
6 | 9 | async def get_system_prompt(model: str, usage: str) -> str: |
7 | 10 | """ |
8 | 11 | Get the system prompt for the model and usage. |
9 | 12 | """ |
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 |
17 | 18 |
|
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 | + ) |
28 | 26 |
|
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