fix: Flip logic in get_model_info

This commit is contained in:
Paul Gauthier (aider) 2024-08-25 07:57:20 -07:00
parent fa7d92a117
commit 5e8f9f72cc

View file

@ -452,52 +452,58 @@ class Model(ModelSettings):
self.get_weak_model(weak_model) self.get_weak_model(weak_model)
def get_model_info(self, model): def get_model_info(self, model):
if not litellm._lazy_module: if litellm._lazy_module:
cache_dir = Path.home() / ".aider" / "caches" # Do it the slow way...
cache_file = cache_dir / "model_prices_and_context_window.json"
cache_dir.mkdir(parents=True, exist_ok=True)
current_time = time.time()
cache_age = (
current_time - cache_file.stat().st_mtime if cache_file.exists() else float("inf")
)
if cache_file.exists() and cache_age < 86400: # 86400 seconds = 1 day
content = safe_read_json(cache_file)
if content:
info = content.get(model)
if info:
return info
# If cache doesn't exist or is old, fetch from GitHub
try: try:
import requests return litellm.get_model_info(model)
except Exception:
return dict()
url = ( cache_dir = Path.home() / ".aider" / "caches"
"https://raw.githubusercontent.com/BerriAI/litellm/main/" cache_file = cache_dir / "model_prices_and_context_window.json"
"model_prices_and_context_window.json" cache_dir.mkdir(parents=True, exist_ok=True)
)
response = requests.get(url, timeout=5) current_time = time.time()
if response.status_code == 200: cache_age = (
content = response.json() current_time - cache_file.stat().st_mtime if cache_file.exists() else float("inf")
safe_write_json(cache_file, content) )
if cache_file.exists() and cache_age < 86400: # 86400 seconds = 1 day
content = safe_read_json(cache_file)
if content:
info = content.get(model)
if info:
return info
# If cache doesn't exist or is old, fetch from GitHub
try:
import requests
url = (
"https://raw.githubusercontent.com/BerriAI/litellm/main/"
"model_prices_and_context_window.json"
)
response = requests.get(url, timeout=5)
if response.status_code == 200:
content = response.json()
safe_write_json(cache_file, content)
info = content.get(model)
if info:
return info
except Exception:
# If fetching from GitHub fails, fall back to local resource
try:
with importlib.resources.open_text(
"litellm", "model_prices_and_context_window_backup.json"
) as f:
content = json.load(f)
info = content.get(model) info = content.get(model)
if info: if info:
return info return info
except Exception: except Exception:
# If fetching from GitHub fails, fall back to local resource pass # If there's any error, fall back to the slow way
try:
with importlib.resources.open_text(
"litellm", "model_prices_and_context_window_backup.json"
) as f:
content = json.load(f)
info = content.get(model)
if info:
return info
except Exception:
pass # If there's any error, fall back to the slow way
# Do it the slow way... # If all else fails, do it the slow way...
try: try:
return litellm.get_model_info(model) return litellm.get_model_info(model)
except Exception: except Exception: