t.me/xtekky
commited on
Commit
•
662959a
1
Parent(s):
38fd688
unofficial chat.openai.com api (experimental)
Browse files
experimental/chat.openai.com.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# experimental, needs chat.openai.com to be loaded with cf_clearance on browser ( can be closed after )
|
2 |
+
|
3 |
+
from tls_client import Session
|
4 |
+
from uuid import uuid4
|
5 |
+
|
6 |
+
from browser_cookie3 import chrome
|
7 |
+
|
8 |
+
def session_auth(client):
|
9 |
+
headers = {
|
10 |
+
'authority': 'chat.openai.com',
|
11 |
+
'accept': '*/*',
|
12 |
+
'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',
|
13 |
+
'cache-control': 'no-cache',
|
14 |
+
'pragma': 'no-cache',
|
15 |
+
'referer': 'https://chat.openai.com/chat',
|
16 |
+
'sec-ch-ua': '"Chromium";v="112", "Google Chrome";v="112", "Not:A-Brand";v="99"',
|
17 |
+
'sec-ch-ua-mobile': '?0',
|
18 |
+
'sec-ch-ua-platform': '"macOS"',
|
19 |
+
'sec-fetch-dest': 'empty',
|
20 |
+
'sec-fetch-mode': 'cors',
|
21 |
+
'sec-fetch-site': 'same-origin',
|
22 |
+
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36',
|
23 |
+
}
|
24 |
+
|
25 |
+
return client.get('https://chat.openai.com/api/auth/session', headers=headers).json()
|
26 |
+
|
27 |
+
client = Session(client_identifier='chrome110')
|
28 |
+
|
29 |
+
for cookie in chrome(domain_name='chat.openai.com'):
|
30 |
+
client.cookies[cookie.name] = cookie.value
|
31 |
+
|
32 |
+
client.headers = {
|
33 |
+
'authority': 'chat.openai.com',
|
34 |
+
'accept': 'text/event-stream',
|
35 |
+
'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',
|
36 |
+
'authorization': 'Bearer ' + session_auth(client)['accessToken'],
|
37 |
+
'cache-control': 'no-cache',
|
38 |
+
'content-type': 'application/json',
|
39 |
+
'origin': 'https://chat.openai.com',
|
40 |
+
'pragma': 'no-cache',
|
41 |
+
'referer': 'https://chat.openai.com/chat',
|
42 |
+
'sec-ch-ua': '"Chromium";v="112", "Google Chrome";v="112", "Not:A-Brand";v="99"',
|
43 |
+
'sec-ch-ua-mobile': '?0',
|
44 |
+
'sec-ch-ua-platform': '"macOS"',
|
45 |
+
'sec-fetch-dest': 'empty',
|
46 |
+
'sec-fetch-mode': 'cors',
|
47 |
+
'sec-fetch-site': 'same-origin',
|
48 |
+
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36',
|
49 |
+
}
|
50 |
+
|
51 |
+
response = client.post('https://chat.openai.com/backend-api/conversation', json = {
|
52 |
+
'action': 'next',
|
53 |
+
'messages': [
|
54 |
+
{
|
55 |
+
'id': str(uuid4()),
|
56 |
+
'author': {
|
57 |
+
'role': 'user',
|
58 |
+
},
|
59 |
+
'content': {
|
60 |
+
'content_type': 'text',
|
61 |
+
'parts': [
|
62 |
+
'hello world',
|
63 |
+
],
|
64 |
+
},
|
65 |
+
},
|
66 |
+
],
|
67 |
+
'parent_message_id': '9b4682f7-977c-4c8a-b5e6-9713e73dfe01',
|
68 |
+
'model': 'text-davinci-002-render-sha',
|
69 |
+
'timezone_offset_min': -120,
|
70 |
+
})
|
71 |
+
|
72 |
+
print(response.text)
|