Memory
Memory #
Bases: BaseMemory
A memory module that waterfalls into memory blocks.
Works by orchestrating around - a FIFO queue of messages - a list of memory blocks - various parameters (pressure size, token limit, etc.)
When the FIFO queue reaches the token limit, the oldest messages within the pressure size are ejected from the FIFO queue. The messages are then processed by each memory block.
When pulling messages from this memory, the memory blocks are processed in order, and the messages are injected into the system message or the latest user message.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
token_limit
|
int
|
The overall token limit of the memory. |
30000
|
token_flush_size
|
int
|
The token size to use for flushing the FIFO queue. |
3000
|
chat_history_token_ratio
|
float
|
Minimum percentage ratio of total token limit reserved for chat history. |
0.7
|
memory_blocks
|
List[BaseMemoryBlock]
|
The list of memory blocks to use. |
<dynamic>
|
memory_blocks_template
|
RichPromptTemplate
|
The template to use for formatting the memory blocks. |
RichPromptTemplate(metadata={}, template_vars=['memory_blocks'], kwargs={}, output_parser=None, template_var_mappings=None, function_mappings=None, template_str='\n<memory>\n{% for (block_name, block_content) in memory_blocks %}\n<{{ block_name }}>\n {% for block in block_content %}\n {% if block.block_type == "text" %}\n{{ block.text }}\n {% elif block.block_type == "image" %}\n {% if block.url %}\n {{ (block.url | string) | image }}\n {% elif block.path %}\n {{ (block.path | string) | image }}\n {% endif %}\n {% elif block.block_type == "audio" %}\n {% if block.url %}\n {{ (block.url | string) | audio }}\n {% elif block.path %}\n {{ (block.path | string) | audio }}\n {% endif %}\n {% endif %}\n {% endfor %}\n</{{ block_name }}>\n{% endfor %}\n</memory>\n')
|
insert_method
|
InsertMethod
|
Whether to inject memory blocks into a system message or into the latest user message. |
<InsertMethod.SYSTEM: 'system'>
|
image_token_size_estimate
|
int
|
The token size estimate for images. |
256
|
audio_token_size_estimate
|
int
|
The token size estimate for audio. |
256
|
tokenizer_fn
|
Callable[list, List]
|
The tokenizer function to use for token counting. |
<dynamic>
|
sql_store
|
SQLAlchemyChatStore
|
The chat store to use for storing messages. |
SQLAlchemyChatStore(table_name='llama_index_memory', async_database_uri='sqlite+aiosqlite:///:memory:')
|
session_id
|
str
|
The key to use for storing messages in the chat store. |
'3b5eda2a-694d-484a-95ec-19f2c5d4355f'
|
Source code in llama-index-core/llama_index/core/memory/memory.py
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 348 349 350 351 352 353 354 355 356 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 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 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 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 |
|
from_defaults
classmethod
#
from_defaults(session_id: Optional[str] = None, chat_history: Optional[List[ChatMessage]] = None, token_limit: int = DEFAULT_TOKEN_LIMIT, memory_blocks: Optional[List[BaseMemoryBlock[Any]]] = None, tokenizer_fn: Optional[Callable[[str], List]] = None, chat_history_token_ratio: float = 0.7, token_flush_size: int = DEFAULT_FLUSH_SIZE, memory_blocks_template: RichPromptTemplate = DEFAULT_MEMORY_BLOCKS_TEMPLATE, insert_method: InsertMethod = SYSTEM, image_token_size_estimate: int = 256, audio_token_size_estimate: int = 256, table_name: str = 'llama_index_memory', async_database_uri: Optional[str] = None, async_engine: Optional[AsyncEngine] = None) -> Memory
Initialize Memory.
Source code in llama-index-core/llama_index/core/memory/memory.py
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 |
|
aget
async
#
aget(**block_kwargs: Any) -> List[ChatMessage]
Get messages with memory blocks included (async).
Source code in llama-index-core/llama_index/core/memory/memory.py
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 |
|
aput
async
#
aput(message: ChatMessage) -> None
Add a message to the chat store and process waterfall logic if needed.
Source code in llama-index-core/llama_index/core/memory/memory.py
600 601 602 603 604 605 606 |
|
aput_messages
async
#
aput_messages(messages: List[ChatMessage]) -> None
Add a list of messages to the chat store and process waterfall logic if needed.
Source code in llama-index-core/llama_index/core/memory/memory.py
608 609 610 611 612 613 614 |
|
aset
async
#
aset(messages: List[ChatMessage]) -> None
Set the chat history.
Source code in llama-index-core/llama_index/core/memory/memory.py
616 617 618 |
|
aget_all
async
#
aget_all(status: Optional[MessageStatus] = None) -> List[ChatMessage]
Get all messages.
Source code in llama-index-core/llama_index/core/memory/memory.py
620 621 622 |
|
areset
async
#
areset(status: Optional[MessageStatus] = None) -> None
Reset the memory.
Source code in llama-index-core/llama_index/core/memory/memory.py
624 625 626 |
|
get #
get(**block_kwargs: Any) -> List[ChatMessage]
Get messages with memory blocks included.
Source code in llama-index-core/llama_index/core/memory/memory.py
630 631 632 |
|
get_all #
get_all(status: Optional[MessageStatus] = None) -> List[ChatMessage]
Get all messages.
Source code in llama-index-core/llama_index/core/memory/memory.py
634 635 636 |
|
put #
put(message: ChatMessage) -> None
Add a message to the chat store and process waterfall logic if needed.
Source code in llama-index-core/llama_index/core/memory/memory.py
638 639 640 |
|
set #
set(messages: List[ChatMessage]) -> None
Set the chat history.
Source code in llama-index-core/llama_index/core/memory/memory.py
642 643 644 |
|
reset #
reset() -> None
Reset the memory.
Source code in llama-index-core/llama_index/core/memory/memory.py
646 647 648 |
|
BaseMemoryBlock #
Bases: BaseModel
, Generic[T]
A base class for memory blocks.
Subclasses must implement the aget
and aput
methods.
Optionally, subclasses can implement the atruncate
method, which is used to reduce the size of the memory block.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name/identifier of the memory block. |
required |
description
|
str | None
|
A description of the memory block. |
None
|
priority
|
int
|
Priority of this memory block (0 = never truncate, 1 = highest priority, etc.). |
0
|
accept_short_term_memory
|
bool
|
Whether to accept puts from messages ejected from the short-term memory. |
True
|
Source code in llama-index-core/llama_index/core/memory/memory.py
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 |
|
aget
async
#
aget(messages: Optional[List[ChatMessage]] = None, **block_kwargs: Any) -> T
Pull the memory block (async).
Returns:
Name | Type | Description |
---|---|---|
T |
T
|
The memory block content. One of: |
T
|
|
|
T
|
|
|
T
|
|
Source code in llama-index-core/llama_index/core/memory/memory.py
83 84 85 86 87 88 89 90 91 92 93 94 |
|
aput
async
#
aput(messages: List[ChatMessage], from_short_term_memory: bool = False, session_id: Optional[str] = None) -> None
Push to the memory block (async).
Source code in llama-index-core/llama_index/core/memory/memory.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
|
atruncate
async
#
atruncate(content: T, tokens_to_truncate: int) -> Optional[T]
Truncate the memory block content to the given token limit.
By default, truncation will remove the entire block content.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
content
|
T
|
The content of type T, depending on what the memory block returns. |
required |
tokens_to_truncate
|
int
|
The number of tokens requested to truncate the content by. Blocks may or may not truncate to the exact number of tokens requested, but it can be used as a hint for the block to truncate. |
required |
Returns:
Type | Description |
---|---|
Optional[T]
|
The truncated content of type T, or None if the content is completely truncated. |
Source code in llama-index-core/llama_index/core/memory/memory.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
|
InsertMethod #
Bases: Enum
Source code in llama-index-core/llama_index/core/memory/memory.py
49 50 51 |
|
StaticMemoryBlock #
Bases: BaseMemoryBlock[List[ContentBlock]]
A memory block that returns static text.
This block is useful for including constant information or instructions in the context without relying on external processing.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the memory block. |
'StaticContent'
|
static_content
|
List[Annotated[Union[TextBlock, ImageBlock, AudioBlock, DocumentBlock], FieldInfo]]
|
Static text or content to be returned by this memory block. |
required |
Source code in llama-index-core/llama_index/core/memory/memory_blocks/static.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
|
VectorMemoryBlock #
Bases: BaseMemoryBlock[str]
A memory block that retrieves relevant information from a vector store.
This block stores conversation history in a vector store and retrieves relevant information based on the most recent messages.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the memory block. |
'RetrievedMessages'
|
vector_store
|
BasePydanticVectorStore
|
The vector store to use for retrieval. |
required |
embed_model
|
BaseEmbedding
|
The embedding model to use for encoding queries and documents. |
<dynamic>
|
similarity_top_k
|
int
|
Number of top results to return. |
2
|
retrieval_context_window
|
int
|
Maximum number of messages to include for context when retrieving. |
5
|
format_template
|
BasePromptTemplate
|
Template for formatting the retrieved information. |
RichPromptTemplate(metadata={}, template_vars=['text'], kwargs={}, output_parser=None, template_var_mappings=None, function_mappings=None, template_str='{{ text }}')
|
node_postprocessors
|
List[BaseNodePostprocessor]
|
List of node postprocessors to apply to the retrieved nodes containing messages. |
<dynamic>
|
Source code in llama-index-core/llama_index/core/memory/memory_blocks/vector.py
19 20 21 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 |
|
FactExtractionMemoryBlock #
Bases: BaseMemoryBlock[str]
A memory block that extracts key facts from conversation history using an LLM.
This block identifies and stores discrete facts disclosed during the conversation, structuring them in XML format for easy parsing and retrieval.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the memory block. |
'ExtractedFacts'
|
llm
|
LLM
|
The LLM to use for fact extraction. |
<dynamic>
|
facts
|
List[str]
|
List of extracted facts from the conversation. |
<dynamic>
|
max_facts
|
int
|
The maximum number of facts to store. |
50
|
fact_extraction_prompt_template
|
BasePromptTemplate
|
Template for the fact extraction prompt. |
RichPromptTemplate(metadata={}, template_vars=['existing_facts'], kwargs={}, output_parser=None, template_var_mappings=None, function_mappings=None, template_str='You are a precise fact extraction system designed to identify key information from conversations.\n\nINSTRUCTIONS:\n1. Review the conversation segment provided prior to this message\n2. Extract specific, concrete facts the user has disclosed or important information discovered\n3. Focus on factual information like preferences, personal details, requirements, constraints, or context\n4. Format each fact as a separate <fact> XML tag\n5. Do not include opinions, summaries, or interpretations - only extract explicit information\n6. Do not duplicate facts that are already in the existing facts list\n\n<existing_facts>\n{{ existing_facts }}\n</existing_facts>\n\nReturn ONLY the extracted facts in this exact format:\n<facts>\n <fact>Specific fact 1</fact>\n <fact>Specific fact 2</fact>\n <!-- More facts as needed -->\n</facts>\n\nIf no new facts are present, return: <facts></facts>')
|
fact_condense_prompt_template
|
BasePromptTemplate
|
Template for the fact condense prompt. |
RichPromptTemplate(metadata={}, template_vars=['existing_facts', 'max_facts'], kwargs={}, output_parser=None, template_var_mappings=None, function_mappings=None, template_str='You are a precise fact condensing system designed to identify key information from conversations.\n\nINSTRUCTIONS:\n1. Review the current list of existing facts\n2. Condense the facts into a more concise list, less than {{ max_facts }} facts\n3. Focus on factual information like preferences, personal details, requirements, constraints, or context\n4. Format each fact as a separate <fact> XML tag\n5. Do not include opinions, summaries, or interpretations - only extract explicit information\n6. Do not duplicate facts that are already in the existing facts list\n\n<existing_facts>\n{{ existing_facts }}\n</existing_facts>\n\nReturn ONLY the condensed facts in this exact format:\n<facts>\n <fact>Specific fact 1</fact>\n <fact>Specific fact 2</fact>\n <!-- More facts as needed -->\n</facts>\n\nIf no new facts are present, return: <facts></facts>')
|
Source code in llama-index-core/llama_index/core/memory/memory_blocks/fact.py
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 |
|