返回提交历史
Modified
README.md
+32
-33
Modified
g4f/Provider/Aivvm.py
+1
-1
Modified
g4f/Provider/Bard.py
+9
-8
Modified
g4f/Provider/ChatgptLogin.py
+8
-1
Modified
g4f/Provider/CodeLinkAva.py
+4
-3
Modified
g4f/Provider/H2o.py
+12
-4
Modified
g4f/Provider/HuggingChat.py
+5
-7
Modified
g4f/Provider/Vitalentum.py
+3
-1
Modified
g4f/models.py
+1
-1
Modified
testing/test_providers.py
+6
-16
XFEstudio/gpt4free
Cache "snlm0e" in Bard Improve error handling in ChatgptLogin Fix async example in readme
82bd6f91
代码差异
10 个文件
+81
-75
@@ -238,43 +238,42 @@ response = g4f.ChatCompletion.create(
238
238
239
239
##### Async Support:
240
240
241
To enhance speed and overall performance, execute providers asynchronously. The total execution time will be determined by the duration of the slowest provider's execution.
241
To enhance speed and overall performance, execute providers asynchronously.
242
The total execution time will be determined by the duration of the slowest provider's execution.
242
243
243
244
```py
244
245
import g4f, asyncio
245
246
246
async def run_async():
247
_providers = [
248
g4f.Provider.AItianhu,
249
g4f.Provider.Acytoo,
250
g4f.Provider.Aichat,
251
g4f.Provider.Ails,
252
g4f.Provider.Aivvm,
253
g4f.Provider.ChatBase,
254
g4f.Provider.ChatgptAi,
255
g4f.Provider.ChatgptLogin,
256
g4f.Provider.CodeLinkAva,
257
g4f.Provider.DeepAi,
258
g4f.Provider.Opchatgpts,
259
g4f.Provider.Vercel,
260
g4f.Provider.Vitalentum,
261
g4f.Provider.Wewordle,
262
g4f.Provider.Ylokh,
263
g4f.Provider.You,
264
g4f.Provider.Yqcloud,
265
]
266
responses = [
267
provider.create_async(
268
model=g4f.models.default,
269
messages=[{"role": "user", "content": "Hello"}],
270
)
271
for provider in _providers
272
]
273
responses = await asyncio.gather(*responses)
274
for idx, provider in enumerate(_providers):
275
print(f"{provider.__name__}:", responses[idx])
276
277
asyncio.run(run_async())
247
_providers = [
248
g4f.Provider.Aichat,
249
g4f.Provider.Aivvm,
250
g4f.Provider.ChatBase,
251
g4f.Provider.Bing,
252
g4f.Provider.CodeLinkAva,
253
g4f.Provider.DeepAi,
254
g4f.Provider.GptGo,
255
g4f.Provider.Wewordle,
256
g4f.Provider.You,
257
g4f.Provider.Yqcloud,
258
]
259
260
async def run_provider(provider: g4f.Provider.AsyncProvider):
261
try:
262
response = await provider.create_async(
263
model=g4f.models.default.name,
264
messages=[{"role": "user", "content": "Hello"}],
265
)
266
print(f"{provider.__name__}:", response)
267
except Exception as e:
268
print(f"{provider.__name__}:", e)
269
270
async def run_all():
271
calls = [
272
run_provider(provider) for provider in _providers
273
]
274
await asyncio.gather(*calls)
275
276
asyncio.run(run_all())
278
277
```
279
278
280
279
### interference openai-proxy api (use with openai python package)
@@ -41,7 +41,7 @@ class Aivvm(AsyncGeneratorProvider):
41
41
headers = {
42
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
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",
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
45
"Origin" : cls.url,
46
46
"Referer" : cls.url + "/",
47
47
"Sec-Fetch-Dest" : "empty",
@@ -13,6 +13,7 @@ class Bard(AsyncProvider):
13
13
url = "https://bard.google.com"
14
14
needs_auth = True
15
15
working = True
16
_snlm0e = None
16
17
17
18
@classmethod
18
19
async def create_async(
@@ -31,7 +32,6 @@ class Bard(AsyncProvider):
31
32
32
33
headers = {
33
34
'authority': 'bard.google.com',
34
'content-type': 'application/x-www-form-urlencoded;charset=UTF-8',
35
35
'origin': 'https://bard.google.com',
36
36
'referer': 'https://bard.google.com/',
37
37
'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 @@ class Bard(AsyncProvider):
42
42
cookies=cookies,
43
43
headers=headers
44
44
) 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()
47
48
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)
52
53
53
54
params = {
54
55
'bl': 'boq_assistant-bard-web-server_20230326.21_p0',
@@ -57,7 +58,7 @@ class Bard(AsyncProvider):
57
58
}
58
59
59
60
data = {
60
'at': snlm0e,
61
'at': cls._snlm0e,
61
62
'f.req': json.dumps([None, json.dumps([[prompt]])])
62
63
}
63
64
@@ -52,7 +52,14 @@ class ChatgptLogin(AsyncProvider):
52
52
}
53
53
async with session.post("https://opchatgpts.net/wp-admin/admin-ajax.php", data=data) as response:
54
54
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
56
63
57
64
@classmethod
58
65
@property
@@ -40,11 +40,12 @@ class CodeLinkAva(AsyncGeneratorProvider):
40
40
}
41
41
async with session.post("https://ava-alpha-api.codelink.io/api/chat", json=data) as response:
42
42
response.raise_for_status()
43
start = "data: "
44
43
async for line in response.content:
45
44
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])
48
49
content = line["choices"][0]["delta"].get("content")
49
50
if content:
50
51
yield content
@@ -23,7 +23,7 @@ class H2o(AsyncGeneratorProvider):
23
23
**kwargs
24
24
) -> AsyncGenerator:
25
25
model = model if model else cls.model
26
headers = {"Referer": "https://gpt-gm.h2o.ai/"}
26
headers = {"Referer": cls.url + "/"}
27
27
28
28
async with ClientSession(
29
29
headers=headers
@@ -36,14 +36,14 @@ class H2o(AsyncGeneratorProvider):
36
36
"searchEnabled": "true",
37
37
}
38
38
async with session.post(
39
"https://gpt-gm.h2o.ai/settings",
39
f"{cls.url}/settings",
40
40
proxy=proxy,
41
41
data=data
42
42
) as response:
43
43
response.raise_for_status()
44
44
45
45
async with session.post(
46
"https://gpt-gm.h2o.ai/conversation",
46
f"{cls.url}/conversation",
47
47
proxy=proxy,
48
48
json={"model": model},
49
49
) as response:
@@ -71,7 +71,7 @@ class H2o(AsyncGeneratorProvider):
71
71
},
72
72
}
73
73
async with session.post(
74
f"https://gpt-gm.h2o.ai/conversation/{conversationId}",
74
f"{cls.url}/conversation/{conversationId}",
75
75
proxy=proxy,
76
76
json=data
77
77
) as response:
@@ -83,6 +83,14 @@ class H2o(AsyncGeneratorProvider):
83
83
if not line["token"]["special"]:
84
84
yield line["token"]["text"]
85
85
86
async with session.delete(
87
f"{cls.url}/conversation/{conversationId}",
88
proxy=proxy,
89
json=data
90
) as response:
91
response.raise_for_status()
92
93
86
94
@classmethod
87
95
@property
88
96
def params(cls):
@@ -25,10 +25,10 @@ class HuggingChat(AsyncGeneratorProvider):
25
25
**kwargs
26
26
) -> AsyncGenerator:
27
27
model = model if model else cls.model
28
if not cookies:
29
cookies = get_cookies(".huggingface.co")
30
28
if proxy and "://" not in proxy:
31
29
proxy = f"http://{proxy}"
30
if not cookies:
31
cookies = get_cookies(".huggingface.co")
32
32
33
33
headers = {
34
34
'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',
@@ -37,7 +37,7 @@ class HuggingChat(AsyncGeneratorProvider):
37
37
cookies=cookies,
38
38
headers=headers
39
39
) as session:
40
async with session.post("https://huggingface.co/chat/conversation", proxy=proxy, json={"model": model}) as response:
40
async with session.post(f"{cls.url}/conversation", proxy=proxy, json={"model": model}) as response:
41
41
conversation_id = (await response.json())["conversationId"]
42
42
43
43
send = {
@@ -62,7 +62,7 @@ class HuggingChat(AsyncGeneratorProvider):
62
62
"web_search_id": ""
63
63
}
64
64
}
65
async with session.post(f"https://huggingface.co/chat/conversation/{conversation_id}", proxy=proxy, json=send) as response:
65
async with session.post(f"{cls.url}/conversation/{conversation_id}", proxy=proxy, json=send) as response:
66
66
if not stream:
67
67
data = await response.json()
68
68
if "error" in data:
@@ -76,8 +76,6 @@ class HuggingChat(AsyncGeneratorProvider):
76
76
first = True
77
77
async for line in response.content:
78
78
line = line.decode("utf-8")
79
if not line:
80
continue
81
79
if line.startswith(start):
82
80
line = json.loads(line[len(start):-1])
83
81
if "token" not in line:
@@ -89,7 +87,7 @@ class HuggingChat(AsyncGeneratorProvider):
89
87
else:
90
88
yield line["token"]["text"]
91
89
92
async with session.delete(f"https://huggingface.co/chat/conversation/{conversation_id}", proxy=proxy) as response:
90
async with session.delete(f"{cls.url}/conversation/{conversation_id}", proxy=proxy) as response:
93
91
response.raise_for_status()
94
92
95
93
@@ -46,7 +46,9 @@ class Vitalentum(AsyncGeneratorProvider):
46
46
response.raise_for_status()
47
47
async for line in response.content:
48
48
line = line.decode()
49
if line.startswith("data: ") and not line.startswith("data: [DONE]"):
49
if line.startswith("data: "):
50
if line.startswith("data: [DONE]"):
51
break
50
52
line = json.loads(line[6:-1])
51
53
content = line["choices"][0]["delta"].get("content")
52
54
if content:
@@ -14,7 +14,7 @@ from .Provider import (
14
14
H2o
15
15
)
16
16
17
@dataclass
17
@dataclass(unsafe_hash=True)
18
18
class Model:
19
19
name: str
20
20
base_provider: str
@@ -1,6 +1,6 @@
1
1
import sys
2
2
from pathlib import Path
3
from colorama import Fore
3
from colorama import Fore, Style
4
4
5
5
sys.path.append(str(Path(__file__).parent.parent))
6
6
@@ -8,10 +8,6 @@ from g4f import BaseProvider, models, Provider
8
8
9
9
logging = False
10
10
11
class Styles:
12
ENDC = "\033[0m"
13
BOLD = "\033[1m"
14
UNDERLINE = "\033[4m"
15
11
16
12
def main():
17
13
providers = get_providers()
@@ -29,11 +25,11 @@ def main():
29
25
print()
30
26
31
27
if failed_providers:
32
print(f"{Fore.RED + Styles.BOLD}Failed providers:{Styles.ENDC}")
28
print(f"{Fore.RED + Style.BRIGHT}Failed providers:{Style.RESET_ALL}")
33
29
for _provider in failed_providers:
34
30
print(f"{Fore.RED}{_provider.__name__}")
35
31
else:
36
print(f"{Fore.GREEN + Styles.BOLD}All providers are working")
32
print(f"{Fore.GREEN + Style.BRIGHT}All providers are working")
37
33
38
34
39
35
def get_providers() -> list[type[BaseProvider]]:
@@ -45,21 +41,15 @@ def get_providers() -> list[type[BaseProvider]]:
45
41
"AsyncProvider",
46
42
"AsyncGeneratorProvider"
47
43
]
48
provider_names = [
49
provider_name
44
return [
45
getattr(Provider, provider_name)
50
46
for provider_name in provider_names
51
47
if not provider_name.startswith("__") and provider_name not in ignore_names
52
48
]
53
return [getattr(Provider, provider_name) for provider_name in provider_names]
54
49
55
50
56
51
def create_response(_provider: type[BaseProvider]) -> str:
57
if _provider.supports_gpt_35_turbo:
58
model = models.gpt_35_turbo.name
59
elif _provider.supports_gpt_4:
60
model = models.gpt_4.name
61
else:
62
model = models.default.name
52
model = models.gpt_35_turbo.name if _provider.supports_gpt_35_turbo else models.default.name
63
53
response = _provider.create_completion(
64
54
model=model,
65
55
messages=[{"role": "user", "content": "Hello, who are you? Answer in detail much as possible."}],