ka1kuk commited on
Commit
feab226
1 Parent(s): 7d62b92

Create ChatgptAi.py

Browse files
Files changed (1) hide show
  1. g4f/Provider/Providers/ChatgptAi.py +51 -0
g4f/Provider/Providers/ChatgptAi.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests, re
3
+ from ...typing import sha256, Dict, get_type_hints
4
+
5
+ url = 'https://chatgpt.ai/gpt-4/'
6
+ model = ['gpt-4']
7
+ supports_stream = True
8
+ needs_auth = False
9
+
10
+
11
+ def _create_completion(model: str, messages: list, stream: bool, **kwargs):
12
+ chat = ''
13
+ for message in messages:
14
+ chat += '%s: %s\n' % (message['role'], message['content'])
15
+ chat += 'user: '
16
+
17
+ response = requests.get('https://chatgpt.ai/')
18
+ nonce, post_id, _, bot_id = re.findall(r'data-nonce="(.*)"\n data-post-id="(.*)"\n data-url="(.*)"\n data-bot-id="(.*)"\n data-width', response.text)[0]
19
+
20
+ headers = {
21
+ 'authority': 'chatgpt.ai',
22
+ 'accept': '*/*',
23
+ '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',
24
+ 'cache-control': 'no-cache',
25
+ 'origin': 'https://chatgpt.ai',
26
+ 'pragma': 'no-cache',
27
+ 'referer': 'https://chatgpt.ai/gpt-4/',
28
+ 'sec-ch-ua': '"Not.A/Brand";v="8", "Chromium";v="114", "Google Chrome";v="114"',
29
+ 'sec-ch-ua-mobile': '?0',
30
+ 'sec-ch-ua-platform': '"Windows"',
31
+ 'sec-fetch-dest': 'empty',
32
+ 'sec-fetch-mode': 'cors',
33
+ 'sec-fetch-site': 'same-origin',
34
+ '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',
35
+ }
36
+ data = {
37
+ '_wpnonce': nonce,
38
+ 'post_id': post_id,
39
+ 'url': 'https://chatgpt.ai/gpt-4',
40
+ 'action': 'wpaicg_chat_shortcode_message',
41
+ 'message': chat,
42
+ 'bot_id': bot_id
43
+ }
44
+
45
+ response = requests.post('https://chatgpt.ai/wp-admin/admin-ajax.php',
46
+ headers=headers, data=data)
47
+
48
+ yield (response.json()['data'])
49
+
50
+ params = f'g4f.Providers.{os.path.basename(__file__)[:-3]} supports: ' + \
51
+ '(%s)' % ', '.join([f"{name}: {get_type_hints(_create_completion)[name].__name__}" for name in _create_completion.__code__.co_varnames[:_create_completion.__code__.co_argcount]])