From cbaad15bc29132ea076a3e3fa7bb6298ae69c628 Mon Sep 17 00:00:00 2001 From: Bagus Indrayana Date: Thu, 17 Aug 2023 21:09:35 +0800 Subject: [PATCH 1/4] add v50 --- .vscode/settings.json | 6 +++++ g4f/Provider/Providers/V50.py | 43 +++++++++++++++++++++++++++++++++++ g4f/Provider/__init__.py | 1 + 3 files changed, 50 insertions(+) create mode 100644 .vscode/settings.json create mode 100644 g4f/Provider/Providers/V50.py diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..d99f2f30 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "[python]": { + "editor.defaultFormatter": "ms-python.black-formatter" + }, + "python.formatting.provider": "none" +} \ No newline at end of file diff --git a/g4f/Provider/Providers/V50.py b/g4f/Provider/Providers/V50.py new file mode 100644 index 00000000..17b26be9 --- /dev/null +++ b/g4f/Provider/Providers/V50.py @@ -0,0 +1,43 @@ +import os, uuid, requests +from ...typing import get_type_hints +url = 'https://p5.v50.ltd' +model = ['gpt-3.5-turbo','gpt-3.5-turbo-16k'] +supports_stream = False +needs_auth = False +working = True + +def _create_completion(model: str, messages: list, stream: bool, temperature: float = 0.7, **kwargs): + + conversation = '' + for message in messages: + conversation += '%s: %s\n' % (message['role'], message['content']) + + conversation += 'assistant: ' + payload = { + "prompt": conversation, + "options": {}, + "systemMessage": ".", + "temperature": temperature, + "top_p": 1, + "model": model, + "user": str(uuid.uuid4()) + } + headers = { + 'authority': 'p5.v50.ltd', + 'accept': 'application/json, text/plain, */*', + 'accept-language': 'id-ID,id;q=0.9,en-US;q=0.8,en;q=0.7', + 'content-type': 'application/json', + 'origin': 'https://p5.v50.ltd', + 'referer': 'https://p5.v50.ltd/', + 'sec-ch-ua-platform': '"Windows"', + 'sec-fetch-dest': 'empty', + 'sec-fetch-mode': 'cors', + 'sec-fetch-site': 'same-origin', + 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36' + } + response = requests.post("https://p5.v50.ltd/api/chat-process", + json=payload, headers=headers, stream=True) + yield response.text + +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]]) \ No newline at end of file diff --git a/g4f/Provider/__init__.py b/g4f/Provider/__init__.py index 9d0442d0..9f1d65f3 100644 --- a/g4f/Provider/__init__.py +++ b/g4f/Provider/__init__.py @@ -25,6 +25,7 @@ from .Providers import ( ChatgptAi, opchatgpts, Raycast, + V50, ) Palm = Bard From 9ab880a82640b7bf30883237591cb6c670526b2c Mon Sep 17 00:00:00 2001 From: Bagus Indrayana Date: Thu, 17 Aug 2023 21:18:15 +0800 Subject: [PATCH 2/4] add proxy and remove stream --- g4f/Provider/Providers/V50.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/g4f/Provider/Providers/V50.py b/g4f/Provider/Providers/V50.py index 17b26be9..52a42d78 100644 --- a/g4f/Provider/Providers/V50.py +++ b/g4f/Provider/Providers/V50.py @@ -36,7 +36,7 @@ def _create_completion(model: str, messages: list, stream: bool, temperature: fl 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36' } response = requests.post("https://p5.v50.ltd/api/chat-process", - json=payload, headers=headers, stream=True) + json=payload, headers=headers, proxies=kwargs['proxy'] if 'proxy' in kwargs else {}) yield response.text params = f'g4f.Providers.{os.path.basename(__file__)[:-3]} supports: ' + \ From 64e8381c32b8dd9517f5f83d6535471c77c67e17 Mon Sep 17 00:00:00 2001 From: Bagus Indrayana Date: Thu, 17 Aug 2023 21:42:00 +0800 Subject: [PATCH 3/4] refactor V50 and Raycast --- g4f/Provider/Raycast.py | 107 +++++++++++++++++++++++----------------- g4f/Provider/V50.py | 101 ++++++++++++++++++++++--------------- 2 files changed, 123 insertions(+), 85 deletions(-) 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})" diff --git a/g4f/Provider/V50.py b/g4f/Provider/V50.py index 02ae18d6..125dd7c5 100644 --- a/g4f/Provider/V50.py +++ b/g4f/Provider/V50.py @@ -1,43 +1,62 @@ -import os, uuid, requests -from ..typing import get_type_hints -url = 'https://p5.v50.ltd' -model = ['gpt-3.5-turbo','gpt-3.5-turbo-16k'] -supports_stream = False -needs_auth = False -working = True +import uuid, requests +from ..typing import Any, CreateResult +from .base_provider import BaseProvider -def _create_completion(model: str, messages: list, stream: bool, temperature: float = 0.7, **kwargs): - conversation = '' - for message in messages: - conversation += '%s: %s\n' % (message['role'], message['content']) - - conversation += 'assistant: ' - payload = { - "prompt": conversation, - "options": {}, - "systemMessage": ".", - "temperature": temperature, - "top_p": 1, - "model": model, - "user": str(uuid.uuid4()) - } - headers = { - 'authority': 'p5.v50.ltd', - 'accept': 'application/json, text/plain, */*', - 'accept-language': 'id-ID,id;q=0.9,en-US;q=0.8,en;q=0.7', - 'content-type': 'application/json', - 'origin': 'https://p5.v50.ltd', - 'referer': 'https://p5.v50.ltd/', - 'sec-ch-ua-platform': '"Windows"', - 'sec-fetch-dest': 'empty', - 'sec-fetch-mode': 'cors', - 'sec-fetch-site': 'same-origin', - 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36' - } - response = requests.post("https://p5.v50.ltd/api/chat-process", - json=payload, headers=headers, proxies=kwargs['proxy'] if 'proxy' in kwargs else {}) - yield response.text - -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]]) \ No newline at end of file +class V50(BaseProvider): + url = 'https://p5.v50.ltd' + supports_gpt_35_turbo = True + supports_stream = False + needs_auth = False + working = True + + @staticmethod + def create_completion( + model: str, + messages: list[dict[str, str]], + stream: bool, + **kwargs: Any, + ) -> CreateResult: + conversation = '' + for message in messages: + conversation += '%s: %s\n' % (message['role'], message['content']) + + conversation += 'assistant: ' + payload = { + "prompt": conversation, + "options": {}, + "systemMessage": ".", + "temperature": kwargs.get("temperature", 0.4), + "top_p": kwargs.get("top_p", 0.4), + "model": model, + "user": str(uuid.uuid4()) + } + headers = { + 'authority': 'p5.v50.ltd', + 'accept': 'application/json, text/plain, */*', + 'accept-language': 'id-ID,id;q=0.9,en-US;q=0.8,en;q=0.7', + 'content-type': 'application/json', + 'origin': 'https://p5.v50.ltd', + 'referer': 'https://p5.v50.ltd/', + 'sec-ch-ua-platform': '"Windows"', + 'sec-fetch-dest': 'empty', + 'sec-fetch-mode': 'cors', + 'sec-fetch-site': 'same-origin', + 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36' + } + response = requests.post("https://p5.v50.ltd/api/chat-process", + json=payload, headers=headers, proxies=kwargs['proxy'] if 'proxy' in kwargs else {}) + yield response.text + + @classmethod + @property + def params(cls): + params = [ + ("model", "str"), + ("messages", "list[dict[str, str]]"), + ("stream", "bool"), + ("temperature", "float"), + ("top_p", "int"), + ] + param = ", ".join([": ".join(p) for p in params]) + return f"g4f.provider.{cls.__name__} supports: ({param})" \ No newline at end of file From d9b066f039c6495423f609740880d4a3f1deb7f7 Mon Sep 17 00:00:00 2001 From: Bagus Indrayana Date: Thu, 17 Aug 2023 21:46:50 +0800 Subject: [PATCH 4/4] provider to Provider in testing --- testing/test_chat_completion.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing/test_chat_completion.py b/testing/test_chat_completion.py index d408d9ec..d091d47b 100644 --- a/testing/test_chat_completion.py +++ b/testing/test_chat_completion.py @@ -8,7 +8,7 @@ import g4f stream = False response = g4f.ChatCompletion.create( model="gpt-3.5-turbo", - provider=g4f.provider.Ails, + provider=g4f.Provider.Ails, messages=[{"role": "user", "content": "hello"}], stream=stream, active_server=5,