返回提交历史
Added
gpt4free/gptworldAi/README.md
+25
-0
Added
gpt4free/gptworldAi/__init__.py
+103
-0
Added
testing/gptworldai_test.py
+18
-0
XFEstudio/gpt4free
add gptworldai
f545f4b4
代码差异
3 个文件
+146
-0
@@ -0,0 +1,25 @@
1
# gptworldAi
2
Written by [hp_mzx](https://github.com/hpsj).
3
4
## Examples:
5
### Completion:
6
```python
7
for chunk in gptworldAi.Completion.create("你是谁", "127.0.0.1:7890"):
8
print(chunk, end="", flush=True)
9
print()
10
```
11
12
### Chat Completion:
13
Support context
14
```python
15
message = []
16
while True:
17
prompt = input("请输入问题:")
18
message.append({"role": "user","content": prompt})
19
text = ""
20
for chunk in gptworldAi.ChatCompletion.create(message,'127.0.0.1:7890'):
21
text = text+chunk
22
print(chunk, end="", flush=True)
23
print()
24
message.append({"role": "assistant", "content": text})
25
```
@@ -0,0 +1,103 @@
1
# -*- coding: utf-8 -*-
2
"""
3
@Time : 2023/5/23 13:37
4
@Auth : Hp_mzx
5
@File :__init__.py.py
6
@IDE :PyCharm
7
"""
8
import json
9
import random
10
import binascii
11
import requests
12
import Crypto.Cipher.AES as AES
13
from fake_useragent import UserAgent
14
15
class ChatCompletion:
16
@staticmethod
17
def create(messages:[],proxy: str = None):
18
url = "https://chat.getgpt.world/api/chat/stream"
19
headers = {
20
"Content-Type": "application/json",
21
"Referer": "https://chat.getgpt.world/",
22
'user-agent': UserAgent().random,
23
}
24
proxies = {'http': 'http://' + proxy, 'https': 'http://' + proxy} if proxy else None
25
data = json.dumps({
26
"messages": messages,
27
"frequency_penalty": 0,
28
"max_tokens": 4000,
29
"model": "gpt-3.5-turbo",
30
"presence_penalty": 0,
31
"temperature": 1,
32
"top_p": 1,
33
"stream": True
34
})
35
signature = ChatCompletion.encrypt(data)
36
res = requests.post(url, headers=headers, data=json.dumps({"signature": signature}), proxies=proxies,stream=True)
37
for chunk in res.iter_content(chunk_size=None):
38
res.raise_for_status()
39
datas = chunk.decode('utf-8').split('data: ')
40
for data in datas:
41
if not data or "[DONE]" in data:
42
continue
43
data_json = json.loads(data)
44
content = data_json['choices'][0]['delta'].get('content')
45
if content:
46
yield content
47
48
49
@staticmethod
50
def random_token(e):
51
token = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
52
n = len(token)
53
return "".join([token[random.randint(0, n - 1)] for i in range(e)])
54
55
@staticmethod
56
def encrypt(e):
57
t = ChatCompletion.random_token(16).encode('utf-8')
58
n = ChatCompletion.random_token(16).encode('utf-8')
59
r = e.encode('utf-8')
60
cipher = AES.new(t, AES.MODE_CBC, n)
61
ciphertext = cipher.encrypt(ChatCompletion.__pad_data(r))
62
return binascii.hexlify(ciphertext).decode('utf-8') + t.decode('utf-8') + n.decode('utf-8')
63
64
@staticmethod
65
def __pad_data(data: bytes) -> bytes:
66
block_size = AES.block_size
67
padding_size = block_size - len(data) % block_size
68
padding = bytes([padding_size] * padding_size)
69
return data + padding
70
71
72
class Completion:
73
@staticmethod
74
def create(prompt:str,proxy:str=None):
75
return ChatCompletion.create([
76
{
77
"content": "You are ChatGPT, a large language model trained by OpenAI.\nCarefully heed the user's instructions. \nRespond using Markdown.",
78
"role": "system"
79
},
80
{"role": "user", "content": prompt}
81
], proxy)
82
83
84
if __name__ == '__main__':
85
# single completion
86
text = ""
87
for chunk in Completion.create("你是谁", "127.0.0.1:7890"):
88
text = text + chunk
89
print(chunk, end="", flush=True)
90
print()
91
92
93
#chat completion
94
message = []
95
while True:
96
prompt = input("请输入问题:")
97
message.append({"role": "user","content": prompt})
98
text = ""
99
for chunk in ChatCompletion.create(message,'127.0.0.1:7890'):
100
text = text+chunk
101
print(chunk, end="", flush=True)
102
print()
103
message.append({"role": "assistant", "content": text})
@@ -0,0 +1,18 @@
1
import gptworldAi
2
3
# single completion
4
for chunk in gptworldAi.Completion.create("你是谁", "127.0.0.1:7890"):
5
print(chunk, end="", flush=True)
6
print()
7
8
# chat completion
9
message = []
10
while True:
11
prompt = input("请输入问题:")
12
message.append({"role": "user", "content": prompt})
13
text = ""
14
for chunk in gptworldAi.ChatCompletion.create(message, '127.0.0.1:7890'):
15
text = text + chunk
16
print(chunk, end="", flush=True)
17
print()
18
message.append({"role": "assistant", "content": text})