|
| 1 | +import asyncio |
| 2 | + |
| 3 | +from coagent.agents.react_agent import ( |
| 4 | + ReActAgent, |
| 5 | + InputMessage, |
| 6 | + InputHistory, |
| 7 | + OutputMessage, |
| 8 | + MessageOutputItem, |
| 9 | + Subagent, |
| 10 | + ToolCallItem, |
| 11 | + ToolCallOutputItem, |
| 12 | + ToolCallProgressItem, |
| 13 | +) |
| 14 | +from coagent.core import AgentSpec, new, init_logger |
| 15 | +from coagent.runtimes import LocalRuntime |
| 16 | + |
| 17 | +""" |
| 18 | +This example shows the agents-as-tools pattern. The frontline agent receives a user message and |
| 19 | +then picks which agents to call, as tools. In this case, it picks from a set of translation |
| 20 | +agents. |
| 21 | +""" |
| 22 | + |
| 23 | + |
| 24 | +spanish_agent = AgentSpec( |
| 25 | + "spanish_agent", |
| 26 | + new( |
| 27 | + ReActAgent, |
| 28 | + name="spanish_agent", |
| 29 | + system="You translate the user's message to Spanish", |
| 30 | + ), |
| 31 | +) |
| 32 | + |
| 33 | +french_agent = AgentSpec( |
| 34 | + "french_agent", |
| 35 | + new( |
| 36 | + ReActAgent, |
| 37 | + name="french_agent", |
| 38 | + system="You translate the user's message to French", |
| 39 | + ), |
| 40 | +) |
| 41 | + |
| 42 | +orchestrator_agent = AgentSpec( |
| 43 | + "orchestrator_agent", |
| 44 | + new( |
| 45 | + ReActAgent, |
| 46 | + name="orchestrator_agent", |
| 47 | + system=( |
| 48 | + "You are a translation agent. You use the tools given to you to translate." |
| 49 | + "If asked for multiple translations, you call the relevant tools in order." |
| 50 | + "You never translate on your own, you always use the provided tools." |
| 51 | + ), |
| 52 | + tools=[ |
| 53 | + Subagent("spanish_agent").as_tool( |
| 54 | + name="translate_to_spanish", |
| 55 | + description="Translate the user's message to Spanish", |
| 56 | + ), |
| 57 | + Subagent("french_agent").as_tool( |
| 58 | + name="translate_to_french", |
| 59 | + description="Translate the user's message to French", |
| 60 | + ), |
| 61 | + ], |
| 62 | + ), |
| 63 | +) |
| 64 | + |
| 65 | + |
| 66 | +async def main(): |
| 67 | + async with LocalRuntime() as runtime: |
| 68 | + await runtime.register(spanish_agent) |
| 69 | + await runtime.register(french_agent) |
| 70 | + await runtime.register(orchestrator_agent) |
| 71 | + |
| 72 | + result = await orchestrator_agent.run( |
| 73 | + InputHistory( |
| 74 | + messages=[ |
| 75 | + InputMessage( |
| 76 | + role="user", |
| 77 | + content="Translate this to Spanish: Hello, how are you doing today?", |
| 78 | + ) |
| 79 | + ] |
| 80 | + ).encode(), |
| 81 | + stream=True, |
| 82 | + ) |
| 83 | + async for chunk in result: |
| 84 | + msg = OutputMessage.decode(chunk) |
| 85 | + i = msg.item |
| 86 | + match i: |
| 87 | + case MessageOutputItem(): |
| 88 | + print(i.raw_item.content[0].text, end="", flush=True) |
| 89 | + case ToolCallItem(): |
| 90 | + print( |
| 91 | + f"\n[tool#{i.raw_item.call_id} call: {i.raw_item.name}]", |
| 92 | + flush=True, |
| 93 | + ) |
| 94 | + case ToolCallProgressItem(): |
| 95 | + print( |
| 96 | + f"\n[tool#{i.raw_item.call_id} progress: {i.raw_item.message}]", |
| 97 | + flush=True, |
| 98 | + ) |
| 99 | + case ToolCallOutputItem(): |
| 100 | + print( |
| 101 | + f"\n[tool#{i.raw_item.call_id} output: {i.raw_item.output}]", |
| 102 | + flush=True, |
| 103 | + ) |
| 104 | + |
| 105 | + |
| 106 | +if __name__ == "__main__": |
| 107 | + init_logger() |
| 108 | + asyncio.run(main()) |
0 commit comments