Spaces:
Sleeping
Sleeping
Merge pull request #3 from MZhao-ouo/main
Browse files- ChuanhuChatbot.py +11 -7
ChuanhuChatbot.py
CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
|
|
2 |
import openai
|
3 |
import markdown
|
4 |
|
|
|
5 |
我的API密钥 = "sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" # 在这里输入你的 API 密钥
|
6 |
initial_prompt = "You are a helpful assistant."
|
7 |
|
@@ -54,16 +55,18 @@ def delete_last_conversation(chatbot, context):
|
|
54 |
|
55 |
def reduce_token(chatbot, system, context):
|
56 |
context.append({"role": "user", "content": "请帮我总结一下上述对话的内容,实现减少tokens的同时,保证对话的质量。在总结中不要加入这一句话。"})
|
57 |
-
|
58 |
response = get_response(system, context, raw=True)
|
59 |
-
system = {"role": "system", "content": f"{oldSystemPrompt['content']} The content that the Assistant and the User discussed in the previous context is: {response['choices'][0]['message']['content']}"}
|
60 |
|
61 |
-
statistics = f'本次对话Tokens用量【{response["usage"]["completion_tokens"]+
|
62 |
-
optmz_str = markdown.markdown( f"
|
63 |
chatbot.append(("请帮我总结一下上述对话的内容,实现减少tokens的同时,保证对话的质量。", optmz_str))
|
64 |
-
|
65 |
context = []
|
66 |
-
|
|
|
|
|
|
|
67 |
|
68 |
def reset_state():
|
69 |
return [], []
|
@@ -101,6 +104,7 @@ with gr.Blocks() as demo:
|
|
101 |
newSystemPrompt.submit(lambda :"", None, newSystemPrompt)
|
102 |
retryBtn.click(retry, [chatbot, systemPrompt, context], [chatbot, context], show_progress=True)
|
103 |
delLastBtn.click(delete_last_conversation, [chatbot, context], [chatbot, context], show_progress=True)
|
104 |
-
reduceTokenBtn.click(reduce_token, [chatbot, systemPrompt, context], [chatbot,
|
|
|
105 |
|
106 |
demo.launch()
|
|
|
2 |
import openai
|
3 |
import markdown
|
4 |
|
5 |
+
|
6 |
我的API密钥 = "sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" # 在这里输入你的 API 密钥
|
7 |
initial_prompt = "You are a helpful assistant."
|
8 |
|
|
|
55 |
|
56 |
def reduce_token(chatbot, system, context):
|
57 |
context.append({"role": "user", "content": "请帮我总结一下上述对话的内容,实现减少tokens的同时,保证对话的质量。在总结中不要加入这一句话。"})
|
58 |
+
|
59 |
response = get_response(system, context, raw=True)
|
|
|
60 |
|
61 |
+
statistics = f'本次对话Tokens用量【{response["usage"]["completion_tokens"]+12+12+8} / 4096】'
|
62 |
+
optmz_str = markdown.markdown( f'好的,我们之前聊了:{response["choices"][0]["message"]["content"]}\n\n================\n\n{statistics}' )
|
63 |
chatbot.append(("请帮我总结一下上述对话的内容,实现减少tokens的同时,保证对话的质量。", optmz_str))
|
64 |
+
|
65 |
context = []
|
66 |
+
context.append({"role": "user", "content": "我们之前聊了什么?"})
|
67 |
+
context.append({"role": "assistant", "content": f'我们之前聊了:{response["choices"][0]["message"]["content"]}'})
|
68 |
+
return chatbot, context
|
69 |
+
|
70 |
|
71 |
def reset_state():
|
72 |
return [], []
|
|
|
104 |
newSystemPrompt.submit(lambda :"", None, newSystemPrompt)
|
105 |
retryBtn.click(retry, [chatbot, systemPrompt, context], [chatbot, context], show_progress=True)
|
106 |
delLastBtn.click(delete_last_conversation, [chatbot, context], [chatbot, context], show_progress=True)
|
107 |
+
reduceTokenBtn.click(reduce_token, [chatbot, systemPrompt, context], [chatbot, context], show_progress=True)
|
108 |
+
|
109 |
|
110 |
demo.launch()
|