Dynamic Prompts in Legion
Dynamic prompts allow you to create flexible system prompts that can change based on context and runtime conditions. This powerful feature enables agents to adapt their behavior and responses based on dynamic values provided during execution.
What are Dynamic Prompts?
Dynamic prompts are system prompts that can:
- Change based on context and runtime conditions
- Include dynamic values that update during execution
- Adapt to user preferences and states
- Support callable default values
- Maintain sections with different update rules
Creating Dynamic Prompts
Here's how to create and use dynamic prompts:
from datetime import datetime
from legion import agent, tool, system_prompt
def get_current_time() -> str:
"""Get formatted current time."""
return datetime.now().strftime("%I:%M %p")
# Create a dynamic system prompt with section IDs for runtime updates
SYSTEM_PROMPT = system_prompt(
sections=[
system_prompt.SystemPromptSection(
content="I am a helpful assistant that adapts my communication style "
"based on context.",
is_dynamic=False # Static section
),
system_prompt.SystemPromptSection(
content="{mood}",
is_dynamic=True,
section_id="mood",
default_value="neutral and ready to help"
),
system_prompt.SystemPromptSection(
content="{context}",
is_dynamic=True,
section_id="context",
default_value="general assistance"
),
system_prompt.SystemPromptSection(
content="{time}",
is_dynamic=True,
section_id="time",
default_value=get_current_time # Callable default
)
]
)
@agent(
model="openai:gpt-4o-mini",
temperature=0.7,
system_prompt=SYSTEM_PROMPT,
tools=[get_weather]
)
class DynamicAssistant:
# Note: Since we are using a dynamic system prompt, we don't need to define
# the system prompt using a docstring
pass
Using Dynamic Prompts
When processing messages, you can provide dynamic values:
# Create an instance
assistant = DynamicAssistant()
# Using defaults (no dynamic values provided)
response = await assistant.aprocess(
"How are you feeling right now?",
dynamic_values=None # Uses default values
)
# Provide dynamic values during process
dynamic_values = {
"mood": "energetic and enthusiastic",
"context": "casual conversation",
"time": "9:00 AM"
}
response = await assistant.aprocess(
"How are you feeling right now?",
dynamic_values=dynamic_values
)
Best Practices
- Use descriptive section IDs for dynamic fields
- Provide meaningful default values
- Use callable defaults for dynamic values that should be computed at runtime
- When using a dynamic system prompt, don't include a docstring on the agent class
- Keep dynamic sections focused on specific aspects of behavior
Use Cases
- Adapting agent personality based on user preferences
- Providing context-specific information
- Updating time-sensitive information
- Changing behavior based on conversation history
- Implementing role-playing scenarios