Keyword
BaseExtractor #
Bases: TransformComponent
Metadata extractor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
is_text_node_only
|
bool
|
|
True
|
show_progress
|
bool
|
Whether to show progress. |
True
|
metadata_mode
|
MetadataMode
|
Metadata mode to use when reading nodes. |
<MetadataMode.ALL: 'all'>
|
node_text_template
|
str
|
Template to represent how node text is mixed with metadata text. |
'[Excerpt from document]\n{metadata_str}\nExcerpt:\n-----\n{content}\n-----\n'
|
disable_template_rewrite
|
bool
|
Disable the node template rewrite. |
False
|
in_place
|
bool
|
Whether to process nodes in place. |
True
|
num_workers
|
int
|
Number of workers to use for concurrent async processing. |
4
|
Source code in llama_index/core/extractors/interface.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | |
class_name
classmethod
#
class_name() -> str
Get class name.
Source code in llama_index/core/extractors/interface.py
73 74 75 76 | |
aextract
abstractmethod
async
#
aextract(nodes: Sequence[BaseNode]) -> List[Dict]
Extracts metadata for a sequence of nodes, returning a list of metadata dictionaries corresponding to each node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nodes
|
Sequence[Document]
|
nodes to extract metadata from |
required |
Source code in llama_index/core/extractors/interface.py
78 79 80 81 82 83 84 85 86 87 | |
extract #
extract(nodes: Sequence[BaseNode]) -> List[Dict]
Extracts metadata for a sequence of nodes, returning a list of metadata dictionaries corresponding to each node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nodes
|
Sequence[Document]
|
nodes to extract metadata from |
required |
Source code in llama_index/core/extractors/interface.py
89 90 91 92 93 94 95 96 97 98 | |
aprocess_nodes
async
#
aprocess_nodes(nodes: Sequence[BaseNode], excluded_embed_metadata_keys: Optional[List[str]] = None, excluded_llm_metadata_keys: Optional[List[str]] = None, **kwargs: Any) -> List[BaseNode]
Post process nodes parsed from documents.
Allows extractors to be chained.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nodes
|
List[BaseNode]
|
nodes to post-process |
required |
excluded_embed_metadata_keys
|
Optional[List[str]]
|
keys to exclude from embed metadata |
None
|
excluded_llm_metadata_keys
|
Optional[List[str]]
|
keys to exclude from llm metadata |
None
|
Source code in llama_index/core/extractors/interface.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | |
acall
async
#
Post process nodes parsed from documents.
Allows extractors to be chained.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nodes
|
List[BaseNode]
|
nodes to post-process |
required |
Source code in llama_index/core/extractors/interface.py
168 169 170 171 172 173 174 175 176 177 178 | |
KeywordExtractor #
Bases: BaseExtractor
Keyword extractor. Node-level extractor. Extracts
excerpt_keywords metadata field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
llm
|
Optional[LLM]
|
LLM |
required |
keywords
|
int
|
number of keywords to extract |
5
|
prompt_template
|
str
|
template for keyword extraction |
'{context_str}. Give {keywords} unique keywords for this document. Format as comma separated. Keywords: '
|
Source code in llama_index/core/extractors/metadata_extractors.py
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | |
PydanticProgramExtractor #
Bases: BaseExtractor, Generic[Model]
Pydantic program extractor.
Uses an LLM to extract out a Pydantic object. Return attributes of that object in a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
program
|
BasePydanticProgram[TypeVar]
|
Pydantic program to extract. |
required |
input_key
|
str
|
Key to use as input to the program (the program template string must expose this key). |
'input'
|
extract_template_str
|
str
|
Template to use for extraction. |
'Here is the content of the section:\n----------------\n{context_str}\n----------------\nGiven the contextual information, extract out a {class_name} object.'
|
Source code in llama_index/core/extractors/metadata_extractors.py
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 | |
aextract
async
#
aextract(nodes: Sequence[BaseNode]) -> List[Dict]
Extract pydantic program.
Source code in llama_index/core/extractors/metadata_extractors.py
525 526 527 528 529 530 531 532 533 534 535 | |
QuestionsAnsweredExtractor #
Bases: BaseExtractor
Questions answered extractor. Node-level extractor.
Extracts questions_this_excerpt_can_answer metadata field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
llm
|
Optional[LLM]
|
LLM |
required |
questions
|
int
|
number of questions to extract |
5
|
prompt_template
|
str
|
template for question extraction, |
'Here is the context:\n{context_str}\n\nGiven the contextual information, generate {num_questions} questions this context can provide specific answers to which are unlikely to be found elsewhere.\n\nHigher-level summaries of surrounding context may be provided as well. Try using these summaries to generate better questions that this context can answer.\n\n'
|
embedding_only
|
bool
|
whether to use embedding only |
True
|
Source code in llama_index/core/extractors/metadata_extractors.py
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | |
SummaryExtractor #
Bases: BaseExtractor
Summary extractor. Node-level extractor with adjacent sharing.
Extracts section_summary, prev_section_summary, next_section_summary
metadata fields.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
llm
|
Optional[LLM]
|
LLM |
required |
summaries
|
List[str]
|
list of summaries to extract: 'self', 'prev', 'next' |
required |
prompt_template
|
str
|
template for summary extraction |
'Here is the content of the section:\n{context_str}\n\nSummarize the key topics and entities of the section. \nSummary: '
|
Source code in llama_index/core/extractors/metadata_extractors.py
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 | |
TitleExtractor #
Bases: BaseExtractor
Title extractor. Useful for long documents. Extracts document_title
metadata field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
llm
|
Optional[LLM]
|
LLM |
required |
nodes
|
int
|
number of nodes from front to use for title extraction |
5
|
node_template
|
str
|
template for node-level title clues extraction |
'Context: {context_str}. Give a title that summarizes all of the unique entities, titles or themes found in the context. Title: '
|
combine_template
|
str
|
template for combining node-level clues into a document-level title |
'{context_str}. Based on the above candidate titles and content, what is the comprehensive title for this document? Title: '
|
is_text_node_only
|
bool
|
|
False
|
Source code in llama_index/core/extractors/metadata_extractors.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | |
DocumentContextExtractor #
Bases: BaseExtractor
An LLM-based context extractor for enhancing RAG accuracy through document analysis.
! Nodes that already have the 'key' in node.metadata will NOT be processed - will be skipped !
This extractor processes documents and their nodes to generate contextual metadata, implementing the approach described in the Anthropic "Contextual Retrieval" blog post. It handles rate limits, document size constraints, and parallel processing of nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_workers
|
int
|
|
4
|
llm
|
LLM
|
|
required |
docstore
|
BaseDocumentStore
|
|
required |
key
|
str
|
|
required |
prompt
|
str
|
|
required |
doc_ids
|
Set[str]
|
|
required |
max_context_length
|
int
|
|
required |
max_output_tokens
|
int
|
|
required |
oversized_document_strategy
|
Literal['warn', 'error', 'ignore']
|
|
required |
DEFAULT_KEY
|
str
|
|
'context'
|
Attributes:
| Name | Type | Description |
|---|---|---|
llm |
LLM
|
Language model instance for generating context |
docstore |
BaseDocumentStore
|
Storage for parent documents |
key |
str
|
Metadata key for storing extracted context |
prompt |
str
|
Prompt template for context generation |
doc_ids |
Set[str]
|
Set of processed document IDs |
max_context_length |
int
|
Maximum allowed document context length |
max_output_tokens |
int
|
Maximum tokens in generated context |
oversized_document_strategy |
OversizeStrategy
|
Strategy for handling large documents |
Example
extractor = DocumentContextExtractor(
docstore=my_docstore,
llm=my_llm,
max_context_length=64000,
max_output_tokens=256
)
metadata_list = await extractor.aextract(nodes)
Source code in llama_index/core/extractors/document_context.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 | |
aextract
async
#
aextract(nodes: Sequence[BaseNode]) -> List[Dict]
Extract context for multiple nodes asynchronously, optimized for loosely ordered nodes. Processes each node independently without guaranteeing sequential document handling. Nodes will be mostly processed in document-order assuming nodes get passed in document-order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nodes
|
Sequence[BaseNode]
|
List of nodes to process, ideally grouped by source document |
required |
Returns:
| Type | Description |
|---|---|
List[Dict]
|
List of metadata dictionaries with generated context |
Source code in llama_index/core/extractors/document_context.py
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 | |
options: members: - KeywordExtractor