返回提交历史
Added
g4f/Provider/Hashnode.py
+79
-0
Modified
g4f/Provider/Phind.py
+2
-2
Modified
g4f/Provider/Yqcloud.py
+3
-2
Modified
g4f/Provider/__init__.py
+3
-0
Modified
g4f/Provider/deprecated/Myshell.py
+1
-43
Modified
g4f/Provider/retry_provider.py
+6
-7
XFEstudio/gpt4free
Fix increase timeout Add Hashnode Provider Fix Yqcloud Provider
63cda8d7
代码差异
6 个文件
+94
-54
@@ -0,0 +1,79 @@
1
from __future__ import annotations
2
3
import secrets
4
from aiohttp import ClientSession
5
6
from ..typing import AsyncResult, Messages
7
from .base_provider import AsyncGeneratorProvider
8
9
class SearchTypes():
10
quick = "quick"
11
code = "code"
12
websearch = "websearch"
13
14
class Hashnode(AsyncGeneratorProvider):
15
url = "https://hashnode.com"
16
supports_gpt_35_turbo = True
17
working = True
18
_sources = []
19
20
@classmethod
21
async def create_async_generator(
22
cls,
23
model: str,
24
messages: Messages,
25
search_type: str = SearchTypes.websearch,
26
proxy: str = None,
27
**kwargs
28
) -> AsyncResult:
29
headers = {
30
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/118.0",
31
"Accept": "*/*",
32
"Accept-Language": "de,en-US;q=0.7,en;q=0.3",
33
"Accept-Encoding": "gzip, deflate, br",
34
"Referer": f"{cls.url}/rix",
35
"Content-Type": "application/json",
36
"Origin": cls.url,
37
"Connection": "keep-alive",
38
"Sec-Fetch-Dest": "empty",
39
"Sec-Fetch-Mode": "cors",
40
"Sec-Fetch-Site": "same-origin",
41
"Pragma": "no-cache",
42
"Cache-Control": "no-cache",
43
"TE": "trailers",
44
}
45
async with ClientSession(headers=headers) as session:
46
prompt = messages[-1]["content"]
47
cls._sources = []
48
if search_type == "websearch":
49
async with session.post(
50
f"{cls.url}/api/ai/rix/search",
51
json={"prompt": prompt},
52
proxy=proxy,
53
) as response:
54
response.raise_for_status()
55
cls._sources = (await response.json())["result"]
56
data = {
57
"chatId": secrets.token_hex(16).zfill(32),
58
"history": messages,
59
"prompt": prompt,
60
"searchType": search_type,
61
"urlToScan": None,
62
"searchResults": cls._sources,
63
}
64
async with session.post(
65
f"{cls.url}/api/ai/rix/completion",
66
json=data,
67
proxy=proxy,
68
) as response:
69
response.raise_for_status()
70
async for chunk in response.content.iter_any():
71
if chunk:
72
yield chunk.decode()
73
74
@classmethod
75
def get_sources(cls) -> list:
76
return [{
77
"title": source["name"],
78
"url": source["url"]
79
} for source in cls._sources]
@@ -1,6 +1,6 @@
1
1
from __future__ import annotations
2
2
3
import random
3
import random, string
4
4
from datetime import datetime
5
5
6
6
from ..typing import AsyncResult, Messages
@@ -22,7 +22,7 @@ class Phind(AsyncGeneratorProvider):
22
22
timeout: int = 120,
23
23
**kwargs
24
24
) -> AsyncResult:
25
chars = 'abcdefghijklmnopqrstuvwxyz0123456789'
25
chars = string.ascii_lowercase + string.digits
26
26
user_id = ''.join(random.choice(chars) for _ in range(24))
27
27
data = {
28
28
"question": format_prompt(messages),
@@ -9,7 +9,7 @@ from .base_provider import AsyncGeneratorProvider, format_prompt
9
9
10
10
class Yqcloud(AsyncGeneratorProvider):
11
11
url = "https://chat9.yqcloud.top/"
12
working = False
12
working = True
13
13
supports_gpt_35_turbo = True
14
14
15
15
@staticmethod
@@ -17,10 +17,11 @@ class Yqcloud(AsyncGeneratorProvider):
17
17
model: str,
18
18
messages: Messages,
19
19
proxy: str = None,
20
timeout: int = 120,
20
21
**kwargs,
21
22
) -> AsyncResult:
22
23
async with StreamSession(
23
headers=_create_header(), proxies={"https": proxy}
24
headers=_create_header(), proxies={"https": proxy}, timeout=timeout
24
25
) as session:
25
26
payload = _create_payload(messages, **kwargs)
26
27
async with session.post("https://api.aichatos.cloud/api/generateStream", json=payload) as response:
@@ -24,6 +24,7 @@ from .GptChatly import GptChatly
24
24
from .GptForLove import GptForLove
25
25
from .GptGo import GptGo
26
26
from .GptGod import GptGod
27
from .Hashnode import Hashnode
27
28
from .Liaobots import Liaobots
28
29
from .Llama2 import Llama2
29
30
from .MyShell import MyShell
@@ -82,6 +83,7 @@ class ProviderUtils:
82
83
'GptForLove': GptForLove,
83
84
'GptGo': GptGo,
84
85
'GptGod': GptGod,
86
'Hashnode': Hashnode,
85
87
'H2o': H2o,
86
88
'HuggingChat': HuggingChat,
87
89
'Komo': Komo,
@@ -154,6 +156,7 @@ __all__ = [
154
156
'GetGpt',
155
157
'GptGo',
156
158
'GptGod',
159
'Hashnode',
157
160
'H2o',
158
161
'HuggingChat',
159
162
'Liaobots',
@@ -174,46 +174,4 @@ def generate_visitor_id(user_agent: str) -> str:
174
174
r = hex(int(random.random() * (16**16)))[2:-2]
175
175
d = xor_hash(user_agent)
176
176
e = hex(1080 * 1920)[2:]
177
return f"{f}-{r}-{d}-{e}-{f}"
178
179
180
181
# update
182
# from g4f.requests import StreamSession
183
184
# async def main():
185
# headers = {
186
# 'authority': 'api.myshell.ai',
187
# 'accept': 'application/json',
188
# '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',
189
# 'content-type': 'application/json',
190
# 'myshell-service-name': 'organics-api',
191
# 'origin': 'https://app.myshell.ai',
192
# 'referer': 'https://app.myshell.ai/',
193
# 'sec-ch-ua': '"Chromium";v="118", "Google Chrome";v="118", "Not=A?Brand";v="99"',
194
# 'sec-ch-ua-mobile': '?0',
195
# 'sec-ch-ua-platform': '"macOS"',
196
# 'sec-fetch-dest': 'empty',
197
# 'sec-fetch-mode': 'cors',
198
# 'sec-fetch-site': 'same-site',
199
# 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36',
200
# 'visitor-id': '18ae8fe5d916d3-0213f29594b17f-18525634-157188-18ae8fe5d916d3',
201
# }
202
203
# json_data = {
204
# 'conversation_scenario': 3,
205
# 'botId': '4738',
206
# 'message': 'hi',
207
# 'messageType': 1,
208
# }
209
210
# async with StreamSession(headers=headers, impersonate="chrome110") as session:
211
# async with session.post(f'https://api.myshell.ai/v1/bot/chat/send_message',
212
# json=json_data) as response:
213
214
# response.raise_for_status()
215
# async for chunk in response.iter_content():
216
# print(chunk.decode("utf-8"))
217
218
# import asyncio
219
# asyncio.run(main())
177
return f"{f}-{r}-{d}-{e}-{f}"
@@ -71,11 +71,10 @@ class RetryProvider(AsyncProvider):
71
71
self.exceptions: Dict[str, Exception] = {}
72
72
for provider in providers:
73
73
try:
74
return await asyncio.wait_for(provider.create_async(model, messages, **kwargs), timeout=60)
75
except asyncio.TimeoutError as e:
76
self.exceptions[provider.__name__] = e
77
if self.logging:
78
print(f"{provider.__name__}: TimeoutError: {e}")
74
return await asyncio.wait_for(
75
provider.create_async(model, messages, **kwargs),
76
timeout=kwargs.get("timeout", 60)
77
)
79
78
except Exception as e:
80
79
self.exceptions[provider.__name__] = e
81
80
if self.logging:
@@ -85,8 +84,8 @@ class RetryProvider(AsyncProvider):
85
84
86
85
def raise_exceptions(self) -> None:
87
86
if self.exceptions:
88
raise RuntimeError("\n".join(["All providers failed:"] + [
87
raise RuntimeError("\n".join(["RetryProvider failed:"] + [
89
88
f"{p}: {self.exceptions[p].__class__.__name__}: {self.exceptions[p]}" for p in self.exceptions
90
89
]))
91
90
92
raise RuntimeError("No provider found")
91
raise RuntimeError("RetryProvider: No provider found")