Develop an AI Agent that uses a custom function
In this exercise you’ll use Azure AI Agent Service to create an agent that uses a custom function as a tool to complete a task. The specific task this agent will perform is to compose and send an email message (which we’ll simulate in this lab by simply printing the email in the console).
This exercise should take approximately 30 minutes to complete.
Create an Azure AI Foundry project
Let’s start by creating an Azure AI Foundry project.
-
In a web browser, open the Azure AI Foundry portal at
https://ai.azure.com
and sign in using your Azure credentials. Close any tips or quick start panes that are opened the first time you sign in, and if necessary use the Azure AI Foundry logo at the top left to navigate to the home page, which looks similar to the following image: - In the home page, select + Create project.
- In the Create a project wizard, enter a suitable project name for (for example,
my-ai-project
) and if an existing hub is suggested, choose the option to create a new one. Then review the Azure resources that will be automatically created to support your hub and project. - Select Customize and specify the following settings for your hub:
- Hub name: A unique name - for example
my-ai-hub
- Subscription: Your Azure subscription
- Resource group: Create a new resource group with a unique name (for example,
my-ai-resources
), or select an existing one - Location: Select Help me choose and then select gpt-4 in the Location helper window and use the recommended region*
- Connect Azure AI Services or Azure OpenAI: Create a new AI Services resource with an appropriate name (for example,
my-ai-services
) or use an existing one - Connect Azure AI Search: Skip connecting
* Model quotas are constrained at the tenant level by regional quotas. In the event of a quota limit being reached later in the exercise, there’s a possibility you may need to create another project in a different region.
- Hub name: A unique name - for example
- Select Next and review your configuration. Then select Create and wait for the process to complete.
-
When your project is created, close any tips that are displayed and review the project page in Azure AI Foundry portal, which should look similar to the following image:
- In the project overview page, in the Project details area, note the Project connection string. Later, you’ll use this connection string to connect to your project in a client application.
Deploy a generative AI model
Now you’re ready to deploy a generative AI language model to support your agent.
- In the pane on the left for your project, in the My assets section, select the Models + endpoints page.
- In the Models + endpoints page, in the Model deployments tab, in the + Deploy model menu, select Deploy base model.
- Search for the gpt-4 model in the list, and then select and confirm it.
- Deploy the model with the following settings by selecting Customize in the deployment details:
- Deployment name: A unique name for your model deployment - for example
gpt-4-model
(remember the name you choose - you’ll need it later) - Deployment type: Standard
- Model version: Select the default version
- Connected AI resource: Select your Azure OpenAI resource connection
- Tokens per Minute Rate Limit (thousands): 5K
- Content filter: DefaultV2
- Enable dynamic quota: Disabled
Note: Reducing the TPM helps avoid over-using the quota available in the subscription you are using. 5,000 TPM is sufficient for the data used in this exercise.
- Deployment name: A unique name for your model deployment - for example
- Wait for the deployment provisioning state to be Completed.
Create an agent client app
Now you’re ready to create a client app that defines an agent and a custom function. Some code has been provided for you in a GitHub repository.
Clone the repo containing the starter code
- Open a new browser tab (keeping the Azure AI Foundry portal open in the existing tab). Then in the new tab, browse to the Azure portal at
https://portal.azure.com
; signing in with your Azure credentials if prompted. -
Use the [>_] button to the right of the search bar at the top of the page to create a new Cloud Shell in the Azure portal, selecting a PowerShell environment. The cloud shell provides a command line interface in a pane at the bottom of the Azure portal.
Note: If you have previously created a cloud shell that uses a Bash environment, switch it to PowerShell.
-
In the cloud shell toolbar, in the Settings menu, select Go to Classic version (this is required to use the code editor).
-
In the PowerShell pane, enter the following commands to clone the GitHub repo containing the code files for this exercise:
rm -r ai-agents -f git clone https://github.com/graememalcolm/ai-agents ai-agents
Tip: As you enter commands into the cloudshell, the ouput may take up a large amount of the screen buffer and the cursor on the current line may be obscured. You can clear the screen by entering the
cls
command to make it easier to focus on each task. -
Enter the following command to change the working directory to the folder containing the code files and list them all.
cd ai-agents/labfiles/agent-tool/python ls -a -l
The provided files include application code and a file for configuration settings.
Configure the application settings
-
In the cloud shell command line pane, enter the following command to install the libraries you’ll use:
pip install python-dotenv azure-identity azure-ai-projects
-
Enter the following command to edit the configuration file that has been provided:
code .env
The file is opened in a code editor.
- In the code file, replace the your_project_connection_string placeholder with the connection string for your project (copied from the project Overview page in the Azure AI Foundry portal), and the your_model_deployment placeholder with the name you assigned to your gpt-4 model deployment.
- After you’ve replaced the placeholders, use the CTRL+S command to save your changes and then use the CTRL+Q command to close the code editor while keeping the cloud shell command line open.
Write code for an agent app
Tip: As you add code, be sure to maintain the correct indentation.
-
Enter the following command to view the functions code file that has been provided:
code user_functions.py
- Review the code in this file, which includes a function named send_email that simulates sending an email by printing its details to the console.
- Close the code file (CTRL+Q) without saving any changes.
-
Enter the following command to edit the agent code file that has been provided:
code agent-tool.py
- Review the existing code, which retrieves the application configuration settings and creates an AIProjectClient for your Azure AI Foundry project.
-
Under the comment Define an agent that uses a custom function, add the following code to complete the app:
# Define an agent that uses a custom function with project_client: functions = FunctionTool(user_functions) toolset = ToolSet() toolset.add(functions) agent = project_client.agents.create_agent( model=MODEL_DEPLOYMENT, name="email-agent", instructions="You are an AI assistant that creates and sends email messages.", toolset=toolset ) print(f"Created agent, ID: {agent.id}") # Create and run a thread with a prompt message thread = project_client.agents.create_thread() message = project_client.agents.create_message( thread_id=thread.id, role="user", content="Create an itemized list of the expense claim items in the following data and calculate the total: " + data + ". Then send the list and total in an email to expenses@contoso.com with the subject 'Expense Claim'." ) run = project_client.agents.create_and_process_run(thread_id=thread.id, agent_id=agent.id) # Check the run status for failures if run.status == "failed": print(f"Run failed: {run.last_error}") # Get messages from the thread and print the last one from the agent messages = project_client.agents.list_messages(thread_id=thread.id) last_msg = messages.get_last_text_message_by_role("assistant") if last_msg: print(f"Last Message: {last_msg.text.value}") # Delete the agent when done project_client.agents.delete_agent(agent.id)
- Review the code, using the comments to understand how it:
- Uses the function tool to add your custom function code to a toolset
- Creates an agent that uses the toolset containing your function code
- Runs a thread with a prompt message requesting an email message for an expense claim
- Checks the status of the run in case there’s a failure
- Retrieves the messages from the completed thread and displays the last one sent by the agent.
- Deletes the agent when it is no longer required.
- Save the code file (CTRL+S) and close the code editor (CTRL+Q) when you have finished; keeping the cloud shell command line pane open.
Sign into Azure and run the app
-
In the cloud shell command line pane, enter the following command to sign into Azure.
az login
- When prompted, follow the instructions to open the sign-in page in a new tab and enter the authentication code provided and your Azure credentials. Then complete the sign in process in the command line, selecting the subscription containing your Azure AI Foundry hub if prompted.
-
After you have signed in, enter the following command to run the application:
python agent-tool.py
The application runs using the credentials for your authenticated Azure session to connect to your project and create and run the agent.
- When the application has finished, review the output, which should include the output from the send_email function as well as the final confirmation message from the agent
Summary
In this exercise, you used the Azure AI Agent Service SDK to create an AI agent that uses a custom function as a tool.
Clean up
If you’ve finished exploring Azure AI Agent Service, you should delete the resources you have created in this exercise to avoid incurring unnecessary Azure costs.
- Return to the browser tab containing the Azure portal (or re-open the Azure portal at
https://portal.azure.com
in a new browser tab) and view the contents of the resource group where you deployed the resources used in this exercise. - On the toolbar, select Delete resource group.
- Enter the resource group name and confirm that you want to delete it.