返回提交历史
Added
unfinished/openaihosted/README.md
+3
-0
Added
unfinished/openaihosted/__init__.py
+38
-0
XFEstudio/gpt4free
added openaihosted folder
e6fc0dc9
代码差异
2 个文件
+41
-0
@@ -0,0 +1,3 @@
1
writegpt.ai
2
to do:
3
- code ref
@@ -0,0 +1,38 @@
1
import requests
2
import json
3
import re
4
5
headers = {
6
'authority': 'openai.a2hosted.com',
7
'accept': 'text/event-stream',
8
'accept-language': 'en-US,en;q=0.9,id;q=0.8,ja;q=0.7',
9
'cache-control': 'no-cache',
10
'sec-fetch-dest': 'empty',
11
'sec-fetch-mode': 'cors',
12
'sec-fetch-site': 'cross-site',
13
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.0.0',
14
}
15
16
def create_query_param(conversation):
17
encoded_conversation = json.dumps(conversation)
18
return encoded_conversation.replace(" ", "%20").replace('"', '%22').replace("'", "%27")
19
20
user_input = input("Enter your message: ")
21
22
data = [
23
{"role": "system", "content": "You are a helpful assistant."},
24
{"role": "user", "content": "hi"},
25
{"role": "assistant", "content": "Hello! How can I assist you today?"},
26
{"role": "user", "content": user_input},
27
]
28
29
query_param = create_query_param(data)
30
url = f'https://openai.a2hosted.com/chat?q={query_param}'
31
32
response = requests.get(url, headers=headers, stream=True)
33
34
for message in response.iter_content(chunk_size=1024):
35
message = message.decode('utf-8')
36
msg_match, num_match = re.search(r'"msg":"(.*?)"', message), re.search(r'\[DONE\] (\d+)', message)
37
if msg_match: print(msg_match.group(1))
38
if num_match: print(num_match.group(1))