1# pylint: disable=line-too-long,useless-suppression2# ------------------------------------3# Copyright (c) Microsoft Corporation.4# Licensed under the MIT License.5# ------------------------------------6 7"""8DESCRIPTION:9 This sample demonstrates how to use azure function agent operations from10 the Azure Agents service using a synchronous client.11 12USAGE:13 python sample_agents_azure_functions.py14 15 Before running the sample:16 17 pip install azure-ai-projects azure-ai-agents azure-identity18 19 Set these environment variables with your own values:20 1) PROJECT_ENDPOINT - The Azure AI Project endpoint, as found in the Overview21 page of your Azure AI Foundry portal.22 2) MODEL_DEPLOYMENT_NAME - The deployment name of the AI model, as found under the "Name" column in23 the "Models + endpoints" tab in your Azure AI Foundry project.24 3) STORAGE_SERVICE_ENDPOINT - the storage service queue endpoint, triggering Azure function.25 26 Please see Getting Started with Azure Functions page for more information on Azure Functions:27 https://learn.microsoft.com/azure/azure-functions/functions-get-started28 **Note:** The Azure Function may be only used in standard agent setup. Please follow the instruction on the web page29 https://github.com/azure-ai-foundry/foundry-samples/tree/main/samples/microsoft/infrastructure-setup/41-standard-agent-setup30 to deploy an agent, capable of calling Azure Functions.31"""32 33import os34from azure.ai.projects import AIProjectClient35from azure.ai.agents.models import AzureFunctionStorageQueue, AzureFunctionTool, MessageRole36from 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 # [START create_agent_with_azure_function_tool]47 storage_service_endpoint = os.environ["STORAGE_SERVICE_ENDPONT"]48 49 azure_function_tool = AzureFunctionTool(50 name="foo",51 description="Get answers from the foo bot.",52 parameters={53 "type": "object",54 "properties": {55 "query": {"type": "string", "description": "The question to ask."},56 "outputqueueuri": {"type": "string", "description": "The full output queue uri."},57 },58 },59 input_queue=AzureFunctionStorageQueue(60 queue_name="azure-function-foo-input",61 storage_service_endpoint=storage_service_endpoint,62 ),63 output_queue=AzureFunctionStorageQueue(64 queue_name="azure-function-tool-output",65 storage_service_endpoint=storage_service_endpoint,66 ),67 )68 69 agent = agents_client.create_agent(70 model=os.environ["MODEL_DEPLOYMENT_NAME"],71 name="azure-function-agent-foo",72 instructions=f"You are a helpful support agent. Use the provided function any time the prompt contains the string 'What would foo say?'. When you invoke the function, ALWAYS specify the output queue uri parameter as '{storage_service_endpoint}/azure-function-tool-output'. Always responds with \"Foo says\" and then the response from the tool.",73 tools=azure_function_tool.definitions,74 )75 print(f"Created agent, agent ID: {agent.id}")76 # [END create_agent_with_azure_function_tool]77 78 # Create a thread79 thread = agents_client.threads.create()80 print(f"Created thread, thread ID: {thread.id}")81 82 # Create a message83 message = agents_client.messages.create(84 thread_id=thread.id,85 role="user",86 content="What is the most prevalent element in the universe? What would foo say?",87 )88 print(f"Created message, message ID: {message.id}")89 90 run = agents_client.runs.create_and_process(thread_id=thread.id, agent_id=agent.id)91 if run.status == "failed":92 print(f"Run failed: {run.last_error}")93 94 # Get messages from the thread95 messages = agents_client.messages.list(thread_id=thread.id)96 97 # Get the last message from agent98 last_msg = agents_client.messages.get_last_message_text_by_role(thread_id=thread.id, role=MessageRole.AGENT)99 if last_msg:100 print(f"Last Message: {last_msg.text.value}")101 102 # Delete the agent once done103 agents_client.delete_agent(agent.id)
Developer SDKs, repos, connectors, and infrastructure helpers. APIs, development tools, and integration resources.