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

XFEstudio/gpt4free

Add py_arkose_generator in OpenaiChat

c796ba22
Heiner Lohaus <hlohaus@users.noreply.github.com>
提交于

代码差异

2 个文件 +14 -49
Modified g4f/Provider/needs_auth/OpenaiChat.py +12 -48
@@ -1,8 +1,7 @@
1 1 from __future__ import annotations
2 2
3 3 import uuid, json, time, os
4 import tempfile, shutil, asyncio
5 import sys, subprocess
4 from py_arkose_generator.arkose import get_values_for_request
6 5
7 6 from ..base_provider import AsyncGeneratorProvider
8 7 from ..helper import get_browser, get_cookies, format_prompt, get_event_loop
@@ -145,57 +144,22 @@ class OpenaiChat(AsyncGeneratorProvider):
145 144 return f"g4f.provider.{cls.__name__} supports: ({param})"
146 145
147 146 async def get_arkose_token(proxy: str = None) -> str:
148 dir = os.path.dirname(os.path.dirname(__file__))
149 include = f'{dir}/npm/node_modules/funcaptcha'
150 147 config = {
151 148 "pkey": "3D86FBBA-9D22-402A-B512-3420086BA6CC",
152 149 "surl": "https://tcr9i.chat.openai.com",
153 "data": {},
154 150 "headers": {
155 151 "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'
156 152 },
157 153 "site": "https://chat.openai.com",
158 "proxy": proxy
159 154 }
160 source = """
161 fun = require({include})
162 config = {config}
163 fun.getToken(config).then(token => {
164 console.log(token.token)
165 })
166 """
167 source = source.replace('{include}', json.dumps(include))
168 source = source.replace('{config}', json.dumps(config))
169 tmp = tempfile.NamedTemporaryFile(delete=False)
170 tmp.write(source.encode())
171 tmp.close()
172 try:
173 return await exec_js(tmp.name)
174 finally:
175 os.unlink(tmp.name)
176
177 async def exec_js(file: str) -> str:
178 node = shutil.which("node")
179 if not node:
180 if debug.logging:
181 print('OpenaiChat: "node" not found')
182 return
183 if sys.platform == 'win32':
184 p = subprocess.Popen(
185 [node, file],
186 stdout=subprocess.PIPE,
187 stderr=subprocess.PIPE
188 )
189 stdout, stderr = p.communicate()
190 if p.returncode == 0:
191 return stdout.decode()
192 raise RuntimeError(f"Exec Error: {stderr.decode()}")
193 p = await asyncio.create_subprocess_exec(
194 node, file,
195 stderr=asyncio.subprocess.PIPE,
196 stdout=asyncio.subprocess.PIPE
197 )
198 stdout, stderr = await p.communicate()
199 if p.returncode == 0:
200 return stdout.decode()
201 raise RuntimeError(f"Exec Error: {stderr.decode()}")
155 args_for_request = get_values_for_request(config)
156 async with StreamSession(
157 proxies={"https": proxy},
158 impersonate="chrome107",
159 ) as session:
160 async with session.post(**args_for_request) as response:
161 response.raise_for_status()
162 decoded_json = await response.json()
163 if "token" in decoded_json:
164 return decoded_json["token"]
165 raise RuntimeError(f"Response: {decoded_json}")
Modified requirements.txt +2 -1
@@ -20,4 +20,5 @@ numpy
20 20 asgiref
21 21 fastapi
22 22 uvicorn
23 flask
23 flask
24 git+https://github.com/hlohaus/py-arkose-token-generator.git