Skip to content

Commit 97066cc

Browse files
Merge pull request #6560 from dargilco/dargilco/update-python-agents-quickstart
Updates to Agents Python quickstart code
2 parents 3afd8f7 + 4953836 commit 97066cc

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

articles/ai-foundry/agents/includes/quickstart-csharp.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ client.Messages.CreateMessage(
9292
MessageRole.User,
9393
"Hi, Agent! Draw a graph for a line with a slope of 4 and y-intercept of 9.");
9494

95-
//Have Agent beging processing user's question with some additional instructions associated with the ThreadRun.
95+
//Have Agent begin processing user's question with some additional instructions associated with the ThreadRun.
9696
ThreadRun run = client.Runs.CreateRun(
9797
thread.Id,
9898
agent.Id,

articles/ai-foundry/agents/includes/quickstart-python.md

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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
5757
import os
58+
from pathlib import Path
5859
from azure.ai.projects import AIProjectClient
5960
from azure.identity import DefaultAzureCredential
6061
from 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
6465
project_endpoint = os.environ["PROJECT_ENDPOINT"] # Ensure the PROJECT_ENDPOINT environment variable is set
6566

@@ -71,40 +72,51 @@ project_client = AIProjectClient(
7172

7273
code_interpreter = CodeInterpreterTool()
7374
with project_client:
74-
# Create an agent with the Bing Grounding tool
75+
# Create an agent with the Code Interpreter tool
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

Comments
 (0)