LlamaIndex Basics
8 examples to get you started with LlamaIndex - 5 basic and 3 intermediate.
Busca en todas las páginas de la documentación
8 examples to get you started with LlamaIndex - 5 basic and 3 intermediate.
You will load a folder of files, build a vector index, ask questions with a query engine, inspect source nodes, and wrap retrieval as an agent tool.
data/ folder with a few .txt or .md files you ownpython -m venv .venv && source .venv/bin/activate
pip install llama-index
export OPENAI_API_KEY="sk-..."
mkdir -p data
echo "Refunds are available within 30 days of purchase with a receipt." > data/refunds.txt
echo "Support hours are Monday to Friday, 9am to 5pm Eastern." > data/support.txtPin integration packages you need at build time (for example OpenAI LLM/embed packages) when the monorepo split requires them.
Load a directory, index it, and ask one question.
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
documents = SimpleDirectoryReader("data").load_data()
index = VectorStoreIndex.from_documents(documents)
query_engine = index.as_query_engine()
response = query_engine.query("What is the refund window?")
print(response)SimpleDirectoryReader picks readers by file extension.Related: The LlamaIndex Mental Model: Documents, Indexes, and Query Engines
Confirm loaders produced the text you expect.
from llama_index.core import SimpleDirectoryReader
documents = SimpleDirectoryReader("data").load_data()
for doc in documents:
print(doc.metadata)
print(doc.text[:200])
print("---")file_path becomes citation material.Skip the filesystem when text already lives in your app.
from llama_index.core import Document, VectorStoreIndex
docs = [
Document(text="Tier A customers get priority routing.", metadata={"tier": "A"}),
Document(text="Tier B customers use standard support.", metadata={"tier": "B"}),
]
index = VectorStoreIndex.from_documents(docs)
print(index.as_query_engine().query("Who gets priority routing?"))similarity_top_kControl how many chunks enter the prompt.
query_engine = index.as_query_engine(similarity_top_k=3)
response = query_engine.query("When is support available?")
print(response)k can improve recall and raise token cost.k is cheaper but may miss multi-hop facts.Grounding starts with knowing which chunks were used.
response = query_engine.query("What is the refund window?")
print(response)
for node in response.source_nodes:
print(node.score, node.node.metadata)
print(node.node.get_content()[:240])Related: Evaluating Retrieval Quality in a LlamaIndex Pipeline
Avoid re-embedding the corpus on every process start.
from llama_index.core import (
VectorStoreIndex,
SimpleDirectoryReader,
StorageContext,
load_index_from_storage,
)
documents = SimpleDirectoryReader("data").load_data()
index = VectorStoreIndex.from_documents(documents)
index.storage_context.persist(persist_dir="./storage")
storage_context = StorageContext.from_defaults(persist_dir="./storage")
index = load_index_from_storage(storage_context)
print(index.as_query_engine().query("Summarize support hours."))Fetch context only when you synthesize elsewhere (or for debugging).
retriever = index.as_retriever(similarity_top_k=2)
nodes = retriever.retrieve("refund policy")
for n in nodes:
print(n.score, n.get_content()[:200])Related: Ingesting and Chunking Documents for an Agent's Knowledge Base
Let an agent decide when to consult your docs.
import asyncio
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
from llama_index.core.tools import QueryEngineTool
from llama_index.core.agent.workflow import FunctionAgent
from llama_index.llms.openai import OpenAI # verify package path at build
documents = SimpleDirectoryReader("data").load_data()
index = VectorStoreIndex.from_documents(documents)
qe = index.as_query_engine(similarity_top_k=3)
tool = QueryEngineTool.from_defaults(
qe,
name="policy_docs",
description="Answer questions using internal refund and support policy documents.",
)
agent = FunctionAgent(
tools=[tool],
llm=OpenAI(model="gpt-4o-mini"), # verify model at build
system_prompt="Use policy_docs for company policy questions. Be concise.",
)
async def main():
response = await agent.run("Can a customer get a refund after 10 days?")
print(str(response))
asyncio.run(main())Stack versions: Pins from the category manifest (verify at build): OpenRouter (~315+ models, July 2026 pricing/fees); LangGraph 1.0+; CrewAI 1.14+; Microsoft Agent Framework 1.0; Vercel AI SDK 6; Pydantic AI (latest); LlamaIndex (latest); OpenAI Agents SDK (latest + MCP); MCP (Linux Foundation governance); A2A (HTTP+SSE+JSON-RPC 2.0); Solana
@solana/web3.js+@solana/spl-token.
Revisado por Chris St. John·Última actualización: 16 jul 2026