Skip to content
Merged
113 changes: 113 additions & 0 deletions scenarios/Agents/samples/code-interpreter/python-code-interpreter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------


"""
FILE: python-sample.py

DESCRIPTION:
This sample demonstrates how to use agent operations with code interpreter from
the Azure Agents service using a synchronous client.

USAGE:
python python-sample.py

Before running the sample:

pip install azure-ai-projects azure-identity

Set this environment variables with your own values:
PROJECT_CONNECTION_STRING - the Azure AI Project connection string, as found in your AI Studio Project.
"""

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 Studio 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"]
)

with project_client:
# upload a file and wait for it to be processed
file = project_client.agents.upload_file_and_poll(
file_path=os.environ["FILE_PATH"],
purpose=FilePurpose.AGENTS
# File path = "../../data/nifty_500_quarterly_results.csv", purpose=FilePurpose.AGENTS
)
print(f"Uploaded file, file ID: {file.id}")

# instantiate the CodeInterpreterTool with the necessary files
code_interpreter = CodeInterpreterTool(file_ids=[file.id])

# notice that CodeInterpreter must be enabled in the agent creation, otherwise the agent will not be able to see the file attachment
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,
)
print(f"Created agent, agent ID: {agent.id}")

# 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.get_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}")

# 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}")

# iterates through file_path_annotations in messages and prints details for each annotation
for file_path_annotation in messages.file_path_annotations:
print("File Paths:")
print(f"Type: {file_path_annotation.type}")
print(f"Text: {file_path_annotation.text}")
print(f"File ID: {file_path_annotation.file_path.file_id}")
print(f"Start Index: {file_path_annotation.start_index}")
print(f"End Index: {file_path_annotation.end_index}")

# delete the agent once done
project_client.agents.delete_agent(agent.id)
print("Deleted agent")
50 changes: 50 additions & 0 deletions scenarios/Agents/samples/code-interpreter/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Code interpreter

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.


## Examples

Run the code samples below and view the output.

>[!NOTE]
> Be sure that you've [installed the SDK](../README.md#install-the-sdk-package) for your language.

* [Python](./python-code-interpreter.py)

# Additional samples

* [Math tutor](https://github.com/openai/openai-cookbook/blob/main/examples/data/oai_docs/tool-code-interpreter.txt)


## 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|
Loading