Files
cleveragents-core/examples/message_router_example.yaml
T

217 lines
5.2 KiB
YAML

# Example configuration demonstrating user-defined message routing
# This shows how to use the generic message_router node type
# where users define their own routing patterns and targets
name: message_router_example
agents:
# Define your agents here
task_handler:
type: llm
config:
provider: openai
model: gpt-4
system_prompt: "You handle various tasks based on user requests"
code_assistant:
type: llm
config:
provider: openai
model: gpt-4
system_prompt: "You help with coding tasks"
research_assistant:
type: llm
config:
provider: openai
model: gpt-4
system_prompt: "You help with research and information gathering"
creative_writer:
type: llm
config:
provider: openai
model: gpt-4
system_prompt: "You help with creative writing tasks"
routes:
main:
type: graph
nodes:
# Entry point
start:
type: START
# Message router node with user-defined routing patterns
message_router:
type: message_router
rules:
# Route based on prefixes
- match_type: prefix
pattern: "CODE:"
target: code_assistant
extract_message: true
separator: ":"
- match_type: prefix
pattern: "RESEARCH:"
target: research_assistant
extract_message: true
separator: ":"
- match_type: prefix
pattern: "WRITE:"
target: creative_writer
extract_message: true
separator: ":"
# Route based on content keywords
- match_type: contains
pattern: "debug"
target: code_assistant
extract_message: false
- match_type: contains
pattern: "investigate"
target: research_assistant
extract_message: false
# Route based on regex patterns
- match_type: regex
pattern: "^\\[URGENT\\](.*)$"
target: priority_handler
extract_message: true
# Route based on exact match
- match_type: exact
pattern: "!help"
target: help_handler
extract_message: false
# Default fallback (using suffix match as a trick)
- match_type: suffix
pattern: "" # Matches everything
target: task_handler
extract_message: false
# Agent nodes
code_assistant:
type: AGENT
agent: code_assistant
research_assistant:
type: AGENT
agent: research_assistant
creative_writer:
type: AGENT
agent: creative_writer
task_handler:
type: AGENT
agent: task_handler
priority_handler:
type: AGENT
agent: task_handler # Using same agent but could be different
help_handler:
type: FUNCTION
function: |
async def help_handler(state):
state["messages"].append({
"role": "assistant",
"content": """Available commands:
CODE: <request> - Route to code assistant
RESEARCH: <request> - Route to research assistant
WRITE: <request> - Route to creative writer
[URGENT] <request> - Mark as priority
!help - Show this help message
Keywords 'debug' or 'investigate' will also trigger routing."""
})
return state
# End point
end:
type: END
edges:
# Initial routing
- source: start
target: message_router
# From router to each possible target
# These edges use the next_node state value set by the router
- source: message_router
target: code_assistant
condition:
type: context_value
key: next_node
value: code_assistant
- source: message_router
target: research_assistant
condition:
type: context_value
key: next_node
value: research_assistant
- source: message_router
target: creative_writer
condition:
type: context_value
key: next_node
value: creative_writer
- source: message_router
target: task_handler
condition:
type: context_value
key: next_node
value: task_handler
- source: message_router
target: priority_handler
condition:
type: context_value
key: next_node
value: priority_handler
- source: message_router
target: help_handler
condition:
type: context_value
key: next_node
value: help_handler
# All agents route back to end
- source: code_assistant
target: end
- source: research_assistant
target: end
- source: creative_writer
target: end
- source: task_handler
target: end
- source: priority_handler
target: end
- source: help_handler
target: end
# Entry point for the graph
entry_point: start
# Output configuration
publications:
- __output__
merges:
- sources: [__input__]
target: main