markredito commited on
Commit
143fb9d
·
verified ·
1 Parent(s): 18333fa

Updated the respond function, added chat_response and used gr.ChatInterface()

Browse files

We've kept the respond function as it was, which handles the interaction with your RAG chain.

The chat_response function has been simplified. It now just calls respond and returns the bot's message.

We're using gr.Blocks() as a wrapper to include the info_text_component and the advanced options.

Inside the gr.Blocks(), we're using gr.ChatInterface() to create the chat interface. This handles most of the chat functionality for us.
We've added some optional parameters to gr.ChatInterface() like title, description, and examples to make the interface more user-friendly. You can adjust these as needed.

The advanced options (sliders) are passed as additional_inputs to the gr.ChatInterface().

Files changed (1) hide show
  1. app.py +17 -18
app.py CHANGED
@@ -112,18 +112,20 @@ info_text_component = Markdown(info_text) # Load it once
112
 
113
  def respond(message, history, max_tokens, temperature, top_p):
114
  # Process user message through RAG chain
115
- response = rag_chain.invoke({"input": message})# Extract the answer from the response
 
116
  if isinstance(response, dict) and 'answer' in response:
117
  answer = response['answer']
118
  else:
119
  answer = str(response) # Convert to string if it's not in the expected format
120
 
121
- # Return the answer in the format expected by Gradio's chat interface
122
  return answer
123
 
124
- # Gradio Chat Interface
 
 
 
125
  with gr.Blocks() as demo:
126
- # Ensure info_text_component is only rendered once
127
  info_text_component.render()
128
 
129
  with gr.Accordion("Advanced Options", open=False):
@@ -131,22 +133,19 @@ with gr.Blocks() as demo:
131
  temperature_slider = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
132
  top_p_slider = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")
133
 
134
- chatbot = gr.Chatbot(height=600)
135
- msg = gr.Textbox(
136
- placeholder="Type your message here (eg. How to do great work by Paul Graham?)",
137
- container=False,
138
- scale=7
139
- )
140
-
141
- def chat_response(message, history, max_tokens, temperature, top_p):
142
- bot_message = respond(message, history, max_tokens, temperature, top_p)
143
- return bot_message
144
-
145
  gr.ChatInterface(
146
- chat_response,
147
- chatbot=chatbot,
148
- textbox=msg,
149
  additional_inputs=[max_tokens_slider, temperature_slider, top_p_slider],
 
 
 
 
 
 
 
 
 
 
150
  )
151
 
152
  if __name__ == "__main__":
 
112
 
113
  def respond(message, history, max_tokens, temperature, top_p):
114
  # Process user message through RAG chain
115
+ response = rag_chain.invoke({"input": message})
116
+ # Extract the answer from the response
117
  if isinstance(response, dict) and 'answer' in response:
118
  answer = response['answer']
119
  else:
120
  answer = str(response) # Convert to string if it's not in the expected format
121
 
 
122
  return answer
123
 
124
+ def chat_response(message, history, max_tokens, temperature, top_p):
125
+ bot_message = respond(message, history, max_tokens, temperature, top_p)
126
+ return bot_message
127
+
128
  with gr.Blocks() as demo:
 
129
  info_text_component.render()
130
 
131
  with gr.Accordion("Advanced Options", open=False):
 
133
  temperature_slider = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
134
  top_p_slider = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")
135
 
 
 
 
 
 
 
 
 
 
 
 
136
  gr.ChatInterface(
137
+ fn=chat_response,
 
 
138
  additional_inputs=[max_tokens_slider, temperature_slider, top_p_slider],
139
+ title="RAG Chatbot",
140
+ description="Ask me anything about the documents in my knowledge base!",
141
+ examples=[
142
+ "How to do great work by Paul Graham?",
143
+ "What are the key points in the article about doing great work?",
144
+ "Can you summarize Paul Graham's advice on career success?"
145
+ ],
146
+ retry_btn="Retry",
147
+ undo_btn="Undo",
148
+ clear_btn="Clear",
149
  )
150
 
151
  if __name__ == "__main__":