Skip to content

Commit b8a90eb

Browse files
committed
v1.5.0 - обновление g4f, бета версия chatgpt-агента
1 parent fdbadad commit b8a90eb

39 files changed

+1141
-769
lines changed

backend/backend.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
app = Flask(__name__)
2121
CORS(app)
22+
2223
LOG = logging.getLogger(__name__)
2324
embedding_proc = embedding_processing()
2425

backend/embedding_processing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def embedding(self, text_list):
4949
response_embedding = self.transform_embedding_to_dict(embeddings_list,text_list)
5050
return response_embedding
5151

52-
def transform_embedding_to_dict(self, embedding_list, text_list, model_name="text-embedding-elmo-002"):
52+
def transform_embedding_to_dict(self, embedding_list, text_list, model_name="text-embedding-ada-002"):
5353
prompt_tokens = sum(len(text) for text in text_list)
5454
total_tokens = sum(len(embedding) for embedding in embedding_list)
5555

config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"openai_api_key": "",
33
"purgpt_api_key": "",
44
"chatty_api_key": "",
5-
"daku_api_key": "",
5+
"daku_api_key": "sk-xxxxxxxxxxxxxxxxxxxxxxxx1",
66
"usage_limit": 999,
77
"language": "ru_RU",
88
"users": [],

g4f/Provider/AItianhu.py

Lines changed: 18 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,38 @@
11
from __future__ import annotations
22

33
import json
4-
from aiohttp import ClientSession, http
4+
from curl_cffi.requests import AsyncSession
55

6-
from ..typing import AsyncGenerator
7-
from .base_provider import AsyncGeneratorProvider, format_prompt
6+
from .base_provider import AsyncProvider, format_prompt
87

98

10-
class AItianhu(AsyncGeneratorProvider):
9+
class AItianhu(AsyncProvider):
1110
url = "https://www.aitianhu.com"
1211
working = True
1312
supports_gpt_35_turbo = True
1413

1514
@classmethod
16-
async def create_async_generator(
15+
async def create_async(
1716
cls,
1817
model: str,
1918
messages: list[dict[str, str]],
2019
proxy: str = None,
2120
**kwargs
22-
) -> AsyncGenerator:
23-
headers = {
24-
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/116.0",
25-
"Accept": "application/json, text/plain, */*",
26-
"Accept-Language": "de,en-US;q=0.7,en;q=0.3",
27-
"Content-Type": "application/json",
28-
"Origin": cls.url,
29-
"Connection": "keep-alive",
30-
"Referer": cls.url + "/",
31-
"Sec-Fetch-Dest": "empty",
32-
"Sec-Fetch-Mode": "cors",
33-
"Sec-Fetch-Site": "same-origin",
21+
) -> str:
22+
data = {
23+
"prompt": format_prompt(messages),
24+
"options": {},
25+
"systemMessage": "You are ChatGPT, a large language model trained by OpenAI. Follow the user's instructions carefully.",
26+
"temperature": 0.8,
27+
"top_p": 1,
28+
**kwargs
3429
}
35-
async with ClientSession(
36-
headers=headers,
37-
version=http.HttpVersion10
38-
) as session:
39-
data = {
40-
"prompt": format_prompt(messages),
41-
"options": {},
42-
"systemMessage": "You are ChatGPT, a large language model trained by OpenAI. Follow the user's instructions carefully.",
43-
"temperature": 0.8,
44-
"top_p": 1,
45-
**kwargs
46-
}
47-
async with session.post(
48-
cls.url + "/api/chat-process",
49-
proxy=proxy,
50-
json=data,
51-
ssl=False,
52-
) as response:
53-
response.raise_for_status()
54-
async for line in response.content:
55-
line = json.loads(line.decode('utf-8'))
56-
token = line["detail"]["choices"][0]["delta"].get("content")
57-
if token:
58-
yield token
30+
async with AsyncSession(proxies={"https": proxy}, impersonate="chrome107", verify=False) as session:
31+
response = await session.post(cls.url + "/api/chat-process", json=data)
32+
response.raise_for_status()
33+
line = response.text.splitlines()[-1]
34+
line = json.loads(line)
35+
return line["text"]
5936

6037

6138
@classmethod

g4f/Provider/Acytoo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ def _create_header():
4141
}
4242

4343

