2024-04-10 09:14:50 +03:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
|
|
|
from .Openai import Openai
|
|
|
|
from ...typing import AsyncResult, Messages
|
|
|
|
|
|
|
|
class OpenRouter(Openai):
|
2024-04-12 21:58:40 +03:00
|
|
|
label = "OpenRouter"
|
2024-04-10 09:14:50 +03:00
|
|
|
url = "https://openrouter.ai"
|
|
|
|
working = True
|
|
|
|
default_model = "openrouter/auto"
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_models(cls):
|
|
|
|
if not cls.models:
|
|
|
|
url = 'https://openrouter.ai/api/v1/models'
|
|
|
|
models = requests.get(url).json()["data"]
|
|
|
|
cls.models = [model['id'] for model in models]
|
|
|
|
return cls.models
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def create_async_generator(
|
|
|
|
cls,
|
|
|
|
model: str,
|
|
|
|
messages: Messages,
|
|
|
|
api_base: str = "https://openrouter.ai/api/v1",
|
|
|
|
**kwargs
|
|
|
|
) -> AsyncResult:
|
|
|
|
return super().create_async_generator(
|
|
|
|
model, messages, api_base=api_base, **kwargs
|
|
|
|
)
|