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 azure .ai .projects import AIProjectClient
29+ from azure .ai .agents .models import CodeInterpreterTool
30+ from azure .ai .agents .models import FilePurpose , MessageRole
31+ from azure .identity import DefaultAzureCredential
32+ from pathlib import Path
33+ # </imports>
34+
35+ # <client_initialization>
36+ endpoint = os .environ ["PROJECT_ENDPOINT" ]
37+ model_deployment_name = os .environ ["MODEL_DEPLOYMENT_NAME" ]
38+
39+ with AIProjectClient (
40+ endpoint = endpoint ,
41+ credential = DefaultAzureCredential (exclude_interactive_browser_credential = False ),
42+ ) as project_client :
43+ # </client_initialization>
44+
45+ # Upload a file and wait for it to be processed
46+ # [START upload_file_and_create_agent_with_code_interpreter]
47+ # <file_upload>
48+ file = project_client .agents .upload_file_and_poll (
49+ file_path = str (Path (__file__ ).parent / "nifty_500_quarterly_results.csv" ), purpose = FilePurpose .AGENTS
50+ )
51+ print (f"Uploaded file, file ID: { file .id } " )
52+ # </file_upload>
53+
54+ # <code_interpreter_setup>
55+ code_interpreter = CodeInterpreterTool (file_ids = [file .id ])
56+ # </code_interpreter_setup>
57+
58+ # <agent_creation>
59+ # Create agent with code interpreter tool and tools_resources
60+ agent = project_client .agents .create_agent (
61+ model = os .environ ["MODEL_DEPLOYMENT_NAME" ],
62+ name = "my-assistant" ,
63+ instructions = "You are helpful assistant" ,
64+ tools = code_interpreter .definitions ,
65+ tool_resources = code_interpreter .resources ,
66+ )
67+ # [END upload_file_and_create_agent_with_code_interpreter]
68+ print (f"Created agent, agent ID: { agent .id } " )
69+ # </agent_creation>
70+
71+ # <thread_management>
72+ thread = project_client .agents .create_thread ()
73+ print (f"Created thread, thread ID: { thread .id } " )
74+
75+ # Create a message
76+ message = project_client .agents .create_message (
77+ thread_id = thread .id ,
78+ role = "user" ,
79+ content = "Could you please create bar chart in TRANSPORTATION sector for the operating profit from the uploaded csv file and provide file to me?" ,
80+ )
81+ print (f"Created message, message ID: { message .id } " )
82+ # </thread_management>
83+
84+ # <message_processing>
85+ run = project_client .agents .create_and_process_run (thread_id = thread .id , agent_id = agent .id )
86+ print (f"Run finished with status: { run .status } " )
87+
88+ if run .status == "failed" :
89+ # Check if you got "Rate limit is exceeded.", then you want to get more quota
90+ print (f"Run failed: { run .last_error } " )
91+ # </message_processing>
92+
93+ # <file_handling>
94+ project_client .agents .delete_file (file .id )
95+ print ("Deleted file" )
96+
97+ # [START get_messages_and_save_files]
98+ messages = project_client .agents .list_messages (thread_id = thread .id )
99+ print (f"Messages: { messages } " )
100+
101+ for image_content in messages .image_contents :
102+ file_id = image_content .image_file .file_id
103+ print (f"Image File ID: { file_id } " )
104+ file_name = f"{ file_id } _image_file.png"
105+ project_client .agents .save_file (file_id = file_id , file_name = file_name )
106+ print (f"Saved image file to: { Path .cwd () / file_name } " )
107+
108+ for file_path_annotation in messages .file_path_annotations :
109+ print (f"File Paths:" )
110+ print (f"Type: { file_path_annotation .type } " )
111+ print (f"Text: { file_path_annotation .text } " )
112+ print (f"File ID: { file_path_annotation .file_path .file_id } " )
113+ print (f"Start Index: { file_path_annotation .start_index } " )
114+ print (f"End Index: { file_path_annotation .end_index } " )
115+ # [END get_messages_and_save_files]
116+ # </file_handling>
117+
118+ last_msg = messages .get_last_text_message_by_role (MessageRole .AGENT )
119+ if last_msg :
120+ print (f"Last Message: { last_msg .text .value } " )
121+
122+ # <cleanup>
123+ project_client .agents .delete_agent (agent .id )
124+ print ("Deleted agent" )
125+ # </cleanup>
0 commit comments