1# pylint: disable=line-too-long,useless-suppression2# ------------------------------------3# Copyright (c) Microsoft Corporation.4# Licensed under the MIT License.5# ------------------------------------6"""7DESCRIPTION:8 This sample demonstrates how to use agent operations with code interpreter from9 the Azure Agents service using a synchronous client.10 11USAGE:12 python sample_agents_code_interpreter_attachment_enterprise_search.py13 14 Before running the sample:15 16 pip install azure-ai-projects azure-ai-agents azure-identity17 18 Set these environment variables with your own values:19 1) PROJECT_ENDPOINT - The Azure AI Project endpoint, as found in the Overview20 page of your Azure AI Foundry portal.21 2) MODEL_DEPLOYMENT_NAME - The deployment name of the AI model, as found under the "Name" column in22 the "Models + endpoints" tab in your Azure AI Foundry project.23 3) AZURE_BLOB_URI - The URI of the blob storage where the file is uploaded. In the format:24 azureml://subscriptions/{subscription-id}/resourcegroups/{resource-group-name}/workspaces/{workspace-name}/datastores/{datastore-name}/paths/{path-to-file}25"""26 27import os28from azure.ai.projects import AIProjectClient29from azure.ai.agents.models import (30 CodeInterpreterTool,31 MessageAttachment,32 MessageRole,33 VectorStoreDataSource,34 VectorStoreDataSourceAssetType,35)36from azure.identity import DefaultAzureCredential37 38project_client = AIProjectClient(39 endpoint=os.environ["PROJECT_ENDPOINT"],40 credential=DefaultAzureCredential(),41)42 43with project_client:44 agents_client = project_client.agents45 46 code_interpreter = CodeInterpreterTool()47 48 # notice that CodeInterpreter must be enabled in the agent creation, otherwise the agent will not be able to see the file attachment49 agent = agents_client.create_agent(50 model=os.environ["MODEL_DEPLOYMENT_NAME"],51 name="my-agent",52 instructions="You are helpful agent",53 tools=code_interpreter.definitions,54 )55 print(f"Created agent, agent ID: {agent.id}")56 57 thread = agents_client.threads.create()58 print(f"Created thread, thread ID: {thread.id}")59 60 # [START upload_file_and_create_message_with_code_interpreter]61 # We will upload the local file to Azure and will use it for vector store creation.62 asset_uri = os.environ["AZURE_BLOB_URI"]63 ds = VectorStoreDataSource(asset_identifier=asset_uri, asset_type=VectorStoreDataSourceAssetType.URI_ASSET)64 65 # Create a message with the attachment66 attachment = MessageAttachment(data_source=ds, tools=code_interpreter.definitions)67 message = agents_client.messages.create(68 thread_id=thread.id, role="user", content="What does the attachment say?", attachments=[attachment]69 )70 # [END upload_file_and_create_message_with_code_interpreter]71 72 print(f"Created message, message ID: {message.id}")73 74 run = agents_client.runs.create_and_process(thread_id=thread.id, agent_id=agent.id)75 print(f"Run finished with status: {run.status}")76 77 if run.status == "failed":78 # Check if you got "Rate limit is exceeded.", then you want to get more quota79 print(f"Run failed: {run.last_error}")80 81 agents_client.delete_agent(agent.id)82 print("Deleted agent")83 84 last_msg = agents_client.messages.get_last_message_text_by_role(thread_id=thread.id, role=MessageRole.AGENT)85 if last_msg:86 print(f"Last Message: {last_msg.text.value}")
Ecosystems, libraries, and foundations to build on. Orchestration frameworks, agent platforms, and development foundations.