Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Generative AI Application Integration Patterns

You're reading from   Generative AI Application Integration Patterns Integrate large language models into your applications

Arrow left icon
Product type Paperback
Published in Sep 2024
Publisher Packt
ISBN-13 9781835887608
Length 218 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Luis Lopez Soria Luis Lopez Soria
Author Profile Icon Luis Lopez Soria
Luis Lopez Soria
Juan Pablo Bustos Juan Pablo Bustos
Author Profile Icon Juan Pablo Bustos
Juan Pablo Bustos
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Introduction to Generative AI Patterns FREE CHAPTER 2. Identifying Generative AI Use Cases 3. Designing Patterns for Interacting with Generative AI 4. Generative AI Batch and Real-Time Integration Patterns 5. Integration Pattern: Batch Metadata Extraction 6. Integration Pattern: Batch Summarization 7. Integration Pattern: Real-Time Intent Classification 8. Integration Pattern: Real-Time Retrieval Augmented Generation 9. Operationalizing Generative AI Integration Patterns 10. Embedding Responsible AI into Your GenAI Applications 11. Other Books You May Enjoy
12. Index

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 question
  • db: The vector database
  • number_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 question
  • history: 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 called demo.
  • 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.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image