Skip to content

Commit 14f7821

Browse files
authored
Merge pull request #2058 from MicrosoftDocs/main
Publish to Live Wednesday 4AM PST, 12/18
2 parents aba0ab5 + 5a86b38 commit 14f7821

File tree

20 files changed

+646
-195
lines changed

20 files changed

+646
-195
lines changed
Lines changed: 276 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,276 @@
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+
16+
# Use Azure Functions with Azure AI Agent Service
17+
18+
::: zone pivot="overview"
19+
20+
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.
21+
22+
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.
23+
24+
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.
25+
26+
## Prerequisites
27+
28+
* [Azure Functions Core Tools v4.x](/azure/azure-functions/functions-run-local)
29+
* [Azure AI Agent Service](../../../../ai-studio/how-to/develop/sdk-overview.md?tabs=sync&pivots=programming-language-python#azure-ai-agent-service)
30+
* [Azurite](https://github.com/Azure/Azurite)
31+
32+
## Prepare your local environment
33+
34+
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.
35+
36+
You can find the template and code used here on [GitHub](https://github.com/Azure-Samples/azure-functions-ai-services-agent-python).
37+
38+
### Create Azure resources for local and cloud dev-test
39+
40+
Once you have your Azure subscription, run the following in a new terminal window to create Azure OpenAI and other resources needed:
41+
42+
```bash
43+
azd init --template https://github.com/Azure-Samples/azure-functions-ai-services-agent-python
44+
```
45+
#### Mac/Linux:
46+
47+
```bash
48+
chmod +x ./infra/scripts/*.sh
49+
```
50+
#### Windows:
51+
52+
```Powershell
53+
set-executionpolicy remotesigned
54+
```
55+
56+
### Provision resources
57+
58+
Run the following command to create the required resources in Azure.
59+
```bash
60+
azd provision
61+
```
62+
63+
### Create local.settings.json
64+
65+
> [!NOTE]
66+
> This file should be in the same folder as `host.json`. It is automatically created if you ran `azd provision`.
67+
68+
```json
69+
{
70+
"IsEncrypted": false,
71+
"Values": {
72+
"FUNCTIONS_WORKER_RUNTIME": "python",
73+
"STORAGE_CONNECTION__queueServiceUri": "https://<storageaccount>.queue.core.windows.net",
74+
"PROJECT_CONNECTION_STRING": "<project connnection for AI Project>",
75+
"AzureWebJobsStorage": "UseDevelopmentStorage=true"
76+
}
77+
}
78+
```
79+
80+
## Run your app using Visual Studio Code
81+
82+
1. Open the folder in a new terminal.
83+
1. Run the `code .` code command to open the project in Visual Studio Code.
84+
1. In the command palette (F1), type `Azurite: Start`, which enables debugging with local storage for Azure Functions runtime.
85+
1. Press **Run/Debug (F5)** to run in the debugger. Select **Debug anyway** if prompted about local emulator not running.
86+
1. Send POST `prompt` endpoints respectively using your HTTP test tool. If you have the [RestClient](https://marketplace.visualstudio.com/items?itemName=humao.rest-client) extension installed, you can execute requests directly from the [`test.http`](https://github.com/Azure-Samples/azure-functions-ai-services-agent-python/blob/main/app/test.http) project file.
87+
88+
89+
## Deploy to Azure
90+
91+
Run this command to provision the function app, with any required Azure resources, and deploy your code:
92+
93+
```shell
94+
azd up
95+
```
96+
97+
You're prompted to supply these required deployment parameters:
98+
99+
| Parameter | Description |
100+
| ---- | ---- |
101+
| _Environment name_ | An environment that's used to maintain a unique deployment context for your app. You won't be prompted if you created the local project using `azd init`.|
102+
| _Azure subscription_ | Subscription in which your resources are created.|
103+
| _Azure location_ | Azure region in which to create the resource group that contains the new Azure resources. Only regions that currently support the Flex Consumption plan are shown.|
104+
105+
After publish completes successfully, `azd` provides you with the URL endpoints of your new functions, but without the function key values required to access the endpoints. To learn how to obtain these same endpoints along with the required function keys, see [Invoke the function on Azure](/azure/azure-functions/create-first-function-azure-developer-cli?pivots=programming-language-dotnet#invoke-the-function-on-azure) in the companion article [Quickstart: Create and deploy functions to Azure Functions using the Azure Developer CLI](/azure/azure-functions/create-first-function-azure-developer-cli?pivots=programming-language-dotnet).
106+
107+
## Redeploy your code
108+
109+
You can run the `azd up` command as many times as you need to both provision your Azure resources and deploy code updates to your function app.
110+
111+
> [!NOTE]
112+
> Deployed code files are always overwritten by the latest deployment package.
113+
114+
## Clean up resources
115+
116+
When you're done working with your function app and related resources, you can use this command to delete the function app and its related resources from Azure and avoid incurring any further costs (--purge does not leave a soft delete of AI resource and recovers your quota):
117+
118+
```shell
119+
azd down --purge
120+
```
121+
122+
::: zone-end
123+
124+
::: zone pivot="code-example"
125+
126+
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
127+
128+
### Supported models
129+
130+
To use all features of function calling including parallel functions, you need to use a model that was released after November 6, 2023.
131+
132+
133+
## Define a function for your agent to call
134+
135+
Start by defining an Azure Function queue trigger function that will process function calls from the queue.
136+
137+
```python
138+
# Function to get the weather from an Azure Storage queue where the AI Agent will send function call information
139+
# 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
140+
@app.function_name(name="GetWeather")
141+
@app.queue_trigger(arg_name="msg", queue_name="input", connection="STORAGE_CONNECTION")
142+
def process_queue_message(msg: func.QueueMessage) -> None:
143+
logging.info('Python queue trigger function processed a queue item')
144+
145+
# Queue to send message to
146+
queue_client = QueueClient(
147+
os.environ["STORAGE_CONNECTION__queueServiceUri"],
148+
queue_name="output",
149+
credential=DefaultAzureCredential(),
150+
message_encode_policy=BinaryBase64EncodePolicy(),
151+
message_decode_policy=BinaryBase64DecodePolicy()
152+
)
153+
154+
# Get the content of the function call message
155+
messagepayload = json.loads(msg.get_body().decode('utf-8'))
156+
location = messagepayload['location']
157+
correlation_id = messagepayload['CorrelationId']
158+
159+
# Send message to queue. Sends a mock message for the weather
160+
result_message = {
161+
'Value': 'Weather is 74 degrees and sunny in ' + location,
162+
'CorrelationId': correlation_id
163+
}
164+
queue_client.send_message(json.dumps(result_message).encode('utf-8'))
165+
166+
logging.info(f"Sent message to output queue with message {result_message}")
167+
```
168+
169+
170+
## Create an AI project client and agent
171+
172+
In the sample below we create a client and an agent that has the tools definition for the Azure Function
173+
174+
```python
175+
# Initialize the client and create agent for the tools Azure Functions that the agent can use
176+
177+
# Create a project client
178+
project_client = AIProjectClient.from_connection_string(
179+
credential=DefaultAzureCredential(),
180+
conn_str=os.environ["PROJECT_CONNECTION_STRING"]
181+
)
182+
183+
# Get the connection string for the storage account to send and receive the function calls to the queues
184+
storage_connection_string = os.environ["STORAGE_CONNECTION__queueServiceUri"]
185+
186+
# Create an agent with the Azure Function tool to get the weather
187+
agent = project_client.agents.create_agent(
188+
model="gpt-4o-mini",
189+
name="azure-function-agent-get-weather",
190+
instructions="You are a helpful support agent. Answer the user's questions to the best of your ability.",
191+
headers={"x-ms-enable-preview": "true"},
192+
tools=[
193+
{
194+
"type": "azure_function",
195+
"azure_function": {
196+
"function": {
197+
"name": "GetWeather",
198+
"description": "Get the weather in a location.",
199+
"parameters": {
200+
"type": "object",
201+
"properties": {
202+
"location": {"type": "string", "description": "The location to look up."}
203+
},
204+
"required": ["location"]
205+
}
206+
},
207+
"input_binding": {
208+
"type": "storage_queue",
209+
"storage_queue": {
210+
"queue_service_uri": storage_connection_string,
211+
"queue_name": "input"
212+
}
213+
},
214+
"output_binding": {
215+
"type": "storage_queue",
216+
"storage_queue": {
217+
"queue_service_uri": storage_connection_string,
218+
"queue_name": "output"
219+
}
220+
}
221+
}
222+
}
223+
],
224+
)
225+
```
226+
227+
## Create a thread for the agent
228+
229+
```python
230+
# Create a thread
231+
thread = project_client.agents.create_thread()
232+
print(f"Created thread, thread ID: {thread.id}")
233+
```
234+
235+
## Create a run and check the output
236+
237+
```python
238+
# Send the prompt to the agent
239+
message = project_client.agents.create_message(
240+
thread_id=thread.id,
241+
role="user",
242+
content="What is the weather in Seattle, WA?",
243+
)
244+
print(f"Created message, message ID: {message.id}")
245+
246+
# Run the agent
247+
run = project_client.agents.create_run(thread_id=thread.id, assistant_id=agent.id)
248+
# 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
249+
while run.status in ["queued", "in_progress", "requires_action"]:
250+
time.sleep(1)
251+
run = project_client.agents.get_run(thread_id=thread.id, run_id=run.id)
252+
253+
if run.status not in ["queued", "in_progress", "requires_action"]:
254+
break
255+
256+
print(f"Run finished with status: {run.status}")
257+
```
258+
259+
### Get the result of the run
260+
261+
```python
262+
# Get messages from the assistant thread
263+
messages = project_client.agents.get_messages(thread_id=thread.id)
264+
print(f"Messages: {messages}")
265+
266+
# Get the last message from the assistant
267+
last_msg = messages.get_last_text_message_by_sender("assistant")
268+
if last_msg:
269+
print(f"Last Message: {last_msg.text.value}")
270+
271+
# Delete the agent once done
272+
project_client.agents.delete_agent(agent.id)
273+
print("Deleted agent")
274+
```
275+
276+
::: zone-end

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,15 @@ To use all features of function calling including parallel functions, you need t
3434

3535
## Define a function for your agent to call
3636

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.
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 in a docstring. Include all your function definitions in a single file, `user_functions.py` which you can then import into your main script.
3838

3939
# [Python](#tab/python)
4040

4141
```python
42+
import json
43+
import datetime
44+
from typing import Any, Callable, Set, Dict, List, Optional
45+
4246
def fetch_weather(location: str) -> str:
4347
"""
4448
Fetches the weather information for the specified location.
@@ -53,6 +57,11 @@ def fetch_weather(location: str) -> str:
5357
weather = mock_weather_data.get(location, "Weather data not available for this location.")
5458
weather_json = json.dumps({"weather": weather})
5559
return weather_json
60+
61+
# Statically defined user functions for fast reference
62+
user_functions: Set[Callable[..., Any]] = {
63+
fetch_weather,
64+
}
5665
```
5766

5867

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

articles/ai-services/document-intelligence/quickstarts/get-started-sdks-rest-api.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ ms.custom:
1111
- devx-track-js
1212
- devx-track-python
1313
ms.topic: quickstart
14-
ms.date: 11/19/2024
14+
ms.date: 12/17/2024
1515
ms.author: lajanuar
1616
zone_pivot_groups: programming-languages-set-formre
1717
---
@@ -29,7 +29,7 @@ zone_pivot_groups: programming-languages-set-formre
2929

3030
**This content applies to:** ![checkmark](../media/yes-icon.png) **v4.0 (GA)** **Earlier versions:** ![blue-checkmark](../media/blue-yes-icon.png) [v3.1 (GA)](?view=doc-intel-3.1.0&preserve-view=true) ![blue-checkmark](../media/blue-yes-icon.png) [v3.0 (GA)](?view=doc-intel-3.0.0&preserve-view=true)
3131

32-
* Get started with Azure AI Document Intelligence latest stable version v4.0 2024-11-30 (GA).
32+
* Get started with Azure AI Document Intelligence latest stable version v4.0 `2024-11-30` (GA).
3333

3434
:::moniker-end
3535

@@ -53,7 +53,7 @@ zone_pivot_groups: programming-languages-set-formre
5353

5454
* You can easily integrate document processing models into your workflows and applications by using a programming language SDK or calling the REST API.
5555

56-
* For this quickstart, we recommend that you use the free service while you're learning the technology. Remember that the number of free pages is limited to 500 per month.
56+
* We recommend that you use the free service while you're learning the technology for this quickstart. Remember that the number of free pages is limited to 500 per month.
5757

5858
To learn more about the API features and development options, visit our [Overview](../overview.md) page.
5959

articles/ai-services/document-intelligence/quickstarts/includes/csharp-sdk.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
---
2-
title: "Quickstart: Document Intelligence C# SDK | v4.0 | v3.1 | v3.0"
2+
title: "Quickstart: Document Intelligence C# SDK"
33
titleSuffix: Azure AI services
44
description: 'Form and document processing, data extraction, and analysis using Document Intelligence C# client library.'
55
author: laujan
66
manager: nitinme
77
ms.service: azure-ai-document-intelligence
88
ms.topic: include
9-
ms.date: 11/19/2024
9+
ms.date: 12/17/2024
1010
ms.author: lajanuar
1111
monikerRange: ">=doc-intel-3.0.0"
1212
---
@@ -16,7 +16,7 @@ monikerRange: ">=doc-intel-3.0.0"
1616
<!-- markdownlint-disable MD029 -->
1717

1818
:::moniker range="doc-intel-4.0.0"
19-
[Client library](/dotnet/api/overview/azure/ai.documentintelligence-readme?view=azure-dotnet-preview&preserve-view=true) | [SDK reference](https://azuresdkdocs.blob.core.windows.net/$web/dotnet/Azure.AI.DocumentIntelligence/1.0.0-beta.3/index.html) | [REST API reference](/rest/api/aiservices/operation-groups?view=rest-aiservices-v4.0%20(2024-11-30)&preserve-view=true) | [Package](https://www.nuget.org/packages/Azure.AI.DocumentIntelligence/1.0.0-beta.3)| [Samples]( https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/documentintelligence/Azure.AI.DocumentIntelligence/samples/README.md)|[Supported REST API version](../../sdk-overview-v4-0.md)
19+
[Client library](/dotnet/api/overview/azure/cognitiveservices/documentintelligence?view=azure-dotnet&preserve-view=true) | [SDK reference](https://azuresdkdocs.blob.core.windows.net/$web/dotnet/Azure.AI.DocumentIntelligence/1.0.0/index.html) | [REST API reference](/rest/api/aiservices/operation-groups?view=rest-aiservices-v4.0%20(2024-11-30)&preserve-view=true) | [Package](https://www.nuget.org/packages/Azure.AI.DocumentIntelligence/1.0.0)| [Samples](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/documentintelligence/Azure.AI.DocumentIntelligence/samples/README.md)|[Supported REST API version](../../sdk-overview-v4-0.md)
2020
:::moniker-end
2121

2222
:::moniker range="doc-intel-3.1.0"
@@ -164,7 +164,7 @@ To interact with the Form Recognizer service, you need to create an instance of
164164
165165
1. Open the **Program.cs** file.
166166

167-
1. Delete the pre-existing code, including the line `Console.Writeline("Hello World!")`, and select one of the following code samples to copy and paste into your application's Program.cs file:
167+
1. Delete the existing code, including the line `Console.Writeline("Hello World!")`, and select one of the following code samples to copy and paste into your application's Program.cs file:
168168

169169
* [**Layout model**](#layout-model)
170170

articles/ai-services/document-intelligence/quickstarts/includes/java-sdk.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
---
2-
title: "Quickstart: Document Intelligence Java SDK (beta) | v3.1 | v3.0"
2+
title: "Quickstart: Document Intelligence Java SDK"
33
titleSuffix: Azure AI services
44
description: Form and document processing, data extraction, and analysis using Document Intelligence Java client library.
55
author: laujan
66
manager: nitinme
77
ms.service: azure-ai-document-intelligence
88
ms.topic: include
9-
ms.date: 11/19/2024
9+
ms.date: 12/17/2024
1010
ms.author: lajanuar
1111
---
1212
<!-- markdownlint-disable MD025 -->
1313
<!-- markdownlint-disable MD036 -->
1414

1515
:::moniker range="doc-intel-4.0.0"
16-
[Client library](/java/api/overview/azure/ai-documentintelligence-readme?view=azure-java-preview&preserve-view=true) | [SDK reference](https://azuresdkdocs.blob.core.windows.net/$web/java/azure-ai-documentintelligence/1.0.0-beta.4/index.html) | [REST API reference](/rest/api/aiservices/operation-groups?view=rest-aiservices-v4.0%20(2024-11-30)&preserve-view=true) | [Package (Maven)](https://mvnrepository.com/artifact/com.azure/azure-ai-documentintelligence/1.0.0-beta.4) | [Samples](https://github.com/Azure/azure-sdk-for-java/tree/azure-ai-documentintelligence_1.0.0-beta.4/sdk/documentintelligence/azure-ai-documentintelligence/src/samples) |[Supported REST API version](../../sdk-overview-v4-0.md)
16+
[Client library](/java/api/overview/azure/ai-documentintelligence-readme?view=azure-java-preview&preserve-view=true) | [SDK reference](https://azuresdkdocs.blob.core.windows.net/$web/java/azure-ai-documentintelligence/1.0.0/index.html) | [REST API reference](/rest/api/aiservices/operation-groups?view=rest-aiservices-v4.0%20(2024-11-30)&preserve-view=true) | [Package (Maven)](https://mvnrepository.com/artifact/com.azure/azure-ai-documentintelligence/1.0.0-beta.4) | [Samples](https://github.com/Azure/azure-sdk-for-java/tree/azure-ai-documentintelligence_1.0.0/sdk/documentintelligence/azure-ai-documentintelligence/src/samples#examples) |[Supported REST API version](../../sdk-overview-v4-0.md)
1717
:::moniker-end
1818

1919
:::moniker range="doc-intel-3.1.0"

0 commit comments

Comments
 (0)