@@ -51,15 +51,16 @@ Set this endpoint as an environment variable named `PROJECT_ENDPOINT`.
51
51
52
52
[ !INCLUDE [ model-name-portal] ( model-name-portal.md )]
53
53
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 ` .
55
55
56
56
``` python
57
57
import os
58
+ from pathlib import Path
58
59
from azure.ai.projects import AIProjectClient
59
60
from azure.identity import DefaultAzureCredential
60
61
from azure.ai.agents.models import CodeInterpreterTool
61
62
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.
63
64
# You need to login to Azure subscription via Azure CLI and set the environment variables
64
65
project_endpoint = os.environ[" PROJECT_ENDPOINT" ] # Ensure the PROJECT_ENDPOINT environment variable is set
65
66
@@ -71,40 +72,51 @@ project_client = AIProjectClient(
71
72
72
73
code_interpreter = CodeInterpreterTool()
73
74
with project_client:
74
- # Create an agent with the Bing Grounding tool
75
+ # Create an agent with the Code Interpreter tool
75
76
agent = project_client.agents.create_agent(
76
77
model = os.environ[" MODEL_DEPLOYMENT_NAME" ], # Model deployment name
77
78
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
79
80
tools = code_interpreter.definitions, # Attach the tool
80
81
)
81
82
print (f " Created agent, ID: { agent.id} " )
82
83
83
84
# Create a thread for communication
84
85
thread = project_client.agents.threads.create()
85
86
print (f " Created thread, ID: { thread.id} " )
86
-
87
+
87
88
# Add a message to the thread
88
89
message = project_client.agents.messages.create(
89
90
thread_id = thread.id,
90
91
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
92
93
)
93
94
print (f " Created message, ID: { message[' id' ]} " )
94
-
95
+
95
96
# 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
+ )
97
102
print (f " Run finished with status: { run.status} " )
98
-
103
+
99
104
# Check if the run failed
100
105
if run.status == " failed" :
101
106
print (f " Run failed: { run.last_error} " )
102
-
107
+
103
108
# Fetch and log all messages
104
109
messages = project_client.agents.messages.list(thread_id = thread.id)
105
110
for message in messages:
106
111
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
+
108
120
# Delete the agent when done
109
121
project_client.agents.delete_agent(agent.id)
110
122
print (" Deleted agent" )
0 commit comments