Skip to content

Commit 29abc01

Browse files
committed
code interpreter
1 parent 9597ffb commit 29abc01

2 files changed

Lines changed: 629 additions & 0 deletions

File tree

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# pylint: disable=line-too-long,useless-suppression
2+
# ------------------------------------
3+
# Copyright (c) Microsoft Corporation.
4+
# Licensed under the MIT License.
5+
# ------------------------------------
6+
7+
"""
8+
DESCRIPTION:
9+
This sample demonstrates how to use agent operations with code interpreter from
10+
the Azure Agents service using a synchronous client.
11+
12+
USAGE:
13+
python sample_agents_code_interpreter.py
14+
15+
Before running the sample:
16+
17+
pip install azure-ai-projects azure-identity
18+
19+
Set these environment variables with your own values:
20+
1) PROJECT_ENDPOINT - The project endpoint, as found in the overview page of your
21+
Azure AI Foundry project.
22+
2) MODEL_DEPLOYMENT_NAME - The deployment name of the AI model, as found under the "Name" column in
23+
the "Models + endpoints" tab in your Azure AI Foundry project.
24+
"""
25+
26+
# <imports>
27+
import os
28+
from dotenv import load_dotenv
29+
load_dotenv()
30+
from azure.ai.projects import AIProjectClient
31+
from azure.ai.agents.models import CodeInterpreterTool
32+
from azure.ai.agents.models import FilePurpose, MessageRole
33+
from azure.identity import DefaultAzureCredential
34+
from pathlib import Path
35+
# </imports>
36+
37+
# <client_initialization>
38+
endpoint = os.environ["PROJECT_ENDPOINT"]
39+
model_deployment_name = os.environ["MODEL_DEPLOYMENT_NAME"]
40+
41+
with AIProjectClient(
42+
endpoint=endpoint,
43+
credential=DefaultAzureCredential(exclude_interactive_browser_credential=False),
44+
) as project_client:
45+
# </client_initialization>
46+
47+
# Upload a file and wait for it to be processed
48+
# [START upload_file_and_create_agent_with_code_interpreter]
49+
# <file_upload>
50+
file = project_client.agents.upload_file_and_poll(
51+
file_path="nifty_500_quarterly_results.csv", purpose=FilePurpose.AGENTS
52+
)
53+
print(f"Uploaded file, file ID: {file.id}")
54+
# </file_upload>
55+
56+
# <code_interpreter_setup>
57+
code_interpreter = CodeInterpreterTool(file_ids=[file.id])
58+
# </code_interpreter_setup>
59+
60+
# <agent_creation>
61+
# Create agent with code interpreter tool and tools_resources
62+
agent = project_client.agents.create_agent(
63+
model=os.environ["MODEL_DEPLOYMENT_NAME"],
64+
name="my-assistant",
65+
instructions="You are helpful assistant",
66+
tools=code_interpreter.definitions,
67+
tool_resources=code_interpreter.resources,
68+
)
69+
# [END upload_file_and_create_agent_with_code_interpreter]
70+
print(f"Created agent, agent ID: {agent.id}")
71+
# </agent_creation>
72+
73+
# <thread_management>
74+
thread = project_client.agents.create_thread()
75+
print(f"Created thread, thread ID: {thread.id}")
76+
77+
# Create a message
78+
message = project_client.agents.create_message(
79+
thread_id=thread.id,
80+
role="user",
81+
content="Could you please create bar chart in TRANSPORTATION sector for the operating profit from the uploaded csv file and provide file to me?",
82+
)
83+
print(f"Created message, message ID: {message.id}")
84+
# </thread_management>
85+
86+
# <message_processing>
87+
run = project_client.agents.create_and_process_run(thread_id=thread.id, agent_id=agent.id)
88+
print(f"Run finished with status: {run.status}")
89+
90+
if run.status == "failed":
91+
# Check if you got "Rate limit is exceeded.", then you want to get more quota
92+
print(f"Run failed: {run.last_error}")
93+
# </message_processing>
94+
95+
# <file_handling>
96+
project_client.agents.delete_file(file.id)
97+
print("Deleted file")
98+
99+
# [START get_messages_and_save_files]
100+
messages = project_client.agents.list_messages(thread_id=thread.id)
101+
print(f"Messages: {messages}")
102+
103+
for image_content in messages.image_contents:
104+
file_id = image_content.image_file.file_id
105+
print(f"Image File ID: {file_id}")
106+
file_name = f"{file_id}_image_file.png"
107+
project_client.agents.save_file(file_id=file_id, file_name=file_name)
108+
print(f"Saved image file to: {Path.cwd() / file_name}")
109+
110+
for file_path_annotation in messages.file_path_annotations:
111+
print(f"File Paths:")
112+
print(f"Type: {file_path_annotation.type}")
113+
print(f"Text: {file_path_annotation.text}")
114+
print(f"File ID: {file_path_annotation.file_path.file_id}")
115+
print(f"Start Index: {file_path_annotation.start_index}")
116+
print(f"End Index: {file_path_annotation.end_index}")
117+
# [END get_messages_and_save_files]
118+
# </file_handling>
119+
120+
last_msg = messages.get_last_text_message_by_role(MessageRole.AGENT)
121+
if last_msg:
122+
print(f"Last Message: {last_msg.text.value}")
123+
124+
# <cleanup>
125+
project_client.agents.delete_agent(agent.id)
126+
print("Deleted agent")
127+
# </cleanup>

0 commit comments

Comments
 (0)