@@ -465,6 +465,84 @@ AgentAwareTrait::
465465 }
466466 }
467467
468+ Agent Memory Management
469+ -----------------------
470+
471+ Symfony AI supports adding contextual memory to agent conversations, allowing the model to recall past interactions or
472+ relevant information from different sources. Memory providers inject information into the system prompt, providing the
473+ model with context without changing your application logic.
474+
475+ Using Memory
476+ ~~~~~~~~~~~~
477+
478+ Memory integration is handled through the ``MemoryInputProcessor `` and one or more ``MemoryProviderInterface `` implementations::
479+
480+ use Symfony\AI\Agent\Agent;
481+ use Symfony\AI\Agent\Memory\MemoryInputProcessor;
482+ use Symfony\AI\Agent\Memory\StaticMemoryProvider;
483+ use Symfony\AI\Platform\Message\Message;
484+ use Symfony\AI\Platform\Message\MessageBag;
485+
486+ // Platform & LLM instantiation
487+
488+ $personalFacts = new StaticMemoryProvider(
489+ 'My name is Wilhelm Tell',
490+ 'I wish to be a swiss national hero',
491+ 'I am struggling with hitting apples but want to be professional with the bow and arrow',
492+ );
493+ $memoryProcessor = new MemoryInputProcessor($personalFacts);
494+
495+ $agent = new Agent($platform, $model, [$memoryProcessor]);
496+ $messages = new MessageBag(Message::ofUser('What do we do today?'));
497+ $response = $agent->call($messages);
498+
499+ Memory Providers
500+ ~~~~~~~~~~~~~~~~
501+
502+ The library includes several memory provider implementations that are ready to use out of the box.
503+
504+ **Static Memory **
505+
506+ Static memory provides fixed information to the agent, such as user preferences, application context, or any other
507+ information that should be consistently available without being directly added to the system prompt::
508+
509+ use Symfony\AI\Agent\Memory\StaticMemoryProvider;
510+
511+ $staticMemory = new StaticMemoryProvider(
512+ 'The user is allergic to nuts',
513+ 'The user prefers brief explanations',
514+ );
515+
516+ **Embedding Provider **
517+
518+ This provider leverages vector storage to inject relevant knowledge based on the user's current message. It can be used
519+ for retrieving general knowledge from a store or recalling past conversation pieces that might be relevant::
520+
521+ use Symfony\AI\Agent\Memory\EmbeddingProvider;
522+
523+ $embeddingsMemory = new EmbeddingProvider(
524+ $platform,
525+ $embeddings, // Your embeddings model for vectorizing user messages
526+ $store // Your vector store to query for relevant context
527+ );
528+
529+ Dynamic Memory Control
530+ ~~~~~~~~~~~~~~~~~~~~~~
531+
532+ Memory is globally configured for the agent, but you can selectively disable it for specific calls when needed. This is
533+ useful when certain interactions shouldn't be influenced by the memory context::
534+
535+ $response = $agent->call($messages, [
536+ 'use_memory' => false, // Disable memory for this specific call
537+ ]);
538+
539+
540+ **Code Examples **
541+
542+ * `Chat with static memory `_
543+ * `Chat with embedding search memory `_
544+
545+
468546.. _`Platform Component` : https://github.com/symfony/ai-platform
469547.. _`Brave Tool` : https://github.com/symfony/ai/blob/main/examples/toolbox/brave.php
470548.. _`Clock Tool` : https://github.com/symfony/ai/blob/main/examples/toolbox/clock.php
@@ -479,3 +557,5 @@ AgentAwareTrait::
479557.. _`RAG with Pinecone` : https://github.com/symfony/ai/blob/main/examples/store/pinecone-similarity-search.php
480558.. _`Structured Output with PHP class` : https://github.com/symfony/ai/blob/main/examples/openai/structured-output-math.php
481559.. _`Structured Output with array` : https://github.com/symfony/ai/blob/main/examples/openai/structured-output-clock.php
560+ .. _`Chat with static memory` : https://github.com/symfony/ai/blob/main/examples/misc/chat-with-memory.php
561+ .. _`Chat with embedding search memory` : https://github.com/symfony/ai/blob/main/examples/store/mariadb-chat-memory.php
0 commit comments