gpt4free/g4f/Provider/ChatgptAi.py

57 lines
2.2 KiB
Python
Raw Normal View History

2023-08-27 11:37:44 -04:00
import re, requests
2023-07-28 06:07:17 -04:00
2023-08-27 11:37:44 -04:00
from ..typing import Any, CreateResult
2023-07-28 06:07:17 -04:00
from .base_provider import BaseProvider
class ChatgptAi(BaseProvider):
2023-08-27 11:37:44 -04:00
url: str = "https://chatgpt.ai/gpt-4/"
working = True
supports_gpt_4 = True
2023-07-28 06:07:17 -04:00
@staticmethod
def create_completion(
model: str,
messages: list[dict[str, str]],
2023-08-27 11:37:44 -04:00
stream: bool, **kwargs: Any) -> CreateResult:
2023-07-28 06:07:17 -04:00
chat = ""
for message in messages:
chat += "%s: %s\n" % (message["role"], message["content"])
chat += "assistant: "
response = requests.get("https://chatgpt.ai/")
nonce, post_id, _, bot_id = re.findall(
r'data-nonce="(.*)"\n data-post-id="(.*)"\n data-url="(.*)"\n data-bot-id="(.*)"\n data-width',
2023-08-27 11:37:44 -04:00
response.text)[0]
2023-07-28 06:07:17 -04:00
headers = {
2023-08-27 11:37:44 -04:00
"authority" : "chatgpt.ai",
"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",
"cache-control" : "no-cache",
"origin" : "https://chatgpt.ai",
"pragma" : "no-cache",
"referer" : "https://chatgpt.ai/gpt-4/",
"sec-ch-ua" : '"Not.A/Brand";v="8", "Chromium";v="114", "Google Chrome";v="114"',
"sec-ch-ua-mobile" : "?0",
"sec-ch-ua-platform" : '"Windows"',
"sec-fetch-dest" : "empty",
"sec-fetch-mode" : "cors",
"sec-fetch-site" : "same-origin",
"user-agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36",
2023-07-28 06:07:17 -04:00
}
data = {
2023-08-27 11:37:44 -04:00
"_wpnonce" : nonce,
"post_id" : post_id,
"url" : "https://chatgpt.ai/gpt-4",
"action" : "wpaicg_chat_shortcode_message",
"message" : chat,
"bot_id" : bot_id,
2023-07-28 06:07:17 -04:00
}
response = requests.post(
2023-08-27 11:37:44 -04:00
"https://chatgpt.ai/wp-admin/admin-ajax.php", headers=headers, data=data)
2023-07-28 06:07:17 -04:00
response.raise_for_status()
2023-08-27 11:37:44 -04:00
yield response.json()["data"]