fix: use importlib.resources.open_text() to read JSON file in get_model_info

This commit is contained in:
Paul Gauthier (aider) 2024-08-25 07:50:58 -07:00
parent f4adc09277
commit 6d7a6ccee3

View file

@ -452,15 +452,16 @@ class Model(ModelSettings):
def get_model_info(self, model): def get_model_info(self, model):
if not litellm._lazy_module: if not litellm._lazy_module:
# Try and do this quickly, without triggering the litellm import # Try and do this quickly, without triggering the litellm import
spec = importlib.util.find_spec("litellm") try:
if spec: with importlib.resources.open_text(
origin = Path(spec.origin) "litellm", "model_prices_and_context_window_backup.json"
fname = origin.parent / "model_prices_and_context_window_backup.json" ) as f:
if fname.exists(): content = json.load(f)
data = json.loads(fname.read_text()) info = content.get(model)
info = data.get(model)
if info: if info:
return info return info
except Exception:
pass # If there's any error, fall back to the slow way
# Do it the slow way... # Do it the slow way...
try: try: