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)
More Related questions...