Installation

Requirements

  • Python 3.10+ (3.11 / 3.12 recommended)
  • pip, or uv / poetry
  • At least one LLM provider API key (OpenAI in this course)

The ecosystem is core + namespaced integrations. pip install llama-index is the starter bundle; extra readers and vector stores are opt-in. Full catalog: LlamaHub.


Virtual environment

python -m venv .venv
# Windows
.venv\Scripts\activate
# macOS / Linux
source .venv/bin/activate

Install packages

Official quickstart:

pip install llama-index

The bundle typically includes:

PackageRole
llama-index-coreIndexes, querying, agents, workflows
llama-index-llms-openaifrom llama_index.llms.openai import OpenAI
llama-index-embeddings-openaiOpenAI embeddings
llama-index-readers-fileLocal file readers

llama-index-core ships some NLTK / tiktoken files so those downloads are less likely at runtime.

Local models instead of OpenAI (example):

pip install llama-index-core llama-index-readers-file \
  llama-index-llms-ollama llama-index-embeddings-huggingface

Vector stores (see Vector Stores):

pip install llama-index-vector-stores-chroma
pip install llama-index-vector-stores-postgres
pip install llama-index-vector-stores-qdrant

Verify:

python -c "from llama_index.core import VectorStoreIndex; print('ok')"

API keys

Historically the default generation model was gpt-3.5-turbo and embeddings text-embedding-ada-002. This course uses gpt-4o-mini and defers to current official docs. OpenAI still needs OPENAI_API_KEY.

Environment variables (recommended)

export OPENAI_API_KEY="sk-..."

Windows PowerShell:

$env:OPENAI_API_KEY = "sk-..."

.env file

pip install python-dotenv
OPENAI_API_KEY=sk-...
from dotenv import load_dotenv
load_dotenv()

Never commit .env. OpenAI-compatible gateways can use the documented OpenAILike LLM / embedding classes with a custom api_base.


Global Settings (set these early)

When you omit a component, LlamaIndex uses Settings. Set them explicitly so defaults cannot drift:

from llama_index.core import Settings
from llama_index.llms.openai import OpenAI
from llama_index.embeddings.openai import OpenAIEmbedding

Settings.llm = OpenAI(model="gpt-4o-mini")
Settings.embed_model = OpenAIEmbedding(model="text-embedding-3-small")
Settings.chunk_size = 512
Settings.chunk_overlap = 50

Local Ollama example:

from llama_index.llms.ollama import Ollama
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
from llama_index.core import Settings

Settings.llm = Ollama(model="llama3.2", request_timeout=120.0)
Settings.embed_model = HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5")

Suggested layout

my-llamaindex-app/
├── .env
├── .gitignore
├── data/              # source documents
├── storage/           # persisted default index
├── src/
│   ├── ingest.py
│   └── query.py
└── requirements.txt

Troubleshooting

OPENAI_API_KEY errors?
The variable is missing in this process. IDE terminals and system shells can differ.

First query is slow / re-embeds every time?
The default index is in-memory. Call index.storage_context.persist(...) or attach a vector DB—Quick Start, Vector Stores.

ModuleNotFoundError: llama_index.vector_stores.chroma?
Core does not vendor every store. Install the matching llama-index-vector-stores-* package.

Dependency conflicts?
Fresh venv; do not mix with llama-index<0.10 (ServiceContext was replaced by Settings).


Next steps

评论