|
| 1 | +Building a Chatbot with Memory |
| 2 | +============================== |
| 3 | + |
| 4 | +This guide demonstrates how to build a chatbot that remembers context across conversations |
| 5 | +using Symfony AI's memory management features. |
| 6 | + |
| 7 | +Overview |
| 8 | +-------- |
| 9 | + |
| 10 | +Memory providers allow your agents to access conversation history and user-specific information, |
| 11 | +enabling more personalized and context-aware responses. In this example, we'll build a personal |
| 12 | +trainer chatbot that remembers facts about the user. |
| 13 | + |
| 14 | +Prerequisites |
| 15 | +------------- |
| 16 | + |
| 17 | +* Symfony AI Agent component installed |
| 18 | +* OpenAI API key (or any other supported platform) |
| 19 | +* Basic understanding of Symfony AI concepts |
| 20 | + |
| 21 | +Implementation |
| 22 | +-------------- |
| 23 | + |
| 24 | +The complete implementation consists of three main parts: |
| 25 | + |
| 26 | +1. Creating a memory provider with user facts |
| 27 | +2. Configuring the agent with memory support |
| 28 | +3. Processing user messages with context awareness |
| 29 | + |
| 30 | +Complete Example |
| 31 | +~~~~~~~~~~~~~~~~ |
| 32 | + |
| 33 | +.. literalinclude:: ../../examples/memory/static.php |
| 34 | + :language: php |
| 35 | + :linenos: |
| 36 | + |
| 37 | +Key Components |
| 38 | +-------------- |
| 39 | + |
| 40 | +Memory Provider |
| 41 | +~~~~~~~~~~~~~~~ |
| 42 | + |
| 43 | +The :class:`Symfony\\AI\\Agent\\Memory\\StaticMemoryProvider` stores fixed information that should be consistently available |
| 44 | +to the agent:: |
| 45 | + |
| 46 | + use Symfony\AI\Agent\Memory\StaticMemoryProvider; |
| 47 | + |
| 48 | + $personalFacts = new StaticMemoryProvider( |
| 49 | + 'My name is Wilhelm Tell', |
| 50 | + 'I wish to be a swiss national hero', |
| 51 | + 'I am struggling with hitting apples but want to be professional with the bow and arrow', |
| 52 | + ); |
| 53 | + |
| 54 | +This information is automatically injected into the system prompt, providing the agent with |
| 55 | +context about the user without cluttering the conversation messages. |
| 56 | + |
| 57 | +Memory Input Processor |
| 58 | +~~~~~~~~~~~~~~~~~~~~~~ |
| 59 | + |
| 60 | +The :class:`Symfony\\AI\\Agent\\Memory\\MemoryInputProcessor` handles the injection of memory content into the agent's context:: |
| 61 | + |
| 62 | + use Symfony\AI\Agent\Memory\MemoryInputProcessor; |
| 63 | + |
| 64 | + $memoryProcessor = new MemoryInputProcessor($personalFacts); |
| 65 | + |
| 66 | +This processor works alongside other input processors like :class:`Symfony\\AI\\Agent\\InputProcessor\\SystemPromptInputProcessor` |
| 67 | +to build a complete context for the agent. |
| 68 | + |
| 69 | +Agent Configuration |
| 70 | +~~~~~~~~~~~~~~~~~~~ |
| 71 | + |
| 72 | +The agent is configured with both system prompt and memory processors:: |
| 73 | + |
| 74 | + use Symfony\AI\Agent\Agent; |
| 75 | + |
| 76 | + $agent = new Agent( |
| 77 | + $platform, |
| 78 | + 'gpt-4o-mini', |
| 79 | + [$systemPromptProcessor, $memoryProcessor] |
| 80 | + ); |
| 81 | + |
| 82 | +Processors are applied in order, allowing you to build up the context progressively. |
| 83 | + |
| 84 | +How It Works |
| 85 | +------------ |
| 86 | + |
| 87 | +1. **Memory Loading**: When a user message is submitted, the ``MemoryInputProcessor`` loads |
| 88 | + relevant facts from the memory provider. |
| 89 | + |
| 90 | +2. **Context Injection**: The memory content is prepended to the system prompt, giving the |
| 91 | + agent access to user-specific information. |
| 92 | + |
| 93 | +3. **Response Generation**: The agent generates a personalized response based on both the |
| 94 | + current message and the remembered context. |
| 95 | + |
| 96 | +4. **Conversation Flow**: The memory persists across multiple calls, enabling continuous |
| 97 | + personalized interactions. |
| 98 | + |
| 99 | +Use Dynamic Memory with Embeddings |
| 100 | +---------------------------------- |
| 101 | + |
| 102 | +For more sophisticated scenarios, use :class:`Symfony\\AI\\Agent\\Memory\\EmbeddingProvider` to retrieve relevant context |
| 103 | +based on semantic similarity:: |
| 104 | + |
| 105 | + use Symfony\AI\Agent\Memory\EmbeddingProvider; |
| 106 | + |
| 107 | + $embeddingsMemory = new EmbeddingProvider( |
| 108 | + $platform, |
| 109 | + $embeddings, // Your embeddings model |
| 110 | + $store // Your vector store |
| 111 | + ); |
| 112 | + |
| 113 | +This approach allows the agent to recall specific pieces of information from a large |
| 114 | +knowledge base based on the current conversation context. |
| 115 | + |
| 116 | +Bundle Configuration |
| 117 | +-------------------- |
| 118 | + |
| 119 | +When using the AI Bundle, you can configure memory directly in your configuration: |
| 120 | + |
| 121 | +.. code-block:: yaml |
| 122 | +
|
| 123 | + # config/packages/ai.yaml |
| 124 | + ai: |
| 125 | + agent: |
| 126 | + trainer: |
| 127 | + model: 'gpt-4o-mini' |
| 128 | + prompt: |
| 129 | + text: 'Provide short, motivating claims' |
| 130 | + memory: 'You are a professional trainer with personalized advice' |
| 131 | +
|
| 132 | +For dynamic memory using a custom service: |
| 133 | + |
| 134 | +.. code-block:: yaml |
| 135 | +
|
| 136 | + ai: |
| 137 | + agent: |
| 138 | + trainer: |
| 139 | + model: 'gpt-4o-mini' |
| 140 | + prompt: |
| 141 | + text: 'Provide short, motivating claims' |
| 142 | + memory: |
| 143 | + service: 'app.user_memory_provider' |
| 144 | +
|
| 145 | +Best Practices |
| 146 | +-------------- |
| 147 | + |
| 148 | +* **Keep Static Memory Concise**: Only include essential facts to avoid overwhelming the agent |
| 149 | +* **Separate Concerns**: Use system prompt for behavior, memory for context |
| 150 | +* **Update Dynamically**: For user-specific applications, update memory as you learn more about the user |
| 151 | +* **Test Without Memory**: Verify your agent works correctly both with and without memory enabled |
| 152 | +* **Monitor Token Usage**: Memory content consumes input tokens, so balance comprehensiveness with cost |
| 153 | + |
| 154 | +Use Cases |
| 155 | +--------- |
| 156 | + |
| 157 | +* **Personal Assistants**: Remember user preferences, habits, and history |
| 158 | +* **Customer Support**: Recall previous interactions and customer details |
| 159 | +* **Educational Tools**: Track student progress and learning style |
| 160 | +* **Healthcare Applications**: Maintain patient history and treatment context |
| 161 | + |
| 162 | +Related Documentation |
| 163 | +--------------------- |
| 164 | + |
| 165 | +* :doc:`../components/agent` - Agent component documentation |
| 166 | +* :doc:`../bundles/ai-bundle` - AI Bundle configuration reference |
| 167 | +* :doc:`rag-implementation` - Retrieval Augmented Generation guide |
0 commit comments