增加设置提示词的块

This commit is contained in:
Tianxiang Zhan 2024-06-06 19:57:58 +08:00 committed by GitHub
parent b562723d59
commit f4fc0a316e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 25 additions and 2 deletions

View File

@ -99,6 +99,7 @@ def parse_text(text):
return text
def predict(history, max_length, top_p, temperature):
stop = StopOnTokens()
messages = []
@ -140,11 +141,14 @@ with gr.Blocks() as demo:
chatbot = gr.Chatbot()
with gr.Row():
with gr.Column(scale=4):
with gr.Column(scale=3):
with gr.Column(scale=12):
user_input = gr.Textbox(show_label=False, placeholder="Input...", lines=10, container=False)
with gr.Column(min_width=32, scale=1):
submitBtn = gr.Button("Submit")
with gr.Column(scale=1):
prompt_input = gr.Textbox(show_label=False, placeholder="Prompt", lines=10, container=False)
pBtn = gr.Button("Set Prompt")
with gr.Column(scale=1):
emptyBtn = gr.Button("Clear History")
max_length = gr.Slider(0, 32768, value=8192, step=1.0, label="Maximum length", interactive=True)
@ -156,10 +160,29 @@ with gr.Blocks() as demo:
return "", history + [[parse_text(query), ""]]
def set_prompt(prompt_text, history):
"""
Sets the initial prompt for the chat session.
Parameters:
- prompt_text (str): The text of the prompt from the prompt textbox.
- history (list): Current chat history.
Returns:
- list: Updated chat history with the new prompt at the beginning.
"""
# Clear any existing history and add the prompt as the first message
return [[parse_text(prompt_text), ""]]
# Connect the 'Set Prompt' button click event to the 'set_prompt' function
pBtn.click(set_prompt, inputs=[prompt_input, chatbot], outputs=chatbot)
pBtn.click()
submitBtn.click(user, [user_input, chatbot], [user_input, chatbot], queue=False).then(
predict, [chatbot, max_length, top_p, temperature], chatbot
)
emptyBtn.click(lambda: None, None, chatbot, queue=False)
demo.queue()
demo.launch(server_name="127.0.0.1", server_port=8000, inbrowser=True, share=True)
demo.launch(server_name="127.0.0.1", server_port=8002, inbrowser=True, share=True)