Skip to content

Commit ceb7f16

Browse files
authored
add auto function call sample (#41885)
* update * Update sample_agents_auto_function_call.py * added async sample and use project client * update * fix spelling * update change log * Update CHANGELOG.md * update version
1 parent 6baa44f commit ceb7f16

File tree

4 files changed

+217
-2
lines changed

4 files changed

+217
-2
lines changed

sdk/ai/azure-ai-agents/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Release History
22

3+
## 1.0.3 (Unreleased)
4+
5+
### Features Added
6+
7+
- Added samples, [`sample_agents_auto_function_call.py`](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-agents/samples/agents_tools/sample_agents_auto_function_call.py) and [`sample_agents_auto_function_call_async.py`](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-agents/samples/agents_async/sample_agents_auto_function_call_async.py).
8+
39
## 1.0.2 (2025-07-01)
410

511
### Bugs Fixed

sdk/ai/azure-ai-agents/README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,8 +419,12 @@ agent = await agents_client.create_agent(
419419

420420
<!-- END SNIPPET -->
421421

422-
Notice that if `enable_auto_function_calls` is called, the SDK will invoke the functions automatically during `create_and_process` or streaming. If you prefer to execute them manually, refer to [`sample_agents_stream_eventhandler_with_functions.py`](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-agents/samples/agents_streaming/sample_agents_stream_eventhandler_with_functions.py) or
423-
[`sample_agents_functions.py`](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-agents/samples/agents_tools/sample_agents_functions.py)
422+
When `enable_auto_function_calls` is called, the SDK will automatically invoke functions during both `create_and_process` and streaming workflows. This simplifies agent logic by handling function execution internally.
423+
424+
- For examples of automatic function calls in action, refer to [`sample_agents_auto_function_call.py`](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-agents/samples/agents_tools/sample_agents_auto_function_call.py) or [`sample_agents_auto_function_call_async.py`](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-agents/samples/agents_async/sample_agents_auto_function_call_async.py).
425+
- If you prefer to manage function execution manually, refer to [`sample_agents_stream_eventhandler_with_functions.py`](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-agents/samples/agents_streaming/sample_agents_stream_eventhandler_with_functions.py) or
426+
[`sample_agents_functions.py`](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-agents/samples/agents_tools/sample_agents_functions.py).
427+
424428

425429
### Create Agent With Azure Function Call
426430

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# ------------------------------------
2+
# Copyright (c) Microsoft Corporation.
3+
# Licensed under the MIT License.
4+
# ------------------------------------
5+
import asyncio
6+
7+
"""
8+
DESCRIPTION:
9+
This sample demonstrates how to enable automatic function calls by calling `enable_auto_function_calls`
10+
using an asynchronous client.
11+
12+
USAGE:
13+
python sample_agents_auto_function_call_async.py
14+
15+
Before running the sample:
16+
17+
pip install azure-ai-agents azure-identity
18+
19+
Set these environment variables with your own values:
20+
1) PROJECT_ENDPOINT - The Azure AI Project endpoint, as found in the Overview
21+
page of your Azure AI Foundry portal.
22+
2) MODEL_DEPLOYMENT_NAME - The deployment name of the AI model, as found under the "Name" column in
23+
the "Models + endpoints" tab in your Azure AI Foundry project.
24+
"""
25+
26+
import os, sys
27+
from azure.ai.projects.aio import AIProjectClient
28+
from azure.identity import DefaultAzureCredential
29+
from azure.ai.agents.models import AsyncFunctionTool, AsyncToolSet
30+
31+
from utils.user_async_functions import user_async_functions
32+
33+
34+
async def main() -> None:
35+
36+
project_client = AIProjectClient(
37+
endpoint=os.environ["PROJECT_ENDPOINT"],
38+
credential=DefaultAzureCredential(),
39+
)
40+
41+
async with project_client:
42+
agents_client = project_client.agents
43+
44+
# To enable tool calls executed automatically you can pass a functions in one of these types:
45+
# Set[Callable[..., Any]], _models.AsyncFunctionTool, or _models.AsyncToolSet
46+
# Example 1:
47+
# agents_client.enable_auto_function_calls(user_async_functions)
48+
# Example 2:
49+
# functions = AsyncFunctionTool(user_async_functions)
50+
# agents_client.enable_auto_function_calls(functions)
51+
# Example 3:
52+
# functions = AsyncFunctionTool(user_async_functions)
53+
# toolset = AsyncToolSet()
54+
# toolset.add(functions)
55+
# agents_client.enable_auto_function_calls(toolset)
56+
agents_client.enable_auto_function_calls(user_async_functions)
57+
# Notices that `enable_auto_function_calls` can be made at any time.
58+
59+
functions = AsyncFunctionTool(user_async_functions)
60+
61+
# Initialize agent.
62+
# Whether you would like the functions to be called automatically or not, it is required to pass functions as tools or toolset.
63+
agent = await agents_client.create_agent(
64+
model=os.environ["MODEL_DEPLOYMENT_NAME"],
65+
name="my-agent",
66+
instructions="You are a helpful agent",
67+
tools=functions.definitions,
68+
)
69+
70+
print(f"Created agent, ID: {agent.id}")
71+
72+
# Create thread for communication
73+
thread = await agents_client.threads.create()
74+
print(f"Created thread, ID: {thread.id}")
75+
76+
# Create message to thread
77+
message = await agents_client.messages.create(
78+
thread_id=thread.id,
79+
role="user",
80+
content="Hello, send an email with the datetime and weather information in New York?",
81+
)
82+
print(f"Created message, ID: {message.id}")
83+
84+
# Create and process agent run in thread with tools
85+
run = await agents_client.runs.create_and_process(thread_id=thread.id, agent_id=agent.id)
86+
print(f"Run finished with status: {run.status}")
87+
88+
if run.status == "failed":
89+
print(f"Run failed: {run.last_error}")
90+
91+
# Delete the agent when done
92+
await agents_client.delete_agent(agent.id)
93+
print("Deleted agent")
94+
95+
# Fetch and log all messages
96+
messages = agents_client.messages.list(thread_id=thread.id)
97+
async for msg in messages:
98+
if msg.text_messages:
99+
last_text = msg.text_messages[-1]
100+
print(f"{msg.role}: {last_text.text.value}")
101+
102+
103+
if __name__ == "__main__":
104+
asyncio.run(main())
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# ------------------------------------
2+
# Copyright (c) Microsoft Corporation.
3+
# Licensed under the MIT License.
4+
# ------------------------------------
5+
6+
"""
7+
DESCRIPTION:
8+
This sample demonstrates how to enable automatic function calls by calling `enable_auto_function_calls`
9+
using a synchronous client.
10+
11+
USAGE:
12+
python sample_agents_auto_function_call.py
13+
14+
Before running the sample:
15+
16+
pip install azure-ai-agents azure-identity
17+
18+
Set these environment variables with your own values:
19+
1) PROJECT_ENDPOINT - The Azure AI Project endpoint, as found in the Overview
20+
page of your Azure AI Foundry portal.
21+
2) MODEL_DEPLOYMENT_NAME - The deployment name of the AI model, as found under the "Name" column in
22+
the "Models + endpoints" tab in your Azure AI Foundry project.
23+
"""
24+
25+
import os, sys
26+
from azure.ai.projects import AIProjectClient
27+
from azure.identity import DefaultAzureCredential
28+
from azure.ai.agents.models import FunctionTool
29+
from samples.utils.user_functions import user_functions
30+
31+
project_client = AIProjectClient(
32+
endpoint=os.environ["PROJECT_ENDPOINT"],
33+
credential=DefaultAzureCredential(),
34+
)
35+
36+
with project_client:
37+
agents_client = project_client.agents
38+
39+
agent = agents_client.create_agent(
40+
model=os.environ["MODEL_DEPLOYMENT_NAME"],
41+
name="my-agent",
42+
instructions="You are helpful agent",
43+
)
44+
45+
# To enable tool calls executed automatically you can pass a functions in one of these types:
46+
# Set[Callable[..., Any]], _models.FunctionTool, or _models.ToolSet
47+
# Example 1:
48+
# agents_client.enable_auto_function_calls(user_functions)
49+
# Example 2:
50+
# functions = FunctionTool(user_functions)
51+
# agents_client.enable_auto_function_calls(functions)
52+
# Example 3:
53+
# functions = FunctionTool(user_functions)
54+
# toolset = ToolSet()
55+
# toolset.add(functions)
56+
# agents_client.enable_auto_function_calls(toolset)
57+
agents_client.enable_auto_function_calls(user_functions)
58+
# Notices that `enable_auto_function_calls` can be made at any time.
59+
60+
functions = FunctionTool(user_functions)
61+
62+
# Initialize agent.
63+
# Whether you would like the functions to be called automatically or not, it is required to pass functions as tools or toolset.
64+
agent = agents_client.create_agent(
65+
model=os.environ["MODEL_DEPLOYMENT_NAME"],
66+
name="my-agent",
67+
instructions="You are a helpful agent",
68+
tools=functions.definitions,
69+
)
70+
71+
print(f"Created agent, ID: {agent.id}")
72+
73+
# Create thread for communication
74+
thread = agents_client.threads.create()
75+
print(f"Created thread, ID: {thread.id}")
76+
77+
# Create message to thread
78+
message = agents_client.messages.create(
79+
thread_id=thread.id,
80+
role="user",
81+
content="Hello, send an email with the datetime and weather information in New York?",
82+
)
83+
print(f"Created message, ID: {message.id}")
84+
85+
# Create and process agent run in thread with tools
86+
run = agents_client.runs.create_and_process(thread_id=thread.id, agent_id=agent.id)
87+
print(f"Run finished with status: {run.status}")
88+
89+
if run.status == "failed":
90+
print(f"Run failed: {run.last_error}")
91+
92+
# Delete the agent when done
93+
agents_client.delete_agent(agent.id)
94+
print("Deleted agent")
95+
96+
# Fetch and log all messages
97+
messages = agents_client.messages.list(thread_id=thread.id)
98+
for msg in messages:
99+
if msg.text_messages:
100+
last_text = msg.text_messages[-1]
101+
print(f"{msg.role}: {last_text.text.value}")

0 commit comments

Comments
 (0)