From 5e8f9f72cc80ccf35f0dc653bded88384aa86233 Mon Sep 17 00:00:00 2001 From: "Paul Gauthier (aider)" Date: Sun, 25 Aug 2024 07:57:20 -0700 Subject: [PATCH] fix: Flip logic in get_model_info --- aider/models.py | 84 ++++++++++++++++++++++++++----------------------- 1 file changed, 45 insertions(+), 39 deletions(-) diff --git a/aider/models.py b/aider/models.py index de748bd9e..a22c599eb 100644 --- a/aider/models.py +++ b/aider/models.py @@ -452,52 +452,58 @@ class Model(ModelSettings): self.get_weak_model(weak_model) def get_model_info(self, model): - if not litellm._lazy_module: - cache_dir = Path.home() / ".aider" / "caches" - 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 + if litellm._lazy_module: + # Do it the slow way... try: - import requests + return litellm.get_model_info(model) + except Exception: + return dict() - 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) + cache_dir = Path.home() / ".aider" / "caches" + 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: + 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) 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) - if info: - return info - except Exception: - pass # If there's any error, fall back to the slow way + 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: return litellm.get_model_info(model) except Exception: