forked from XksA-me/ChatGPT-3.5-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo-cmd.py
102 lines (85 loc) · 3.16 KB
/
demo-cmd.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import openai
import json
import os
os.environ["HTTP_PROXY"] = "http://127.0.0.1:7890"
os.environ["HTTPS_PROXY"] = "http://127.0.0.1:7890"
# 获取 api
def get_api_key():
# 可以自己根据自己实际情况实现
# 以我为例子,我是存在一个 openai_key 文件里,json 格式
'''
{"api": "你的 api keys"}
'''
openai_key_file = './envs/openai_key'
with open(openai_key_file, 'r', encoding='utf-8') as f:
openai_key = json.loads(f.read())
return openai_key['api']
openai.api_key = get_api_key()
class ChatGPT:
def __init__(self, user):
self.user = user
self.messages = [{"role": "system", "content": "一个有10年Python开发经验的资深算法工程师"}]
self.filename="./user_messages.json"
def ask_gpt(self):
# q = "用python实现:提示手动输入3个不同的3位数区间,输入结束后计算这3个区间的交集,并输出结果区间"
rsp = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=self.messages
)
return rsp.get("choices")[0]["message"]["content"]
def writeTojson(self):
try:
# 判断文件是否存在
if not os.path.exists(self.filename):
with open(self.filename, "w") as f:
# 创建文件
pass
# 读取
with open(self.filename, 'r', encoding='utf-8') as f:
content = f.read()
msgs = json.loads(content) if len(content) > 0 else {}
# 追加
msgs.update({self.user : self.messages})
# 写入
with open(self.filename, 'w', encoding='utf-8') as f:
json.dump(msgs, f)
except Exception as e:
print(f"错误代码:{e}")
def main():
user = input("请输入用户名称: ")
chat = ChatGPT(user)
# 循环
while 1:
# 限制对话次数
if len(chat.messages) > 11:
print("******************************")
print("*********强制重置对话**********")
print("******************************")
# 写入之前信息
chat.writeTojson()
user = input("请输入用户名称: ")
chat = ChatGPT(user)
# 提问
q = input(f"【{chat.user}】")
# 逻辑判断
if q == "0":
print("*********退出程序**********")
# 写入之前信息
chat.writeTojson()
break
elif q == "1":
print("**************************")
print("*********重置对话**********")
print("**************************")
# 写入之前信息
chat.writeTojson()
user = input("请输入用户名称: ")
chat = ChatGPT(user)
continue
# 提问-回答-记录
chat.messages.append({"role": "user", "content": q})
answer = chat.ask_gpt()
print(f"【ChatGPT】{answer}")
chat.messages.append({"role": "assistant", "content": answer})
if __name__ == '__main__':
main()