Building an AI application with LlamaIndex
AI agents are becoming a key part of how AI is evolving and driving new technologies. One of the tools that makes it easy to make AI applications is LlamaIndex. It is an open-source library designed to allow developers to create customized AI applications. It provides a modular and flexible approach in situations like integrating a chatbot, document analysis or any NLP-based tasks.
Lets see some key features of LlamaIndex:
- Ease of Integration: It works well with various data sources, whether it's structured or semi-structured data.
- Natural Language Querying: It allows us to query our data using natural language making it easier for users to interact with the system.
- Support for Large Datasets: It is built to handle large amounts of data and is perfect for businesses with growing datasets.
- Flexible Indexing Methods: Depending on our needs it offers different indexing options, giving us the flexibility to choose the best approach for our data.
Building a Movie Recommendation Bot
Before building the movie recommendation bot, let's understand its working:
- Query Input: A user enters a query like "Show me some action movies"
- Document Retrieval: The retriever searches through the indexed movie data (stored in the VectorStoreIndex) to find relevant documents based on the genre mentioned like "Action".
- Response Generation: The generator processes the retrieved documents and the user's query to form a recommendation.
Lets build a bot which recommends the movies on the basis of rating.
Step 1: Installing Required Packages and Dependencies
- llama-index: A framework to help index and query large datasets.
- transformers: Hugging Face’s library to access pre-trained models.
- torch: Torch is the deep learning framework for running models like GPT.
- accelerate, bitsandbytes: Libraries to optimize model loading and memory management, especially when working with large models.
!pip install - -upgrade llama - index llama - index - embeddings - huggingface llama - index - llms - huggingface transformers torch accelerate bitsandbytes
Step 2: Importing Required Libraries
from llama_index.core import Settings, SimpleDirectoryReader, VectorStoreIndex, Document
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
from llama_index.llms.huggingface import HuggingFaceLLM
import torch
import os
import pandas as pd
Step 3: Setting Up the Models and Embeddings
- Embedding model: A pre-trained Hugging Face model (all-MiniLM-L6-v2) is used to convert movie-related text data into embeddings.
- Language model: Another Hugging Face model (TinyLlama-1.1B-Chat-v1.0) is configured for generating responses based on user queries. It uses specific settings for tokenization, context window and the number of tokens.
Settings.embed_model = HuggingFaceEmbedding(
model_name="sentence-transformers/all-MiniLM-L6-v2")
Settings.llm = HuggingFaceLLM(
model_name="TinyLlama/TinyLlama-1.1B-Chat-v1.0",
tokenizer_name="TinyLlama/TinyLlama-1.1B-Chat-v1.0",
context_window=2048,
max_new_tokens=256,
device_map="auto",
model_kwargs={"torch_dtype": torch.float16, "load_in_4bit": True}
)

Step 4 Loading Data from CSV
- Creating Document Objects: The Document objects are created by iterating over each row in the DataFrame. For each row, the relevant movie data (ID, title, genre, rating) is extracted and passed to the Document constructor.
- NOTE: Replace "path_to_your_movie_recommendations_with_names.csv" with the csv file.
You can download dataset from here.
csv_file_path = "path_to_your_movie_recommendations_with_names.csv"
try:
df = pd.read_csv(csv_file_path)
except FileNotFoundError:
print(f"Error: CSV file not found at {csv_file_path}")
print(f"Current working directory: {os.getcwd()}")
except Exception as e:
print(f"An unexpected error occurred while reading the CSV: {e}")
Step 5: Document Creation and Indexing
- The VectorStoreIndex is built from the movie data, enabling fast querying.
- The query engine is created to interact with the index.
movies_data = [
Document(text=f"MovieID: {row['movie_id']}, Title: {row['title']}, Genre: {row['genre']}, Rating: {row['rating']}",
metadata={"movie_id": row['movie_id'], "title": row['title'], "genre": row['genre'], "rating": row['rating']})
for index, row in df.iterrows()
]
index = VectorStoreIndex.from_documents(movies_data)
query_engine = index.as_query_engine()
Step 6: User Interaction Loop
- The bot enters a while loop, asking the user for movie recommendations based on their preferred genre.
- If the user types 'exit' the loop ends and the conversation stops.
print("Welcome to MovieRecBot! ")
print("How can I assist you in finding your next movie to watch? (Type 'exit' to end)")
while True:
query_string = input("\nWhat type of movie are you in the mood for?\n ")
if query_string.lower() == 'exit':
print("Thank you for using MovieRecs! Enjoy your movie and have a great day!\n")
break
Step 7: Querying and Formatting Recommendations
- The response is split by newline characters into individual lines.
- Any irrelevant lines like notes or empty lines are filtered out.
- The relevant movie titles are extracted from the filtered recommendations, formatted and stored in a list (formatted_recommendations).
response = query_engine.query(
f"List titles and genres of movies with the genre {query_string} from the provided data. Provide at least 3 recommendations if available.")
response_lines = str(response).split('\n')
filtered_recommendations = [
line for line in response_lines
if line.strip() and "Note: The query is not specific to the data provided" not in line
]
formatted_recommendations = []
for rec in filtered_recommendations:
if "Title:" in rec:
try:
title_part = rec.split("Title:")[1].split(",")[0].strip()
formatted_recommendations.append(f"- {title_part}")
except:
formatted_recommendations.append(f"- {rec.strip()}")
elif rec.strip():
formatted_recommendations.append(f"- {rec.strip()}")
limited_formatted_recommendations = formatted_recommendations[:5]
Step 8: Display the Result
- If recommendations are available, they are joined into a final string (final_response).
- If no recommendations are found an apology message is returned.
- The final response is printed, showing the movie recommendations or an apology message.
if limited_formatted_recommendations:
final_response = "\n".join(limited_formatted_recommendations)
else:
final_response = "Sorry, I couldn't find any movies of that genre in my list."
if limited_formatted_recommendations:
final_response = "\n".join(limited_formatted_recommendations)
else:
final_response = "Sorry, I couldn't find any movies of that genre in my list."
print(f"\nMovieRecs Assistant:\n{final_response}")
Output

You can download source code from here.
Use case of LlamaIndex
- Recommendation Systems: LlamaIndex can recommend products, movies or content by indexing user preferences and matching them with relevant data.
- Document Search: It helps create search engines that allow users to query large datasets likr company policies, FAQs, etc using natural language.
- Chatbots and Virtual Assistants: Build AI chatbots that understand user queries and provide intelligent responses by indexing documents, FAQs and previous conversations.
- Sentiment Analysis: It is used to analyze customer reviews or social media posts, gauging public sentiment about products or services.
- Multilingual Support: Build systems capable of searching and retrieving data across multiple languages, ideal for global customer support.