返回提交历史
Modified
gpt4free/theb/README.md
+6
-3
Modified
gpt4free/theb/__init__.py
+11
-3
XFEstudio/gpt4free
theb: multiple messages
fb054408
代码差异
2 个文件
+17
-6
@@ -5,7 +5,10 @@
5
5
from gpt4free import theb
6
6
7
7
# simple streaming completion
8
for token in theb.Completion.create('hello world'):
9
print(token, end='', flush=True)
10
print("")
8
9
while True:
10
x = input()
11
for token in theb.Completion.create(x):
12
print(token, end='', flush=True)
13
print("")
11
14
```
@@ -17,6 +17,7 @@ class Completion:
17
17
timer = None
18
18
message_queue = Queue()
19
19
stream_completed = False
20
last_msg_id = None
20
21
21
22
@staticmethod
22
23
def request(prompt: str, proxy: Optional[str]=None):
@@ -28,26 +29,33 @@ class Completion:
28
29
}
29
30
30
31
proxies = {'http': 'http://' + proxy, 'https': 'http://' + proxy} if proxy else None
31
32
33
options = {}
34
if Completion.last_msg_id:
35
options['parentMessageId'] = Completion.last_msg_id
36
32
37
requests.post(
33
38
'https://chatbot.theb.ai/api/chat-process',
34
39
headers=headers,
35
40
proxies=proxies,
36
41
content_callback=Completion.handle_stream_response,
37
json={'prompt': prompt, 'options': {}},
42
json={'prompt': prompt, 'options': options},
38
43
)
39
44
40
45
Completion.stream_completed = True
41
46
42
47
@staticmethod
43
48
def create(prompt: str, proxy: Optional[str]=None) -> Generator[str, None, None]:
49
Completion.stream_completed = False
44
50
Thread(target=Completion.request, args=[prompt, proxy]).start()
45
51
46
52
while not Completion.stream_completed or not Completion.message_queue.empty():
47
53
try:
48
54
message = Completion.message_queue.get(timeout=0.01)
49
55
for message in findall(Completion.regex, message):
50
yield loads(Completion.part1 + message + Completion.part2)['delta']
56
message_json = loads(Completion.part1 + message + Completion.part2)
57
Completion.last_msg_id = message_json['id']
58
yield message_json['delta']
51
59
52
60
except Empty:
53
61
pass