/azure-functions-ai-services-agent-python

Sample to show using Azure Functions to run action functions from Azure AI Services

Primary LanguageBicepMIT LicenseMIT

Azure Functions

Using Azure Functions to enable function calling from Azure AI Agent service

This sample highlights 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.

You can learn more about Azure functions in the Official documentation

The app folder contains the function code used in this sample while the infra folder contains all of the Azure resources that need to be created.

Prerequisites

Prepare your local environment

Create Azure resources for local and cloud dev-test

Once you have your Azure subscription, run the following in a new terminal window to create Azure OpenAI and other resources needed:

azd init --template https://github.com/Azure-Samples/azure-functions-ai-services-agent-python

Mac/Linux:

chmod +x ./infra/scripts/*.sh 

Windows:

set-executionpolicy remotesigned

Run the follow command to provision resources in Azure

azd provision

Create local.settings.json (Should be in the same folder as host.json. Automatically created if you ran azd provision)

{
  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "python",
    "STORAGE_CONNECTION__queueServiceUri": "https://<storageaccount>.queue.core.windows.net",
    "PROJECT_CONNECTION_STRING": "<project connnection for AI Project>",
    "AzureWebJobsStorage": "UseDevelopmentStorage=true"
    }
}

Run your app using Visual Studio Code

  1. Open the folder in a new terminal.
  2. Run the code . code command to open the project in Visual Studio Code.
  3. In the command palette (F1), type Azurite: Start, which enables debugging with local storage for Azure Functions runtime.
  4. Press Run/Debug (F5) to run in the debugger. Select Debug anyway if prompted about local emulator not running.
  5. Send POST prompt endpoints respectively using your HTTP test tool. If you have the RestClient extension installed, you can execute requests directly from the test.http project file.

Deploy to Azure

Run this command to provision the function app, with any required Azure resources, and deploy your code:

azd up

You're prompted to supply these required deployment parameters:

Parameter Description
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.
Azure subscription Subscription in which your resources are created.
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.

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 in the companion article Quickstart: Create and deploy functions to Azure Functions using the Azure Developer CLI.

Redeploy your code

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.

Note

Deployed code files are always overwritten by the latest deployment package.

Clean up resources

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):

azd down --purge