Skip to content

Commit

Permalink
Add OpenRouter support (#996)
Browse files Browse the repository at this point in the history
* Add openrouter.ai support

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
akaihola and pre-commit-ci[bot] authored Sep 16, 2024
1 parent bf44c2a commit fcb2d71
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/jupyter-ai-magics/jupyter_ai_magics/aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
"ernie-bot": "qianfan:ERNIE-Bot",
"ernie-bot-4": "qianfan:ERNIE-Bot-4",
"titan": "bedrock:amazon.titan-tg1-large",
"openrouter-claude": "openrouter:anthropic/claude-3.5-sonnet:beta",
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from typing import Dict

from jupyter_ai_magics import BaseProvider
from jupyter_ai_magics.providers import EnvAuthStrategy, TextField
from langchain_core.pydantic_v1 import root_validator
from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env
from langchain_openai import ChatOpenAI


class ChatOpenRouter(ChatOpenAI):
@property
def lc_secrets(self) -> Dict[str, str]:
return {"openai_api_key": "OPENROUTER_API_KEY"}


class OpenRouterProvider(BaseProvider, ChatOpenRouter):
id = "openrouter"
name = "OpenRouter"
models = [
"*"
] # OpenRouter supports multiple models, so we use "*" to indicate it's a registry
model_id_key = "model_name"
pypi_package_deps = ["langchain_openai"]
auth_strategy = EnvAuthStrategy(name="OPENROUTER_API_KEY")
registry = True

fields = [
TextField(
key="openai_api_base", label="API Base URL (optional)", format="text"
),
]

def __init__(self, **kwargs):
openrouter_api_key = kwargs.pop("openrouter_api_key", None)
openrouter_api_base = kwargs.pop(
"openai_api_base", "https://openrouter.ai/api/v1"
)

super().__init__(
openai_api_key=openrouter_api_key,
openai_api_base=openrouter_api_base,
**kwargs,
)

@root_validator(pre=False, skip_on_failure=True, allow_reuse=True)
def validate_environment(cls, values: Dict) -> Dict:
"""Validate that api key and python package exists in environment."""
values["openai_api_key"] = convert_to_secret_str(
get_from_dict_or_env(values, "openai_api_key", "OPENROUTER_API_KEY")
)
return super().validate_environment(values)

@classmethod
def is_api_key_exc(cls, e: Exception):
import openai

if isinstance(e, openai.AuthenticationError):
error_details = e.json_body.get("error", {})
return error_details.get("code") == "invalid_api_key"
return False
1 change: 1 addition & 0 deletions packages/jupyter-ai-magics/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ nvidia-chat = "jupyter_ai_magics.partner_providers.nvidia:ChatNVIDIAProvider"
together-ai = "jupyter_ai_magics:TogetherAIProvider"
gemini = "jupyter_ai_magics.partner_providers.gemini:GeminiProvider"
mistralai = "jupyter_ai_magics.partner_providers.mistralai:MistralAIProvider"
openrouter = "jupyter_ai_magics.partner_providers.openrouter:OpenRouterProvider"

[project.entry-points."jupyter_ai.embeddings_model_providers"]
azure = "jupyter_ai_magics.partner_providers.openai:AzureOpenAIEmbeddingsProvider"
Expand Down

0 comments on commit fcb2d71

Please sign in to comment.