forked from chatanywhere/GPT_API_free
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.py
45 lines (37 loc) · 1.46 KB
/
demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import openai
# openai.log = "debug"
openai.api_key = "sk-"
openai.api_base = "https://api.chatanywhere.com.cn/v1"
# 非流式响应
# completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hello world!"}])
# print(completion.choices[0].message.content)
def gpt_35_api_stream(messages: list):
"""为提供的对话消息创建新的回答 (流式传输)
Args:
messages (list): 完整的对话消息
api_key (str): OpenAI API 密钥
Returns:
tuple: (results, error_desc)
"""
try:
response = openai.ChatCompletion.create(
model='gpt-3.5-turbo',
messages=messages,
stream=True,
)
completion = {'role': '', 'content': ''}
for event in response:
if event['choices'][0]['finish_reason'] == 'stop':
print(f'收到的完成数据: {completion}')
break
for delta_k, delta_v in event['choices'][0]['delta'].items():
print(f'流响应数据: {delta_k} = {delta_v}')
completion[delta_k] += delta_v
messages.append(completion) # 直接在传入参数 messages 中追加消息
return (True, '')
except Exception as err:
return (False, f'OpenAI API 异常: {err}')
if __name__ == '__main__':
messages = [{'role': 'user','content': '鲁迅和周树人的关系'},]
print(gpt_35_api_stream(messages))
print(messages)