Building an AI application with LlamaIndex (original) (raw)

Last Updated : 14 Apr, 2026

LlamaIndex is an open source library that helps build AI applications by integrating agents with various data sources, offering a modular approach for tasks like chatbots, document analysis and NLP. Here we will build a movie recommendation bot using LlamaIndex, where a user query is processed, relevant data is retrieved and recommendations are generated.

**Step 1: Installing Required Packages

!pip install --upgrade llama-index llama-index-embeddings-huggingface llama-index-llms-huggingface transformers torch accelerate bitsandbytes

`

**Step 2: Importing Required Libraries

Python `

from llama_index.core import Settings, VectorStoreIndex, Document from llama_index.embeddings.huggingface import HuggingFaceEmbedding from llama_index.llms.huggingface import HuggingFaceLLM import torch import pandas as pd import os

`

**Step 3: Setting Up Models and Embeddings

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.float32} )

`

model-training-

Training

**Step 4 Loading Data from CSV

You can download dataset from here.

Python `

csv_file_path = "/content/movie_recommendations_with_names.csv"

df = pd.read_csv(csv_file_path)

`

**Step 5: Document Creation and Indexing

movies_data = [ Document( text=f"Title: {row['title']}, Genre: {row['genre']}, Rating: {row['rating']}", metadata={ "title": row['title'], "genre": row['genre'], "rating": row['rating'] } ) for _, row in df.iterrows() ] index = VectorStoreIndex.from_documents(movies_data) query_engine = index.as_query_engine()

`

**Step 6: Querying and Displaying Recommendations

print("Welcome to MovieRecBot!") print("Type 'exit' to quit.\n")

while True: query_string = input("What type of movie are you in the mood for?\n> ")

if query_string.lower() == "exit":
    print("Thanks for using MovieRecBot!")
    break

prompt = f"""
Recommend top 5 movies for genre: {query_string}.
Only return:
Title - Rating

Do NOT return numbers alone.
"""

response = query_engine.query(prompt)

print("\nRecommended Movies:\n")
print(response.response)

`

**Output:

output

Output

You can download source code from here.

Applications