返回提交历史
Modified
g4f/Provider/Yupp.py
+60
-9
XFEstudio/gpt4free
Refactor Yupp provider to enhance media handling and improve request structure
5ca51339
代码差异
1 个文件
+60
-9
@@ -12,6 +12,8 @@ from ..providers.response import Reasoning, PlainTextResponse, PreviewResponse,
12
12
from ..errors import RateLimitError, ProviderException, MissingAuthError
13
13
from ..cookies import get_cookies
14
14
from ..tools.auth import AuthManager
15
from ..tools.media import merge_media
16
from ..image import is_accepted_format, to_bytes
15
17
from .yupp.models import YuppModelManager
16
18
from .helper import get_last_message
17
19
from ..debug import log
@@ -198,13 +200,11 @@ class Yupp(AbstractProvider, ProviderModelMixin):
198
200
Create completion using Yupp.ai API with account rotation
199
201
"""
200
202
# Initialize Yupp accounts and models
201
if not api_key and len(YUPP_ACCOUNTS) <= 1:
203
if not api_key:
202
204
api_key = get_cookies("yupp.ai", False).get("__Secure-yupp.session-token")
203
205
if api_key:
204
206
load_yupp_accounts(api_key)
205
207
log_debug(f"Yupp provider initialized with {len(YUPP_ACCOUNTS)} accounts")
206
elif YUPP_ACCOUNTS:
207
log_debug(f"Yupp provider using existing accounts: {len(YUPP_ACCOUNTS)}")
208
208
else:
209
209
raise MissingAuthError("No Yupp accounts configured. Set YUPP_API_KEY environment variable.")
210
210
@@ -232,7 +232,7 @@ class Yupp(AbstractProvider, ProviderModelMixin):
232
232
233
233
try:
234
234
yield from cls._make_yupp_request(
235
account, prompt, model, url_uuid, **kwargs
235
account, messages, prompt, model, url_uuid, **kwargs
236
236
)
237
237
return # Success, exit the loop
238
238
@@ -261,14 +261,66 @@ class Yupp(AbstractProvider, ProviderModelMixin):
261
261
def _make_yupp_request(
262
262
cls,
263
263
account: Dict[str, Any],
264
question: str,
264
messages: List[Dict[str, str]],
265
prompt: str,
265
266
model_id: str,
266
267
url_uuid: Optional[str] = None,
268
media: List[str] = None,
267
269
next_action: str = "7f2a2308b5fc462a2c26df714cb2cccd02a9c10fbb",
268
270
**kwargs
269
271
) -> Generator[str, Any, None]:
270
272
"""Make actual request to Yupp.ai"""
273
274
session = create_requests_session()
275
276
files = []
277
for file, name in list(merge_media(media, messages)):
278
data = to_bytes(file)
279
url = "https://yupp.ai/api/trpc/chat.createPresignedURLForUpload?batch=1"
280
payload = {
281
"0": {
282
"json": {
283
"fileName": name,
284
"fileSize": len(data),
285
"contentType": is_accepted_format(data)
286
}
287
}
288
}
289
response = session.post(url, json=payload, headers={
290
"Content-Type": "application/json",
291
"Cookie": f"__Secure-yupp.session-token={account['token']}",
292
})
293
response.raise_for_status()
294
upload_info = response.json()[0]["result"]["data"]["json"]
295
upload_url = upload_info["signedUrl"]
296
upload_resp = session.put(upload_url, data=data, headers={
297
"Content-Type": is_accepted_format(data),
298
"Cookie": f"__Secure-yupp.session-token={account['token']}",
299
"Content-Length": str(len(data)),
300
})
301
upload_resp.raise_for_status()
302
url = "https://yupp.ai/api/trpc/chat.createAttachmentForUploadedFile?batch=1"
303
response = session.post(url, json={
304
"0": {
305
"json": {
306
"fileName": name,
307
"contentType": is_accepted_format(data),
308
"fileId": upload_info["fileId"],
309
}
310
}
311
}, cookies={
312
"__Secure-yupp.session-token": account["token"]
313
})
314
response.raise_for_status()
315
attachment = response.json()[0]["result"]["data"]["json"]
316
files.append({
317
"fileName": attachment["file_name"],
318
"contentType": attachment["content_type"],
319
"attachmentId": attachment["attachment_id"],
320
"chatMessageId": ""
321
})
271
322
323
272
324
# Build request
273
325
if url_uuid is None:
274
326
url_uuid = str(uuid.uuid4())
@@ -286,10 +338,10 @@ class Yupp(AbstractProvider, ProviderModelMixin):
286
338
payload = [
287
339
url_uuid,
288
340
str(uuid.uuid4()),
289
question,
341
prompt,
290
342
"$undefined",
291
343
"$undefined",
292
[],
344
files,
293
345
"$undefined",
294
346
[{"modelName": model_id, "promptModifierId": "$undefined"}] if model_id else "none",
295
347
"text",
@@ -298,7 +350,6 @@ class Yupp(AbstractProvider, ProviderModelMixin):
298
350
]
299
351
300
352
# Send request
301
session = create_requests_session()
302
353
response = session.post(
303
354
url,
304
355
data=json.dumps(payload),
@@ -309,7 +360,7 @@ class Yupp(AbstractProvider, ProviderModelMixin):
309
360
response.raise_for_status()
310
361
311
362
yield from cls._process_stream_response(
312
response.iter_lines(), account, session, question, model_id
363
response.iter_lines(), account, session, prompt, model_id
313
364
)
314
365
315
366
@classmethod