返回提交历史
Modified
.github/workflows/unittest.yml
+1
-1
Modified
g4f/Provider/needs_auth/OpenaiChat.py
+40
-12
XFEstudio/gpt4free
Fix unittests
55e5cf16
代码差异
2 个文件
+41
-13
@@ -16,4 +16,4 @@ jobs:
16
16
- name: Install requirements
17
17
- run: pip install -r requirements.txt
18
18
- name: Run tests
19
run: python -m etc.unittest.main
19
- run: python -m etc.unittest.main
@@ -49,7 +49,8 @@ class OpenaiChat(AsyncGeneratorProvider):
49
49
image: ImageType = None,
50
50
**kwargs
51
51
) -> Response:
52
"""Create a new conversation or continue an existing one
52
"""
53
Create a new conversation or continue an existing one
53
54
54
55
Args:
55
56
prompt: The user input to start or continue the conversation
@@ -96,7 +97,8 @@ class OpenaiChat(AsyncGeneratorProvider):
96
97
headers: dict,
97
98
image: ImageType
98
99
) -> ImageResponse:
99
"""Upload an image to the service and get the download URL
100
"""
101
Upload an image to the service and get the download URL
100
102
101
103
Args:
102
104
session: The StreamSession object to use for requests
@@ -149,7 +151,8 @@ class OpenaiChat(AsyncGeneratorProvider):
149
151
150
152
@classmethod
151
153
async def _get_default_model(cls, session: StreamSession, headers: dict):
152
"""Get the default model name from the service
154
"""
155
Get the default model name from the service
153
156
154
157
Args:
155
158
session: The StreamSession object to use for requests
@@ -172,7 +175,8 @@ class OpenaiChat(AsyncGeneratorProvider):
172
175
173
176
@classmethod
174
177
def _create_messages(cls, prompt: str, image_response: ImageResponse = None):
175
"""Create a list of messages for the user input
178
"""
179
Create a list of messages for the user input
176
180
177
181
Args:
178
182
prompt: The user input as a string
@@ -222,10 +226,20 @@ class OpenaiChat(AsyncGeneratorProvider):
222
226
"""
223
227
Retrieves the image response based on the message content.
224
228
225
:param session: The StreamSession object.
226
:param headers: HTTP headers for the request.
227
:param line: The line of response containing image information.
228
:return: An ImageResponse object with the image details.
229
This method processes the message content to extract image information and retrieves the
230
corresponding image from the backend API. It then returns an ImageResponse object containing
231
the image URL and the prompt used to generate the image.
232
233
Args:
234
session (StreamSession): The StreamSession object used for making HTTP requests.
235
headers (dict): HTTP headers to be used for the request.
236
line (dict): A dictionary representing the line of response that contains image information.
237
238
Returns:
239
ImageResponse: An object containing the image URL and the prompt, or None if no image is found.
240
241
Raises:
242
RuntimeError: If there'san error in downloading the image, including issues with the HTTP request or response.
229
243
"""
230
244
if "parts" not in line["message"]["content"]:
231
245
return
@@ -244,6 +258,20 @@ class OpenaiChat(AsyncGeneratorProvider):
244
258
245
259
@classmethod
246
260
async def _delete_conversation(cls, session: StreamSession, headers: dict, conversation_id: str):
261
"""
262
Deletes a conversation by setting its visibility to False.
263
264
This method sends an HTTP PATCH request to update the visibility of a conversation.
265
It's used to effectively delete a conversation from being accessed or displayed in the future.
266
267
Args:
268
session (StreamSession): The StreamSession object used for making HTTP requests.
269
headers (dict): HTTP headers to be used for the request.
270
conversation_id (str): The unique identifier of the conversation to be deleted.
271
272
Raises:
273
HTTPError: If the HTTP request fails or returns an unsuccessful status code.
274
"""
247
275
async with session.patch(
248
276
f"{cls.url}/backend-api/conversation/{conversation_id}",
249
277
json={"is_visible": False},
@@ -283,7 +311,7 @@ class OpenaiChat(AsyncGeneratorProvider):
283
311
history_disabled (bool): Flag to disable history and training.
284
312
action (str): Type of action ('next', 'continue', 'variant').
285
313
conversation_id (str): ID of the conversation.
286
parent_id (str): ID of the parent message.
314
parent_id (str): ID of the parent message.
287
315
image (ImageType): Image to include in the conversation.
288
316
response_fields (bool): Flag to include response fields in the output.
289
317
**kwargs: Additional keyword arguments.
@@ -397,7 +425,7 @@ class OpenaiChat(AsyncGeneratorProvider):
397
425
await cls._delete_conversation(session, headers, conversation_id)
398
426
399
427
@classmethod
400
def _browse_access_token(cls, proxy: str = None) -> tuple[str, dict]:
428
def _browse_access_token(cls, proxy: str = None, timeout: int = 1200) -> tuple[str, dict]:
401
429
"""
402
430
Browse to obtain an access token.
403
431
@@ -410,7 +438,7 @@ class OpenaiChat(AsyncGeneratorProvider):
410
438
driver = get_browser(proxy=proxy)
411
439
try:
412
440
driver.get(f"{cls.url}/")
413
WebDriverWait(driver, 1200).until(EC.presence_of_element_located((By.ID, "prompt-textarea")))
441
WebDriverWait(driver, timeout).until(EC.presence_of_element_located((By.ID, "prompt-textarea")))
414
442
access_token = driver.execute_script(
415
443
"let session = await fetch('/api/auth/session');"
416
444
"let data = await session.json();"
@@ -471,7 +499,7 @@ class ResponseFields:
471
499
self.conversation_id = conversation_id
472
500
self.message_id = message_id
473
501
self._end_turn = end_turn
474
502
475
503
class Response():
476
504
"""
477
505
Class to encapsulate a response from the chat service.