XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 1
返回提交历史

XFEstudio/gpt4free

refactor V50 and Raycast

64e8381c
Bagus Indrayana <bagusindrayanaindo@gmail.com>
提交于

代码差异

2 个文件 +123 -85
Modified g4f/Provider/Raycast.py +63 -44
@@ -1,50 +1,69 @@
1 1 import json
2 import os
3
4 2 import requests
5 from g4f.typing import get_type_hints
6
7 url = "https://backend.raycast.com/api/v1/ai/chat_completions"
8 model = ['gpt-3.5-turbo', 'gpt-4']
9 supports_stream = True
10 needs_auth = True
11 working = True
3 from ..typing import Any, CreateResult
4 from .base_provider import BaseProvider
12 5
13 6
14 def _create_completion(model: str, messages: list, stream: bool, **kwargs):
15 auth = kwargs.get('auth')
16 headers = {
17 'Accept': 'application/json',
18 'Accept-Language': 'en-US,en;q=0.9',
19 'Authorization': f'Bearer {auth}',
20 'Content-Type': 'application/json',
21 'User-Agent': 'Raycast/0 CFNetwork/1410.0.3 Darwin/22.6.0',
22 }
23 parsed_messages = []
24 for message in messages:
25 parsed_messages.append({
26 'author': message['role'],
27 'content': {'text': message['content']}
28 })
29 data = {
30 "debug": False,
31 "locale": "en-CN",
32 "messages": parsed_messages,
33 "model": model,
34 "provider": "openai",
35 "source": "ai_chat",
36 "system_instruction": "markdown",
37 "temperature": 0.5
38 }
39 response = requests.post(url, headers=headers, json=data, stream=True)
40 for token in response.iter_lines():
41 if b'data: ' not in token:
42 continue
43 completion_chunk = json.loads(token.decode().replace('data: ', ''))
44 token = completion_chunk['text']
45 if token != None:
46 yield token
7 class Raycast(BaseProvider):
8 url = "https://raycast.com"
9 # model = ['gpt-3.5-turbo', 'gpt-4']
10 supports_gpt_35_turbo = True
11 supports_gpt_4 = True
12 supports_stream = True
13 needs_auth = True
14 working = True
47 15
16 @staticmethod
17 def create_completion(
18 model: str,
19 messages: list[dict[str, str]],
20 stream: bool,
21 **kwargs: Any,
22 ) -> CreateResult:
23 auth = kwargs.get('auth')
24 headers = {
25 'Accept': 'application/json',
26 'Accept-Language': 'en-US,en;q=0.9',
27 'Authorization': f'Bearer {auth}',
28 'Content-Type': 'application/json',
29 'User-Agent': 'Raycast/0 CFNetwork/1410.0.3 Darwin/22.6.0',
30 }
31 parsed_messages = []
32 for message in messages:
33 parsed_messages.append({
34 'author': message['role'],
35 'content': {'text': message['content']}
36 })
37 data = {
38 "debug": False,
39 "locale": "en-CN",
40 "messages": parsed_messages,
41 "model": model,
42 "provider": "openai",
43 "source": "ai_chat",
44 "system_instruction": "markdown",
45 "temperature": 0.5
46 }
47 response = requests.post("https://backend.raycast.com/api/v1/ai/chat_completions", headers=headers, json=data, stream=True)
48 for token in response.iter_lines():
49 if b'data: ' not in token:
50 continue
51 completion_chunk = json.loads(token.decode().replace('data: ', ''))
52 token = completion_chunk['text']
53 if token != None:
54 yield token
48 55
49 params = f'g4f.Providers.{os.path.basename(__file__)[:-3]} supports: ' + \
50 '(%s)' % ', '.join([f"{name}: {get_type_hints(_create_completion)[name].__name__}" for name in _create_completion.__code__.co_varnames[:_create_completion.__code__.co_argcount]])
56 @classmethod
57 @property
58 def params(cls):
59 params = [
60 ("model", "str"),
61 ("messages", "list[dict[str, str]]"),
62 ("stream", "bool"),
63 ("temperature", "float"),
64 ("top_p", "int"),
65 ("model", "str"),
66 ("auth", "str"),
67 ]
68 param = ", ".join([": ".join(p) for p in params])
69 return f"g4f.provider.{cls.__name__} supports: ({param})"
Modified g4f/Provider/V50.py +60 -41
@@ -1,43 +1,62 @@
1 import os, uuid, requests
2 from ..typing import get_type_hints
3 url = 'https://p5.v50.ltd'
4 model = ['gpt-3.5-turbo','gpt-3.5-turbo-16k']
5 supports_stream = False
6 needs_auth = False
7 working = True
1 import uuid, requests
2 from ..typing import Any, CreateResult
3 from .base_provider import BaseProvider
8 4
9 def _create_completion(model: str, messages: list, stream: bool, temperature: float = 0.7, **kwargs):
10 5
11 conversation = ''
12 for message in messages:
13 conversation += '%s: %s\n' % (message['role'], message['content'])
14
15 conversation += 'assistant: '
16 payload = {
17 "prompt": conversation,
18 "options": {},
19 "systemMessage": ".",
20 "temperature": temperature,
21 "top_p": 1,
22 "model": model,
23 "user": str(uuid.uuid4())
24 }
25 headers = {
26 'authority': 'p5.v50.ltd',
27 'accept': 'application/json, text/plain, */*',
28 'accept-language': 'id-ID,id;q=0.9,en-US;q=0.8,en;q=0.7',
29 'content-type': 'application/json',
30 'origin': 'https://p5.v50.ltd',
31 'referer': 'https://p5.v50.ltd/',
32 'sec-ch-ua-platform': '"Windows"',
33 'sec-fetch-dest': 'empty',
34 'sec-fetch-mode': 'cors',
35 'sec-fetch-site': 'same-origin',
36 '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'
37 }
38 response = requests.post("https://p5.v50.ltd/api/chat-process",
39 json=payload, headers=headers, proxies=kwargs['proxy'] if 'proxy' in kwargs else {})
40 yield response.text
41
42 params = f'g4f.Providers.{os.path.basename(__file__)[:-3]} supports: ' + \
43 '(%s)' % ', '.join([f"{name}: {get_type_hints(_create_completion)[name].__name__}" for name in _create_completion.__code__.co_varnames[:_create_completion.__code__.co_argcount]])
6 class V50(BaseProvider):
7 url = 'https://p5.v50.ltd'
8 supports_gpt_35_turbo = True
9 supports_stream = False
10 needs_auth = False
11 working = True
12
13 @staticmethod
14 def create_completion(
15 model: str,
16 messages: list[dict[str, str]],
17 stream: bool,
18 **kwargs: Any,
19 ) -> CreateResult:
20 conversation = ''
21 for message in messages:
22 conversation += '%s: %s\n' % (message['role'], message['content'])
23
24 conversation += 'assistant: '
25 payload = {
26 "prompt": conversation,
27 "options": {},
28 "systemMessage": ".",
29 "temperature": kwargs.get("temperature", 0.4),
30 "top_p": kwargs.get("top_p", 0.4),
31 "model": model,
32 "user": str(uuid.uuid4())
33 }
34 headers = {
35 'authority': 'p5.v50.ltd',
36 'accept': 'application/json, text/plain, */*',
37 'accept-language': 'id-ID,id;q=0.9,en-US;q=0.8,en;q=0.7',
38 'content-type': 'application/json',
39 'origin': 'https://p5.v50.ltd',
40 'referer': 'https://p5.v50.ltd/',
41 'sec-ch-ua-platform': '"Windows"',
42 'sec-fetch-dest': 'empty',
43 'sec-fetch-mode': 'cors',
44 'sec-fetch-site': 'same-origin',
45 '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'
46 }
47 response = requests.post("https://p5.v50.ltd/api/chat-process",
48 json=payload, headers=headers, proxies=kwargs['proxy'] if 'proxy' in kwargs else {})
49 yield response.text
50
51 @classmethod
52 @property
53 def params(cls):
54 params = [
55 ("model", "str"),
56 ("messages", "list[dict[str, str]]"),
57 ("stream", "bool"),
58 ("temperature", "float"),
59 ("top_p", "int"),
60 ]
61 param = ", ".join([": ".join(p) for p in params])
62 return f"g4f.provider.{cls.__name__} supports: ({param})"