0
# LlamaIndex Core
1
2
LlamaIndex Core provides the foundational framework for building LLM applications, particularly RAG (Retrieval-Augmented Generation) systems. It includes essential abstractions for LLMs, Vector Stores, Embeddings, Storage, and Callables that serve as building blocks for data-driven LLM applications. The core library is designed for extensibility through subclasses and works seamlessly with over 300 LlamaIndex integration packages.
3
4
## Package Information
5
6
- **Package Name**: llama-index-core
7
- **Language**: Python
8
- **Installation**: `pip install llama-index-core`
9
- **Version**: 0.13.4
10
- **License**: MIT
11
12
## Core Imports
13
14
```python
15
import llama_index.core
16
```
17
18
Common imports for building RAG applications:
19
20
```python
21
from llama_index.core import VectorStoreIndex, Document, ServiceContext, StorageContext
22
from llama_index.core import Settings
23
from llama_index.core.llms import LLM
24
from llama_index.core.embeddings import BaseEmbedding
25
```
26
27
## Basic Usage
28
29
```python
30
from llama_index.core import VectorStoreIndex, Document, Settings
31
from llama_index.core.llms import MockLLM
32
from llama_index.core.embeddings import MockEmbedding
33
34
# Configure global settings
35
Settings.llm = MockLLM()
36
Settings.embed_model = MockEmbedding(embed_dim=384)
37
38
# Create documents
39
documents = [
40
Document(text="This is a sample document about machine learning."),
41
Document(text="LlamaIndex helps build RAG applications with LLMs."),
42
Document(text="Vector stores enable semantic search over documents.")
43
]
44
45
# Create vector store index
46
index = VectorStoreIndex.from_documents(documents)
47
48
# Query the index
49
query_engine = index.as_query_engine()
50
response = query_engine.query("What is LlamaIndex?")
51
print(response.response)
52
53
# Use as retriever
54
retriever = index.as_retriever(similarity_top_k=2)
55
nodes = retriever.retrieve("machine learning")
56
for node in nodes:
57
print(f"Score: {node.score}, Text: {node.text}")
58
```
59
60
## Architecture
61
62
LlamaIndex Core follows a modular architecture built around several key abstractions:
63
64
- **Documents & Nodes**: Core data structures representing textual content with metadata
65
- **Indices**: Storage and retrieval abstractions (Vector, Tree, Keyword, Graph-based)
66
- **LLMs & Embeddings**: Pluggable language model and embedding interfaces
67
- **Query Engines**: High-level interfaces for question-answering over data
68
- **Retrievers**: Components for finding relevant information from indices
69
- **Settings**: Global configuration system for LLMs, embeddings, and other components
70
71
This design enables composable RAG applications where developers can mix and match components based on their specific requirements, while the extensive integration ecosystem provides concrete implementations for popular services.
72
73
## Capabilities
74
75
### Document Processing & Node Management
76
77
Core data structures and utilities for handling documents, creating nodes, and managing textual content with metadata and relationships.
78
79
```python { .api }
80
class Document:
81
def __init__(self, text: str, metadata: Optional[dict] = None, **kwargs): ...
82
83
class TextNode:
84
def __init__(self, text: str, metadata: Optional[dict] = None, **kwargs): ...
85
86
class NodeWithScore:
87
def __init__(self, node: BaseNode, score: Optional[float] = None): ...
88
```
89
90
[Documents & Nodes](./documents-nodes.md)
91
92
### Index Construction & Management
93
94
Multiple index types for different data organization patterns, including vector-based semantic search, tree structures, keyword tables, and knowledge graphs.
95
96
```python { .api }
97
class VectorStoreIndex:
98
@classmethod
99
def from_documents(cls, documents: Sequence[Document], **kwargs) -> "VectorStoreIndex": ...
100
def as_query_engine(self, **kwargs) -> BaseQueryEngine: ...
101
def as_retriever(self, **kwargs) -> BaseRetriever: ...
102
103
class TreeIndex:
104
@classmethod
105
def from_documents(cls, documents: Sequence[Document], **kwargs) -> "TreeIndex": ...
106
107
class KeywordTableIndex:
108
@classmethod
109
def from_documents(cls, documents: Sequence[Document], **kwargs) -> "KeywordTableIndex": ...
110
```
111
112
[Indices](./indices.md)
113
114
### Query Engines & Question Answering
115
116
High-level interfaces for question-answering, including basic retrieval engines, multi-step reasoning, routing, and specialized SQL/pandas query engines.
117
118
```python { .api }
119
class BaseQueryEngine:
120
def query(self, str_or_query_bundle: Union[str, QueryBundle]) -> RESPONSE_TYPE: ...
121
122
class RetrieverQueryEngine:
123
def __init__(self, retriever: BaseRetriever, response_synthesizer: Optional[BaseSynthesizer] = None): ...
124
125
def get_response_synthesizer(**kwargs) -> BaseSynthesizer: ...
126
```
127
128
[Query Engines](./query-engines.md)
129
130
### Retrieval Systems
131
132
Components for finding and ranking relevant information from indices, including vector similarity, keyword matching, and advanced retrieval strategies.
133
134
```python { .api }
135
class BaseRetriever:
136
def retrieve(self, str_or_query_bundle: Union[str, QueryBundle]) -> List[NodeWithScore]: ...
137
138
class VectorIndexRetriever:
139
def __init__(self, index: VectorStoreIndex, similarity_top_k: int = 10, **kwargs): ...
140
141
class RecursiveRetriever:
142
def __init__(self, root_id: str, retriever_dict: Dict[str, BaseRetriever]): ...
143
```
144
145
[Retrievers](./retrievers.md)
146
147
### LLM & Embedding Interfaces
148
149
Pluggable interfaces for language models and embedding systems, supporting both synchronous and asynchronous operations with extensive customization options.
150
151
```python { .api }
152
class LLM:
153
def complete(self, prompt: str, **kwargs) -> CompletionResponse: ...
154
def chat(self, messages: Sequence[ChatMessage], **kwargs) -> ChatResponse: ...
155
156
class BaseEmbedding:
157
def get_text_embedding(self, text: str) -> List[float]: ...
158
def get_text_embeddings(self, texts: List[str]) -> List[List[float]]: ...
159
160
class MockLLM(CustomLLM): ...
161
class MockEmbedding(BaseEmbedding): ...
162
```
163
164
[LLMs & Embeddings](./llms-embeddings.md)
165
166
### Text Processing & Node Parsing
167
168
Comprehensive text splitting, parsing, and preprocessing capabilities including sentence splitting, semantic chunking, code-aware parsing, and hierarchical document processing.
169
170
```python { .api }
171
class SentenceSplitter:
172
def __init__(self, chunk_size: int = 1024, chunk_overlap: int = 20, **kwargs): ...
173
def split_text(self, text: str) -> List[str]: ...
174
175
class SemanticSplitterNodeParser:
176
def __init__(self, embed_model: Optional[BaseEmbedding] = None, **kwargs): ...
177
178
class MarkdownNodeParser:
179
def get_nodes_from_documents(self, documents: Sequence[Document]) -> List[BaseNode]: ...
180
```
181
182
[Node Parsers](./node-parsers.md)
183
184
### Response Processing & Postprocessing
185
186
Components for processing and refining retrieved results, including similarity filtering, reranking, metadata replacement, and recency scoring.
187
188
```python { .api }
189
class SimilarityPostprocessor:
190
def __init__(self, similarity_cutoff: Optional[float] = None): ...
191
192
class LLMRerank:
193
def __init__(self, llm: Optional[LLM] = None, top_n: int = 10): ...
194
195
class PrevNextNodePostprocessor:
196
def __init__(self, docstore: BaseDocumentStore, num_nodes: int = 1): ...
197
```
198
199
[Postprocessors](./postprocessors.md)
200
201
### Prompt Templates & Management
202
203
Flexible prompt templating system supporting chat templates, conditional prompts, and integration with various LLM formats.
204
205
```python { .api }
206
class PromptTemplate:
207
def __init__(self, template: str, **kwargs): ...
208
def format(self, **kwargs) -> str: ...
209
210
class ChatPromptTemplate:
211
def __init__(self, message_templates: List[ChatMessage]): ...
212
213
class SelectorPromptTemplate:
214
def __init__(self, default_template: BasePromptTemplate, conditionals: List[Tuple[Callable, BasePromptTemplate]]): ...
215
```
216
217
[Prompts](./prompts.md)
218
219
### Storage & Persistence
220
221
Storage abstractions and context management for persisting indices, documents, and vector stores with support for various backends.
222
223
```python { .api }
224
class StorageContext:
225
@classmethod
226
def from_defaults(cls, **kwargs) -> "StorageContext": ...
227
228
def load_index_from_storage(storage_context: StorageContext, **kwargs) -> BaseIndex: ...
229
def load_indices_from_storage(storage_context: StorageContext, **kwargs) -> List[BaseIndex]: ...
230
```
231
232
[Storage](./storage.md)
233
234
### Agent Framework & Tools
235
236
Agent implementations supporting ReAct reasoning, function calling, and workflow orchestration with comprehensive tool integration.
237
238
```python { .api }
239
class ReActAgent:
240
def __init__(self, tools: List[BaseTool], llm: LLM, **kwargs): ...
241
def chat(self, message: str, **kwargs) -> AgentChatResponse: ...
242
243
class FunctionTool:
244
@classmethod
245
def from_defaults(cls, fn: Callable, **kwargs) -> "FunctionTool": ...
246
247
class QueryEngineTool:
248
def __init__(self, query_engine: BaseQueryEngine, metadata: ToolMetadata): ...
249
```
250
251
[Agents & Tools](./agents-tools.md)
252
253
### Evaluation Framework
254
255
Comprehensive evaluation capabilities for RAG systems including retrieval metrics, response quality assessment, and dataset generation.
256
257
```python { .api }
258
class RetrieverEvaluator:
259
def __init__(self, metrics: Optional[List[BaseMetric]] = None): ...
260
261
class FaithfulnessEvaluator:
262
def __init__(self, llm: Optional[LLM] = None): ...
263
264
class HitRate:
265
def compute(self, query: str, expected_ids: List[str], retrieved_ids: List[str]) -> RetrievalMetricResult: ...
266
```
267
268
[Evaluation](./evaluation.md)
269
270
### Global Configuration
271
272
Centralized configuration system for managing LLMs, embeddings, callback handlers, and other global settings across the application.
273
274
```python { .api }
275
class Settings:
276
llm: Optional[LLM] = None
277
embed_model: Optional[BaseEmbedding] = None
278
callback_manager: Optional[CallbackManager] = None
279
280
def set_global_service_context(service_context: ServiceContext) -> None: ...
281
def set_global_handler(handler: BaseCallbackHandler) -> None: ...
282
```
283
284
[Settings & Configuration](./settings.md)
285
286
## Core Types
287
288
```python { .api }
289
class QueryBundle:
290
def __init__(self, query_str: str, embedding: Optional[List[float]] = None, **kwargs): ...
291
292
class NodeRelationship(str, Enum):
293
SOURCE = "SOURCE"
294
PREVIOUS = "PREVIOUS"
295
NEXT = "NEXT"
296
PARENT = "PARENT"
297
CHILD = "CHILD"
298
299
class MetadataMode(str, Enum):
300
ALL = "all"
301
EMBED = "embed"
302
LLM = "llm"
303
NONE = "none"
304
305
Response = Union[str, ChatResponse, CompletionResponse]
306
RESPONSE_TYPE = Union[Response, StreamingResponse]
307
```