Skip to content
Notifications
Clear all

How do I integrate a simple REST API as a tool for an agent? The docs are confusing.

4 Posts
3 Users
0 Reactions
16 Views
(@devops_grunt_2024)
Honorable Member
Joined: 7 months ago
Posts: 535
Topic starter   [#27868]

Trying to get an AutoGen agent to call a simple internal API. The official example is a bloated mess of decorators and nested classes for what should be a `curl` in a shell script.

Here's the "simple" way I got it working after wasting an hour. Define your function, slap the `@tool` decorator on it. The agent's `llm_config` needs the tool spec registered.

```python
from autogen import AssistantAgent, UserProxyAgent, register_function
import requests

def get_weather(location: str) -> str:
"""Call the weather API for a given location."""
# Your actual API call here
response = requests.get(f"http://internal-api/weather?city={location}")
return response.text

# Create the agent that can use tools
assistant = AssistantAgent(
name="assistant",
llm_config={
"tools": [{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get current weather for a location",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string"}
},
"required": ["location"]
}
}
}]
}
)

user_proxy = UserProxyAgent(name="user_proxy", code_execution_config=False)

# This links the Python function to the tool spec
register_function(get_weather, caller=assistant, executor=user_proxy, name="get_weather")
```

Now the agent can call `get_weather`. It's just a wrapper. The docs make it seem like you need a PhD in their framework. You don't. It's a JSON schema and a function. The rest is theater.


If it ain't broke, don't 'upgrade' it.


   
Quote
(@brian7)
Reputable Member
Joined: 3 months ago
Posts: 254
 

That example is exactly what I was missing. I kept trying to make a whole class for it. But you still need to call register_function(get_weather), right? And then add it to the agent config? Can the assistant just see it automatically after registration, or do you have to list it in the "tools" array every time?



   
ReplyQuote
(@crm_hopper)
Honorable Member
Joined: 7 months ago
Posts: 472
 

Yeah, the decorator handles the spec. But you still need to register it with `register_function` and then explicitly list it in the agent's `tools` config. It's not automatic, which is the whole annoying extra step everyone misses.

Honestly, for a one-off tool, sometimes it's less code to just skip the decorator and build the spec dict yourself. Less magic to debug.


CRM is a necessary evil


   
ReplyQuote
(@brian7)
Reputable Member
Joined: 3 months ago
Posts: 254
 

That's a good point about the manual spec being clearer for debugging. But if you're registering multiple tools, doesn't skipping the decorator mean you have to duplicate the function signature in the spec dict? Seems like that could get messy.

When you say "list it in the agent's tools config", do you mean the `llm_config` dictionary or a separate argument? I think I got tripped up on that too.



   
ReplyQuote