Skip to content

Commit 8696616

Browse files
committed
Add an example of integrating smolagents
1 parent f04e72e commit 8696616

File tree

6 files changed

+141
-16
lines changed

6 files changed

+141
-16
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -446,14 +446,14 @@ triage = AgentSpec(
446446
- [mcp](examples/mcp)
447447
- [structured-outputs](examples/structured-outputs)
448448
- [deepseek-r1](examples/deepseek-r1)
449-
- [ping-pong](examples/ping-pong)
450-
- [stream-ping-pong](examples/stream-ping-pong)
451-
- [discovery](examples/discovery)
452449
- [translator](examples/translator)
450+
- [discovery](examples/discovery)
453451
- [notification](examples/notification)
454-
- [opencsg](examples/opencsg)
455452
- [app-builder](examples/app-builder)
456-
- [autogen](examples/autogen)
453+
- [opencsg](examples/opencsg)
454+
- [framework-integration](examples/framework-integration)
455+
- [ping-pong](examples/ping-pong)
456+
- [stream-ping-pong](examples/stream-ping-pong)
457457
- [cos](examples/cos)
458458

459459

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Integration with other Agent Frameworks
2+
3+
4+
- [Integration with smolagents](smolagents/README.md)
5+
- [Integration with AutoGen](autogen/README.md)

examples/autogen/README.md renamed to examples/framework-integration/autogen/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ export MODEL_BASE_URL="your-base-url"
3030
export MODEL_API_VERSION="your-api-version"
3131
export MODEL_API_KEY="your-api-key"
3232
33-
python examples/using-autogen/autogen.py
33+
python examples/framework-integration/autogen/agent.py
3434
```
3535

36-
Then start a client in another terminal.
36+
Then communicate with the agent using the `coagent` CLI:
3737

3838
```bash
39-
python examples/rich_client.py autogen
39+
coagent agent -H type:ChatMessage --chat -d '{"role":"user","content":"What is the weather like in Beijing?"}'
4040
```
4141

4242

examples/autogen/autogen.py renamed to examples/framework-integration/autogen/agent.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import asyncio
22
import os
33

4-
from coagent.agents.chat_agent import ChatHistory, ChatMessage
4+
from coagent.agents import ChatMessage
55
from coagent.core import (
66
AgentSpec,
77
BaseAgent,
@@ -12,6 +12,7 @@
1212
set_stderr_logger,
1313
)
1414
from coagent.runtimes import NATSRuntime
15+
1516
from autogen_agentchat.agents import AssistantAgent
1617
from autogen_agentchat.task import TextMentionTermination
1718
from autogen_agentchat.teams import RoundRobinGroupChat
@@ -53,24 +54,22 @@ class AutoGenWeatherAgent(BaseAgent):
5354
agent_team = RoundRobinGroupChat([weather_agent], termination_condition=termination)
5455

5556
@handler
56-
async def handle(self, msg: ChatHistory, ctx: Context) -> ChatHistory:
57+
async def handle(self, msg: ChatMessage, ctx: Context) -> ChatMessage:
5758
# Run the team and return the result.
58-
result = await self.agent_team.run(task=msg.messages[-1].content)
59+
result = await self.agent_team.run(task=msg.content)
5960
content = result.messages[-2].content
60-
msg.messages.append(ChatMessage(role="assistant", content=content))
61-
return msg
61+
return ChatMessage(role="assistant", content=content)
6262

6363

64-
autogen = AgentSpec("autogen", new(AutoGenWeatherAgent))
64+
agent = AgentSpec("agent", new(AutoGenWeatherAgent))
6565

6666

6767
async def main():
6868
async with NATSRuntime.from_servers() as runtime:
69-
await runtime.register(autogen)
69+
await runtime.register(agent)
7070
await idle_loop()
7171

7272

7373
if __name__ == "__main__":
7474
set_stderr_logger("TRACE")
75-
7675
asyncio.run(main())
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Using AutoGen
2+
3+
This example demonstrates how to use [smolagents' ToolCallingAgent][1] in Coagent.
4+
5+
References:
6+
7+
- [Agent from any LLM][2]
8+
9+
10+
## Prerequisites
11+
12+
- Install `coagent` (see [Installation](../../README.md#installation)).
13+
- Start a NATS server (see [Distributed](../../README.md#distributed)).
14+
- Install `smolagents`:
15+
16+
```bash
17+
pip install smolagents
18+
```
19+
20+
21+
## Quick Start
22+
23+
First, start a server in one terminal:
24+
25+
```bash
26+
export MODEL_ID="your-model-id"
27+
export MODEL_BASE_URL="your-base-url"
28+
export MODEL_API_KEY="your-api-key"
29+
30+
python examples/framework-integration/smolagents/agent.py
31+
```
32+
33+
Then communicate with the agent using the `coagent` CLI:
34+
35+
```bash
36+
coagent agent -H type:ChatMessage --chat -d '{"role":"user","content":"What is the weather like in Beijing?"}'
37+
```
38+
39+
40+
[1]: https://huggingface.co/docs/smolagents/reference/agents#smolagents.ToolCallingAgent
41+
[2]: https://github.com/huggingface/smolagents/blob/main/examples/agent_from_any_llm.py
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import asyncio
2+
import os
3+
from typing import Callable
4+
5+
from coagent.agents import ChatMessage, ModelClient
6+
from coagent.agents.util import run_in_thread
7+
from coagent.core import (
8+
AgentSpec,
9+
BaseAgent,
10+
Context,
11+
handler,
12+
idle_loop,
13+
new,
14+
set_stderr_logger,
15+
)
16+
from coagent.runtimes import NATSRuntime
17+
18+
from smolagents import LiteLLMModel, tool as smolagents_tool
19+
from smolagents.agents import ToolCallingAgent
20+
21+
22+
class ReActAgent(BaseAgent):
23+
def __init__(self, tools: list[Callable], client: ModelClient):
24+
super().__init__()
25+
26+
model = LiteLLMModel(
27+
model_id=client.model,
28+
api_base=client.base_url,
29+
api_key=client.api_key,
30+
)
31+
# Convert tools to smolagents tools.
32+
tools = [smolagents_tool(t) for t in tools]
33+
34+
self.smol_agent = ToolCallingAgent(tools=tools, model=model)
35+
36+
@handler
37+
async def handle(self, msg: ChatMessage, ctx: Context) -> ChatMessage:
38+
response = await self.run_agent(msg.content)
39+
return ChatMessage(role="assistant", content=response)
40+
41+
@run_in_thread
42+
def run_agent(self, task: str):
43+
return self.smol_agent.run(task)
44+
45+
46+
def get_weather(location: str, celsius: bool = False) -> str:
47+
"""
48+
Get weather in the next days at given location.
49+
Secretly this tool does not care about the location, it hates the weather everywhere.
50+
51+
Args:
52+
location: the location
53+
celsius: the temperature
54+
"""
55+
return "The weather is UNGODLY with torrential rains and temperatures below -10°C"
56+
57+
58+
agent = AgentSpec(
59+
"agent",
60+
new(
61+
ReActAgent,
62+
tools=[get_weather],
63+
client=ModelClient(
64+
model=os.getenv("MODEL_ID"),
65+
base_url=os.getenv("MODEL_BASE_URL"),
66+
api_key=os.getenv("MODEL_API_KEY"),
67+
),
68+
),
69+
)
70+
71+
72+
async def main():
73+
async with NATSRuntime.from_servers() as runtime:
74+
await runtime.register(agent)
75+
await idle_loop()
76+
77+
78+
if __name__ == "__main__":
79+
set_stderr_logger()
80+
asyncio.run(main())

0 commit comments

Comments
 (0)