ConversationalChain / .history /app_20230720140423.py
Rohan Kataria
new files
0365501
raw
history blame
2.24 kB
import gradio as gr
from src.main import ChatWrapper
agent = ChatWrapper('openai', '') # default agnet_state
def update_agent(api_key: str, selection: str):
global agent
agent = ChatWrapper(chain_type=selection, api_key=api_key)
label_text = "Put your HuggingFace API Key" if selection == "falcon" else "Put your OpenAI API Key"
return agent, {"markdown": label_text} # This is agent state
def chat(message):
global agent
agent(message) # Get a response to the current message
history = agent.history # Access the entire chat history
return history, history # Return the history twice to update both the chatbot and the state
block = gr.Blocks(css=".gradio-container {background-color: lightgray}")
agent_api_label = gr.Markdown()
with block:
with gr.Row():
gr.Markdown("<h1><center>ConversationalChain App 🤖</center></h1>")
selection = gr.Dropdown(label="Select Agent", choices=["falcon", "openai"], default="openai")
api_key_textbox = gr.Textbox(
agent_api_label,
placeholder="Paste your OpenAI API key (sk-...)",
show_label=True,
lines=1,
type="password",
)
chatbot = gr.Chatbot()
with gr.Row():
message = gr.Textbox(
label="What's your question?",
placeholder="What's the answer to life, the universe, and everything?",
lines=1,
)
submit = gr.Button(value="Send", variant="secondary").style(full_width=False)
gr.Examples(
examples=[
"Hi! How's it going?",
"What should I do tonight?",
"Whats 2 + 2?",
],
inputs=message,
)
gr.HTML("<center>View more at <a href='https://ai.rohankataria.com'>ai.rohankataria.com</a></center>")
state = gr.State()
agent_state = gr.State()
submit.click(chat, inputs=[message], outputs=[chatbot, state])
message.submit(chat, inputs=[message], outputs=[chatbot, state])
api_key_textbox.change(update_agent, inputs=[api_key_textbox, selection], outputs=[agent_state])
selection.change(update_agent, inputs=[api_key_textbox, selection], outputs=[agent_state, agent_api_label])
block.launch(debug=True)