To create a tool in the Azure AI Foundry using Python, you can define a function that you want the agent to call when needed. This is done by passing the function to the agent's tools parameter during the agent's creation. Here’s a simple example of how to create a function tool:
- Define Your Function: Create a function that performs the desired task. You can use type annotations to provide descriptions for the parameters.
from typing import Annotated from pydantic import Field def get_weather( location: Annotated[str, Field(description="The location to get the weather for.")) ) -> str: return f"The weather in {location} is cloudy with a high of 15°C." - Use the
ai_functionDecorator: Optionally, you can use theai_functiondecorator to specify the function's name and description explicitly.from agent_framework import ai_function @ai_function(name="weather_tool", description="Retrieves weather information for any location") def get_weather(...): ... # function implementation - Create the Agent: When creating the agent, pass the function tool to the
toolsparameter.import asyncio from agent_framework.azure import AzureOpenAIChatClient from azure.identity import AzureCliCredential agent = AzureOpenAIChatClient(credential=AzureCliCredential()).create_agent( instructions="You are a helpful assistant", tools=get_weather ) - Run the Agent: You can now run the agent, and it will be able to call the
get_weatherfunction when needed.async def main(): result = await agent.run("What is the weather like in Amsterdam?") print(result.text) asyncio.run(main())
This approach allows you to create custom function tools that your agent can utilize effectively.