@@ -51,15 +51,16 @@ Set this endpoint as an environment variable named `PROJECT_ENDPOINT`.
5151
5252[ !INCLUDE [ model-name-portal] ( model-name-portal.md )]
5353
54- Save the name of your model deployment name as an environment variable named ` MODEL_DEPLOYMENT_NAME ` .
54+ Save the name of your model deployment name as an environment variable named ` MODEL_DEPLOYMENT_NAME ` .
5555
5656``` python
5757import os
58+ from pathlib import Path
5859from azure.ai.projects import AIProjectClient
5960from azure.identity import DefaultAzureCredential
6061from azure.ai.agents.models import CodeInterpreterTool
6162
62- # Create an Azure AI Client from an endpoint, copied from your Azure AI Foundry project.
63+ # Create an AIProjectClient from an endpoint, copied from your Azure AI Foundry project.
6364# You need to login to Azure subscription via Azure CLI and set the environment variables
6465project_endpoint = os.environ[" PROJECT_ENDPOINT" ] # Ensure the PROJECT_ENDPOINT environment variable is set
6566
@@ -75,36 +76,47 @@ with project_client:
7576 agent = project_client.agents.create_agent(
7677 model = os.environ[" MODEL_DEPLOYMENT_NAME" ], # Model deployment name
7778 name = " my-agent" , # Name of the agent
78- instructions = " You are a helpful agent " , # Instructions for the agent
79+ instructions = " You politely help with math questions. Use the Code Interpreter tool when asked to visualize numbers. " , # Instructions for the agent
7980 tools = code_interpreter.definitions, # Attach the tool
8081 )
8182 print (f " Created agent, ID: { agent.id} " )
8283
8384 # Create a thread for communication
8485 thread = project_client.agents.threads.create()
8586 print (f " Created thread, ID: { thread.id} " )
86-
87+
8788 # Add a message to the thread
8889 message = project_client.agents.messages.create(
8990 thread_id = thread.id,
9091 role = " user" , # Role of the message sender
91- content = " What is the weather in Seattle today? " , # Message content
92+ content = " Hi, Agent! Draw a graph for a line with a slope of 4 and y-intercept of 9. " , # Message content
9293 )
9394 print (f " Created message, ID: { message[' id' ]} " )
94-
95+
9596 # Create and process an agent run
96- run = project_client.agents.runs.create_and_process(thread_id = thread.id, agent_id = agent.id)
97+ run = project_client.agents.runs.create_and_process(
98+ thread_id = thread.id,
99+ agent_id = agent.id,
100+ additional_instructions = " Please address the user as Jane Doe. The user has a premium account" ,
101+ )
97102 print (f " Run finished with status: { run.status} " )
98-
103+
99104 # Check if the run failed
100105 if run.status == " failed" :
101106 print (f " Run failed: { run.last_error} " )
102-
107+
103108 # Fetch and log all messages
104109 messages = project_client.agents.messages.list(thread_id = thread.id)
105110 for message in messages:
106111 print (f " Role: { message.role} , Content: { message.content} " )
107-
112+
113+ # Save every image file in the message
114+ for img in message.image_contents:
115+ file_id = img.image_file.file_id
116+ file_name = f " { file_id} _image_file.png "
117+ project_client.agents.files.save(file_id = file_id, file_name = file_name)
118+ print (f " Saved image file to: { Path.cwd() / file_name} " )
119+
108120 # Delete the agent when done
109121 project_client.agents.delete_agent(agent.id)
110122 print (" Deleted agent" )
0 commit comments