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 Agent operations with the Bing Custom Search tool from10 the Azure Agents service using a synchronous client.11 For more information on the Bing Custom Search tool, see: https://aka.ms/AgentCustomSearchDoc12 13USAGE:14 python sample_agents_bing_custom_search.py15 16 Before running the sample:17 18 pip install azure-identity19 pip install --pre azure-ai-projects20 21 Set this environment variables with your own values:22 1) PROJECT_ENDPOINT - The Azure AI Project endpoint, as found in the Overview23 page of your Azure AI Foundry portal.24 2) MODEL_DEPLOYMENT_NAME - The deployment name of the AI model, as found under the "Name" column in25 the "Models + endpoints" tab in your Azure AI Foundry project.26 3) BING_CUSTOM_CONNECTION_NAME - The name of a connection to the custom search Bing resource as it is27 listed in Azure AI Foundry connected resources.28 4) BING_CONFIGURATION_NAME - the name of a search configuration in Grounding with Bing Custom Search29 resource.30"""31 32import os33from azure.ai.projects import AIProjectClient34from azure.identity import DefaultAzureCredential35from azure.ai.agents.models import BingCustomSearchTool, ListSortOrder36 37project_client = AIProjectClient(38 endpoint=os.environ["PROJECT_ENDPOINT"],39 credential=DefaultAzureCredential(),40)41 42conn_id = project_client.connections.get(os.environ["BING_CUSTOM_CONNECTION_NAME"]).id43 44# Initialize Bing Custom Search tool with connection id and instance name45bing_custom_tool = BingCustomSearchTool(connection_id=conn_id, instance_name=os.environ["BING_CONFIGURATION_NAME"])46 47# Create Agent with the Bing Custom Search tool and process Agent run48with project_client:49 agents_client = project_client.agents50 51 agent = agents_client.create_agent(52 model=os.environ["MODEL_DEPLOYMENT_NAME"],53 name="my-agent",54 instructions="You are a helpful agent",55 tools=bing_custom_tool.definitions,56 )57 print(f"Created agent, ID: {agent.id}")58 59 # Create thread for communication60 thread = agents_client.threads.create()61 print(f"Created thread, ID: {thread.id}")62 63 # Create message to thread64 message = agents_client.messages.create(65 thread_id=thread.id,66 role="user",67 content="How many medals did the USA win in the 2024 summer olympics?",68 )69 print(f"Created message, ID: {message.id}")70 71 # Create and process Agent run in thread with tools72 run = agents_client.runs.create_and_process(thread_id=thread.id, agent_id=agent.id)73 print(f"Run finished with status: {run.status}")74 75 if run.status == "failed":76 print(f"Run failed: {run.last_error}")77 78 # Delete the Agent when done79 agents_client.delete_agent(agent.id)80 print("Deleted agent")81 82 # Fetch and log all messages83 messages = agents_client.messages.list(thread_id=thread.id, order=ListSortOrder.ASCENDING)84 for msg in messages:85 if msg.text_messages:86 responses = []87 for text_message in msg.text_messages:88 responses.append(text_message.text.value)89 message = " ".join(responses)90 for annotation in msg.url_citation_annotations:91 message = message.replace(92 annotation.text, f" [{annotation.url_citation.title}]({annotation.url_citation.url})"93 )94 print(f"{msg.role}: {message}")
Ecosystems, libraries, and foundations to build on. Orchestration frameworks, agent platforms, and development foundations.