44-
def _create_payload(messages: list[dict[str, str]], temperature: float = 0.5, **kwargs):
44+
def _create_payload(messages: list[dict[str, str]], model: str, temperature: float = 0.5, **kwargs):
4545
return {
4646
'key' : '',
47-
'model' : 'gpt-3.5-turbo',
47+
'model' : model,
4848
'messages' : messages,
4949
'temperature' : temperature,
5050
'password' : ''

g4f/Provider/Aivvm.py

Lines changed: 53 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,77 @@
11
from __future__ import annotations
2+
import requests
23

3-
from aiohttp import ClientSession
4-
5-
from .base_provider import AsyncGeneratorProvider
6-
from ..typing import AsyncGenerator
4+
from .base_provider import BaseProvider
5+
from ..typing import CreateResult
76

87
models = {
9-
"gpt-4": {
10-
"id": "gpt-4",
11-
"name": "GPT-4",
12-
},
13-
"gpt-3.5-turbo": {
14-
"id": "gpt-3.5-turbo",
15-
"name": "GPT-3.5",
16-
},
17-
"gpt-3.5-turbo-16k": {
18-
"id": "gpt-3.5-turbo-16k",
19-
"name": "GPT-3.5-16k",
20-
},
8+
'gpt-3.5-turbo': {'id': 'gpt-3.5-turbo', 'name': 'GPT-3.5'},
9+
'gpt-3.5-turbo-0613': {'id': 'gpt-3.5-turbo-0613', 'name': 'GPT-3.5-0613'},
10+
'gpt-3.5-turbo-16k': {'id': 'gpt-3.5-turbo-16k', 'name': 'GPT-3.5-16K'},
11+
'gpt-3.5-turbo-16k-0613': {'id': 'gpt-3.5-turbo-16k-0613', 'name': 'GPT-3.5-16K-0613'},
12+
'gpt-4': {'id': 'gpt-4', 'name': 'GPT-4'},
13+
'gpt-4-0613': {'id': 'gpt-4-0613', 'name': 'GPT-4-0613'},
14+
'gpt-4-32k': {'id': 'gpt-4-32k', 'name': 'GPT-4-32K'},
15+
'gpt-4-32k-0613': {'id': 'gpt-4-32k-0613', 'name': 'GPT-4-32K-0613'},
2116
}
2217

23-
class Aivvm(AsyncGeneratorProvider):
24-
url = "https://chat.aivvm.com"
18+
class Aivvm(BaseProvider):
19+
url = 'https://chat.aivvm.com'
20+
supports_stream = True
2521
working = True
2622
supports_gpt_35_turbo = True
2723
supports_gpt_4 = True
2824

29-
3025
@classmethod
31-
async def create_async_generator(
32-
cls,
26+
def create_completion(cls,
3327
model: str,
3428
messages: list[dict[str, str]],
35-
proxy: str = None,
29+
stream: bool,
3630
**kwargs
37-
) -> AsyncGenerator:
38-
model = model if model else "gpt-3.5-turbo"
39-
if model not in models:
31+
) -> CreateResult:
32+
if not model:
33+
model = "gpt-3.5-turbo"
34+
elif model not in models:
4035
raise ValueError(f"Model are not supported: {model}")
36+
4137
headers = {
42-
"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",
43-
"Accept" : "*/*",
44-
"Accept-language" : "en,fr-FR;q=0.9,fr;q=0.8,es-ES;q=0.7,es;q=0.6,en-US;q=0.5,am;q=0.4,de;q=0.3",
45-
"Origin" : cls.url,
46-
"Referer" : cls.url + "/",
47-
"Sec-Fetch-Dest" : "empty",
48-
"Sec-Fetch-Mode" : "cors",
49-
"Sec-Fetch-Site" : "same-origin",
38+
"authority" : "chat.aivvm.com",
39+
"accept" : "*/*",
40+
"accept-language" : "en,fr-FR;q=0.9,fr;q=0.8,es-ES;q=0.7,es;q=0.6,en-US;q=0.5,am;q=0.4,de;q=0.3",
41+
"content-type" : "application/json",
42+
"origin" : "https://chat.aivvm.com",
43+
"referer" : "https://chat.aivvm.com/",
44+
"sec-ch-ua" : '"Google Chrome";v="117", "Not;A=Brand";v="8", "Chromium";v="117"',
45+
"sec-ch-ua-mobile" : "?0",
46+
"sec-ch-ua-platform" : '"macOS"',
47+
"sec-fetch-dest" : "empty",
48+
"sec-fetch-mode" : "cors",
49+
"sec-fetch-site" : "same-origin",
50+
"user-agent" : "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36",
5051
}
51-
async with ClientSession(
52-
headers=headers
53-
) as session:
54-
data = {
55-
"temperature": 1,
56-
"key": "",
57-
"messages": messages,
58-
"model": models[model],
59-
"prompt": "",
60-
**kwargs
61-
}
62-
async with session.post(cls.url + "/api/chat", json=data, proxy=proxy) as response:
63-
response.raise_for_status()
64-
async for stream in response.content.iter_any():
65-
yield stream.decode()
6652

53+
json_data = {
54+
"model" : models[model],
55+
"messages" : messages,
56+
"key" : "",
57+
"prompt" : "You are ChatGPT, a large language model trained by OpenAI. Follow the user's instructions carefully. Respond using markdown.",
58+
"temperature" : kwargs.get("temperature", 0.7)
59+
}
60+
61+
response = requests.post(
62+
"https://chat.aivvm.com/api/chat", headers=headers, json=json_data, stream=True)
63+
64+
for line in response.iter_content(chunk_size=1048):
65+
yield line.decode('utf-8')
6766

6867
@classmethod
6968
@property
7069
def params(cls):
7170
params = [
72-
("model", "str"),
73-
("messages", "list[dict[str, str]]"),
74-
("stream", "bool"),
75-
("temperature", "float"),
71+
('model', 'str'),
72+
('messages', 'list[dict[str, str]]'),
73+
('stream', 'bool'),
74+
('temperature', 'float'),
7675
]
77-
param = ", ".join([": ".join(p) for p in params])
78-
return f"g4f.provider.{cls.__name__} supports: ({param})"
76+
param = ', '.join([': '.join(p) for p in params])
77+
return f'g4f.provider.{cls.__name__} supports: ({param})'

g4f/Provider/Bard.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class Bard(AsyncProvider):
1313
url = "https://bard.google.com"
1414
needs_auth = True
1515
working = True
16+
_snlm0e = None
1617

1718
@classmethod
1819
async def create_async(
@@ -31,7 +32,6 @@ async def create_async(
3132

3233
headers = {
3334
'authority': 'bard.google.com',
34-
'content-type': 'application/x-www-form-urlencoded;charset=UTF-8',
3535
'origin': 'https://bard.google.com',
3636
'referer': 'https://bard.google.com/',
3737
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36',
@@ -42,13 +42,14 @@ async def create_async(
4242
cookies=cookies,
4343
headers=headers
4444
) as session:
45-
async with session.get(cls.url, proxy=proxy) as response:
46-
text = await response.text()
45+
if not cls._snlm0e:
46+
async with session.get(cls.url, proxy=proxy) as response:
47+
text = await response.text()
4748

48-
match = re.search(r'SNlM0e\":\"(.*?)\"', text)
49-
if not match:
50-
raise RuntimeError("No snlm0e value.")
51-
snlm0e = match.group(1)
49+
match = re.search(r'SNlM0e\":\"(.*?)\"', text)
50+
if not match:
51+
raise RuntimeError("No snlm0e value.")
52+
cls._snlm0e = match.group(1)
5253

5354
params = {
5455
'bl': 'boq_assistant-bard-web-server_20230326.21_p0',
@@ -57,7 +58,7 @@ async def create_async(
5758
}
5859

5960
data = {
60-
'at': snlm0e,
61+
'at': cls._snlm0e,
6162
'f.req': json.dumps([None, json.dumps([[prompt]])])
6263
}
6364

g4f/Provider/ChatgptLogin.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,14 @@ async def create_async(
5252
}
5353
async with session.post("https://opchatgpts.net/wp-admin/admin-ajax.php", data=data) as response:
5454
response.raise_for_status()
55-
return (await response.json())["data"]
55+
data = await response.json()
56+
if "data" in data:
57+
return data["data"]
58+
elif "msg" in data:
59+
raise RuntimeError(data["msg"])
60+
else:
61+
raise RuntimeError(f"Response: {data}")
62+
5663

5764
@classmethod
5865
@property

g4f/Provider/CodeLinkAva.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,12 @@ async def create_async_generator(
4040
}
4141
async with session.post("https://ava-alpha-api.codelink.io/api/chat", json=data) as response:
4242
response.raise_for_status()
43-
start = "data: "
4443
async for line in response.content:
4544
line = line.decode()
46-
if line.startswith("data: ") and not line.startswith("data: [DONE]"):
47-
line = json.loads(line[len(start):-1])
45+
if line.startswith("data: "):
46+
if line.startswith("data: [DONE]"):
47+
break
48+
line = json.loads(line[6:-1])
4849
content = line["choices"][0]["delta"].get("content")
4950
if content:
5051
yield content

g4f/Provider/DeepAi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ async def create_async_generator(
5656
async with ClientSession(
5757
headers=headers
5858
) as session:
59-
async with session.post("https://api.deepai.org/make_me_a_pizza", proxy=proxy, data=payload) as response:
59+
async with session.post("https://api.deepai.org/make_me_a_sandwich", proxy=proxy, data=payload) as response:
6060
response.raise_for_status()
6161
async for stream in response.content.iter_any():
6262
if stream:

0 commit comments

Comments
 (0)