from typing import Union,Literal,Optional,Dict,Any
from pydantic import BaseModel,Field
from stringdale import Define,Scope,V,E,Condition
from stringdale.chat import Chat
Router Pattern
Sub Agents
= Chat(model='gpt-4o-mini',
rhyming_agent =[{'role':'system','content':"""
messages Answer the following questions using rhyming words.
"""},
'role':'user','content':'{{question}}'},
{
],
)
await rhyming_agent(question='what is the capital of france?')
{'role': 'assistant',
'content': 'In Paris, the heart beats bright, a city of charm, a glorious sight.',
'meta': {'input_tokens': 129, 'output_tokens': 24}}
= Chat(model='gpt-4o-mini',
joke_agent =[{'role':'system','content':"""
messages Answer the following question with a joke.
"""},
'role':'user','content':'{{question}}'},
{
])
await joke_agent(question='what is the capital of france?')
{'role': 'assistant',
'content': 'Why did the French chef get arrested? Because he was caught beating the eggs in Paris!',
'meta': {'input_tokens': 127, 'output_tokens': 26}}
= Chat(model='gpt-4o-mini',
yo_mama_chat =[{'role':'system','content':"""
messages Answer the following question with a joke about the person's mother.
"""},
'role':'user','content':'{{question}}'},
{
])
await yo_mama_chat(question='what is the capital of france?')
{'role': 'assistant',
'content': 'Why did your mother go to Paris? Because she heard the Eiffel Tower was leaning towards her after all the times you forgot your mother’s birthday!',
'meta': {'input_tokens': 131, 'output_tokens': 36}}
with Define('yo mama agent') as YoMamaAgent:
'yo mama',yo_mama_chat,inputs=['Start(**)'],outputs=['End'])
V( YoMamaAgent.draw()
= YoMamaAgent()
yo_mama_agent
yo_mama_agent.run_all({'question':'what is the capital of france?'
})
{'role': 'assistant',
'content': 'Why did your mother go to Paris? Because she heard the Eiffel Tower was leaning towards her after all the times you forgot your mother’s birthday!',
'meta': {'input_tokens': 131, 'output_tokens': 36}}
Router Factory
def RouterFactory(sub_agents:Dict[str,Any],default_choice:str):
= Chat(model='gpt-4o-mini',
router =[{'role':'system','content':"""
messages Choose the best sub-agent to answer the following question from among the following options:
{% for name,description in choice_descriptions.items() %}
- {{name}}: {{description}}
{% endfor %}
"""},
'role':'user','content':'{{question}}'},
{
],= {name:agent['description'] for name,agent in sub_agents.items()},
choice_descriptions =list(sub_agents.keys())
choices
)
with Define('Router',type='decision') as CompoundRouter:
with Scope('flow'):
'choose_route',router,inputs=['Start(question=.)'])
V('router',
V(=[
inputs'choose_route(choice=content)',
'Start(question=.)'
],)
= 'rhyme'
default for choice,agent in sub_agents.items():
= agent['func']
agent_func =['End'])
V(choice,agent_func,outputsif choice == default_choice:
f'router->{choice}(question=question)')
E(else:
f'router->{choice}(question=question)',cond=Condition(choice,'(0=choice)',name=f'choice=={choice}'))
E(
return CompoundRouter
= {
sub_agents 'rhyme':{
'description':'this agent is good at rhyming',
'func':rhyming_agent
},'joke':{
'description':'this agent is good at telling jokes',
'func':joke_agent
},'yo_mama':{
'description':'this agent is specifically good at telling jokes about mothers',
'func':yo_mama_chat
}
}
= RouterFactory(sub_agents,'rhyme')
Router Router.draw()
= Router()
d for trace in d.run({'question':"what is the capital of france?, I like yo mama jokes"}):
=False,skip_passthrough=True) trace.pprint(show_input
Node choose_route:
{ 'output': { 'content': 'yo_mama',
'meta': {'input_tokens': 208, 'output_tokens': 11},
'role': 'assistant'}}
================================================================================
Node yo_mama:
{ 'output': { 'content': 'Why did your mother go to Paris? Because she heard '
'the Eiffel Tower was leaning towards her!',
'meta': {'input_tokens': 141, 'output_tokens': 26},
'role': 'assistant'}}
================================================================================