Python / Python Modern Generative AI and Agents Interview Questions
What is LlamaIndex and how does it compare to LangChain for RAG use cases?
LlamaIndex (formerly GPT Index) is a data framework specialised for connecting LLMs to diverse data sources. While LangChain is a general-purpose composable LLM framework covering agents, chains, memory, and RAG, LlamaIndex focuses almost exclusively on the data ingestion and indexing layer — providing more sophisticated out-of-the-box RAG patterns like query routing, recursive retrieval, and knowledge graphs.
# pip install llama-index llama-index-llms-openai llama-index-embeddings-openai
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Settings
from llama_index.llms.openai import OpenAI
from llama_index.embeddings.openai import OpenAIEmbedding
# ── Configure global settings
Settings.llm = OpenAI(model='gpt-4o-mini', temperature=0)
Settings.embed_model = OpenAIEmbedding(model='text-embedding-3-small')
Settings.chunk_size = 1024
# ── Load and index documents in 3 lines
docs = SimpleDirectoryReader('./docs').load_data()
index = VectorStoreIndex.from_documents(docs) # embeds and indexes
engine = index.as_query_engine() # wraps retriever + LLM
response = engine.query('What are the key conclusions of the report?')
print(response.response)
print(response.source_nodes[0].text[:200]) # retrieved passage
# ── Persist index to disk and reload
index.storage_context.persist('./index_store')
from llama_index.core import StorageContext, load_index_from_storage
storage = StorageContext.from_defaults(persist_dir='./index_store')
index2 = load_index_from_storage(storage)
# ── Advanced: Sub-question engine (breaks complex queries into sub-queries)
from llama_index.core.query_engine import SubQuestionQueryEngine
from llama_index.core.tools import QueryEngineTool
q_tool = QueryEngineTool.from_defaults(query_engine=engine,
description='Annual report 2024')
sub_engine = SubQuestionQueryEngine.from_defaults(query_engine_tools=[q_tool])
resp = sub_engine.query('Compare revenue and profit growth, then summarise trends.')
print(resp.response)
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
