Building a Chatbot with Memory
This guide demonstrates how to build a chatbot that remembers context across conversations using Symfony AI's memory management features.
Overview
Memory providers allow your agents to access conversation history and user-specific information, enabling more personalized and context-aware responses. In this example, we'll build a personal trainer chatbot that remembers facts about the user.
Prerequisites
- Symfony AI Agent component installed
- OpenAI API key (or any other supported platform)
- Basic understanding of Symfony AI concepts
Implementation
The complete implementation consists of three main parts:
- Creating a memory provider with user facts
- Configuring the agent with memory support
- Processing user messages with context awareness
Complete Example
:language: php :linenos:
Key Components
Memory Provider
The StaticMemoryProvider stores fixed information that should be consistently available to the agent:
1 2 3 4 5 6 7
use Symfony\AI\Agent\Memory\StaticMemoryProvider;
$personalFacts = new StaticMemoryProvider(
'My name is Wilhelm Tell',
'I wish to be a swiss national hero',
'I am struggling with hitting apples but want to be professional with the bow and arrow',
);
This information is automatically injected into the system prompt, providing the agent with context about the user without cluttering the conversation messages.
Memory Input Processor
The MemoryInputProcessor handles the injection of memory content into the agent's context:
1 2 3
use Symfony\AI\Agent\Memory\MemoryInputProcessor;
$memoryProcessor = new MemoryInputProcessor($personalFacts);
This processor works alongside other input processors like SystemPromptInputProcessor to build a complete context for the agent.
Agent Configuration
The agent is configured with both system prompt and memory processors:
1 2 3 4 5 6 7
use Symfony\AI\Agent\Agent;
$agent = new Agent(
$platform,
'gpt-4o-mini',
[$systemPromptProcessor, $memoryProcessor]
);
Processors are applied in order, allowing you to build up the context progressively.
How It Works
- Memory Loading: When a user message is submitted, the
MemoryInputProcessorloads relevant facts from the memory provider. - Context Injection: The memory content is prepended to the system prompt, giving the agent access to user-specific information.
- Response Generation: The agent generates a personalized response based on both the current message and the remembered context.
- Conversation Flow: The memory persists across multiple calls, enabling continuous personalized interactions.
Use Dynamic Memory with Embeddings
For more sophisticated scenarios, use EmbeddingProvider to retrieve relevant context based on semantic similarity:
1 2 3 4 5 6 7
use Symfony\AI\Agent\Memory\EmbeddingProvider;
$embeddingsMemory = new EmbeddingProvider(
$platform,
$embeddings, // Your embeddings model
$store // Your vector store
);
This approach allows the agent to recall specific pieces of information from a large knowledge base based on the current conversation context.
Bundle Configuration
When using the AI Bundle, you can configure memory directly in your configuration:
1 2 3 4 5 6 7 8
# config/packages/ai.yaml
ai:
agent:
trainer:
model: 'gpt-4o-mini'
prompt:
text: 'Provide short, motivating claims'
memory: 'You are a professional trainer with personalized advice'
For dynamic memory using a custom service:
1 2 3 4 5 6 7 8
ai:
agent:
trainer:
model: 'gpt-4o-mini'
prompt:
text: 'Provide short, motivating claims'
memory:
service: 'app.user_memory_provider'
Best Practices
- Keep Static Memory Concise: Only include essential facts to avoid overwhelming the agent
- Separate Concerns: Use system prompt for behavior, memory for context
- Update Dynamically: For user-specific applications, update memory as you learn more about the user
- Test Without Memory: Verify your agent works correctly both with and without memory enabled
- Monitor Token Usage: Memory content consumes input tokens, so balance comprehensiveness with cost
Use Cases
- Personal Assistants: Remember user preferences, habits, and history
- Customer Support: Recall previous interactions and customer details
- Educational Tools: Track student progress and learning style
- Healthcare Applications: Maintain patient history and treatment context
Related Documentation
- Symfony AI - Agent Component - Agent component documentation
- AI Bundle - AI Bundle configuration reference
- Implementing Retrieval Augmented Generation (RAG) - Retrieval Augmented Generation guide