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" )
0 commit comments