mirror of
https://github.com/xtekky/gpt4free.git
synced 2024-11-10 19:11:01 +00:00
Fix ChatgptAi Provider
This commit is contained in:
parent
78f93bb737
commit
fc15181110
@ -6,7 +6,7 @@ sys.path.append(str(Path(__file__).parent.parent))
|
|||||||
sys.path.append(str(Path(__file__).parent.parent.parent))
|
sys.path.append(str(Path(__file__).parent.parent.parent))
|
||||||
|
|
||||||
import g4f
|
import g4f
|
||||||
from testing.test_providers import get_providers
|
from testing._providers import get_providers
|
||||||
from testing.log_time import log_time_async
|
from testing.log_time import log_time_async
|
||||||
|
|
||||||
async def create_async(provider):
|
async def create_async(provider):
|
||||||
|
@ -1,36 +1,34 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import re
|
import re, html, json, string, random
|
||||||
from aiohttp import ClientSession
|
from aiohttp import ClientSession
|
||||||
|
|
||||||
from ..typing import Messages
|
from ..typing import Messages, AsyncResult
|
||||||
from .base_provider import AsyncProvider, format_prompt
|
from .base_provider import AsyncGeneratorProvider
|
||||||
|
|
||||||
|
|
||||||
class ChatgptAi(AsyncProvider):
|
class ChatgptAi(AsyncGeneratorProvider):
|
||||||
url: str = "https://chatgpt.ai/"
|
url: str = "https://chatgpt.ai"
|
||||||
working = True
|
working = True
|
||||||
supports_gpt_35_turbo = True
|
supports_gpt_35_turbo = True
|
||||||
_nonce = None
|
_system = None
|
||||||
_post_id = None
|
|
||||||
_bot_id = None
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def create_async(
|
async def create_async_generator(
|
||||||
cls,
|
cls,
|
||||||
model: str,
|
model: str,
|
||||||
messages: Messages,
|
messages: Messages,
|
||||||
proxy: str = None,
|
proxy: str = None,
|
||||||
**kwargs
|
**kwargs
|
||||||
) -> str:
|
) -> AsyncResult:
|
||||||
headers = {
|
headers = {
|
||||||
"authority" : "chatgpt.ai",
|
"authority" : "chatgpt.ai",
|
||||||
"accept" : "*/*",
|
"accept" : "*/*",
|
||||||
"accept-language" : "en,fr-FR;q=0.9,fr;q=0.8,es-ES;q=0.7,es;q=0.6,en-US;q=0.5,am;q=0.4,de;q=0.3",
|
"accept-language" : "en-US",
|
||||||
"cache-control" : "no-cache",
|
"cache-control" : "no-cache",
|
||||||
"origin" : "https://chatgpt.ai",
|
"origin" : cls.url,
|
||||||
"pragma" : "no-cache",
|
"pragma" : "no-cache",
|
||||||
"referer" : cls.url,
|
"referer" : f"{cls.url}/",
|
||||||
"sec-ch-ua" : '"Not.A/Brand";v="8", "Chromium";v="114", "Google Chrome";v="114"',
|
"sec-ch-ua" : '"Not.A/Brand";v="8", "Chromium";v="114", "Google Chrome";v="114"',
|
||||||
"sec-ch-ua-mobile" : "?0",
|
"sec-ch-ua-mobile" : "?0",
|
||||||
"sec-ch-ua-platform" : '"Windows"',
|
"sec-ch-ua-platform" : '"Windows"',
|
||||||
@ -42,34 +40,40 @@ class ChatgptAi(AsyncProvider):
|
|||||||
async with ClientSession(
|
async with ClientSession(
|
||||||
headers=headers
|
headers=headers
|
||||||
) as session:
|
) as session:
|
||||||
if not cls._nonce:
|
if not cls._system:
|
||||||
async with session.get(cls.url, proxy=proxy) as response:
|
async with session.get(cls.url, proxy=proxy) as response:
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
text = await response.text()
|
text = await response.text()
|
||||||
result = re.search(r'data-nonce="(.*?)"', text)
|
result = re.search(r"data-system='(.*?)'", text)
|
||||||
if result:
|
if result:
|
||||||
cls._nonce = result.group(1)
|
cls._system = json.loads(html.unescape(result.group(1)))
|
||||||
result = re.search(r'data-post-id="(.*?)"', text)
|
if not cls._system:
|
||||||
if result:
|
raise RuntimeError("System args not found")
|
||||||
cls._post_id = result.group(1)
|
|
||||||
result = re.search(r'data-bot-id="(.*?)"', text)
|
|
||||||
if result:
|
|
||||||
cls._bot_id = result.group(1)
|
|
||||||
if not cls._nonce or not cls._post_id or not cls._bot_id:
|
|
||||||
raise RuntimeError("Nonce, post-id or bot-id not found")
|
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
"_wpnonce": cls._nonce,
|
"botId": cls._system["botId"],
|
||||||
"post_id": cls._post_id,
|
"customId": cls._system["customId"],
|
||||||
"url": "https://chatgpt.ai",
|
"session": cls._system["sessionId"],
|
||||||
"action": "wpaicg_chat_shortcode_message",
|
"chatId": "".join(random.choices(f"{string.ascii_lowercase}{string.digits}", k=11)),
|
||||||
"message": format_prompt(messages),
|
"contextId": cls._system["contextId"],
|
||||||
"bot_id": cls._bot_id
|
"messages": messages,
|
||||||
|
"newMessage": messages[-1]["content"],
|
||||||
|
"stream": True
|
||||||
}
|
}
|
||||||
async with session.post(
|
async with session.post(
|
||||||
"https://chatgpt.ai/wp-admin/admin-ajax.php",
|
f"{cls.url}/wp-json/mwai-ui/v1/chats/submit",
|
||||||
proxy=proxy,
|
proxy=proxy,
|
||||||
data=data
|
json=data
|
||||||
) as response:
|
) as response:
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
return (await response.json())["data"]
|
async for line in response.content:
|
||||||
|
if line.startswith(b"data: "):
|
||||||
|
try:
|
||||||
|
line = json.loads(line[6:])
|
||||||
|
assert "type" in line
|
||||||
|
except:
|
||||||
|
raise RuntimeError(f"Broken line: {line.decode()}")
|
||||||
|
if line["type"] == "live":
|
||||||
|
yield line["data"]
|
||||||
|
elif line["type"] == "end":
|
||||||
|
break
|
@ -14,11 +14,15 @@ class GeekGpt(BaseProvider):
|
|||||||
supports_gpt_4 = True
|
supports_gpt_4 = True
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def create_completion(cls,
|
def create_completion(
|
||||||
model: str,
|
cls,
|
||||||
messages: Messages,
|
model: str,
|
||||||
stream: bool, **kwargs) -> CreateResult:
|
messages: Messages,
|
||||||
|
stream: bool,
|
||||||
|
**kwargs
|
||||||
|
) -> CreateResult:
|
||||||
|
if not model:
|
||||||
|
model = "gpt-3.5-turbo"
|
||||||
json_data = {
|
json_data = {
|
||||||
'messages': messages,
|
'messages': messages,
|
||||||
'model': model,
|
'model': model,
|
||||||
|
Loading…
Reference in New Issue
Block a user