I developed a lightweight chatbot that leverages Retrieval-Augmented Generated to provide contextually relevant responses. I chose to embed a short biography of me which the local LLM uses to answer questions about me. My idea is that anyone could use it to answer some questions they may have about me.
RAG stands for Retrieval-Augmented Generation. RAG chatbots work by combining two models: a retrieval model and a generation model. The retrieval model is used to search for relevant information from a given source, in this case a small database. The generation model then uses this information to generate a response to the user's query. The key difference between RAG chatbots and traditional chatbots is that RAG chatbots use a retrieval model to supplement the generation model's capabilities. This allows RAG chatbots to generate more accurate and relevant responses to user queries.
A vector database is a type of database that stores data in a numerical format known as vectors. These vectors are generated using machine learning algorithms that convert the input data into a numerical representation that can be easily searched and retrieved. In the context of this Retrieval-Augmented Generation application, the vector database is used to embed PDFs into numerical vectors that can be searched and retrieved using a search engine. The search engine uses the numerical vectors to determine the similarity between the input query and the stored PDFs. The most similar PDFs are then retrieved and used to generate new text that is relevant to the input query.
import os
from datetime import datetime
from werkzeug.utils import secure_filename
from langchain_community.document_loaders import UnstructuredPDFLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
from get_vector_db import get_vector_db
TEMP_FOLDER = os.getenv('TEMP_FOLDER', './_temp')
def allowed_file(filename):
return filename.lower().endswith('.pdf')
def save_file(file):
filename = f"{datetime.now().timestamp()}_{secure_filename(file.filename)}"
file_path = os.path.join(TEMP_FOLDER, filename)
file.save(file_path)
return file_path
def load_and_split_data(file_path):
loader = UnstructuredPDFLoader(file_path=file_path)
data = loader.load()
text_splitter = RecursiveCharacterTextSplitter(chunk_size=7500, chunk_overlap=100)
return text_splitter.split_documents(data)
def embed(file):
if file and allowed_file(file.filename):
file_path = save_file(file)
chunks = load_and_split_data(file_path)
db = get_vector_db()
db.add_documents(chunks)
db.persist()
os.remove(file_path)
return True
return False
I wrote a web service using Flask that leverages the predefined machine learning algorithms to generate text based on input queries and relevant documents. The endpoint is responsible for receiving input queries, retrieving relevant documents from a database, and generating new text that is relevant to the input query.
import os
from dotenv import load_dotenv
from flask import Flask, request, jsonify, make_response
from embed import embed
from query import query
from delete import delete_persisted_db
from get_vector_db import get_vector_db
load_dotenv()
TEMP_FOLDER = os.getenv('TEMP_FOLDER', './_temp')
os.makedirs(TEMP_FOLDER, exist_ok=True)
app = Flask(__name__)
@app.after_request
def after_request(response):
header = response.headers
header['Access-Control-Allow-Origin'] = '*'
return response
@app.route('/delete', methods=['DELETE'])
def route_delete():
try:
delete_persisted_db()
return {"message": "Database has been deleted."}
except FileNotFoundError as e:
raise HTTPException(status_code=404, detail=str(e))
@app.route('/embed', methods=['POST'])
def route_embed():
if 'file' not in request.files:
return jsonify({"error": "No file part"}), 400
file = request.files['file']
if file.filename == '':
return jsonify({"error": "No selected file"}), 400
embedded = embed(file)
return jsonify({"message": "File embedded successfully"}) if embedded else jsonify({"error": "Embedding failed"}), 400
@app.route('/query', methods=['POST', 'OPTIONS'])
def route_query():
if request.method == "OPTIONS": # CORS preflight
return _build_cors_preflight_response()
elif request.method == "POST": # The actual request following the preflight
data = request.get_json()
response = query(data.get('query'))
return _corsify_actual_response(jsonify(response))
else:
raise RuntimeError("Weird - don't know how to handle method {}".format(request.method))
def _build_cors_preflight_response():
response = make_response()
response.headers.add("Access-Control-Allow-Origin", "*")
response.headers.add('Access-Control-Allow-Headers', "*")
response.headers.add('Access-Control-Allow-Methods', "*")
return response
def _corsify_actual_response(response):
response.headers.add("Access-Control-Allow-Origin", "*")
return response
if __name__ == '__main__':
app.run(host="127.0.0.1", port=8080, debug=True)
Retrieval-Augmented Generation (RAG) is a promising field of research in artificial intelligence that has the potential to revolutionize the way we interact with technology in the future. RAG combines the strengths of retrieval methods, such as keyword search and question-answering, with the power of generative models to create a more advanced and intuitive way of accessing information. In the not so distant future, RAG applications will enable users to ask complex questions and receive detailed, relevant answers, even when the specific information is not stored in the model's knowledge base. By combining the strengths of retrieval and generation, RAG models could lead to more intuitive, natural, and human-like interactions with machines, revolutionizing the way we access information, create content, and collaborate with AI.
Copyright © Lucas Thormann