Spaces:
Sleeping
Sleeping
test commit
Browse files- ChuanhuChatbot.py +26 -9
ChuanhuChatbot.py
CHANGED
@@ -2,24 +2,27 @@ import gradio as gr
|
|
2 |
import openai
|
3 |
import markdown
|
4 |
|
5 |
-
my_api_key = "sk-
|
6 |
initial_prompt = "You are a helpful assistant."
|
7 |
|
8 |
class ChatGPT:
|
9 |
def __init__(self, apikey) -> None:
|
10 |
openai.api_key = apikey
|
11 |
self.system = {"role": "system", "content": initial_prompt}
|
12 |
-
|
|
|
13 |
|
14 |
def get_response(self, messages):
|
15 |
-
response = openai.ChatCompletion.create(
|
16 |
model="gpt-3.5-turbo",
|
17 |
messages=[self.system, *messages],
|
18 |
)
|
19 |
-
statistics = f'
|
20 |
-
message = response["choices"][0]["message"]["content"]
|
|
|
21 |
message_with_stats = f'{message}\n\n================\n\n{statistics}'
|
22 |
message_with_stats = markdown.markdown(message_with_stats)
|
|
|
23 |
return message, message_with_stats
|
24 |
|
25 |
def predict(self, chatbot, input_sentence, context):
|
@@ -53,7 +56,19 @@ class ChatGPT:
|
|
53 |
return [], []
|
54 |
chatbot = chatbot[:-1]
|
55 |
context = context[:-2]
|
56 |
-
return chatbot, context
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
|
59 |
def reset_state():
|
@@ -69,9 +84,10 @@ with gr.Blocks() as demo:
|
|
69 |
with gr.Column():
|
70 |
txt = gr.Textbox(show_label=False, placeholder="💬 在这里输入").style(container=False)
|
71 |
with gr.Row():
|
72 |
-
emptyBth = gr.Button("
|
73 |
-
retryBth = gr.Button("
|
74 |
-
delLastBth = gr.Button("
|
|
|
75 |
|
76 |
system = gr.Textbox(show_label=True, placeholder=f"在这里输入新的System Prompt...", label="更改 System prompt").style(container=True)
|
77 |
syspromptTxt = gr.Textbox(show_label=True, placeholder=initial_prompt, interactive=False, label="目前的 System prompt").style(container=True)
|
@@ -83,5 +99,6 @@ with gr.Blocks() as demo:
|
|
83 |
system.submit(lambda :"", None, system)
|
84 |
retryBth.click(mychatGPT.retry, [chatbot, state], [chatbot, state], show_progress=True)
|
85 |
delLastBth.click(mychatGPT.delete_last_conversation, [chatbot, state], [chatbot, state], show_progress=True)
|
|
|
86 |
|
87 |
demo.launch()
|
|
|
2 |
import openai
|
3 |
import markdown
|
4 |
|
5 |
+
my_api_key = "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" # input your api_key
|
6 |
initial_prompt = "You are a helpful assistant."
|
7 |
|
8 |
class ChatGPT:
|
9 |
def __init__(self, apikey) -> None:
|
10 |
openai.api_key = apikey
|
11 |
self.system = {"role": "system", "content": initial_prompt}
|
12 |
+
self.context = []
|
13 |
+
self.response = None
|
14 |
|
15 |
def get_response(self, messages):
|
16 |
+
self.response = openai.ChatCompletion.create(
|
17 |
model="gpt-3.5-turbo",
|
18 |
messages=[self.system, *messages],
|
19 |
)
|
20 |
+
statistics = f'本次对话Tokens用量【{self.response["usage"]["total_tokens"]} / 4096】 ( 提问+上文 {self.response["usage"]["prompt_tokens"]},回答 {self.response["usage"]["completion_tokens"]} )'
|
21 |
+
message = self.response["choices"][0]["message"]["content"]
|
22 |
+
|
23 |
message_with_stats = f'{message}\n\n================\n\n{statistics}'
|
24 |
message_with_stats = markdown.markdown(message_with_stats)
|
25 |
+
|
26 |
return message, message_with_stats
|
27 |
|
28 |
def predict(self, chatbot, input_sentence, context):
|
|
|
56 |
return [], []
|
57 |
chatbot = chatbot[:-1]
|
58 |
context = context[:-2]
|
59 |
+
return chatbot, context
|
60 |
+
|
61 |
+
def reduce_token(self, chatbot, context):
|
62 |
+
context.append({"role": "user", "content": "请帮我总结一下上述对话的内容,实现减少tokens的同时,保证对话的质量。在总结中不要加入这一句话。"})
|
63 |
+
message, message_with_stats = self.get_response(context)
|
64 |
+
self.system = {"role": "system", "content": f"You are a helpful assistant. The content that the Assistant and the User discussed in the previous context is: {message}."}
|
65 |
+
|
66 |
+
statistics = f'本次对话Tokens用量【{self.response["usage"]["completion_tokens"]+23} / 4096】'
|
67 |
+
optmz_str = markdown.markdown( f"System prompt已经更新, 请继续对话\n\n================\n\n{statistics}" )
|
68 |
+
chatbot.append(("请帮我总结一下上述对话的内容,实现减少tokens的同时,保证对话的质量。", optmz_str))
|
69 |
+
|
70 |
+
context = []
|
71 |
+
return chatbot, context, self.system["content"]
|
72 |
|
73 |
|
74 |
def reset_state():
|
|
|
84 |
with gr.Column():
|
85 |
txt = gr.Textbox(show_label=False, placeholder="💬 在这里输入").style(container=False)
|
86 |
with gr.Row():
|
87 |
+
emptyBth = gr.Button("新的对话")
|
88 |
+
retryBth = gr.Button("重新生成")
|
89 |
+
delLastBth = gr.Button("删除上条对话")
|
90 |
+
reduceTokenBth = gr.Button("优化Tokens")
|
91 |
|
92 |
system = gr.Textbox(show_label=True, placeholder=f"在这里输入新的System Prompt...", label="更改 System prompt").style(container=True)
|
93 |
syspromptTxt = gr.Textbox(show_label=True, placeholder=initial_prompt, interactive=False, label="目前的 System prompt").style(container=True)
|
|
|
99 |
system.submit(lambda :"", None, system)
|
100 |
retryBth.click(mychatGPT.retry, [chatbot, state], [chatbot, state], show_progress=True)
|
101 |
delLastBth.click(mychatGPT.delete_last_conversation, [chatbot, state], [chatbot, state], show_progress=True)
|
102 |
+
reduceTokenBth.click(mychatGPT.reduce_token, [chatbot, state], [chatbot, state, syspromptTxt], show_progress=True)
|
103 |
|
104 |
demo.launch()
|