You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -308,100 +308,103 @@ For any issues with the TypeScript code, create an issue on the [sample code rep
308
308
### Prerequisites
309
309
To make a function call we need to create and deploy the Azure function. In the code snippet below, we have an example of function on C# which can be used by the code above.
310
310
311
-
```csharp
312
-
namespaceFunctionProj
313
-
{
314
-
publicclassResponse
315
-
{
316
-
publicrequiredstringValue { get; set; }
317
-
publicrequiredstringCorrelationId { get; set; }
318
-
}
319
-
320
-
publicclassArguments
321
-
{
322
-
publicrequiredstringOutputQueueUri { get; set; }
323
-
publicrequiredstringCorrelationId { get; set; }
324
-
}
325
-
326
-
publicclassFoo
311
+
```csharp
312
+
namespace FunctionProj
327
313
{
328
-
privatereadonlyILogger<Foo> _logger;
329
-
330
-
publicFoo(ILogger<Foo> logger)
314
+
public class Response
331
315
{
332
-
_logger=logger;
316
+
public required string Value { get; set; }
317
+
public required string CorrelationId { get; set; }
public void Run([QueueTrigger("azure-function-foo-input")] Arguments input, FunctionContext executionContext)
337
+
{
338
+
var logger = executionContext.GetLogger("Foo");
339
+
logger.LogInformation("C# Queue function processed a request.");
340
+
341
+
// We have to provide the Managed identity for function resource
342
+
// and allow this identity a Queue Data Contributor role on the storage account.
343
+
var cred = new DefaultAzureCredential();
344
+
var queueClient = new QueueClient(new Uri(input.OutputQueueUri), cred,
345
+
new QueueClientOptions { MessageEncoding = QueueMessageEncoding.Base64 });
346
+
347
+
var response = new Response
348
+
{
349
+
Value = "Bar",
350
+
// Important! Correlation ID must match the input correlation ID.
351
+
CorrelationId = input.CorrelationId
352
+
};
353
+
354
+
var jsonResponse = JsonSerializer.Serialize(response);
355
+
queueClient.SendMessage(jsonResponse);
356
+
}
356
357
}
357
358
}
358
-
}
359
-
```
359
+
```
360
360
361
361
In this code we define function input and output class: `Arguments` and `Response` respectively. These two data classes will be serialized in JSON. It is important that these both contain field `CorrelationId`, which is the same between input and output.
362
362
363
363
In our example the function will be stored in the storage account, created with the AI hub. For that we need to allow key access to that storage. In Azure portal go to Storage account > Settings > Configuration and set "Allow storage account key access" to Enabled. If it is not done, the error will be displayed "The remote server returned an error: (403) Forbidden." To create the function resource that will host our function, install azure-cli python package and run the next command:
364
364
365
-
```shell
366
-
pip install -U azure-cli
367
-
az login
368
-
az functionapp create --resource-group your-resource-group --consumption-plan-location region --runtime dotnet-isolated --functions-version 4 --name function_name --storage-account storage_account_already_present_in_resource_group --app-insights existing_or_new_application_insights_name
369
-
```
365
+
```shell
366
+
pip install -U azure-cli
367
+
az login
368
+
az functionapp create --resource-group your-resource-group --consumption-plan-location region --runtime dotnet-isolated --functions-version 4 --name function_name --storage-account storage_account_already_present_in_resource_group --app-insights existing_or_new_application_insights_name
369
+
```
370
370
371
-
This function writes data to the output queue and hence needs to be authenticated to Azure, so we will need to assign the function system identity and provide it `Storage Queue Data Contributor`. To do that in Azure portal select the function, located in `your-resource-group` resource group and in Settings>Identity, switch it on and click Save. After that assign the `Storage Queue Data Contributor` permission on storage account used by our function (`storage_account_already_present_in_resource_group` in the script above) for just assigned System Managed identity.
371
+
This function writes data to the output queue and hence needs to be authenticated to Azure, so we will need to assign the function system identity and provide it `Storage Queue Data Contributor`. To do that in Azure portal select the function, located in `your-resource-group` resource group and in Settings>Identity, switch it on and click Save. After that assign the `Storage Queue Data Contributor` permission on storage account used by our function (storage_account_already_present_in_resource_group in the script above) for just assigned System Managed identity.
372
372
373
373
Now we will create the function itself. Install [.NET](https://dotnet.microsoft.com/download) and [Core Tools](https://go.microsoft.com/fwlink/?linkid=2174087) and create the function project using next commands.
**Note:** There is a "Azure Queue Storage trigger", however the attempt to use it results in error for now.
383
384
We have created a project, containing HTTP-triggered azure function with the logic in `Foo.cs` file. As far as we need to trigger Azure function by a new message in the queue, we will replace the content of a Foo.cs by the C# sample code above.
384
385
To deploy the function run the command from dotnet project folder:
385
386
386
-
```
387
-
func azure functionapp publish function_name
388
-
```
387
+
```console
388
+
func azure functionapp publish function_name
389
+
```
389
390
390
391
In the `storage_account_already_present_in_resource_group` select the `Queue service` and create two queues: `azure-function-foo-input` and `azure-function-tool-output`. Note that the same queues are used in our sample. To check that the function is working, place the next message into the `azure-function-foo-input` and replace `storage_account_already_present_in_resource_group` by the actual resource group name, or just copy the output queue address.
Next, we will monitor the output queue or the message. You should receive the next message.
399
-
```json
400
-
{
401
-
"Value": "Bar",
402
-
"CorrelationId": "42"
403
-
}
404
-
```
401
+
402
+
```json
403
+
{
404
+
"Value": "Bar",
405
+
"CorrelationId": "42"
406
+
}
407
+
```
405
408
Please note that the input `CorrelationId` is the same as output.
406
409
*Hint:* Place multiple messages to input queue and keep second internet browser window with the output queue open and hit the refresh button on the portal user interface, so that you will not miss the message. If the message instead went to `azure-function-foo-input-poison` queue, the function completed with error, please check your setup.
407
410
After we have tested the function and made sure it works, please make sure that the Azure AI Project have the next roles for the storage account: `Storage Account Contributor`, `Storage Blob Data Contributor`, `Storage File Data Privileged Contributor`, `Storage Queue Data Contributor` and `Storage Table Data Contributor`. Now the function is ready to be used by the agent.
@@ -412,8 +415,6 @@ In the example below we are calling function "foo", which responds "Bar".
412
415
413
416
1. First, we set up the necessary configuration, initialize the `PersistentAgentsClient`, define the `AzureFunctionToolDefinition` for our Azure Function, and then create the agent. This step includes all necessary `using` directives.
414
417
415
-
Common setup:
416
-
417
418
```csharp
418
419
usingAzure;
419
420
usingAzure.AI.Agents.Persistent;
@@ -467,11 +468,7 @@ In the example below we are calling function "foo", which responds "Bar".
0 commit comments