From f6ef3cb2237d8c336e915ef77ddbe6f37934c4fd Mon Sep 17 00:00:00 2001 From: MIDORIBIN Date: Fri, 28 Jul 2023 19:07:17 +0900 Subject: refactor: refactor provider --- g4f/Provider/Raycast.py | 56 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 g4f/Provider/Raycast.py (limited to 'g4f/Provider/Raycast.py') diff --git a/g4f/Provider/Raycast.py b/g4f/Provider/Raycast.py new file mode 100644 index 00000000..9441f629 --- /dev/null +++ b/g4f/Provider/Raycast.py @@ -0,0 +1,56 @@ +import json + +import requests + +from ..provider.base_provider import BaseProvider +from ..typing import Any, CreateResult + + +class Raycast(BaseProvider): + url = "https://backend.raycast.com/api/v1/ai/chat_completions" + working = True + supports_stream = True + supports_gpt_35_turbo = True + supports_gpt_4 = True + needs_auth = 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: list[dict[str, Any]] = [] + 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, + } + url = "https://backend.raycast.com/api/v1/ai/chat_completions" + 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 -- cgit v1.2.3 From 6d96a1efd27b94c564da6da9cddf3bfceaf7c0ed Mon Sep 17 00:00:00 2001 From: abc <98614666+xtekky@users.noreply.github.com> Date: Thu, 17 Aug 2023 14:57:37 +0200 Subject: ~ | major refractoring | v0.0.2.0 --- g4f/Provider/Raycast.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'g4f/Provider/Raycast.py') diff --git a/g4f/Provider/Raycast.py b/g4f/Provider/Raycast.py index 9441f629..49481202 100644 --- a/g4f/Provider/Raycast.py +++ b/g4f/Provider/Raycast.py @@ -2,7 +2,7 @@ import json import requests -from ..provider.base_provider import BaseProvider +from ..Provider.base_provider import BaseProvider from ..typing import Any, CreateResult -- cgit v1.2.3