XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 1
返回提交历史

XFEstudio/gpt4free

Update

28ff9610
hlohaus <983577+hlohaus@users.noreply.github.com>
提交于

代码差异

3 个文件 +40 -28
Renamed g4f/Provider/ChatGPTLightweight.py +29 -20
@@ -9,19 +9,19 @@ import re
9 9 import time
10 10 import uuid
11 11
12 from ...typing import AsyncResult, Messages
13 from ...requests import StreamSession
14 from ...requests.cdp import CDPSession
15 from ...requests.raise_for_status import raise_for_status
16 from ..base_provider import AsyncGeneratorProvider, ProviderModelMixin
17 from ..helper import format_prompt
18 from ..openai.proofofwork import generate_proof_token
19 from ..openai.new import get_requirements_token, get_config
20 from ..openai.turnstile_vm import process_turnstile_new
21 from ...providers.response import JsonConversation, FinishReason
22 from ...cookies import get_cookies
23 from ...config import COOKIES_DIR, CUSTOM_COOKIES_DIR
24 from ... import debug
12 from ..typing import AsyncResult, Messages
13 from ..requests import StreamSession
14 from ..requests.cdp import CDPSession
15 from ..requests.raise_for_status import raise_for_status
16 from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
17 from .helper import format_prompt
18 from .openai.proofofwork import generate_proof_token
19 from .openai.new import get_requirements_token, get_config
20 from .openai.turnstile_vm import process_turnstile_new
21 from ..providers.response import JsonConversation, FinishReason
22 from ..cookies import get_cookies
23 from ..config import COOKIES_DIR, CUSTOM_COOKIES_DIR
24 from .. import debug
25 25
26 26
27 27 class Conversation(JsonConversation):
@@ -257,7 +257,7 @@ class ChatGPTLightweight(AsyncGeneratorProvider, ProviderModelMixin):
257 257 client-hint headers the browser actually sends, so later requests
258 258 match the fingerprint the cookies were issued for.
259 259 """
260 async with CDPSession(proxy=proxy) as session:
260 async with CDPSession(proxy=proxy, headless=False) as session:
261 261 try:
262 262 await session.navigate(f"{cls.url}/?q=Hello")
263 263 except Exception as e:
@@ -319,15 +319,15 @@ class ChatGPTLightweight(AsyncGeneratorProvider, ProviderModelMixin):
319 319 proxy: str = None,
320 320 timeout: int = 120,
321 321 ) -> str:
322 if cls._auth_state is None:
322 # if cls._auth_state is None:
323 323 # Harvest fresh cookies and headers from a real browser before
324 324 # falling back to cached HAR captures: a cf_clearance minted for
325 325 # the current IP passes the gate far more reliably.
326 try:
327 cls._auth_state = await cls._harvest_via_cdp(proxy=proxy)
328 cls._auth_state_loaded_at = time.time()
329 except Exception as e:
330 debug.log(f"ChatGPTLightweight: CDP harvest failed: {e}")
326 # try:
327 # cls._auth_state = await cls._harvest_via_cdp(proxy=proxy)
328 # cls._auth_state_loaded_at = time.time()
329 # except Exception as e:
330 # debug.log(f"ChatGPTLightweight: CDP harvest failed: {e}")
331 331 auth_cookies, auth_headers = cls._load_auth_state()
332 332 # Reuse the captured visitor id when available so the session stays
333 333 # consistent with the cf_clearance cookie it was issued with.
@@ -502,3 +502,12 @@ class ChatGPTLightweight(AsyncGeneratorProvider, ProviderModelMixin):
502 502 debug.log(f"ChatGPTLightweight: no assistant block in response: {text[:300]!r}")
503 503 return None
504 504 return html.unescape("".join(blocks[index] for index in sorted(blocks)))
505
506 if __name__ == "__main__":
507 import asyncio
508
509 async def main():
510 response = await ChatGPTLightweight._fetch_reply("Hello", Conversation("auto"))
511 print(response)
512
513 asyncio.run(main())
Modified g4f/Provider/__init__.py +1 -1
@@ -438,7 +438,7 @@ class ProviderLoader:
438 438
439 439 return OpenaiChat
440 440 elif name == "ChatGPTLightweight":
441 from g4f.Provider.needs_auth.ChatGPTLightweight import ChatGPTLightweight
441 from g4f.Provider.ChatGPTLightweight import ChatGPTLightweight
442 442
443 443 return ChatGPTLightweight
444 444 elif name == "OpenaiTemplate":
Modified g4f/requests/raise_for_status.py +10 -7
@@ -47,17 +47,20 @@ async def raise_for_status_async(
47 47 message = await response.json()
48 48 if isinstance(message, list):
49 49 message = message[0] if message else {}
50 error = message.get("error")
51 if isinstance(error, dict):
52 message = error.get("message")
53 else:
54 message = message.get("message", message)
55 if isinstance(error, str):
56 message = f"{error}: {message}"
50 if isinstance(message, dict):
51 error = message.get("error")
52 if isinstance(error, dict):
53 message = error.get("message") or error
54 else:
55 message = message.get("message", message)
56 if isinstance(error, str):
57 message = f"{error}: {message}"
57 58 except json.JSONDecodeError:
58 59 message = await response.text()
59 60 else:
60 61 message = await response.text()
62 if not isinstance(message, str):
63 message = json.dumps(message, default=str)
61 64 if content_type.startswith(
62 65 "text/html"
63 66 ) or message.strip().lower().startswith("<!DOCTYPE".lower()):