From c1b2ff20de98236c3f2bff4af578619eec1003a2 Mon Sep 17 00:00:00 2001 From: "Stefan Hladnik (aider)" Date: Tue, 18 Mar 2025 03:02:19 +0700 Subject: [PATCH] feat: Add method to fetch model info from openrouter pages --- aider/models.py | 45 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/aider/models.py b/aider/models.py index cc54f03d3..8289a424a 100644 --- a/aider/models.py +++ b/aider/models.py @@ -211,7 +211,7 @@ class ModelInfoManager: def get_model_info(self, model): cached_info = self.get_model_from_cached_json_db(model) - + litellm_info = None if litellm._lazy_module or not cached_info: try: @@ -219,13 +219,52 @@ class ModelInfoManager: except Exception as ex: if "model_prices_and_context_window.json" not in str(ex): print(str(ex)) - + if litellm_info: return litellm_info - + + if not cached_info and model.startswith("openrouter/"): + openrouter_info = self.fetch_openrouter_model_info(model) + if openrouter_info: + return openrouter_info + return cached_info + def fetch_openrouter_model_info(self, model): + """ + Fetch model info by scraping the openrouter model page. + Expected URL: https://openrouter.ai/ + Example: openrouter/qwen/qwen-2.5-72b-instruct:free + Returns a dict with keys: max_input_tokens, input_cost, output_cost. + """ + url_part = model[len("openrouter/"):] + url = "https://openrouter.ai/" + url_part + import requests + try: + response = requests.get(url, timeout=5) + if response.status_code != 200: + return {} + html = response.text + from bs4 import BeautifulSoup + soup = BeautifulSoup(html, "html.parser") + text = soup.get_text() + import re + context_match = re.search(r"([\d,]+)\s*context", text) + if context_match: + context_str = context_match.group(1).replace(",", "") + context_size = int(context_str) + else: + context_size = None + input_cost_match = re.search(r"\$\s*0\s*/M input tokens", text, re.IGNORECASE) + output_cost_match = re.search(r"\$\s*0\s*/M output tokens", text, re.IGNORECASE) + input_cost = 0 if input_cost_match else None + output_cost = 0 if output_cost_match else None + return {"max_input_tokens": context_size, "input_cost": input_cost, "output_cost": output_cost} + except Exception as e: + print("Error fetching openrouter info:", str(e)) + return {} + model_info_manager = ModelInfoManager()