Skip to content

Commit f591317

Browse files
committed
Migrating code-interpreter module to the samples folder.
1 parent 2a9c440 commit f591317

File tree

2 files changed

+160
-0
lines changed

2 files changed

+160
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# ------------------------------------
2+
# Copyright (c) Microsoft Corporation.
3+
# Licensed under the MIT License.
4+
# ------------------------------------
5+
6+
"""
7+
FILE: python-sample.py
8+
9+
DESCRIPTION:
10+
This sample demonstrates how to use agent operations with code interpreter from
11+
the Azure Agents service using a synchronous client.
12+
13+
USAGE:
14+
python python-sample.py
15+
16+
Before running the sample:
17+
18+
pip install azure-ai-projects azure-identity
19+
20+
Set this environment variables with your own values:
21+
PROJECT_CONNECTION_STRING - the Azure AI Project connection string, as found in your AI Studio Project.
22+
"""
23+
24+
import os
25+
from azure.ai.projects import AIProjectClient
26+
from azure.ai.projects.models import CodeInterpreterTool
27+
from azure.ai.projects.models import FilePurpose
28+
from azure.identity import DefaultAzureCredential
29+
from pathlib import Path
30+
31+
# Create an Azure AI Client from a connection string, copied from your AI Studio project.
32+
# At the moment, it should be in the format "<HostName>;<AzureSubscriptionId>;<ResourceGroup>;<HubName>"
33+
# Customer needs to login to Azure subscription via Azure CLI and set the environment variables
34+
project_client = AIProjectClient.from_connection_string(
35+
credential=DefaultAzureCredential(), conn_str=os.environ["PROJECT_CONNECTION_STRING"]
36+
)
37+
38+
with project_client:
39+
40+
# upload a file and wait for it to be processed
41+
file = project_client.agents.upload_file_and_poll(
42+
file_path="../../data/nifty_500_quarterly_results.csv", purpose=FilePurpose.AGENTS
43+
)
44+
print(f"Uploaded file, file ID: {file.id}")
45+
46+
# instantiate the CodeInterpreterTool with the necessary files
47+
code_interpreter = CodeInterpreterTool(file_ids=[file.id])
48+
49+
# notice that CodeInterpreter must be enabled in the agent creation, otherwise the agent will not be able to see the file attachment
50+
agent = project_client.agents.create_agent(
51+
model="gpt-4o-mini",
52+
name="my-agent",
53+
instructions="You are helpful agent",
54+
tools=code_interpreter.definitions,
55+
tool_resources=code_interpreter.resources,
56+
)
57+
print(f"Created agent, agent ID: {agent.id}")
58+
59+
# create a thread
60+
thread = project_client.agents.create_thread()
61+
print(f"Created thread, thread ID: {thread.id}")
62+
63+
# create a message
64+
message = project_client.agents.create_message(
65+
thread_id=thread.id,
66+
role="user",
67+
content="Could you please create bar chart in the TRANSPORTATION sector for the operating profit from the uploaded csv file and provide file to me?",
68+
)
69+
print(f"Created message, message ID: {message.id}")
70+
71+
# create and execute a run
72+
run = project_client.agents.create_and_process_run(thread_id=thread.id, assistant_id=agent.id)
73+
print(f"Run finished with status: {run.status}")
74+
75+
if run.status == "failed":
76+
# Check if you got "Rate limit is exceeded.", then you want to get more quota
77+
print(f"Run failed: {run.last_error}")
78+
79+
# delete the original file from the agent to free up space (note: this does not delete your version of the file)
80+
project_client.agents.delete_file(file.id)
81+
print("Deleted file")
82+
83+
# print the messages from the agent
84+
messages = project_client.agents.get_messages(thread_id=thread.id)
85+
print(f"Messages: {messages}")
86+
87+
# get the most recent message from the assistant
88+
last_msg = messages.get_last_text_message_by_sender("assistant")
89+
if last_msg:
90+
print(f"Last Message: {last_msg.text.value}")
91+
92+
# save the newly created file
93+
for image_content in messages.image_contents:
94+
print(f"Image File ID: {image_content.image_file.file_id}")
95+
file_name = f"{image_content.image_file.file_id}_image_file.png"
96+
project_client.agents.save_file(file_id=image_content.image_file.file_id, file_name=file_name)
97+
print(f"Saved image file to: {Path.cwd() / file_name}")
98+
99+
# iterates through file_path_annotations in messages and prints details for each annotation
100+
for file_path_annotation in messages.file_path_annotations:
101+
print(f"File Paths:")
102+
print(f"Type: {file_path_annotation.type}")
103+
print(f"Text: {file_path_annotation.text}")
104+
print(f"File ID: {file_path_annotation.file_path.file_id}")
105+
print(f"Start Index: {file_path_annotation.start_index}")
106+
print(f"End Index: {file_path_annotation.end_index}")
107+
108+
# delete the agent once done
109+
project_client.agents.delete_agent(agent.id)
110+
print("Deleted agent")
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Code interpreter
2+
3+
Code Interpreter allows the Agent to write and run Python code in a sandboxed execution environment. With Code Interpreter enabled, your Agent can run code iteratively to solve more challenging code, math, and data analysis problems.
4+
5+
6+
## Examples
7+
8+
Run the code samples below and view the output.
9+
10+
>[!NOTE]
11+
> Be sure that you've [installed the SDK](../../quickstart.md#install-the-sdk-package) for your language.
12+
13+
* [Python](./python-code-interpreter.py)
14+
15+
# Additional samples
16+
17+
* [Math tutor](https://github.com/openai/openai-cookbook/blob/main/examples/data/oai_docs/tool-code-interpreter.txt)
18+
19+
20+
## Supported file types
21+
22+
|File format|MIME Type|
23+
|---|---|
24+
|.c| text/x-c |
25+
|.cpp|text/x-c++ |
26+
|.csv|application/csv|
27+
|.docx|application/vnd.openxmlformats-officedocument.wordprocessingml.document|
28+
|.html|text/html|
29+
|.java|text/x-java|
30+
|.json|application/json|
31+
|.md|text/markdown|
32+
|.pdf|application/pdf|
33+
|.php|text/x-php|
34+
|.pptx|application/vnd.openxmlformats-officedocument.presentationml.presentation|
35+
|.py|text/x-python|
36+
|.py|text/x-script.python|
37+
|.rb|text/x-ruby|
38+
|.tex|text/x-tex|
39+
|.txt|text/plain|
40+
|.css|text/css|
41+
|.jpeg|image/jpeg|
42+
|.jpg|image/jpeg|
43+
|.js|text/javascript|
44+
|.gif|image/gif|
45+
|.png|image/png|
46+
|.tar|application/x-tar|
47+
|.ts|application/typescript|
48+
|.xlsx|application/vnd.openxmlformats-officedocument.spreadsheetml.sheet|
49+
|.xml|application/xml or "text/xml"|
50+
|.zip|application/zip|

0 commit comments

Comments
 (0)