Skip to content

Commit aa7bc2c

Browse files
committed
updating function calling
1 parent 8d5e154 commit aa7bc2c

File tree

1 file changed

+100
-64
lines changed

1 file changed

+100
-64
lines changed

articles/ai-services/agents/how-to/tools/function-calling.md

Lines changed: 100 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ To use all features of function calling including parallel functions, you need t
3232

3333
::: zone pivot="code-example"
3434

35-
# [Python](#tab/python)
35+
## Define a function for your agent to call
36+
37+
Start by defining a function for your agent to call. When you create a function for an agent to call, you describe its structure of it with any required parameters.
3638

37-
When you create a function for an agent to call, you describe its structure of it with any required parameters. For example, `fetch_weather` simulates the response of a possible weather function.
39+
# [Python](#tab/python)
3840

3941
```python
4042
def fetch_weather(location: str) -> str:
@@ -57,34 +59,9 @@ def fetch_weather(location: str) -> str:
5759
See the [python file on GitHub](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-projects/samples/agents/user_functions.py) for an example of a full series of function definitions. This file is referred to as `user_functions.py` in the following example below.
5860

5961

60-
In the sample below we create a client and define a `toolset` which will be used to process the functions defined in `user_functions`.
61-
62-
```python
63-
import os
64-
from azure.ai.projects import AIProjectClient
65-
from azure.identity import DefaultAzureCredential
66-
from azure.ai.projects.models import FunctionTool, ToolSet
67-
from user_functions import user_functions # found in the user_functions.py file.
68-
69-
# Create an Azure AI Client from a connection string, copied from your AI Studio project.
70-
# It should be in the format "<HostName>;<AzureSubscriptionId>;<ResourceGroup>;<HubName>"
71-
# Customers need to login to Azure subscription via Azure CLI and set the environment variables
72-
73-
project_client = AIProjectClient.from_connection_string(
74-
credential=DefaultAzureCredential(),
75-
conn_str=os.environ["PROJECT_CONNECTION_STRING"],
76-
)
77-
78-
# Initialize agent toolset with user functions
79-
functions = FunctionTool(user_functions)
80-
toolset = ToolSet()
81-
toolset.add(functions)
82-
```
8362

8463
# [C#](#tab/csharp)
8564

86-
When you create a function for an agent to call, you describe its structure of it with any required parameters. For example, the following functions are two examples - one that requires no parameters, and one that requires one parameter.
87-
8865
```csharp
8966
// Example of a function that defines no parameters
9067
string GetUserFavoriteCity() => "Seattle, WA";
@@ -141,54 +118,68 @@ ToolOutput GetResolvedToolOutput(RequiredToolCall toolCall)
141118

142119
---
143120

121+
## Create a client
144122

145-
## Submitting function outputs
123+
# [Python](#tab/python)
124+
125+
In the sample below we create a client and define a `toolset` which will be used to process the functions defined in `user_functions`.
126+
127+
```python
128+
import os
129+
from azure.ai.projects import AIProjectClient
130+
from azure.identity import DefaultAzureCredential
131+
from azure.ai.projects.models import FunctionTool, ToolSet
132+
from user_functions import user_functions # user functions which can be found in a user_functions.py file.
146133

147-
You can then create an agent, then create a thread and message object that will trigger a call to the function. The helper function defined earlier will help. Complete the **Run** by submitting the tool output from the functions you call.
134+
# Create an Azure AI Client from a connection string, copied from your AI Studio project.
135+
# It should be in the format "<HostName>;<AzureSubscriptionId>;<ResourceGroup>;<HubName>"
136+
# Customers need to login to Azure subscription via Azure CLI and set the environment variables
137+
138+
project_client = AIProjectClient.from_connection_string(
139+
credential=DefaultAzureCredential(),
140+
conn_str=os.environ["PROJECT_CONNECTION_STRING"],
141+
)
142+
143+
# Initialize agent toolset with user functions
144+
functions = FunctionTool(user_functions)
145+
toolset = ToolSet()
146+
toolset.add(functions)
147+
```
148+
149+
# [C#](#tab/csharp)
150+
151+
```csharp
152+
// note: parallel function calling is only supported with newer models like gpt-4-1106-preview
153+
Response<Agent> agentResponse = await client.CreateAgentAsync(
154+
model: "gpt-4-1106-preview",
155+
name: "SDK Test Agent - Functions",
156+
instructions: "You are a weather bot. Use the provided functions to help answer questions. "
157+
+ "Customize your responses to the user's preferences as much as possible and use friendly "
158+
+ "nicknames for cities whenever possible.",
159+
tools: new List<ToolDefinition> { getUserFavoriteCityTool, getCityNicknameTool, getCurrentWeatherAtLocationTool }
160+
);
161+
Agent agent = agentResponse.Value;
162+
```
163+
164+
---
165+
166+
167+
## Submitting function outputs
148168

