返回提交历史
Modified
etc/tool/copilot.py
+4
-2
Modified
g4f/Provider/Bing.py
+17
-11
Modified
g4f/Provider/HuggingChat.py
+10
-2
Modified
g4f/Provider/Liaobots.py
+10
-2
Modified
g4f/Provider/PerplexityLabs.py
+9
-2
Modified
g4f/Provider/bing/create_images.py
+11
-4
XFEstudio/gpt4free
Add aiohttp_socks support
8864b70e
代码差异
6 个文件
+61
-23
@@ -126,6 +126,7 @@ def analyze_code(pull: PullRequest, diff: str)-> list[dict]:
126
126
for line in diff.split('\n'):
127
127
if line.startswith('+++ b/'):
128
128
current_file_path = line[6:]
129
changed_lines = []
129
130
elif line.startswith('@@'):
130
131
match = re.search(r'\+([0-9]+?),', line)
131
132
if match:
@@ -137,9 +138,10 @@ def analyze_code(pull: PullRequest, diff: str)-> list[dict]:
137
138
for review in response.get('reviews', []):
138
139
review['path'] = current_file_path
139
140
comments.append(review)
140
changed_lines = []
141
141
current_file_path = None
142
elif not line.startswith('-'):
142
elif line.startswith('-'):
143
changed_lines.append(line)
144
else:
143
145
changed_lines.append(f"{offset_line}:{line}")
144
146
offset_line += 1
145
147
@@ -6,7 +6,7 @@ import os
6
6
import uuid
7
7
import time
8
8
from urllib import parse
9
from aiohttp import ClientSession, ClientTimeout
9
from aiohttp import ClientSession, ClientTimeout, BaseConnector
10
10
11
11
from ..typing import AsyncResult, Messages, ImageType
12
12
from ..image import ImageResponse
@@ -39,6 +39,7 @@ class Bing(AsyncGeneratorProvider):
39
39
proxy: str = None,
40
40
timeout: int = 900,
41
41
cookies: dict = None,
42
connector: BaseConnector = None,
42
43
tone: str = Tones.balanced,
43
44
image: ImageType = None,
44
45
web_search: bool = False,
@@ -67,8 +68,15 @@ class Bing(AsyncGeneratorProvider):
67
68
cookies = {**Defaults.cookies, **cookies} if cookies else Defaults.cookies
68
69
69
70
gpt4_turbo = True if model.startswith("gpt-4-turbo") else False
71
72
if proxy and not connector:
73
try:
74
from aiohttp_socks import ProxyConnector
75
connector = ProxyConnector.from_url(proxy)
76
except ImportError:
77
raise RuntimeError('Install "aiohttp_socks" package for proxy support')
70
78
71
return stream_generate(prompt, tone, image, context, proxy, cookies, web_search, gpt4_turbo, timeout)
79
return stream_generate(prompt, tone, image, context, cookies, connector, web_search, gpt4_turbo, timeout)
72
80
73
81
def create_context(messages: Messages) -> str:
74
82
"""
@@ -253,8 +261,8 @@ async def stream_generate(
253
261
tone: str,
254
262
image: ImageType = None,
255
263
context: str = None,
256
proxy: str = None,
257
264
cookies: dict = None,
265
connector: BaseConnector = None,
258
266
web_search: bool = False,
259
267
gpt4_turbo: bool = False,
260
268
timeout: int = 900
@@ -266,7 +274,6 @@ async def stream_generate(
266
274
:param tone: The desired tone for the response.
267
275
:param image: The image type involved in the response.
268
276
:param context: Additional context for the prompt.
269
:param proxy: Proxy settings for the request.
270
277
:param cookies: Cookies for the session.
271
278
:param web_search: Flag to enable web search.
272
279
:param gpt4_turbo: Flag to enable GPT-4 Turbo.
@@ -278,10 +285,10 @@ async def stream_generate(
278
285
headers["Cookie"] = "; ".join(f"{k}={v}" for k, v in cookies.items())
279
286
280
287
async with ClientSession(
281
timeout=ClientTimeout(total=timeout), headers=headers
288
timeout=ClientTimeout(total=timeout), headers=headers, connector=connector
282
289
) as session:
283
conversation = await create_conversation(session, proxy)
284
image_response = await upload_image(session, image, tone, proxy) if image else None
290
conversation = await create_conversation(session)
291
image_response = await upload_image(session, image, tone) if image else None
285
292
if image_response:
286
293
yield image_response
287
294
@@ -289,8 +296,7 @@ async def stream_generate(
289
296
async with session.ws_connect(
290
297
'wss://sydney.bing.com/sydney/ChatHub',
291
298
autoping=False,
292
params={'sec_access_token': conversation.conversationSignature},
293
proxy=proxy
299
params={'sec_access_token': conversation.conversationSignature}
294
300
) as wss:
295
301
await wss.send_str(format_message({'protocol': 'json', 'version': 1}))
296
302
await wss.receive(timeout=timeout)
@@ -322,7 +328,7 @@ async def stream_generate(
322
328
elif message.get('contentType') == "IMAGE":
323
329
prompt = message.get('text')
324
330
try:
325
image_response = ImageResponse(await create_images(session, prompt, proxy), prompt)
331
image_response = ImageResponse(await create_images(session, prompt), prompt)
326
332
except:
327
333
response_txt += f"\nhttps://www.bing.com/images/create?q={parse.quote(prompt)}"
328
334
final = True
@@ -342,4 +348,4 @@ async def stream_generate(
342
348
raise Exception(f"{result['value']}: {result['message']}")
343
349
return
344
350
finally:
345
await delete_conversation(session, conversation, proxy)
351
await delete_conversation(session, conversation)
@@ -2,7 +2,7 @@ from __future__ import annotations
2
2
3
3
import json, uuid
4
4
5
from aiohttp import ClientSession
5
from aiohttp import ClientSession, BaseConnector
6
6
7
7
from ..typing import AsyncResult, Messages
8
8
from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
@@ -33,6 +33,7 @@ class HuggingChat(AsyncGeneratorProvider, ProviderModelMixin):
33
33
messages: Messages,
34
34
stream: bool = True,
35
35
proxy: str = None,
36
connector: BaseConnector = None,
36
37
web_search: bool = False,
37
38
cookies: dict = None,
38
39
**kwargs
@@ -43,9 +44,16 @@ class HuggingChat(AsyncGeneratorProvider, ProviderModelMixin):
43
44
headers = {
44
45
'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',
45
46
}
47
if proxy and not connector:
48
try:
49
from aiohttp_socks import ProxyConnector
50
connector = ProxyConnector.from_url(proxy)
51
except ImportError:
52
raise RuntimeError('Install "aiohttp_socks" package for proxy support')
46
53
async with ClientSession(
47
54
cookies=cookies,
48
headers=headers
55
headers=headers,
56
connector=connector
49
57
) as session:
50
58
async with session.post(f"{cls.url}/conversation", json={"model": cls.get_model(model)}, proxy=proxy) as response:
51
59
conversation_id = (await response.json())["conversationId"]
@@ -2,7 +2,7 @@ from __future__ import annotations
2
2
3
3
import uuid
4
4
5
from aiohttp import ClientSession
5
from aiohttp import ClientSession, BaseConnector
6
6
7
7
from ..typing import AsyncResult, Messages
8
8
from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
@@ -91,6 +91,7 @@ class Liaobots(AsyncGeneratorProvider, ProviderModelMixin):
91
91
messages: Messages,
92
92
auth: str = None,
93
93
proxy: str = None,
94
connector: BaseConnector = None,
94
95
**kwargs
95
96
) -> AsyncResult:
96
97
headers = {
@@ -100,9 +101,16 @@ class Liaobots(AsyncGeneratorProvider, ProviderModelMixin):
100
101
"referer": f"{cls.url}/",
101
102
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36",
102
103
}
104
if proxy and not connector:
105
try:
106
from aiohttp_socks import ProxyConnector
107
connector = ProxyConnector.from_url(proxy)
108
except ImportError:
109
raise RuntimeError('Install "aiohttp_socks" package for proxy support')
103
110
async with ClientSession(
104
111
headers=headers,
105
cookie_jar=cls._cookie_jar
112
cookie_jar=cls._cookie_jar,
113
connector=connector
106
114
) as session:
107
115
cls._auth_code = auth if isinstance(auth, str) else cls._auth_code
108
116
if not cls._auth_code:
@@ -2,7 +2,7 @@ from __future__ import annotations
2
2
3
3
import random
4
4
import json
5
from aiohttp import ClientSession
5
from aiohttp import ClientSession, BaseConnector
6
6
7
7
from ..typing import AsyncResult, Messages
8
8
from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
@@ -32,6 +32,7 @@ class PerplexityLabs(AsyncGeneratorProvider, ProviderModelMixin):
32
32
model: str,
33
33
messages: Messages,
34
34
proxy: str = None,
35
connector: BaseConnector = None,
35
36
**kwargs
36
37
) -> AsyncResult:
37
38
headers = {
@@ -47,7 +48,13 @@ class PerplexityLabs(AsyncGeneratorProvider, ProviderModelMixin):
47
48
"Sec-Fetch-Site": "same-site",
48
49
"TE": "trailers",
49
50
}
50
async with ClientSession(headers=headers) as session:
51
if proxy and not connector:
52
try:
53
from aiohttp_socks import ProxyConnector
54
connector = ProxyConnector.from_url(proxy)
55
except ImportError:
56
raise RuntimeError('Install "aiohttp_socks" package for proxy support')
57
async with ClientSession(headers=headers, connector=connector) as session:
51
58
t = format(random.getrandbits(32), '08x')
52
59
async with session.get(
53
60
f"{API_URL}?EIO=4&transport=polling&t={t}",
@@ -7,7 +7,7 @@ import asyncio
7
7
import time
8
8
import json
9
9
import os
10
from aiohttp import ClientSession
10
from aiohttp import ClientSession, BaseConnector
11
11
from bs4 import BeautifulSoup
12
12
from urllib.parse import quote
13
13
from typing import Generator, List, Dict
@@ -50,7 +50,7 @@ def wait_for_login(driver: WebDriver, timeout: int = TIMEOUT_LOGIN) -> None:
50
50
raise RuntimeError("Timeout error")
51
51
time.sleep(0.5)
52
52
53
def create_session(cookies: Dict[str, str]) -> ClientSession:
53
def create_session(cookies: Dict[str, str], proxy: str = None, connector: BaseConnector = None) -> ClientSession:
54
54
"""
55
55
Creates a new client session with specified cookies and headers.
56
56
@@ -79,7 +79,13 @@ def create_session(cookies: Dict[str, str]) -> ClientSession:
79
79
}
80
80
if cookies:
81
81
headers["Cookie"] = "; ".join(f"{k}={v}" for k, v in cookies.items())
82
return ClientSession(headers=headers)
82
if proxy and not connector:
83
try:
84
from aiohttp_socks import ProxyConnector
85
connector = ProxyConnector.from_url(proxy)
86
except ImportError:
87
raise RuntimeError('Install "aiohttp_socks" package for proxy support')
88
return ClientSession(headers=headers, connector=connector)
83
89
84
90
async def create_images(session: ClientSession, prompt: str, proxy: str = None, timeout: int = TIMEOUT_IMAGE_CREATION) -> List[str]:
85
91
"""
@@ -214,7 +220,8 @@ class CreateImagesBing:
214
220
cookies = self.cookies or get_cookies(".bing.com")
215
221
if "_U" not in cookies:
216
222
raise RuntimeError('"_U" cookie is missing')
217
async with create_session(cookies) as session:
223
proxy = os.environ.get("G4F_PROXY")
224
async with create_session(cookies, proxy) as session:
218
225
images = await create_images(session, prompt, self.proxy)
219
226
return ImageResponse(images, prompt)
220
227