Azure AI Agent service Code Interpreter
Code Interpreter allows the agents 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. When your Agent writes code that fails to run, it can iterate on this code by modifying and running different code until the code execution succeeds.
Important
Code Interpreter has additional charges beyond the token based fees for Azure OpenAI usage. If your Agent calls Code Interpreter simultaneously in two different threads, two code interpreter sessions are created. Each session is active by default for one hour.
Supported models
The models page contains the most up-to-date information on regions/models where agents and code interpreter are supported.
We recommend using Agents with the latest models to take advantage of the new features, larger context windows, and more up-to-date training data.
Define imports and create a project client
To use code interpreter, first add the import
statements shown in the example, and create a project client, which will contain a connection string to your AI project, and will be used to authenticate API calls.
import os
from azure.ai.projects import AIProjectClient
from azure.ai.projects.models import CodeInterpreterTool
from azure.ai.projects.models import FilePurpose
from azure.identity import DefaultAzureCredential
from pathlib import Path
# Create an Azure AI Client from a connection string, copied from your AI Foundry project.
# At the moment, it should be in the format "<HostName>;<AzureSubscriptionId>;<ResourceGroup>;<HubName>"
# Customer needs to login to Azure subscription via Azure CLI and set the environment variables
project_client = AIProjectClient.from_connection_string(
credential=DefaultAzureCredential(), conn_str=os.environ["PROJECT_CONNECTION_STRING"]
)
Upload a file
Upload the file using the upload_and_poll()
function, specifying the file path and the FilePurpose.AGENTS
purpose.
# Upload a file and add it to the client
file = project_client.agents.upload_file_and_poll(
file_path="nifty_500_quarterly_results.csv", purpose=FilePurpose.AGENTS
)
print(f"Uploaded file, file ID: {file.id}")
Create an agent with the code interpreter tool
Define the code_interpreter
tool with CodeInterpreterTool()
and include the file ID of the file you uploaded. Afterwards, create the agent with tools
set to code_interpreter.definitions
and tool_resources
set to code_interpreter.resources
.
code_interpreter = CodeInterpreterTool(file_ids=[file.id])
# create agent with code interpreter tool and tools_resources
agent = project_client.agents.create_agent(
model="gpt-4o-mini",
name="my-agent",
instructions="You are helpful agent",
tools=code_interpreter.definitions,
tool_resources=code_interpreter.resources,
)
Create a thread, message, and get the agent response
Next create a thread with create_thread()
and attach a message to it using create_message()
that will trigger the code interpreter tool. Afterwards, create and execute a run with create_and_process_run()
. Once the run finishes, you can delete the file from the agent with delete_file()
to free up space in the agent. Finally, print the messages from the agent.
# create a thread
thread = project_client.agents.create_thread()
print(f"Created thread, thread ID: {thread.id}")
# create a message
message = project_client.agents.create_message(
thread_id=thread.id,
role="user",
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?",
)
print(f"Created message, message ID: {message.id}")
# create and execute a run
run = project_client.agents.create_and_process_run(thread_id=thread.id, assistant_id=agent.id)
print(f"Run finished with status: {run.status}")
if run.status == "failed":
# Check if you got "Rate limit is exceeded.", then you want to get more quota
print(f"Run failed: {run.last_error}")
# delete the original file from the agent to free up space (note: this does not delete your version of the file)
project_client.agents.delete_file(file.id)
print("Deleted file")
# print the messages from the agent
messages = project_client.agents.list_messages(thread_id=thread.id)
print(f"Messages: {messages}")
# get the most recent message from the assistant
last_msg = messages.get_last_text_message_by_sender("assistant")
if last_msg:
print(f"Last Message: {last_msg.text.value}")
Download files generated by code interpreter
Files generated by Code Interpreter can be found in the Agent message responses. You can download image file generated by code interpreter, by iterating through the response's image_contents
and calling save_file()
with a name and the file ID.
# save the newly created file
for image_content in messages.image_contents:
print(f"Image File ID: {image_content.image_file.file_id}")
file_name = f"{image_content.image_file.file_id}_image_file.png"
project_client.agents.save_file(file_id=image_content.image_file.file_id, file_name=file_name)
print(f"Saved image file to: {Path.cwd() / file_name}")
Supported file types
File format | MIME Type |
---|---|
.c |
text/x-c |
.cpp |
text/x-c++ |
.csv |
application/csv |
.docx |
application/vnd.openxmlformats-officedocument.wordprocessingml.document |
.html |
text/html |
.java |
text/x-java |
.json |
application/json |
.md |
text/markdown |
.pdf |
application/pdf |
.php |
text/x-php |
.pptx |
application/vnd.openxmlformats-officedocument.presentationml.presentation |
.py |
text/x-python |
.py |
text/x-script.python |
.rb |
text/x-ruby |
.tex |
text/x-tex |
.txt |
text/plain |
.css |
text/css |
.jpeg |
image/jpeg |
.jpg |
image/jpeg |
.js |
text/javascript |
.gif |
image/gif |
.png |
image/png |
.tar |
application/x-tar |
.ts |
application/typescript |
.xlsx |
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet |
.xml |
application/xml or text/xml |
.zip |
application/zip |
See also
- Learn more about agents.