Spaces:
Sleeping
Sleeping
Merge branch 'main' into main
Browse files- ChuanhuChatbot.py +36 -2
- README.md +7 -7
ChuanhuChatbot.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
import openai
|
3 |
import os
|
@@ -93,12 +94,33 @@ def reduce_token(chatbot, system, context, myKey):
|
|
93 |
statistics = f'本次对话Tokens用量【{response["usage"]["completion_tokens"]+12+12+8} / 4096】'
|
94 |
optmz_str = parse_text( f'好的,我们之前聊了:{response["choices"][0]["message"]["content"]}\n\n================\n\n{statistics}' )
|
95 |
chatbot.append(("请帮我总结一下上述对话的内容,实现减少tokens的同时,保证对话的质量。", optmz_str))
|
96 |
-
|
97 |
context = []
|
98 |
context.append({"role": "user", "content": "我们之前聊了什么?"})
|
99 |
context.append({"role": "assistant", "content": f'我们之前聊了:{response["choices"][0]["message"]["content"]}'})
|
100 |
return chatbot, context
|
101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
|
103 |
def reset_state():
|
104 |
return [], []
|
@@ -122,6 +144,7 @@ with gr.Blocks() as demo:
|
|
122 |
context = gr.State([])
|
123 |
systemPrompt = gr.State(update_system(initial_prompt))
|
124 |
myKey = gr.State("sk-xxxxxxxxxxxxxxxxxxxxx")
|
|
|
125 |
|
126 |
with gr.Row():
|
127 |
with gr.Column(scale=12):
|
@@ -133,9 +156,16 @@ with gr.Blocks() as demo:
|
|
133 |
retryBtn = gr.Button("🔄 重新生成")
|
134 |
delLastBtn = gr.Button("🗑️ 删除上条对话")
|
135 |
reduceTokenBtn = gr.Button("♻️ 优化Tokens")
|
136 |
-
|
137 |
newSystemPrompt = gr.Textbox(show_label=True, placeholder=f"在这里输入新的System Prompt...", label="更改 System prompt").style(container=True)
|
138 |
systemPromptDisplay = gr.Textbox(show_label=True, value=initial_prompt, interactive=False, label="目前的 System prompt").style(container=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
139 |
|
140 |
txt.submit(predict, [chatbot, txt, systemPrompt, context, myKey], [chatbot, context], show_progress=True)
|
141 |
txt.submit(lambda :"", None, txt)
|
@@ -149,6 +179,10 @@ with gr.Blocks() as demo:
|
|
149 |
delLastBtn.click(delete_last_conversation, [chatbot, context], [chatbot, context], show_progress=True)
|
150 |
reduceTokenBtn.click(reduce_token, [chatbot, systemPrompt, context, myKey], [chatbot, context], show_progress=True)
|
151 |
keyTxt.submit(set_apikey, [keyTxt, myKey], [keyTxt, myKey], show_progress=True)
|
|
|
|
|
|
|
|
|
152 |
|
153 |
|
154 |
demo.launch()
|
|
|
1 |
+
import json
|
2 |
import gradio as gr
|
3 |
import openai
|
4 |
import os
|
|
|
94 |
statistics = f'本次对话Tokens用量【{response["usage"]["completion_tokens"]+12+12+8} / 4096】'
|
95 |
optmz_str = parse_text( f'好的,我们之前聊了:{response["choices"][0]["message"]["content"]}\n\n================\n\n{statistics}' )
|
96 |
chatbot.append(("请帮我总结一下上述对话的内容,实现减少tokens的同时,保证对话的质量。", optmz_str))
|
97 |
+
|
98 |
context = []
|
99 |
context.append({"role": "user", "content": "我们之前聊了什么?"})
|
100 |
context.append({"role": "assistant", "content": f'我们之前聊了:{response["choices"][0]["message"]["content"]}'})
|
101 |
return chatbot, context
|
102 |
|
103 |
+
def save_chat_history(filepath, system, context):
|
104 |
+
if filepath == "":
|
105 |
+
return
|
106 |
+
history = {"system": system, "context": context}
|
107 |
+
with open(f"{filepath}.json", "w") as f:
|
108 |
+
json.dump(history, f)
|
109 |
+
|
110 |
+
def load_chat_history(fileobj):
|
111 |
+
with open(fileobj.name, "r") as f:
|
112 |
+
history = json.load(f)
|
113 |
+
context = history["context"]
|
114 |
+
chathistory = []
|
115 |
+
for i in range(0, len(context), 2):
|
116 |
+
chathistory.append((parse_text(context[i]["content"]), parse_text(context[i+1]["content"])))
|
117 |
+
return chathistory , history["system"], context, history["system"]["content"]
|
118 |
+
|
119 |
+
def get_history_names():
|
120 |
+
with open("history.json", "r") as f:
|
121 |
+
history = json.load(f)
|
122 |
+
return list(history.keys())
|
123 |
+
|
124 |
|
125 |
def reset_state():
|
126 |
return [], []
|
|
|
144 |
context = gr.State([])
|
145 |
systemPrompt = gr.State(update_system(initial_prompt))
|
146 |
myKey = gr.State("sk-xxxxxxxxxxxxxxxxxxxxx")
|
147 |
+
topic = gr.State("未命名对话历史记录")
|
148 |
|
149 |
with gr.Row():
|
150 |
with gr.Column(scale=12):
|
|
|
156 |
retryBtn = gr.Button("🔄 重新生成")
|
157 |
delLastBtn = gr.Button("🗑️ 删除上条对话")
|
158 |
reduceTokenBtn = gr.Button("♻️ 优化Tokens")
|
|
|
159 |
newSystemPrompt = gr.Textbox(show_label=True, placeholder=f"在这里输入新的System Prompt...", label="更改 System prompt").style(container=True)
|
160 |
systemPromptDisplay = gr.Textbox(show_label=True, value=initial_prompt, interactive=False, label="目前的 System prompt").style(container=True)
|
161 |
+
with gr.Accordion(label="保存/加载对话历史记录(在文本框中输入文件名,点击“保存对话”按钮,历史记录文件会被存储到本地)", open=False):
|
162 |
+
with gr.Column():
|
163 |
+
with gr.Row():
|
164 |
+
with gr.Column(scale=6):
|
165 |
+
saveFileName = gr.Textbox(show_label=True, placeholder=f"在这里输入保存的文件名...", label="保存对话", value="对话历史记录").style(container=True)
|
166 |
+
with gr.Column(scale=1):
|
167 |
+
saveBtn = gr.Button("💾 保存对话")
|
168 |
+
uploadBtn = gr.UploadButton("📂 读取对话", file_count="single", file_types=["json"])
|
169 |
|
170 |
txt.submit(predict, [chatbot, txt, systemPrompt, context, myKey], [chatbot, context], show_progress=True)
|
171 |
txt.submit(lambda :"", None, txt)
|
|
|
179 |
delLastBtn.click(delete_last_conversation, [chatbot, context], [chatbot, context], show_progress=True)
|
180 |
reduceTokenBtn.click(reduce_token, [chatbot, systemPrompt, context, myKey], [chatbot, context], show_progress=True)
|
181 |
keyTxt.submit(set_apikey, [keyTxt, myKey], [keyTxt, myKey], show_progress=True)
|
182 |
+
reduceTokenBtn.click(reduce_token, [chatbot, systemPrompt, context, myKey], [chatbot, context], show_progress=True)
|
183 |
+
keyTxt.submit(set_apikey, [keyTxt, myKey], [keyTxt, myKey], show_progress=True)
|
184 |
+
uploadBtn.upload(load_chat_history, uploadBtn, [chatbot, systemPrompt, context, systemPromptDisplay], show_progress=True)
|
185 |
+
saveBtn.click(save_chat_history, [saveFileName, systemPrompt, context], None, show_progress=True)
|
186 |
|
187 |
|
188 |
demo.launch()
|
README.md
CHANGED
@@ -6,6 +6,13 @@
|
|
6 |
GUI for ChatGPT API
|
7 |
<img width="1204" alt="截屏2023-03-03 13 59 46" src="https://user-images.githubusercontent.com/51039745/222643242-c0b90a54-8f07-4fb6-b88e-ef338fd80f49.png">
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
## 安装方式
|
10 |
|
11 |
- 填入你的 OpenAI API 密钥
|
@@ -59,10 +66,3 @@ docker run -d --name chatgpt -e my_api_key="替换成API" --network host chuanh
|
|
59 |
docker logs chatgpt
|
60 |
```
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
## 使用技巧
|
66 |
-
|
67 |
-
- 使用System Prompt可以很有效地设定前提条件
|
68 |
-
- 对于长对话,可以使用“优化Tokens”按钮减少Tokens占用。
|
|
|
6 |
GUI for ChatGPT API
|
7 |
<img width="1204" alt="截屏2023-03-03 13 59 46" src="https://user-images.githubusercontent.com/51039745/222643242-c0b90a54-8f07-4fb6-b88e-ef338fd80f49.png">
|
8 |
|
9 |
+
## 使用技巧
|
10 |
+
|
11 |
+
- 使用System Prompt可以很有效地设定前提条件
|
12 |
+
- 对于长对话,可以使用“优化Tokens”按钮减少Tokens占用。
|
13 |
+
- 如果部署到服务器,将程序最后一句改成`demo.launch(server_name="0.0.0.0", server_port=99999)`。其中`99999`是端口号,应该是1000-65535任意可用端口,请自行更改为实际端口号。
|
14 |
+
- 如果需要获取公共链接,将程序最后一句改成`demo.launch(share=True)`。注意程序必须在运行,才能通过公共链接访问
|
15 |
+
|
16 |
## 安装方式
|
17 |
|
18 |
- 填入你的 OpenAI API 密钥
|
|
|
66 |
docker logs chatgpt
|
67 |
```
|
68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|