Commit
·
0746311
1
Parent(s):
07be514
added dynamic model list
Browse files- app/model_loader.py +92 -0
app/model_loader.py
ADDED
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import httpx
|
2 |
+
import asyncio
|
3 |
+
import json
|
4 |
+
from typing import List, Dict, Optional, Any
|
5 |
+
|
6 |
+
# Assuming config.py is in the same directory level for Docker execution
|
7 |
+
import config as app_config
|
8 |
+
|
9 |
+
_model_cache: Optional[Dict[str, List[str]]] = None
|
10 |
+
_cache_lock = asyncio.Lock()
|
11 |
+
|
12 |
+
async def fetch_and_parse_models_config() -> Optional[Dict[str, List[str]]]:
|
13 |
+
"""
|
14 |
+
Fetches the model configuration JSON from the URL specified in app_config.
|
15 |
+
Parses it and returns a dictionary with 'vertex_models' and 'vertex_express_models'.
|
16 |
+
Returns None if fetching or parsing fails.
|
17 |
+
"""
|
18 |
+
if not app_config.MODELS_CONFIG_URL:
|
19 |
+
print("ERROR: MODELS_CONFIG_URL is not set in the environment/config.")
|
20 |
+
return None
|
21 |
+
|
22 |
+
print(f"Fetching model configuration from: {app_config.MODELS_CONFIG_URL}")
|
23 |
+
try:
|
24 |
+
async with httpx.AsyncClient() as client:
|
25 |
+
response = await client.get(app_config.MODELS_CONFIG_URL)
|
26 |
+
response.raise_for_status() # Raise an exception for HTTP errors (4xx or 5xx)
|
27 |
+
data = response.json()
|
28 |
+
|
29 |
+
# Basic validation of the fetched data structure
|
30 |
+
if isinstance(data, dict) and \
|
31 |
+
"vertex_models" in data and isinstance(data["vertex_models"], list) and \
|
32 |
+
"vertex_express_models" in data and isinstance(data["vertex_express_models"], list):
|
33 |
+
print("Successfully fetched and parsed model configuration.")
|
34 |
+
return {
|
35 |
+
"vertex_models": data["vertex_models"],
|
36 |
+
"vertex_express_models": data["vertex_express_models"]
|
37 |
+
}
|
38 |
+
else:
|
39 |
+
print(f"ERROR: Fetched model configuration has an invalid structure: {data}")
|
40 |
+
return None
|
41 |
+
except httpx.RequestError as e:
|
42 |
+
print(f"ERROR: HTTP request failed while fetching model configuration: {e}")
|
43 |
+
return None
|
44 |
+
except json.JSONDecodeError as e:
|
45 |
+
print(f"ERROR: Failed to decode JSON from model configuration: {e}")
|
46 |
+
return None
|
47 |
+
except Exception as e:
|
48 |
+
print(f"ERROR: An unexpected error occurred while fetching/parsing model configuration: {e}")
|
49 |
+
return None
|
50 |
+
|
51 |
+
async def get_models_config() -> Dict[str, List[str]]:
|
52 |
+
"""
|
53 |
+
Returns the cached model configuration.
|
54 |
+
If not cached, fetches and caches it.
|
55 |
+
Returns a default empty structure if fetching fails.
|
56 |
+
"""
|
57 |
+
global _model_cache
|
58 |
+
async with _cache_lock:
|
59 |
+
if _model_cache is None:
|
60 |
+
print("Model cache is empty. Fetching configuration...")
|
61 |
+
_model_cache = await fetch_and_parse_models_config()
|
62 |
+
if _model_cache is None: # If fetching failed, use a default empty structure
|
63 |
+
print("WARNING: Using default empty model configuration due to fetch/parse failure.")
|
64 |
+
_model_cache = {"vertex_models": [], "vertex_express_models": []}
|
65 |
+
return _model_cache
|
66 |
+
|
67 |
+
async def get_vertex_models() -> List[str]:
|
68 |
+
config = await get_models_config()
|
69 |
+
return config.get("vertex_models", [])
|
70 |
+
|
71 |
+
async def get_vertex_express_models() -> List[str]:
|
72 |
+
config = await get_models_config()
|
73 |
+
return config.get("vertex_express_models", [])
|
74 |
+
|
75 |
+
async def refresh_models_config_cache() -> bool:
|
76 |
+
"""
|
77 |
+
Forces a refresh of the model configuration cache.
|
78 |
+
Returns True if successful, False otherwise.
|
79 |
+
"""
|
80 |
+
global _model_cache
|
81 |
+
print("Attempting to refresh model configuration cache...")
|
82 |
+
async with _cache_lock:
|
83 |
+
new_config = await fetch_and_parse_models_config()
|
84 |
+
if new_config is not None:
|
85 |
+
_model_cache = new_config
|
86 |
+
print("Model configuration cache refreshed successfully.")
|
87 |
+
return True
|
88 |
+
else:
|
89 |
+
print("ERROR: Failed to refresh model configuration cache.")
|
90 |
+
# Optionally, decide if we want to clear the old cache or keep it
|
91 |
+
# _model_cache = {"vertex_models": [], "vertex_express_models": []} # To clear
|
92 |
+
return False
|