~ Merge pull request #816

add V50 Provider & refactor Raycast (need testing with auth)
This commit is contained in:
Tekky 2023-08-20 05:49:53 +02:00 committed by GitHub
commit f55b24a914
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 108 additions and 75 deletions

6
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,6 @@
{
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter"
},
"python.formatting.provider": "none"
}

View File

@ -1,50 +0,0 @@
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
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
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]])

View File

@ -1,18 +1,17 @@
import json
import requests
from ..Provider.base_provider import BaseProvider
from ..typing import Any, CreateResult
from .base_provider import BaseProvider
class Raycast(BaseProvider):
url = "https://backend.raycast.com/api/v1/ai/chat_completions"
working = True
supports_stream = True
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(
@ -21,20 +20,20 @@ class Raycast(BaseProvider):
stream: bool,
**kwargs: Any,
) -> CreateResult:
auth = kwargs.get("auth")
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",
'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]] = []
parsed_messages = []
for message in messages:
parsed_messages.append(
{"author": message["role"], "content": {"text": message["content"]}}
)
parsed_messages.append({
'author': message['role'],
'content': {'text': message['content']}
})
data = {
"debug": False,
"locale": "en-CN",
@ -43,14 +42,28 @@ class Raycast(BaseProvider):
"provider": "openai",
"source": "ai_chat",
"system_instruction": "markdown",
"temperature": 0.5,
"temperature": 0.5
}
url = "https://backend.raycast.com/api/v1/ai/chat_completions"
response = requests.post(url, headers=headers, json=data, stream=True)
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:
if b'data: ' not in token:
continue
completion_chunk = json.loads(token.decode().replace("data: ", ""))
token = completion_chunk["text"]
completion_chunk = json.loads(token.decode().replace('data: ', ''))
token = completion_chunk['text']
if token != None:
yield token
@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})"

62
g4f/Provider/V50.py Normal file
View File

@ -0,0 +1,62 @@
import uuid, requests
from ..typing import Any, CreateResult
from .base_provider import BaseProvider
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})"

View File

@ -25,6 +25,7 @@ from .You import You
from .Yqcloud import Yqcloud
from .Equing import Equing
from .FastGpt import FastGpt
from .V50 import V50
__all__ = [
"BaseProvider",
@ -53,5 +54,6 @@ __all__ = [
"You",
"Yqcloud",
"Equing",
"FastGpt"
"FastGpt",
"V50"
]

View File

@ -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,