2023-10-26 19:32:49 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-01-23 18:44:48 +00:00
|
|
|
import requests
|
2024-05-05 21:38:31 +00:00
|
|
|
from ..typing import AsyncResult, Messages
|
2024-04-07 08:36:13 +00:00
|
|
|
from .needs_auth.Openai import Openai
|
2023-10-26 19:32:49 +00:00
|
|
|
|
2024-04-07 08:36:13 +00:00
|
|
|
class DeepInfra(Openai):
|
2024-04-12 18:58:40 +00:00
|
|
|
label = "DeepInfra"
|
2024-01-01 22:23:45 +00:00
|
|
|
url = "https://deepinfra.com"
|
|
|
|
working = True
|
2024-05-15 19:07:49 +00:00
|
|
|
needs_auth = True
|
2024-01-01 22:23:45 +00:00
|
|
|
supports_stream = True
|
|
|
|
supports_message_history = True
|
2024-05-19 17:27:48 +00:00
|
|
|
default_model = "meta-llama/Meta-Llama-3-70B-Instruct"
|
2024-04-21 23:27:48 +00:00
|
|
|
default_vision_model = "llava-hf/llava-1.5-7b-hf"
|
|
|
|
model_aliases = {
|
2024-04-22 18:02:17 +00:00
|
|
|
'dbrx-instruct': 'databricks/dbrx-instruct',
|
2024-04-21 23:27:48 +00:00
|
|
|
}
|
2024-04-07 08:36:13 +00:00
|
|
|
|
2024-01-26 06:54:13 +00:00
|
|
|
@classmethod
|
|
|
|
def get_models(cls):
|
|
|
|
if not cls.models:
|
|
|
|
url = 'https://api.deepinfra.com/models/featured'
|
2024-01-30 03:15:25 +00:00
|
|
|
models = requests.get(url).json()
|
2024-04-07 08:36:13 +00:00
|
|
|
cls.models = [model['model_name'] for model in models if model["type"] == "text-generation"]
|
2024-01-26 06:54:13 +00:00
|
|
|
return cls.models
|
2024-01-23 18:44:48 +00:00
|
|
|
|
|
|
|
@classmethod
|
2024-04-07 08:36:13 +00:00
|
|
|
def create_async_generator(
|
2024-01-23 18:44:48 +00:00
|
|
|
cls,
|
2024-01-01 22:23:45 +00:00
|
|
|
model: str,
|
|
|
|
messages: Messages,
|
|
|
|
stream: bool,
|
2024-04-07 08:36:13 +00:00
|
|
|
api_base: str = "https://api.deepinfra.com/v1/openai",
|
|
|
|
temperature: float = 0.7,
|
|
|
|
max_tokens: int = 1028,
|
2024-01-01 22:23:45 +00:00
|
|
|
**kwargs
|
|
|
|
) -> AsyncResult:
|
2023-10-26 19:32:49 +00:00
|
|
|
headers = {
|
2024-01-01 22:23:45 +00:00
|
|
|
'Accept-Encoding': 'gzip, deflate, br',
|
|
|
|
'Accept-Language': 'en-US',
|
2023-11-24 14:16:00 +00:00
|
|
|
'Connection': 'keep-alive',
|
|
|
|
'Origin': 'https://deepinfra.com',
|
|
|
|
'Referer': 'https://deepinfra.com/',
|
|
|
|
'Sec-Fetch-Dest': 'empty',
|
|
|
|
'Sec-Fetch-Mode': 'cors',
|
|
|
|
'Sec-Fetch-Site': 'same-site',
|
|
|
|
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36',
|
|
|
|
'X-Deepinfra-Source': 'web-embed',
|
|
|
|
'sec-ch-ua': '"Google Chrome";v="119", "Chromium";v="119", "Not?A_Brand";v="24"',
|
|
|
|
'sec-ch-ua-mobile': '?0',
|
|
|
|
'sec-ch-ua-platform': '"macOS"',
|
2023-10-26 19:32:49 +00:00
|
|
|
}
|
2024-04-07 08:36:13 +00:00
|
|
|
return super().create_async_generator(
|
|
|
|
model, messages,
|
|
|
|
stream=stream,
|
|
|
|
api_base=api_base,
|
|
|
|
temperature=temperature,
|
|
|
|
max_tokens=max_tokens,
|
|
|
|
headers=headers,
|
|
|
|
**kwargs
|
|
|
|
)
|