From 6d7a6ccee3be6b2ef96052c6b67fab9dcb10c7c9 Mon Sep 17 00:00:00 2001 From: "Paul Gauthier (aider)" Date: Sun, 25 Aug 2024 07:50:58 -0700 Subject: [PATCH] fix: use importlib.resources.open_text() to read JSON file in get_model_info --- aider/models.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/aider/models.py b/aider/models.py index 909af9f82..6a580331d 100644 --- a/aider/models.py +++ b/aider/models.py @@ -452,15 +452,16 @@ class Model(ModelSettings): def get_model_info(self, model): if not litellm._lazy_module: # Try and do this quickly, without triggering the litellm import - spec = importlib.util.find_spec("litellm") - if spec: - origin = Path(spec.origin) - fname = origin.parent / "model_prices_and_context_window_backup.json" - if fname.exists(): - data = json.loads(fname.read_text()) - info = data.get(model) + 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... try: