|
2 | 2 |
|
3 | 3 | ## Agent Memory
|
4 | 4 |
|
| 5 | +The `AgentMemory` class manages conversation history and state for AI agents: |
| 6 | + |
| 7 | +```python |
| 8 | +from atomic_agents.lib.components.agent_memory import AgentMemory |
| 9 | +from atomic_agents.lib.base.base_io_schema import BaseIOSchema |
| 10 | + |
| 11 | +# Initialize memory with optional max messages |
| 12 | +memory = AgentMemory(max_messages=10) |
| 13 | + |
| 14 | +# Add messages with proper schemas |
| 15 | +memory.add_message( |
| 16 | + role="assistant", |
| 17 | + content=BaseIOSchema(...) |
| 18 | +) |
| 19 | + |
| 20 | +# Access history |
| 21 | +history = memory.get_history() |
| 22 | + |
| 23 | +# Manage turns |
| 24 | +memory.initialize_turn() # Start new turn |
| 25 | +turn_id = memory.get_current_turn_id() |
| 26 | + |
| 27 | +# Persistence |
| 28 | +serialized = memory.dump() # Save to string |
| 29 | +memory.load(serialized) # Load from string |
| 30 | + |
| 31 | +# Create copy |
| 32 | +new_memory = memory.copy() |
| 33 | +``` |
| 34 | + |
| 35 | +Key features: |
| 36 | +- Message history management with role-based messages |
| 37 | +- Turn-based conversation tracking |
| 38 | +- Support for multimodal content |
| 39 | +- Serialization and deserialization |
| 40 | +- Memory size management |
| 41 | +- Deep copy functionality |
| 42 | + |
| 43 | +For full API details: |
5 | 44 | ```{eval-rst}
|
6 | 45 | .. automodule:: atomic_agents.lib.components.agent_memory
|
7 | 46 | :members:
|
|
12 | 51 |
|
13 | 52 | ## System Prompt Generator
|
14 | 53 |
|
| 54 | +The `SystemPromptGenerator` creates structured system prompts for AI agents: |
| 55 | + |
| 56 | +```python |
| 57 | +from atomic_agents.lib.components.system_prompt_generator import ( |
| 58 | + SystemPromptGenerator, |
| 59 | + SystemPromptContextProviderBase |
| 60 | +) |
| 61 | + |
| 62 | +# Create basic generator |
| 63 | +generator = SystemPromptGenerator( |
| 64 | + background=[ |
| 65 | + "You are a helpful AI assistant.", |
| 66 | + "You specialize in technical support." |
| 67 | + ], |
| 68 | + steps=[ |
| 69 | + "1. Understand the user's request", |
| 70 | + "2. Analyze available information", |
| 71 | + "3. Provide clear solutions" |
| 72 | + ], |
| 73 | + output_instructions=[ |
| 74 | + "Use clear, concise language", |
| 75 | + "Include step-by-step instructions", |
| 76 | + "Cite relevant documentation" |
| 77 | + ] |
| 78 | +) |
| 79 | + |
| 80 | +# Generate prompt |
| 81 | +prompt = generator.generate_prompt() |
| 82 | +``` |
| 83 | + |
| 84 | +### Context Providers |
| 85 | + |
| 86 | +Create custom context providers by extending `SystemPromptContextProviderBase`: |
| 87 | + |
| 88 | +```python |
| 89 | +class CustomContextProvider(SystemPromptContextProviderBase): |
| 90 | + def __init__(self, title: str): |
| 91 | + super().__init__(title=title) |
| 92 | + self.data = {} |
| 93 | + |
| 94 | + def get_info(self) -> str: |
| 95 | + return f"Custom context: {self.data}" |
| 96 | + |
| 97 | +# Use with generator |
| 98 | +generator = SystemPromptGenerator( |
| 99 | + background=["You are a helpful AI assistant."], |
| 100 | + context_providers={ |
| 101 | + "custom": CustomContextProvider("Custom Info") |
| 102 | + } |
| 103 | +) |
| 104 | +``` |
| 105 | + |
| 106 | +For full API details: |
15 | 107 | ```{eval-rst}
|
16 | 108 | .. automodule:: atomic_agents.lib.components.system_prompt_generator
|
17 | 109 | :members:
|
|
0 commit comments