Skip to content

Conversation

@DZunke
Copy link
Contributor

@DZunke DZunke commented Jul 14, 2025

Q A
Bug fix? no
New feature? yes
Docs? yes
Issues
License MIT

See original contribution at php-llm/llm-chain#387

This PR introduces a flexible memory system that allows the LLM to recall contextual information that are permantent for the conversation. In difference to tools it can be always utilized, when use_memory option is not disabled. It would be possible to fetch memory by a tool with a system instruction like always call the tool foo_bar but, for me, this feels like a bad design to always force the model to do a tool call without further need.

Currently i have added just two memory providers to show what my idea is. I could also think about a write layer to fill a memory, together with a read layer, this could, for example, be working good with a graph database and tools for memory handling. But these are ideas for the future.

So for this first throw i hope you get what i was thinking about to reach. I decided to inject the memory to the system prompt, when it is available, instead of adding a second system prompt to the message bag. But this was just a 50/50 thinking. I tried both seem to be working equally, at least for open ai. I am open to change it back again.

What do you think?

@DZunke DZunke requested review from Nyholm and chr-hertel as code owners July 14, 2025 08:19
@DZunke DZunke force-pushed the agent-memory-processor branch 2 times, most recently from 3418c1f to 848ccc9 Compare July 14, 2025 08:29
@OskarStark OskarStark added the Agent Issues & PRs about the AI Agent component label Jul 14, 2025
@OskarStark OskarStark changed the title Add basic setup for memory injections to system prompt [Agent] Add basic setup for memory injections to system prompt Jul 14, 2025
Copy link
Member

@chr-hertel chr-hertel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for moving this - that saves me some time 🙏

Found one small left over in the example, and please remove declare(strict_types=1); - then we're good to merge this :)

@OskarStark
Copy link
Contributor

please remove declare(strict_types=1); - then we're good to merge this :)

Can we update the PHP-CS-Fixer config?

@DZunke DZunke force-pushed the agent-memory-processor branch from 848ccc9 to d9b13f9 Compare July 15, 2025 18:05
@DZunke DZunke requested a review from OskarStark as a code owner July 15, 2025 18:05
@DZunke DZunke requested a review from chr-hertel July 15, 2025 18:06
@chr-hertel chr-hertel added the Feature New feature label Jul 16, 2025
Copy link
Member

@chr-hertel chr-hertel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good now 👍

image

@chr-hertel
Copy link
Member

Thank you @DZunke.

@chr-hertel chr-hertel merged commit 059db10 into symfony:main Jul 16, 2025
13 checks passed
OskarStark added a commit that referenced this pull request Sep 15, 2025
…skarStark)

This PR was squashed before being merged into the main branch.

Discussion
----------

[AI Bundle] Add memory provider configuration support

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| Docs?         | yes
| Issues        | Follows #117
| License       | MIT

This PR introduces comprehensive memory provider configuration capabilities for AI agents, enabling both simple static memory and advanced dynamic memory scenarios.

## 🚀 Key Features

### Smart Detection Logic
The system automatically detects the intent:
- If the value matches an existing service/alias → uses that service directly
- If no matching service exists → creates `StaticMemoryProvider` with the string as static content

### Memory as System Prompt
- When only `memory` is provided (no `prompt`), memory serves as the system prompt
- When both `memory` and `prompt` are provided, memory is prepended to the prompt

### Configuration Examples

**Static Memory (Most Common):**
```yaml
ai:
  agent:
    my_agent:
      model:
        class: 'Symfony\AI\Platform\Bridge\OpenAi\Gpt'
        name: !php/const Symfony\AI\Platform\Bridge\OpenAi\Gpt::GPT_4O_MINI
      memory: 'You have access to user preferences and conversation history'
      prompt:
        text: 'You are a helpful assistant.'
```

**Dynamic Memory (Advanced):**
```yaml
ai:
  agent:
    my_agent:
      model:
        class: 'Symfony\AI\Platform\Bridge\OpenAi\Gpt'
        name: !php/const Symfony\AI\Platform\Bridge\OpenAi\Gpt::GPT_4O_MINI
      memory: 'my_custom_memory_service'  # References existing service
      prompt:
        text: 'You are a helpful assistant.'
```

**Memory as System Prompt:**
```yaml
ai:
  agent:
    my_agent:
      model:
        class: 'Symfony\AI\Platform\Bridge\OpenAi\Gpt'
        name: !php/const Symfony\AI\Platform\Bridge\OpenAi\Gpt::GPT_4O_MINI
      memory: 'You are a helpful assistant with access to user context.'
      # No prompt needed - memory serves as system prompt
```

This enhancement significantly improves the developer experience while maintaining full backward compatibility and adding powerful new memory capabilities for AI agents.

cc `@DZunke`

Commits
-------

f4ac5b6 [AI Bundle] Add memory provider configuration support
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Agent Issues & PRs about the AI Agent component Feature New feature Status: Needs Work

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants