Skip to content

Commit c5f8f68

Browse files
committed
AI Agent tool: Functions
1 parent d8f09e4 commit c5f8f68

File tree

1 file changed

+162
-2
lines changed

1 file changed

+162
-2
lines changed

articles/ai-services/agents/how-to/tools/azure-functions.md

Lines changed: 162 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ services: azure-ai-agent-service
66
manager: nitinme
77
ms.service: azure-ai-agent-service
88
ms.topic: how-to
9-
ms.date: 01/30/2025
9+
ms.date: 04/15/2025
1010
author: aahill
1111
ms.author: aahi
1212
ms.custom: azure-ai-agents
@@ -140,6 +140,7 @@ To use all features of function calling including parallel functions, you need t
140140

141141
Start by defining an Azure Function queue trigger function that will process function calls from the queue.
142142

143+
# [Python](#tab/python)
143144
```python
144145
# Function to get the weather from an Azure Storage queue where the AI Agent will send function call information
145146
# 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
@@ -172,10 +173,58 @@ def process_queue_message(msg: func.QueueMessage) -> None:
172173
logging.info(f"Sent message to output queue with message {result_message}")
173174
```
174175

176+
# [TypeScript](#tab/typescript)
177+
178+
```typescript
179+
import type { InvocationContext } from "@azure/functions";
180+
import { app, output } from '@azure/functions';
181+
182+
const inputQueueName = "input";
183+
const outputQueueName = "output";
184+
185+
interface QueueItem {
186+
location: string;
187+
coorelationId: string;
188+
}
189+
190+
interface ProcessedQueueItem {
191+
Value: string;
192+
CorrelationId: string;
193+
}
194+
195+
const temperatures = [60, 65, 70, 75, 80, 85];
196+
const descriptions = ["sunny", "cloudy", "rainy", "stormy", "windy"];
197+
198+
const queueOutput = output.storageQueue({
199+
queueName: outputQueueName,
200+
connection: 'STORAGE_CONNECTION',
201+
});
202+
203+
export async function processQueueTrigger(queueItem: QueueItem, context: InvocationContext): Promise<ProcessedQueueItem> {
204+
context.log('QUEUE:', queueItem);
205+
206+
const randomTemp = temperatures[Math.floor(Math.random() * temperatures.length)];
207+
const randomDescription = descriptions[Math.floor(Math.random() * descriptions.length)];
208+
209+
return {
210+
Value: `${queueItem.location} weather is ${randomTemp} degrees and ${randomDescription}`,
211+
CorrelationId: queueItem.coorelationId,
212+
};
213+
}
214+
215+
app.storageQueue('storageQueueTrigger1', {
216+
queueName: inputQueueName,
217+
connection: 'STORAGE_CONNECTION',
218+
extraOutputs: [queueOutput],
219+
handler: processQueueTrigger,
220+
});
221+
```
222+
223+
---
175224

176225
## Create an AI project client and agent
177226

178-
In the sample below we create a client and an agent that has the tools definition for the Azure Function
227+
In the sample below we create a client and an agent that has the tools definition for the Azure Function.
179228

180229
# [Python](#tab/python)
181230
```python
@@ -231,6 +280,68 @@ agent = project_client.agents.create_agent(
231280
)
232281
```
233282

