Q&A Bot (RAG)
Last updated
Last updated
This tutorial is based on the work done by Vulture Prime in creating a Q&A Chatbot. The Chatbot in this case interacts with user-uploaded documents, answering questions based on the content.
In the frontend development phase of the Q&A Chatbot project, we chose the following libraries and frameworks:
Next.js: Installed using create-next-app
for project setup.
Tailwind CSS: Utilized for styling the user interface.
react-hook-form: Employed for form management, reducing complexity in handling form states.
zod: Used in conjunction with react-hook-form for type-safe input validation.
Understanding the backend API is crucial in this step. Key points include:
Familiarity with API endpoints, data formats (parameters or body), and the sequence of steps.
Handling CORS issues, ensuring the frontend and backend can communicate seamlessly.
API Interfaces obtained:
API LoadAndStore Interface:
GET {endpoint}/loadAndStore
Response: 200 (OK) with information about the default URL, chunk size, chunk overlap, and collection name.
Request Body
API Query Interfaces:
POST {endpoint}/queryWithOutRetrieval
POST {endpoint}/queryWithRetrieval
Body: Contains the query and collection name.
Request Body
URL input validation is crucial to prevent incorrect data from being sent to the backend. This involved using react-hook-form
and zod
for frontend validation.
Example validation schema using zod
:
Error handling for API calls was implemented to display meaningful messages on the website in case of failures. This ensures users can understand and troubleshoot issues easily.
After error handling, thorough testing of the frontend was conducted to ensure it met the project requirements. Optimization focused on improving code readability and user experience. The frontend was then deployed, involving building and addressing any errors before handing over deployment to the DevOps team.
How to create RAG with Langchain Link
The backend development started with setting up an EC2 server. Python was confirmed to be installed, and necessary libraries such as langchain
, chromadb
, fastapi
, and uvicorn
were installed.
FastAPI was used to create the backend API. A simple initial API endpoint was created, and additional functionality like file upload was added.
Begin by creating a main.py
file with a simple endpoint:
To add more functionality, you can implement methods like POST for file uploads.
Run FastAPI using:
Integration with RAG involved understanding the main steps: importing data and querying data. Functions for uploading data and querying data were developed and associated with corresponding API methods.
Chroma vector database was integrated to enhance data querying capabilities. The chromadb
library was installed and configured as the vector database for Langchain.
CORS (Cross-Origin Resource Sharing) was configured to enhance security for frontend integration. Add the following middleware in FastAPI:
The backend code was deployed, and the use of tools like screen
for detached execution was explained.
Ensure that the security group of the EC2 instance allows traffic on the specified port (default is 8000).
AWS API Gateway was introduced to manage traffic and authentication for added security and features.
The backend was tested by querying data, and tuning involved adjusting parameters like data division and AI creativity to achieve desired results.