Gradio-Demo-App / livechat.py
FloydTan1987's picture
Upload 5 files
b895330 verified
raw
history blame
No virus
1.63 kB
import gradio as gr
import ollama
def format_history(msg: str, history: list[list[str, str]], system_prompt: str):
chat_history = [{"role": "system", "content":system_prompt}]
for query, response in history:
chat_history.append({"role": "user", "content": query})
chat_history.append({"role": "assistant", "content": response})
chat_history.append({"role": "user", "content": msg})
return chat_history
def generate_response(msg: str, history: list[list[str, str]], system_prompt: str):
chat_history = format_history(msg, history, system_prompt)
response = ollama.chat(model='llama2', stream=True, messages=chat_history)
message = ""
for partial_resp in response:
token = partial_resp["message"]["content"]
message += token
yield message
chatbot = gr.ChatInterface(
generate_response,
chatbot=gr.Chatbot(
avatar_images=["cx.jpg", "agent.jpg"],
height="64vh"
),
additional_inputs=[
gr.Textbox(
"Behave as if you are order placer agent.",
label="System Prompt"
)
],
title="Door Treats",
description="Please ask any questions you may have about your order.πŸ€”",
theme="soft",
submit_btn="β¬… Send",
retry_btn="πŸ”„ Regenerate Response",
undo_btn="↩ Delete Previous",
clear_btn="πŸ—‘οΈ Clear Chat"
)
chatbot.launch()