Skip to content

Commit 27d8a1f

Browse files
committed
article
1 parent dafeae7 commit 27d8a1f

File tree

2 files changed

+235
-0
lines changed

2 files changed

+235
-0
lines changed
Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
---
2+
title: 'How to use Azure Functions with the Azure AI Search tool'
3+
titleSuffix: Azure OpenAI
4+
description: Learn how to use Azure functions with Azure AI Agents.
5+
services: azure-ai-agent-service
6+
manager: nitinme
7+
ms.service: azure
8+
ms.topic: how-to
9+
ms.date: 12/11/2024
10+
author: aahill
11+
ms.author: aahi
12+
ms.custom: azure-ai-agents
13+
zone_pivot_groups: selection-function-calling
14+
---
15+
::: zone pivot="overview"
16+
17+
The Azure AI Agents service integrates with Azure Functions, enabling you to create intelligent, event-driven applications with minimal overhead. This combination allows AI-driven workflows to leverage the scalability and flexibility of serverless computing, making it easier to build and deploy solutions that respond to real-time events or complex workflows.
18+
19+
Azure Functions provide support for triggers and bindings, which simplify how your AI Agents interact with external systems and services. Triggers determine when a function executes—such as an HTTP request, message from a queue, or a file upload to Azure Blob Storage and allows agents to act dynamically based on incoming events.
20+
21+
Meanwhile, bindings facilitate streamlined connections to input or output data sources, such as databases or APIs, without requiring extensive boilerplate code. For instance, you can configure a trigger to execute an Azure Function whenever a customer message is received in a chatbot and use output bindings to send a response via the Azure AI Agent.
22+
23+
# Prerequisites
24+
25+
* [Azure Functions Core Tools v4.x](https://learn.microsoft.com/azure/azure-functions/functions-run-local?tabs=v4%2Cwindows%2Cnode%2Cportal%2Cbash)
26+
* [Azure AI Agent Service](https://learn.microsoft.com/en-us/azure/ai-studio/how-to/develop/sdk-overview?tabs=sync&pivots=programming-language-python#azure-ai-agent-service)
27+
* [Azurite](https://github.com/Azure/Azurite)
28+
29+
## Prepare your local environment
30+
31+
The following examples highlight how to use the Azure AI Agent service function calling where function calls are placed on a storage queue by the Agent service to be processed by an Azure Function listening to that queue.
32+
33+
You can find the template and code used here on [GitHub](https://github.com/Azure-Samples/azure-functions-ai-services-agent-python).
34+
35+
### Create Azure resources for local and cloud dev-test
36+
37+
Once you have your Azure subscription, run the following in a new terminal window to create Azure OpenAI and other resources needed:
38+
39+
```bash
40+
azd init --template https://github.com/Azure-Samples/azure-functions-ai-services-agent-python
41+
```
42+
#### Mac/Linux:
43+
44+
```bash
45+
chmod +x ./infra/scripts/*.sh
46+
```
47+
#### Windows:
48+
49+
```Powershell
50+
set-executionpolicy remotesigned
51+
```
52+
53+
### Provision resources
54+
55+
Run the following command to create the required resources in Azure.
56+
```bash
57+
azd provision
58+
```
59+
60+
### Create local.settings.json
61+
62+
> [!NOTE]
63+
> This file should be in the same folder as `host.json`. It is automatically created if you ran `azd provision`.
64+
65+
```json
66+
{
67+
"IsEncrypted": false,
68+
"Values": {
69+
"FUNCTIONS_WORKER_RUNTIME": "python",
70+
"STORAGE_CONNECTION__queueServiceUri": "https://<storageaccount>.queue.core.windows.net",
71+
"PROJECT_CONNECTION_STRING": "<project connnection for AI Project>",
72+
"AzureWebJobsStorage": "UseDevelopmentStorage=true"
73+
}
74+
}
75+
```
76+
77+
::: zone-end
78+
79+
::: zone pivot="code-examples"
80+
81+
# Azure AI Agents function calling with Azure Functions
82+
83+
Azure AI Agents supports function calling, which allows you to describe the structure of functions to an Assistant and then return the functions that need to be called along with their arguments. This example shows how to use Azure Functions to process the function calls through queue messages in Azure Storage. You can see a complete working sample on https://github.com/Azure-Samples/azure-functions-ai-services-agent-python
84+
85+
### Supported models
86+
87+
To use all features of function calling including parallel functions, you need to use a model that was released after November 6, 2023.
88+
89+
90+
## Define a function for your agent to call
91+
92+
Start by defining an Azure Function queue trigger function that will process function calls from the queue.
93+
94+
```python
95+
# Function to get the weather from an Azure Storage queue where the AI Agent will send function call information
96+
# It returns the mock weather to an output queue with the correlation id for the AI Agent service to pick up the result of the function call
97+
@app.function_name(name="GetWeather")
98+
@app.queue_trigger(arg_name="msg", queue_name="input", connection="STORAGE_CONNECTION")
99+
def process_queue_message(msg: func.QueueMessage) -> None:
100+
logging.info('Python queue trigger function processed a queue item')
101+
102+
# Queue to send message to
103+
queue_client = QueueClient(
104+
os.environ["STORAGE_CONNECTION__queueServiceUri"],
105+
queue_name="output",
106+
credential=DefaultAzureCredential(),
107+
message_encode_policy=BinaryBase64EncodePolicy(),
108+
message_decode_policy=BinaryBase64DecodePolicy()
109+
)
110+
111+
# Get the content of the function call message
112+
messagepayload = json.loads(msg.get_body().decode('utf-8'))
113+
location = messagepayload['location']
114+
correlation_id = messagepayload['CorrelationId']
115+
116+
# Send message to queue. Sends a mock message for the weather
117+
result_message = {
118+
'Value': 'Weather is 74 degrees and sunny in ' + location,
119+
'CorrelationId': correlation_id
120+
}
121+
queue_client.send_message(json.dumps(result_message).encode('utf-8'))
122+
123+
logging.info(f"Sent message to output queue with message {result_message}")
124+
```
125+
126+
127+
## Create an AI project client and agent
128+
129+
In the sample below we create a client and an agent that has the tools definition for the Azure Function
130+
131+
```python
132+
# Initialize the client and create agent for the tools Azure Functions that the agent can use
133+
134+
# Create a project client
135+
project_client = AIProjectClient.from_connection_string(
136+
credential=DefaultAzureCredential(),
137+
conn_str=os.environ["PROJECT_CONNECTION_STRING"]
138+
)
139+
140+
# Get the connection string for the storage account to send and receive the function calls to the queues
141+
storage_connection_string = os.environ["STORAGE_CONNECTION__queueServiceUri"]
142+
143+
# Create an agent with the Azure Function tool to get the weather
144+
agent = project_client.agents.create_agent(
145+
model="gpt-4o-mini",
146+
name="azure-function-agent-get-weather",
147+
instructions="You are a helpful support agent. Answer the user's questions to the best of your ability.",
148+
headers={"x-ms-enable-preview": "true"},
149+
tools=[
150+
{
151+
"type": "azure_function",
152+
"azure_function": {
153+
"function": {
154+
"name": "GetWeather",
155+
"description": "Get the weather in a location.",
156+
"parameters": {
157+
"type": "object",
158+
"properties": {
159+
"location": {"type": "string", "description": "The location to look up."}
160+
},
161+
"required": ["location"]
162+
}
163+
},
164+
"input_binding": {
165+
"type": "storage_queue",
166+
"storage_queue": {
167+
"queue_service_uri": storage_connection_string,
168+
"queue_name": "input"
169+
}
170+
},
171+
"output_binding": {
172+
"type": "storage_queue",
173+
"storage_queue": {
174+
"queue_service_uri": storage_connection_string,
175+
"queue_name": "output"
176+
}
177+
}
178+
}
179+
}
180+
],
181+
)
182+
```
183+
184+
## Create a thread for the agent
185+
186+
```python
187+
# Create a thread
188+
thread = project_client.agents.create_thread()
189+
print(f"Created thread, thread ID: {thread.id}")
190+
```
191+
192+
## Create a run and check the output
193+
194+
```python
195+
# Send the prompt to the agent
196+
message = project_client.agents.create_message(
197+
thread_id=thread.id,
198+
role="user",
199+
content="What is the weather in Seattle, WA?",
200+
)
201+
print(f"Created message, message ID: {message.id}")
202+
203+
# Run the agent
204+
run = project_client.agents.create_run(thread_id=thread.id, assistant_id=agent.id)
205+
# Monitor and process the run status. The function call should be placed on the input queue by the Agent service for the Azure Function to pick up when requires_action is returned
206+
while run.status in ["queued", "in_progress", "requires_action"]:
207+
time.sleep(1)
208+
run = project_client.agents.get_run(thread_id=thread.id, run_id=run.id)
209+
210+
if run.status not in ["queued", "in_progress", "requires_action"]:
211+
break
212+
213+
print(f"Run finished with status: {run.status}")
214+
```
215+
216+
### Get the result of the run
217+
218+
```python
219+
# Get messages from the assistant thread
220+
messages = project_client.agents.get_messages(thread_id=thread.id)
221+
print(f"Messages: {messages}")
222+
223+
# Get the last message from the assistant
224+
last_msg = messages.get_last_text_message_by_sender("assistant")
225+
if last_msg:
226+
print(f"Last Message: {last_msg.text.value}")
227+
228+
# Delete the agent once done
229+
project_client.agents.delete_agent(agent.id)
230+
print("Deleted agent")
231+
```
232+
233+
::: zone-end

articles/ai-services/agents/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ items:
4646
href: how-to/tools/code-interpreter.md
4747
- name: Use OpenAPI defined tools
4848
href: how-to/tools/openapi-spec.md
49+
- name: Azure Functions
50+
href: how-to/tools/azure-functions.md
4951
- name: Content filtering
5052
href: ../openai/how-to/content-filters.md?context=/azure/ai-services/agents/context/context
5153
- name: Use your own resources

0 commit comments

Comments
 (0)