This repository contains the code for a conversational AI chatbot designed to answer questions about any provided files or documents. The bot combines Retrieval Augmented Generation (RAG) principles with Google's Gemini language models and LlamaIndex to provide highly contextualized responses based on user queries.
The chatbot retrieves relevant documents, ranks them based on relevance, and uses Google's Gemini model to generate responses. It is ideal for applications requiring domain-specific knowledge retrieval and conversational interfaces.
- Document Retrieval: Retrieves relevant Mirascope documentation based on semantic similarity.
- Relevance Ranking: Reranks retrieved documents using an LLM-based postprocessor.
- Conversational Interface: Provides an interactive chatbot experience for users.
- Custom Parsing: Parses LLM-generated responses to extract relevance scores and choice numbers.
- Integration with Google Gemini: Uses advanced language models for response generation.
- Persistence: Saves and loads indices to avoid reprocessing documents in future sessions.
- Python 3.x
llama-index
mirascope
pydantic
- A valid Google API key is required for accessing Google Gemini models. The key must be securely stored and retrieved using tools like Kaggle Secrets or environment variables.
-
Clone the repository:
git clone cd
-
Install dependencies:
pip install llama-index mirascope pydantic
-
Set up your Google API key:
- For Kaggle:
Use
UserSecretsClient
to securely retrieve the API key. - Locally: Store the API key in an environment variable or a secure file.
- For Kaggle:
Use
The custom_parse_choice_select_answer_fn
function parses LLM-generated answers to extract choice numbers and relevance scores.
- Splits the raw answer into lines.
- Extracts numerical values using regular expressions.
- Filters out invalid lines based on format and constraints.
"answer_num: 1, answer_relevance: 0.85\nanswer_num: 2, answer_relevance: 0.75"
([1, 2], [0.85, 0.75])
The get_documents
function retrieves relevant documentation chunks using LlamaIndex's query engine.
- Retrieves top 10 similar chunks (
similarity_top_k=10
). - Applies LLM-based reranking using
LLMRerank
. - Summarizes retrieved nodes into a cohesive response (
response_mode="tree_summarize"
).
context = get_documents("What is Mirascope?")
print(context)
Encapsulates the chatbot's functionality, including document retrieval, response generation, and user interaction.
_step
: Defines how prompts are structured and sent to Google's Gemini model._get_response
: Combines document retrieval and response generation.run
: Provides an interactive interface for users.
(User): What is Mirascope?
(Assistant): Mirascope is a tool designed for...
(User): exit
- User enters a query.
- The bot retrieves relevant documents using semantic search and reranking.
- Documents are passed as context to Google's Gemini model along with the user's question.
- The model generates a response based on both its general knowledge and the provided context.
question = "Explain Mirascope's features."
context = get_documents(question)
response = MirascopeBot()._step(context, question)
print(response.content)
Modify similarity_top_k
or choice_batch_size
in the query engine configuration to change how many documents are retrieved or reranked.
Integrate metadata filtering for more targeted retrieval:
filtered_query_engine = loaded_index.as_query_engine(
filters={"category": "documentation"}
)
- Requires a valid Google API key for functionality.
- Designed specifically for Mirascope documentation; may require adjustments for other domains.
- Limited scalability with the default vector store (
SimpleVectorStore
). Consider using external vector databases like Pinecone or Chroma for larger datasets.
- Support for multilingual queries and responses.
- Integration with external vector databases for scalability.
- Improved error handling and user feedback mechanisms.
Store your API key securely using tools like Kaggle Secrets or environment variables, depending on your environment.
Yes! Replace the document directory path in SimpleDirectoryReader
with your own dataset path.
The bot will return "No documents found." This can be customized in the get_documents
function.
The default vector store works well for small to medium datasets; larger datasets should use external vector databases like Pinecone or Chroma.
Absolutely! Update the @prompt_template
decorator in _step()
to customize system instructions or user prompts.