Use case demo
The following is the code for building a demo using Gradio; in this case, we will use an additional function that will perform the RAG pipeline. When you run this code, a Gradio interface will open in your default web browser, displaying three main sections:
- Fintech Assistant heading
- Chatbot area
- Text input box
Users can type their questions into the input box and submit them. The chat function will be called, which will use the answer_question
function to retrieve the relevant context from the vector database, generate an answer using the RAG pipeline, and update the chatbot interface with the user’s question and the generated response.
The Gradio interface provides a user-friendly way for users to interact with the RAG pipeline system, making it easier to test and demonstrate its capabilities. Additionally, Gradio offers various customization options and features, such as support for different input and output components, styling, and deployment options. We start by installing Gradio:
#In this case we will use a Gradio interface to interact
#with the system
#Install Gradio
!pip install --upgrade gradio
Next, we define two helper functions that build upon the previously explained generate()
function:
import gradio as gr
def answer_question(query, db, number_of_results):
context = get_context(query, db, number_of_results)
answer = generate(prompt_template.format(query=query, context=context))
return(answer.text)
def chat(message, history):
response = answer_question(message,db, MAX_RESULTS)
history.append((message, response))
return "", history
The answer_question(...)
function takes three arguments:
query
: The user’s questiondb
: The vector databasenumber_of_results
: The maximum number of context results to retrieve from the database
It then calls the get_context
function (not shown in the provided code) to retrieve the relevant context information – from the vector database, based on the user’s query and the specified number of results. The retrieved context is then formatted within the prompt_template
string and passed to the generate
function – covered in the previous sections – to obtain the answer.
At the end of the function execution, the generated answer is returned as a string.
The chat(...)
function takes two arguments:
message
: The user’s questionhistory
: A list representing the conversation history
It then calls the answer_question()
function with the user’s question, the vector database (db
), and the maximum number of results (MAX_RESULTS
).
The generated response is appended to the history list, along with the user’s question. The function returns an empty string and the updated history list, which will be used to update the chatbot interface.
The Gradio app
With the helper functions defined, we can now create the Gradio interface:
with gr.Blocks() as demo:
gr.Markdown("Fintech Assistant")
chatbot = gr.Chatbot(show_label=False)
message = gr.Textbox(placeholder="Enter your question")
message.submit(chat, [message, chatbot],[message, chatbot] )
demo.launch(debug=True)
Here’s what’s happening in this code:
with gr.Blocks() as demo
creates a Gradio interface block calleddemo
.gr.Markdown(...)
displays a Markdown-formatted heading for the chatbot interface.gr.Chatbot(...)
creates a Gradio chatbot component, which will display the conversation history.gr.Textbox(...)
creates a text input box where users can enter their questions.message.submit(...)
sets up an event handler for when the user submits their question. It calls the chat function with the user’s input (message) and the chatbot instance and updates the message and chatbot components with the returned values.demo.launch(...)
launches the Gradio interface in debug mode, allowing you to interact with the chatbot.
Refer to the GitHub directory of this chapter for the complete code that demonstrates how all the pieces described above fit together.