Code Flow and Library Choices
This section explains the flow of the code and the rationale behind using different libraries.
Core Logic
- Document Loading:
The
data_ingestion()
function loads documents from theDATA_DIR
.If the directory is empty, it copies the pre-loaded document.
Using SimpleDirectoryReader from LlamaIndex makes it easy to read files from a directory.
- Index Creation:
The
VectorStoreIndex.from_documents()
function creates a vector store index from the loaded documents using LlamaIndex.This function takes the llm (LLM model) and embed_model (embedding model) as parameters, allowing for the use of specific models.
- Index Storage:
The index is saved to a persistent directory (
PERSIST_DIR
) using theStorageContext
from LlamaIndex. This enables the index to be loaded across sessions.
- Query Handling:
The
handle_query()
function loads the index from storage or creates a new one if it doesn’t exist.It then checks for greetings or feature requests.
For document-based questions, the query_engine from the index is used to get a response.
Cosine similarity is calculated between the query embedding and the response embedding to ensure the answer is contextually relevant.
Library Choices
LlamaIndex: Chosen for its specialized capabilities in document-based Q&A, including indexing, storage, and retrieval.
Streamlit: Used for building the user interface, providing a user-friendly chatbot experience.
Hugging Face: Used for both the LLM (
meta-llama/Meta-Llama-3-8B-Instruct
) and the embedding model (BAAI/bge-small-en-v1.5
).Scikit-learn: Used for calculating cosine similarity, which is used to determine the relevance of the answers.
This code flow highlights the key components and libraries used to create a robust and efficient document-based question-answering chatbot.