From 64e8381c32b8dd9517f5f83d6535471c77c67e17 Mon Sep 17 00:00:00 2001 From: Bagus Indrayana Date: Thu, 17 Aug 2023 21:42:00 +0800 Subject: refactor V50 and Raycast --- g4f/Provider/Raycast.py | 107 ++++++++++++++++++++++++++++-------------------- 1 file changed, 63 insertions(+), 44 deletions(-) (limited to 'g4f/Provider/Raycast.py') diff --git a/g4f/Provider/Raycast.py b/g4f/Provider/Raycast.py index 1704b4e8..1f13c9fa 100644 --- a/g4f/Provider/Raycast.py +++ b/g4f/Provider/Raycast.py @@ -1,50 +1,69 @@ import json -import os - import requests -from g4f.typing import get_type_hints - -url = "https://backend.raycast.com/api/v1/ai/chat_completions" -model = ['gpt-3.5-turbo', 'gpt-4'] -supports_stream = True -needs_auth = True -working = True +from ..typing import Any, CreateResult +from .base_provider import BaseProvider -def _create_completion(model: str, messages: list, stream: bool, **kwargs): - 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', - } - parsed_messages = [] - for message in messages: - parsed_messages.append({ - 'author': message['role'], - 'content': {'text': message['content']} - }) - data = { - "debug": False, - "locale": "en-CN", - "messages": parsed_messages, - "model": model, - "provider": "openai", - "source": "ai_chat", - "system_instruction": "markdown", - "temperature": 0.5 - } - response = requests.post(url, headers=headers, json=data, stream=True) - 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 +class Raycast(BaseProvider): + url = "https://raycast.com" + # model = ['gpt-3.5-turbo', 'gpt-4'] + supports_gpt_35_turbo = True + supports_gpt_4 = True + supports_stream = True + needs_auth = True + working = True + @staticmethod + def create_completion( + model: str, + messages: list[dict[str, str]], + stream: bool, + **kwargs: Any, + ) -> 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', + } + parsed_messages = [] + for message in messages: + parsed_messages.append({ + 'author': message['role'], + 'content': {'text': message['content']} + }) + data = { + "debug": False, + "locale": "en-CN", + "messages": parsed_messages, + "model": model, + "provider": "openai", + "source": "ai_chat", + "system_instruction": "markdown", + "temperature": 0.5 + } + response = requests.post("https://backend.raycast.com/api/v1/ai/chat_completions", headers=headers, json=data, stream=True) + 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 -params = f'g4f.Providers.{os.path.basename(__file__)[:-3]} supports: ' + \ - '(%s)' % ', '.join([f"{name}: {get_type_hints(_create_completion)[name].__name__}" for name in _create_completion.__code__.co_varnames[:_create_completion.__code__.co_argcount]]) + @classmethod + @property + def params(cls): + params = [ + ("model", "str"), + ("messages", "list[dict[str, str]]"), + ("stream", "bool"), + ("temperature", "float"), + ("top_p", "int"), + ("model", "str"), + ("auth", "str"), + ] + param = ", ".join([": ".join(p) for p in params]) + return f"g4f.provider.{cls.__name__} supports: ({param})" -- cgit v1.2.3