返回提交历史
Added
g4f/Provider/ChatGptEs.py
+95
-0
Modified
g4f/Provider/__init__.py
+1
-0
XFEstudio/gpt4free
Added gpt-4o provider
f13b214c
代码差异
2 个文件
+96
-0
@@ -0,0 +1,95 @@
1
from __future__ import annotations
2
3
from ..typing import Messages, CreateResult
4
from ..providers.base_provider import AbstractProvider, ProviderModelMixin
5
6
import requests, os, re, json
7
8
class ChatGptEs(AbstractProvider, ProviderModelMixin):
9
label = "ChatGptEs"
10
working = True
11
supports_message_history = True
12
supports_system_message = True
13
supports_stream = False
14
15
@classmethod
16
def create_completion(
17
cls,
18
model: str,
19
messages: Messages,
20
stream: bool,
21
**kwargs
22
) -> CreateResult:
23
24
if model not in [
25
'gpt-4o',
26
'gpt-4o-mini',
27
'chatgpt-4o-latest'
28
]:
29
raise ValueError(f"Unsupported model: {model}")
30
31
headers = {
32
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
33
'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',
34
'cache-control': 'no-cache',
35
'pragma': 'no-cache',
36
'priority': 'u=0, i',
37
'referer': 'https://www.google.com/',
38
'sec-ch-ua': '"Google Chrome";v="129", "Not=A?Brand";v="8", "Chromium";v="129"',
39
'sec-ch-ua-mobile': '?0',
40
'sec-ch-ua-platform': '"macOS"',
41
'sec-fetch-dest': 'document',
42
'sec-fetch-mode': 'navigate',
43
'sec-fetch-site': 'cross-site',
44
'sec-fetch-user': '?1',
45
'upgrade-insecure-requests': '1',
46
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36',
47
}
48
49
response = requests.get('https://chatgpt.es/', headers=headers)
50
nonce_ = re.findall(r'data-nonce="(.+?)"', response.text)[0]
51
post_id = re.findall(r'data-post-id="(.+?)"', response.text)[0]
52
53
headers = {
54
'accept': '*/*',
55
'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',
56
'cache-control': 'no-cache',
57
'origin': 'https://chatgpt.es',
58
'pragma': 'no-cache',
59
'priority': 'u=1, i',
60
'referer': 'https://chatgpt.es/',
61
'sec-ch-ua': '"Google Chrome";v="129", "Not=A?Brand";v="8", "Chromium";v="129"',
62
'sec-ch-ua-mobile': '?0',
63
'sec-ch-ua-platform': '"macOS"',
64
'sec-fetch-dest': 'empty',
65
'sec-fetch-mode': 'cors',
66
'sec-fetch-site': 'same-origin',
67
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36',
68
}
69
70
conversation_history = [
71
"Human: stricly respond in the same language as my prompt, preferably english"
72
]
73
74
for message in messages[:-1]:
75
if message['role'] == "user":
76
conversation_history.append(f"Human: {message['content']}")
77
else:
78
conversation_history.append(f"AI: {message['content']}")
79
80
payload = {
81
'_wpnonce': nonce_,
82
'post_id': post_id,
83
'url': 'https://chatgpt.es',
84
'action': 'wpaicg_chat_shortcode_message',
85
'message': messages[-1]['content'],
86
'bot_id': '0',
87
'chatbot_identity': 'shortcode',
88
'wpaicg_chat_client_id': os.urandom(5).hex(),
89
'wpaicg_chat_history': json.dumps(conversation_history)
90
}
91
92
response = requests.post('https://chatgpt.es/wp-admin/admin-ajax.php',
93
headers=headers, data=payload).json()
94
95
return (response['data'])
@@ -62,6 +62,7 @@ from .Vercel import Vercel
62
62
from .WhiteRabbitNeo import WhiteRabbitNeo
63
63
from .You import You
64
64
from .ChatGpt import ChatGpt
65
from .ChatGptEs import ChatGptEs
65
66
66
67
import sys
67
68