-
-
Notifications
You must be signed in to change notification settings - Fork 340
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
bf44c2a
commit fcb2d71
Showing
3 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
packages/jupyter-ai-magics/jupyter_ai_magics/partner_providers/openrouter.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters