From 7a6a28d2b990a67255a8422692ad4f17352c2403 Mon Sep 17 00:00:00 2001 From: "irfan.putra" Date: Wed, 7 May 2025 11:54:07 +0700 Subject: [PATCH 1/2] feat: add openrouter support in litellm --- pr_agent/algo/ai_handlers/litellm_ai_handler.py | 12 ++++++++++++ pr_agent/settings/.secrets_template.toml | 5 ++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/pr_agent/algo/ai_handlers/litellm_ai_handler.py b/pr_agent/algo/ai_handlers/litellm_ai_handler.py index 61af7383..ba4630f4 100644 --- a/pr_agent/algo/ai_handlers/litellm_ai_handler.py +++ b/pr_agent/algo/ai_handlers/litellm_ai_handler.py @@ -118,6 +118,18 @@ class LiteLLMAIHandler(BaseAiHandler): litellm.api_base = self.api_base openai.api_base = self.api_base + # Support for Openrouter models + if get_settings().get("OPENROUTER.KEY", None): + openrouter_api_key = get_settings().get("OPENROUTER.KEY", None) + os.environ["OPENROUTER_API_KEY"] = openrouter_api_key + litellm.api_key = openrouter_api_key + openai.api_key = openrouter_api_key + + openrouter_api_base = get_settings().get("OPENROUTER.API_BASE", "https://openrouter.ai/api/v1") + os.environ["OPENROUTER_API_BASE"] = openrouter_api_base + self.api_base = openrouter_api_base + litellm.api_base = openrouter_api_base + # Models that only use user meessage self.user_message_only_models = USER_MESSAGE_ONLY_MODELS diff --git a/pr_agent/settings/.secrets_template.toml b/pr_agent/settings/.secrets_template.toml index 05f7bc0e..1022f8fa 100644 --- a/pr_agent/settings/.secrets_template.toml +++ b/pr_agent/settings/.secrets_template.toml @@ -107,4 +107,7 @@ key = "" client_id = "" # Your Azure AD application client ID client_secret = "" # Your Azure AD application client secret tenant_id = "" # Your Azure AD tenant ID -api_base = "" # Your Azure OpenAI service base URL (e.g., https://openai.xyz.com/) \ No newline at end of file +api_base = "" # Your Azure OpenAI service base URL (e.g., https://openai.xyz.com/) + +[openrouter] +key = "" \ No newline at end of file From 5061fb5a24243878019e8557a2369123f4a51818 Mon Sep 17 00:00:00 2001 From: "irfan.putra" Date: Wed, 7 May 2025 12:04:45 +0700 Subject: [PATCH 2/2] docs: add support for openrouter --- docs/docs/usage-guide/changing_a_model.md | 16 ++++++++++++++++ pr_agent/settings/.secrets_template.toml | 3 ++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/docs/docs/usage-guide/changing_a_model.md b/docs/docs/usage-guide/changing_a_model.md index a60076b5..c0b2bc28 100644 --- a/docs/docs/usage-guide/changing_a_model.md +++ b/docs/docs/usage-guide/changing_a_model.md @@ -278,6 +278,22 @@ key = "..." # your Codestral api key (you can obtain a Codestral key from [here](https://console.mistral.ai/codestral)) +### Openrouter + +To use model from Openrouter, for example, set: + +```toml +[config] # in configuration.toml +model="openrouter/anthropic/claude-3.7-sonnet" +fallback_models=["openrouter/deepseek/deepseek-chat"] +custom_model_max_tokens=20000 + +[openrouter] # in .secrets.toml or passed an environment variable openrouter__key +key = "..." # your openrouter api key +``` + +(you can obtain an Openrouter API key from [here](https://openrouter.ai/settings/keys)) + ### Custom models If the relevant model doesn't appear [here](https://github.com/Codium-ai/pr-agent/blob/main/pr_agent/algo/__init__.py), you can still use it as a custom model: diff --git a/pr_agent/settings/.secrets_template.toml b/pr_agent/settings/.secrets_template.toml index 1022f8fa..6572677d 100644 --- a/pr_agent/settings/.secrets_template.toml +++ b/pr_agent/settings/.secrets_template.toml @@ -110,4 +110,5 @@ tenant_id = "" # Your Azure AD tenant ID api_base = "" # Your Azure OpenAI service base URL (e.g., https://openai.xyz.com/) [openrouter] -key = "" \ No newline at end of file +key = "" +api_base = "" \ No newline at end of file