02 Exercise π οΈ: Build an AI Agent with Code Interpreter
In this hands-on lab, you will use Azure AI Agent Service to build an agent that analyzes data and dynamically generates charts using the built-in Code Interpreter tool.
π Table of Contents
- Create Azure AI Foundry Project
- Create an Agent Client App
- Configure Application Settings
- Write Code for the Agent App
- Sign into Azure and Run the App
- π Summary
- π§Ή Clean Up
π§ Create Azure AI Foundry Project
- Open https://ai.azure.com and sign in.
- Click Create an agent.
- When prompted:
- Provide a valid Project name
- Expand Advanced options:
- Azure AI Foundry resource: your resource name
- Subscription: your Azure subscription
- Resource group: existing or new
- Region: any supported region
- Click Create and wait for provisioning.
- The Agents Playground will open automatically.
- In the left pane, click Overview.
- Copy your project endpoint and save it for later use.
π» Create an Agent Client App
- Clone the provided GitHub repo:
git clone https://github.com/MicrosoftLearning/mslearn-ai-agents ai-agents
cd ai-agents/Labfiles/02-build-ai-agent/Python
ls -a -l
βοΈ Configure Application Settings
-
Set up your Python virtual environment: ```bash python -m venv labenv ./labenv/Scripts/Activate.ps1 pip install -r requirements.txt azure-ai-projects
2. Open the .env config file: ```bash code .env ``` 3. Replace your_project_endpoint with your actual Foundry project endpoint. 4. Save (CTRL+S) and close (CTRL+Q) the editor. ## π§ Write Code for the Agent App Assicurati di rispettare lβindentazione corretta durante lβinserimento del codice. 1. Apri il file ```bash code agent.py ``` 2. Import needed classes ```bash from azure.identity import DefaultAzureCredential from azure.ai.agents import AgentsClient from azure.ai.agents.models import FilePurpose, CodeInterpreterTool, ListSortOrder, MessageRole ``` 3. Connect to the project ```bash agent_client = AgentsClient( endpoint=project_endpoint, credential=DefaultAzureCredential( exclude_environment_credential=True, exclude_managed_identity_credential=True ) ) with agent_client: ``` 4. Upload data and create a Code Interpreter Tool ```bash 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]) ``` 5. Define the Agent ```bash 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}") ``` 6. Create a thread for the conversation ```bash thread = agent_client.threads.create() ``` 7. Send the prompt to the Agent ```bash 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) ``` 8. Check errors ```bash if run.status == "failed": print(f"Run failed: {run.last_error}") ``` 9. Retrieve the last agent answer ```bash 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}") ``` 10. Show history of the conversation ```bash 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") ``` 11. Download generated files ```bash 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}") ``` 12. Delete resources ```bash agent_client.delete_agent(agent.id) ``` ## π Sign into Azure and Run the App 1. Autenticate yourself: ```bash az login
-
Esecute the app:
-
Insert the prompt:
-
Continue the conversation, so insert 'quit' to exit.
-
Download the generated files .png: