2023-09-03 11:26:26 +03:00
|
|
|
from __future__ import annotations
|
2023-08-27 18:37:44 +03:00
|
|
|
|
2023-09-03 11:26:26 +03:00
|
|
|
import json
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
2023-10-09 14:33:20 +03:00
|
|
|
from ...typing import CreateResult, Messages
|
2024-01-01 19:48:57 +03:00
|
|
|
from ..base_provider import AbstractProvider
|
2023-08-09 18:03:47 +03:00
|
|
|
|
|
|
|
|
2024-01-01 19:48:57 +03:00
|
|
|
class Raycast(AbstractProvider):
|
2023-08-27 18:37:44 +03:00
|
|
|
url = "https://raycast.com"
|
|
|
|
supports_gpt_35_turbo = True
|
|
|
|
supports_gpt_4 = True
|
|
|
|
supports_stream = True
|
|
|
|
needs_auth = True
|
|
|
|
working = True
|
2023-08-09 18:03:47 +03:00
|
|
|
|
2023-08-17 16:42:00 +03:00
|
|
|
@staticmethod
|
|
|
|
def create_completion(
|
|
|
|
model: str,
|
2023-10-09 14:33:20 +03:00
|
|
|
messages: Messages,
|
2023-08-17 16:42:00 +03:00
|
|
|
stream: bool,
|
2023-10-09 14:33:20 +03:00
|
|
|
proxy: str = None,
|
|
|
|
**kwargs,
|
2023-08-17 16:42:00 +03:00
|
|
|
) -> CreateResult:
|
|
|
|
auth = kwargs.get('auth')
|
|
|
|
headers = {
|
|
|
|
'Accept': 'application/json',
|
|
|
|
'Accept-Language': 'en-US,en;q=0.9',
|
|
|
|
'Authorization': f'Bearer {auth}',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'User-Agent': 'Raycast/0 CFNetwork/1410.0.3 Darwin/22.6.0',
|
|
|
|
}
|
2023-10-23 10:46:25 +03:00
|
|
|
parsed_messages = [
|
|
|
|
{'author': message['role'], 'content': {'text': message['content']}}
|
|
|
|
for message in messages
|
|
|
|
]
|
2023-08-17 16:42:00 +03:00
|
|
|
data = {
|
|
|
|
"debug": False,
|
|
|
|
"locale": "en-CN",
|
|
|
|
"messages": parsed_messages,
|
|
|
|
"model": model,
|
|
|
|
"provider": "openai",
|
|
|
|
"source": "ai_chat",
|
|
|
|
"system_instruction": "markdown",
|
|
|
|
"temperature": 0.5
|
|
|
|
}
|
2023-10-09 14:33:20 +03:00
|
|
|
response = requests.post(
|
|
|
|
"https://backend.raycast.com/api/v1/ai/chat_completions",
|
|
|
|
headers=headers,
|
|
|
|
json=data,
|
|
|
|
stream=True,
|
|
|
|
proxies={"https": proxy}
|
|
|
|
)
|
2023-08-17 16:42:00 +03:00
|
|
|
for token in response.iter_lines():
|
|
|
|
if b'data: ' not in token:
|
|
|
|
continue
|
|
|
|
completion_chunk = json.loads(token.decode().replace('data: ', ''))
|
|
|
|
token = completion_chunk['text']
|
|
|
|
if token != None:
|
|
|
|
yield token
|