149169
# [Python](#tab/python)
150170

151171
```python
152172

153173
# Create agent with toolset and process a run
154-
with project_client:
155-
agent = project_client.agents.create_agent(
156-
model="gpt-4o-mini", name="my-agent", instructions="You are a helpful agent", toolset=toolset
157-
)
158-
print(f"Created agent, ID: {agent.id}")
159-
160-
# Create thread for communication
161-
thread = project_client.agents.create_thread()
162-
print(f"Created thread, ID: {thread.id}")
163-
164-
# Create message to thread
165-
message = project_client.agents.create_message(
166-
thread_id=thread.id,
167-
role="user",
168-
content="Hello, send an email with the datetime and weather information in New York?",
169-
)
170-
print(f"Created message, ID: {message.id}")
171-
172-
# Create and process agent run in thread with tools
173-
run = project_client.agents.create_and_process_run(thread_id=thread.id, assistant_id=agent.id)
174-
print(f"Run finished with status: {run.status}")
175-
176-
if run.status == "failed":
177-
print(f"Run failed: {run.last_error}")
178-
179-
# Delete the agent when done
180-
project_client.agents.delete_agent(agent.id)
181-
print("Deleted agent")
182-
183-
# Fetch and log all messages
184-
messages = project_client.agents.list_messages(thread_id=thread.id)
185-
print(f"Messages: {messages}")
174+
175+
agent = project_client.agents.create_agent(
176+
model="gpt-4o-mini", name="my-agent", instructions="You are a helpful agent", toolset=toolset
177+
)
178+
print(f"Created agent, ID: {agent.id}")
186179
```
187180

188181
# [C#](#tab/csharp)
189182

190-
You can then create an agent with the `toolbox` object defined earlier, then create a thread and message object that will trigger a call to the function. Complete the **Run** by submitting the tool output from the functions you call.
191-
192183
```csharp
193184
// note: parallel function calling is only supported with newer models like gpt-4-1106-preview
194185
Response<Agent> agentResponse = await client.CreateAgentAsync(
@@ -200,8 +191,46 @@ Response<Agent> agentResponse = await client.CreateAgentAsync(
200191
tools: new List<ToolDefinition> { getUserFavoriteCityTool, getCityNicknameTool, getCurrentWeatherAtLocationTool }
201192
);
202193
Agent agent = agentResponse.Value;
203-
#endregion
194+
```
195+
196+
---
197+
198+
## Create a thread
199+
200+
# [python](#tab/python)
201+
202+
```python
203+
# Create thread for communication
204+
thread = project_client.agents.create_thread()
205+
print(f"Created thread, ID: {thread.id}")
206+
207+
# Create message to thread
208+
message = project_client.agents.create_message(
209+
thread_id=thread.id,
210+
role="user",
211+
content="Hello, send an email with the datetime and weather information in New York?",
212+
)
213+
print(f"Created message, ID: {message.id}")
214+
215+
# Create and process agent run in thread with tools
216+
run = project_client.agents.create_and_process_run(thread_id=thread.id, assistant_id=agent.id)
217+
print(f"Run finished with status: {run.status}")
218+
219+
if run.status == "failed":
220+
print(f"Run failed: {run.last_error}")
204221

222+
# Delete the agent when done
223+
project_client.agents.delete_agent(agent.id)
224+
print("Deleted agent")
225+
226+
# Fetch and log all messages
227+
messages = project_client.agents.list_messages(thread_id=thread.id)
228+
print(f"Messages: {messages}")
229+
```
230+
231+
# [C#](#tab/csharp)
232+
233+
```csharp
205234
Response<AgentThread> threadResponse = await client.CreateThreadAsync();
206235
AgentThread thread = threadResponse.Value;
207236

@@ -259,6 +288,13 @@ foreach (ThreadMessage threadMessage in messages)
259288

260289
---
261290

291+
## Create a run and check the output
292+
293+
# [python](#tab/python)
294+
295+
# [C#](#tab/csharp)
296+
297+
262298
::: zone-end
263299

264300
## See also

0 commit comments

Comments
 (0)