How to convert Swagger JSON to FunctionTool? #5158
Answered
by
Ben-xiang
easonktsai
asked this question in
Q&A
-
How can I dynamically convert a Swagger JSON into FunctionTool? In Autogen 0.2, I convert swagger json by this snippet for function calling. def create_api_function(*args, **kwargs):
auth = kwargs.get("Authorization")
plugin = kwargs.get("plugin")
def function_template(*args, **kwargs):
# skip...
if "GET" in method.upper():
payload = {}
format_url += "?"
for attr, value in kwargs.items():
format_url += f"{attr}={value}&"
elif "POST" in method.upper():
payload = json.dumps(kwargs.get("payload") or kwargs.get("requestBody") or kwargs)
headers = {"Content-Type": "application/json"}
auth_headers = build_auth_header(plugin=plugin, Authorization=auth)
headers.update(auth_headers)
response = requests.request(method, format_url, headers=headers, data=payload)
retrun response
url = kwargs.get("url")
method = kwargs.get("method")
function_template.__name__ = kwargs.get("name")
return function_template How to migrate to v0.4 for tool using? async def get_weather(location: str) -> str:
return f"The weather in {location} is sunny."
tool_list = FunctionTool(create_api_function, name=function_name, description=desc)
assistant = AssistantAgent(
"Assistant",
model_client=model_client,
tools=[get_weather, tool_list],
)
termination = TextMentionTermination("TERMINATE")
team = RoundRobinGroupChat([assistant], termination_condition=termination)
await Console(team.run_stream(task="What's the weather in New York?")) And error message: |
Beta Was this translation helpful? Give feedback.
Answered by
Ben-xiang
Feb 3, 2025
Replies: 1 comment 6 replies
-
You need to pass an instance of the returned function from But what you did is quite interesting and potentially useful as a separate wrapper class, e.g., @EItanya, what do you think about this? |
Beta Was this translation helpful? Give feedback.
6 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@ekzhu Our goal is similar to the first point you mentioned. We want to dynamically generate functions from the Swagger JSON for the Agent to use. We successfully achieved this by referring to #5170, which helped us create multi customized httpTool base on Swagger JSON before initializing the Agent. We really appreciate your help!