Building a Document Q&A Bot With Web Interface

Tags: #OpenAI #LangChain #FastUI #RAG #Python #AWS
Code: https://github.com/pluto-lang/pluto/tree/main/examples/rag-qa-bot-with-web (opens in a new tab)


Building a Document Q&A Bot With Web Interface

Let me introduce you to an incredibly simple way to build a document Q&A bot, which allows you to create a personalized Web Q&A bot based on your GitHub documentation repository in just 5 minutes.

Demo

Brief Introduction

First, let's quickly go over the working principle and implementation method of this document Q&A bot.

The main logic of this document bot is: upon initialization, it downloads documents from the GitHub repository where the documents are stored. Then, through LangChain, it calls OpenAI's Embeddings model to generate vectors for the documents and saves them to AWS S3 to avoid wasting Tokens by recreating document vectors. When a user inputs a question, LangChain is used again to generate vectors for the question. Then, the FAISS vector database retrieves related documents, and finally, GPT-3.5 is used to synthesize an answer. Additionally, this document Q&A bot automatically updates the document vectors at 0:00 UTC every day.

To construct this document Q&A bot, we utilized a series of frameworks and tools including LangChain, FastUI, and Pluto, and eventually deployed it on AWS. LangChain is used for the main Q&A functionality, invoking models such as OpenAI's Embedding and GPT-3.5; FastUI is used for the Web interface shown above; Pluto is in charge of creating and configuring cloud resources, as well as application deployment. With Pluto, you can create and configure resources such as AWS Lambda and S3 by just creating a few variables in the code.

Getting Started

To make it easy for everyone to get started, I've made this document Q&A bot into a CodeSandbox template. Simply click the link below to open CodeSandbox's online IDE, then click Fork in the top right corner to copy the project to your account. After that, you are free to modify the code and deploy it to AWS with one click. Open in CodeSandbox (opens in a new tab)

Next, I will introduce how to step by step build your own document Q&A bot.

Prepare Tokens

Before you start, you need to prepare several key Tokens, including a GitHub Token for downloading document data from GitHub, an OpenAI API Key for invoking OpenAI models, and AWS credentials for application deployment, among others.

Modify Basic Configuration

Once you enter the development environment, the console will automatically display the Configure AWS Certificate tab. Enter your AWS certificate information here to ensure the application can be successfully deployed to AWS. You can leave the output format field blank. After filling in the other necessary information, if everything is correct, you will see a green check mark ✔️ next to the tab name.

Configure AWS Certificate

Next, we need to modify the configuration of the bot. Open the .env.local file, which contains the bot's basic configuration, including the GitHub repository where the documents are stored, the repository branch, the relative path of the documents in the repository, OpenAI's API Key, GitHub Token, etc. You will need to modify these configurations according to your actual situation.

PROJECT_NAME = "Pluto" # Project name, related to the title of the Web page
REPO = "pluto-lang/website" # GitHub repository storing the documents
BRANCH = "main" # Branch of the repository
DOC_RELATIVE_PATH = "pages" # Relative path of the documents in the repository
 
OPENAI_BASE_URL = "https://api.openai.com/v1" # Base URL of the OpenAI API
OPENAI_API_KEY = "<replace_with_your_openai_api_key>" # OpenAI API Key
GITHUB_ACCESS_KEY = "<replace_with_your_github_access_key>" # GitHub Token

To customize your robot, such as changing the style of the robot's responses, you can achieve this by altering the prompt variable in the app/main.py code file.

One-Click Deployment

After the configuration is complete, simply click the terminal icon and select Deploy from the menu to deploy the document Q&A bot to AWS, without any additional steps on your part. The deployment may take about a minute. Once it's done, the deployment URL will be displayed in the console, and you can access the document Q&A bot by clicking this URL!

Pluto Deployment

Note that due to AWS Lambda cold starts and the delay in building the vector database, the first visit may require a few seconds to a few tens of seconds of patience.

Destruction

If you want to take the application offline, simply click Destroy from the menu, and the resources created in AWS will be completely deleted.

Pluto Destruction

Summary

The implementation of this document Q&A bot is very simple; you only need to modify a few configurations to deploy it on AWS.

The reason it can be implemented so effortlessly is that you don't need to worry about the creation and configuration of cloud resources or the deployment of applications; this is mainly thanks to Pluto's capabilities. Pluto can automatically deduce the resources an application depends on from the code and automatically create and configure these resources, allowing developers to focus on the implementation of business logic.

If you want to learn more about Pluto's features, feel free to visit Pluto's official documentation (opens in a new tab) or the GitHub repository (opens in a new tab). If you could give a Star🌟, that would be even better! We also welcome everyone to submit issues and PRs.

More Resources