0
# LlamaIndex
1
2
LlamaIndex is a comprehensive data framework designed for building Large Language Model (LLM) applications, serving as the bridge between LLMs and various data sources. It provides a unified interface for building Retrieval-Augmented Generation (RAG) systems, enabling developers to connect their LLMs to structured and unstructured data including documents, APIs, databases, and knowledge bases.
3
4
The framework offers modular architecture with over 300 integration packages for different LLM providers, embedding models, and vector stores, allowing users to build customized solutions with their preferred technology stack. LlamaIndex simplifies the process of indexing, querying, and retrieving relevant information for LLM applications, supporting various data ingestion methods, advanced retrieval strategies, and sophisticated query engines that can handle complex multi-step reasoning tasks across diverse data sources.
5
6
## Package Information
7
8
- **Package Name**: llama-index
9
- **Language**: Python
10
- **Installation**: `pip install llama-index`
11
12
## Core Imports
13
14
```python
15
import llama_index
16
```
17
18
Common usage patterns for core functionality:
19
20
```python
21
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Settings
22
from llama_index.core.llms import LLM
23
from llama_index.core.embeddings import BaseEmbedding
24
```
25
26
Integration imports follow the pattern:
27
28
```python
29
from llama_index.llms.openai import OpenAI
30
from llama_index.embeddings.openai import OpenAIEmbedding
31
```
32
33
## Basic Usage
34
35
```python
36
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Settings
37
from llama_index.llms.openai import OpenAI
38
from llama_index.embeddings.openai import OpenAIEmbedding
39
40
# Configure global settings
41
Settings.llm = OpenAI(model="gpt-3.5-turbo", temperature=0.1)
42
Settings.embed_model = OpenAIEmbedding(model="text-embedding-ada-002")
43
44
# Load documents
45
documents = SimpleDirectoryReader("data").load_data()
46
47
# Create index
48
index = VectorStoreIndex.from_documents(documents)
49
50
# Create query engine
51
query_engine = index.as_query_engine()
52
53
# Query your data
54
response = query_engine.query("What are the main topics in the documents?")
55
print(response)
56
```
57
58
## Architecture
59
60
LlamaIndex's modular architecture enables flexible RAG application development:
61
62
- **Indices**: Data structures for efficient information retrieval (vector, keyword, tree, graph-based)
63
- **Query Engines**: Orchestrate retrieval and response synthesis with various strategies
64
- **Retrievers**: Handle document retrieval with similarity search, keyword matching, and hybrid approaches
65
- **Response Synthesizers**: Generate coherent responses from retrieved context using different summarization strategies
66
- **Node Parsers**: Process documents into chunks with various splitting strategies
67
- **Agents & Workflows**: Support complex multi-step reasoning and tool usage patterns
68
- **Storage**: Persistent storage for indices and metadata with various backends
69
- **Integrations**: 300+ integrations for LLMs, embeddings, vector stores, and data sources
70
71
This design allows every component to be customized and provides the foundation for LlamaIndex's role as a comprehensive RAG framework, supporting everything from simple document Q&A to sophisticated multi-agent workflows.
72
73
## Capabilities
74
75
### Data Indexing
76
77
Core data structures for organizing and retrieving information, including vector-based semantic search, keyword extraction, hierarchical trees, and knowledge graphs.
78
79
```python { .api }
80
class VectorStoreIndex:
81
@classmethod
82
def from_documents(cls, documents, **kwargs): ...
83
def as_query_engine(self, **kwargs): ...
84
def as_retriever(self, **kwargs): ...
85
86
class SummaryIndex:
87
@classmethod
88
def from_documents(cls, documents, **kwargs): ...
89
90
class TreeIndex:
91
@classmethod
92
def from_documents(cls, documents, **kwargs): ...
93
94
class KnowledgeGraphIndex:
95
@classmethod
96
def from_documents(cls, documents, **kwargs): ...
97
```
98
99
[Data Indexing](./data-indexing.md)
100
101
### Query Processing
102
103
Query engines that orchestrate retrieval and response generation with support for various strategies including basic retrieval, sub-question decomposition, routing, and multi-step reasoning.
104
105
```python { .api }
106
class RetrieverQueryEngine:
107
def __init__(self, retriever, response_synthesizer=None): ...
108
def query(self, query_bundle): ...
109
110
class RouterQueryEngine:
111
def __init__(self, selector, query_engines): ...
112
113
class SubQuestionQueryEngine:
114
def __init__(self, question_gen, query_engines): ...
115
```
116
117
[Query Processing](./query-processing.md)
118
119
### Document Processing
120
121
Document loading, parsing, and chunking functionality for various file formats with intelligent text splitting strategies.
122
123
```python { .api }
124
class SimpleDirectoryReader:
125
def __init__(self, input_dir=None, input_files=None, **kwargs): ...
126
def load_data(self): ...
127
128
class Document:
129
def __init__(self, text, metadata=None, **kwargs): ...
130
131
class SentenceSplitter:
132
def __init__(self, chunk_size=1024, chunk_overlap=200, **kwargs): ...
133
def split_text(self, text): ...
134
```
135
136
[Document Processing](./document-processing.md)
137
138
### LLM Integration
139
140
Unified interface for various language models with support for completion, chat, and function calling APIs.
141
142
```python { .api }
143
class LLM:
144
def complete(self, prompt, **kwargs): ...
145
def chat(self, messages, **kwargs): ...
146
147
class OpenAI(LLM):
148
def __init__(self, model="gpt-3.5-turbo", **kwargs): ...
149
```
150
151
[LLM Integration](./llm-integration.md)
152
153
### Embedding Models
154
155
Text embedding functionality supporting various providers for semantic similarity and vector search operations.
156
157
```python { .api }
158
class BaseEmbedding:
159
def get_text_embedding(self, text): ...
160
def get_query_embedding(self, query): ...
161
162
class OpenAIEmbedding(BaseEmbedding):
163
def __init__(self, model="text-embedding-ada-002", **kwargs): ...
164
```
165
166
[LLM Integration](./llm-integration.md)
167
168
### Retrievers
169
170
Advanced retrieval strategies including fusion, hierarchical, and routing approaches for sophisticated document retrieval patterns.
171
172
```python { .api }
173
class AutoMergingRetriever:
174
def __init__(self, vector_retriever, storage_context, **kwargs): ...
175
176
class QueryFusionRetriever:
177
def __init__(self, retrievers, similarity_top_k=None, **kwargs): ...
178
179
class RouterRetriever:
180
def __init__(self, selector, retriever_tools, **kwargs): ...
181
```
182
183
[Retrievers](./retrievers.md)
184
185
### Response Synthesis
186
187
Response generation strategies for combining retrieved context into coherent answers with various summarization approaches.
188
189
```python { .api }
190
class TreeSummarize:
191
def __init__(self, **kwargs): ...
192
193
class Refine:
194
def __init__(self, **kwargs): ...
195
196
def get_response_synthesizer(response_mode="compact", **kwargs): ...
197
```
198
199
[Response Synthesis](./response-synthesis.md)
200
201
### Agents & Workflows
202
203
Multi-agent systems and workflow orchestration for complex reasoning tasks, tool usage, and multi-step problem solving.
204
205
```python { .api }
206
class ReActAgent:
207
def __init__(self, tools, llm, **kwargs): ...
208
def chat(self, message): ...
209
210
class Workflow:
211
def __init__(self, **kwargs): ...
212
def add_step(self, step_fn): ...
213
214
@step
215
def custom_step(ctx: Context, ev: Event) -> Event: ...
216
```
217
218
[Agents & Workflows](./agents-workflows.md)
219
220
### Storage & Settings
221
222
Storage backends and global configuration for persisting indices, managing contexts, and configuring system-wide settings.
223
224
```python { .api }
225
class StorageContext:
226
@classmethod
227
def from_defaults(cls, persist_dir=None, **kwargs): ...
228
def persist(self, persist_dir=None): ...
229
230
class Settings:
231
llm: LLM = None
232
embed_model: BaseEmbedding = None
233
node_parser: NodeParser = None
234
```
235
236
[Storage & Settings](./storage-settings.md)
237
238
### Prompts
239
240
Template system for customizing LLM prompts with support for various formatting options and conditional logic.
241
242
```python { .api }
243
class PromptTemplate:
244
def __init__(self, template, **kwargs): ...
245
def format(self, **kwargs): ...
246
247
class ChatPromptTemplate:
248
def __init__(self, message_templates, **kwargs): ...
249
```
250
251
[Prompts](./prompts.md)