chatbot / app.py
zhangjf's picture
Add forgetting long-term history
94d4320
raw
history blame
4.2 kB
import openai
import tiktoken
import json
import os
openai.api_key = os.getenv('API_KEY')
def ask(question, history):
history = history + [question]
try:
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=forget_long_term([
{"role":"user" if i%2==0 else "assistant", "content":content}
for i,content in enumerate(history)
])
)["choices"][0]["message"]["content"]
while response.startswith("\n"):
response = response[1:]
except Exception as e:
print(e)
response = 'Timeout! Please wait a few minutes and retry'
history = history + [response]
with open("dialogue.txt", "a", encoding='utf-8') as f:
f.write(json.dumps(history, ensure_ascii=False)+"\n")
return history
def forget_long_term(messages, max_num_tokens=4000):
def num_tokens_from_messages(messages, model="gpt-3.5-turbo"):
"""Returns the number of tokens used by a list of messages."""
try:
encoding = tiktoken.encoding_for_model(model)
except KeyError:
encoding = tiktoken.get_encoding("cl100k_base")
if model == "gpt-3.5-turbo": # note: future models may deviate from this
num_tokens = 0
for message in messages:
num_tokens += 4 # every message follows <im_start>{role/name}\n{content}<im_end>\n
for key, value in message.items():
num_tokens += len(encoding.encode(value))
if key == "name": # if there's a name, the role is omitted
num_tokens += -1 # role is always required and always 1 token
num_tokens += 2 # every reply is primed with <im_start>assistant
return num_tokens
else:
raise NotImplementedError(f"""num_tokens_from_messages() is not presently implemented for model {model}.
See https://github.com/openai/openai-python/blob/main/chatml.md for information on how messages are converted to tokens.""")
while num_tokens_from_messages(messages)>max_num_tokens:
messages = messages[1:]
return messages
import gradio as gr
def predict(question, history=[]):
history = ask(question, history)
response = [(history[i].replace("\n","<br>"),history[i+1].replace("\n","<br>")) for i in range(0,len(history)-1,2)]
return "", history, response
with gr.Blocks() as demo:
examples = [
['200字介绍一下凯旋门:'],
['网上购物有什么小窍门?'],
['补全下述对三亚的介绍:\n三亚位于海南岛的最南端,是'],
['将这句文言文翻译成英语:"逝者如斯夫,不舍昼夜。"'],
['Question: What\'s the best winter resort city? User: A 10-year professional traveler. Answer: '],
['How to help my child to make friends with his classmates? answer this question step by step:'],
['polish the following statement for a paper: In this section, we perform case study to give a more intuitive demonstration of our proposed strategies and corresponding explanation.'],
]
gr.Markdown(
"""
朋友你好,
这是我利用[gradio](https://gradio.app/creating-a-chatbot/)编写的一个小网页,用于以网页的形式给大家分享ChatGPT请求服务,希望你玩的开心
p.s. 响应时间和问题复杂程度相关,<del>一般能在10~20秒内出结果</del>用了新的api已经提速到大约5秒内了
""")
chatbot = gr.Chatbot()
state = gr.State([])
with gr.Row():
txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter").style(container=False)
txt.submit(predict, [txt, state], [txt, state, chatbot])
with gr.Row():
gen = gr.Button("Submit")
clr = gr.Button("Clear")
gen.click(fn=predict, inputs=[txt, state], outputs=[txt, state, chatbot])
def clear(value):
return [], []
clr.click(clear, inputs=clr, outputs=[chatbot, state])
gr_examples = gr.Examples(examples=examples, inputs=txt)
demo.launch()