Overview
This exercise demonstrates how to develop an AI agent using Azure AI Foundry. The agent analyzes data and generates charts dynamically using the Code Interpreter tool.
Steps & Configuration Details
1. Create an Azure AI Foundry Project
- Open Azure AI Foundry portal (https://ai.azure.com) and sign in.
- Select Create an agent.
- Configuration Items:
- Azure AI Foundry Resource: A valid name.
- Subscription: Your Azure subscription.
- Resource Group: Select or create a resource group.
- Region: Choose any AI Services-supported location.
- Project Name: A unique name.
After creation, the Agents playground opens automatically, with a GPT-4o base model deployed.
2. Configure the Agent Client App
- Open Azure Portal (https://portal.azure.com).
- Launch Azure Cloud Shell (PowerShell environment).
- Clone the repository:
rm -r ai-agents -f git clone https://github.com/MicrosoftLearning/mslearn-ai-agents ai-agents
- Navigate to the correct folder:
cd ai-agents/Labfiles/02-build-ai-agent/Python
- Install dependencies:
python -m venv labenv ./labenv/bin/Activate.ps1 pip install -r requirements.txt azure-ai-projects
- Open the configuration file:
code .env
- Update Configuration Values:
- Project Endpoint (copied from Azure AI Foundry portal).
- Save the configuration file.
3. Write Code for the Agent App
- Open the code file:
code agent.py
- Add references:
from azure.identity import DefaultAzureCredential from azure.ai.agents import AgentsClient from azure.ai.agents.models import FilePurpose, CodeInterpreterTool, ListSortOrder, MessageRole
- Connect to the Agent client:
agent_client = AgentsClient( endpoint=project_endpoint, credential=DefaultAzureCredential( exclude_environment_credential=True, exclude_managed_identity_credential=True ) ) with agent_client:
- Upload the data file and create a CodeInterpreterTool:
file = agent_client.files.upload_and_poll( file_path=file_path, purpose=FilePurpose.AGENTS ) print(f"Uploaded {file.filename}") code_interpreter = CodeInterpreterTool(file_ids=[file.id])
- Define an agent that uses the CodeInterpreterTool:
agent = agent_client.create_agent( model=model_deployment, name="data-agent", instructions="You are an AI agent that analyzes the data in the file that has been uploaded. If the user requests a chart, create it and save it as a .png file.", tools=code_interpreter.definitions, tool_resources=code_interpreter.resources, ) print(f"Using agent: {agent.name}")
- Create a thread for the conversation:
thread = agent_client.threads.create()
- Send a prompt to the agent:
message = agent_client.messages.create( thread_id=thread.id, role="user", content=user_prompt, ) run = agent_client.runs.create_and_process(thread_id=thread.id, agent_id=agent.id) if run.status == "failed": print(f"Run failed: {run.last_error}")
- Show the latest response from the agent:
last_msg = agent_client.messages.get_last_message_text_by_role( thread_id=thread.id, role=MessageRole.AGENT, ) if last_msg: print(f"Last Message: {last_msg.text.value}")
- Get the conversation history:
print("\nConversation Log:\n") messages = agent_client.messages.list(thread_id=thread.id, order=ListSortOrder.ASCENDING) for message in messages: if message.text_messages: last_msg = message.text_messages[-1] print(f"{message.role}: {last_msg.text.value}\n")
- Retrieve generated files:
for msg in messages: for img in msg.image_contents: file_id = img.image_file.file_id file_name = f"{file_id}_image_file.png" agent_client.files.save(file_id=file_id, file_name=file_name) print(f"Saved image file to: {Path.cwd() / file_name}")
- Clean up:
agent_client.delete_agent(agent.id)
4. Run the Agent App
- Sign into Azure:
az login
- Run the application:
python agent.py
- Example prompt:
What's the category with the highest cost?
- Request a chart:
Create a pie chart showing cost by category.
- The agent should generate a .png file with the requested chart.
5. Clean Up
- Delete Azure resources to avoid unnecessary costs:
- Open Azure Portal (https://portal.azure.com).
- Navigate to Resource Groups.
- Select the resource group and click Delete.