283+
284+
# [TypeScript](#tab/typescript)
285+
286+
```typescript
287+
import {
288+
AIProjectsClient
289+
} from "@azure/ai-projects";
290+
import { DefaultAzureCredential } from "@azure/identity";
291+
292+
const model = "gpt-4o-mini"
293+
const inputQueueName = "input";
294+
const outputQueueName = "output";
295+
const projectConnectionString = process.env.PROJECT_CONNECTION_STRING as string;
296+
const storageConnectionString = process.env.STORAGE_CONNECTION__queueServiceUri as string;
297+
298+
const projectClient = AIProjectsClient.fromConnectionString(
299+
projectConnectionString || "",
300+
new DefaultAzureCredential(),
301+
);
302+
303+
const agent = await projectClient.agents.createAgent(
304+
model, {
305+
name: "azure-function-agent-get-weather",
306+
instructions: "You are a helpful support agent. Answer the user's questions to the best of your ability.",
307+
requestOptions: {
308+
headers: { "x-ms-enable-preview": "true" }
309+
},
310+
tools: [
311+
{
312+
type: "azure_function",
313+
azureFunction: {
314+
function: {
315+
name: "GetWeather",
316+
description: "Get the weather in a location.",
317+
parameters: {
318+
type: "object",
319+
properties: {
320+
location: { type: "string", description: "The location to look up." },
321+
},
322+
required: ["location"],
323+
},
324+
},
325+
inputBinding: {
326+
type: "storage_queue",
327+
storageQueue: {
328+
queueServiceEndpoint: storageConnectionString,
329+
queueName: inputQueueName,
330+
},
331+
},
332+
outputBinding: {
333+
type: "storage_queue",
334+
storageQueue: {
335+
queueServiceEndpoint: storageConnectionString,
336+
queueName: outputQueueName,
337+
},
338+
},
339+
},
340+
},
341+
],
342+
});
343+
```
344+
234345
# [REST API](#tab/rest)
235346
Follow the [REST API Quickstart](../../quickstart.md?pivots=rest-api) to set the right values for the environment variables `AZURE_AI_AGENTS_TOKEN` and `AZURE_AI_AGENTS_ENDPOINT`. Then create the agent using:
236347
```console
@@ -287,6 +398,13 @@ thread = project_client.agents.create_thread()
287398
print(f"Created thread, thread ID: {thread.id}")
288399
```
289400

401+
# [TypeScript](#tab/typescript)
402+
403+
```typescript
404+
const thread = await projectClient.agents.createThread();
405+
console.log(`Created thread, thread ID: ${thread.id}`);
406+
```
407+
290408
# [REST API](#tab/rest)
291409
```console
292410
curl $AZURE_AI_AGENTS_ENDPOINT/threads?api-version=2024-12-01-preview \
@@ -320,6 +438,31 @@ while run.status in ["queued", "in_progress", "requires_action"]:
320438

321439
print(f"Run finished with status: {run.status}")
322440
```
441+
442+
# [TypeScript](#tab/typescript)
443+
444+
```typescript
445+
const message = await projectClient.agents.createMessage(
446+
thread.id,
447+
{
448+
role: "user",
449+
content: body?.prompt
450+
});
451+
context.log(`Created message, message ID: ${message.id}`);
452+
453+
let run = await projectClient.agents.createRun(
454+
thread.id,
455+
agent.id
456+
);
457+
458+
while (["queued", "in_progress", "requires_action"].includes(run.status)) {
459+
await new Promise((resolve) => setTimeout(resolve, 1000));
460+
run = await projectClient.agents.getRun(thread.id, run.id);
461+
}
462+
463+
context.log(`Run finished with status: ${run.status}`);
464+
```
465+
323466
# [REST API](#tab/rest)
324467
```console
325468
curl $AZURE_AI_AGENTS_ENDPOINT/threads/thread_abc123/messages?api-version=2024-12-01-preview \
@@ -364,6 +507,23 @@ project_client.agents.delete_agent(agent.id)
364507
print("Deleted agent")
365508
```
366509

510+
# [TypeScript](#tab/typescript)
511+
512+
```typescript
513+
const { data: messages } = await projectClient.agents.listMessages(thread.id);
514+
515+
const lastMessage = messages.find((msg: any) => msg.sender === "assistant");
516+
517+
let lastMessageContent: string = "";
518+
if (lastMessage) {
519+
lastMessageContent = lastMessage.content.join(", ");
520+
context.log(`Last Message: ${lastMessageContent}`);
521+
}
522+
523+
await projectClient.agents.deleteAgent(agent.id);
524+
context.log("Deleted agent");
525+
```
526+
367527
# [REST API](#tab/rest)
368528
```console
369529
curl $AZURE_AI_AGENTS_ENDPOINT/threads/thread_abc123/messages?api-version=2024-12-01-preview \

0 commit comments

Comments
 (0)