1# pylint: disable=line-too-long,useless-suppression
2# ------------------------------------
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 from
10 the Azure Agents service using a synchronous client.
11 For more information on the Bing Custom Search tool, see: https://aka.ms/AgentCustomSearchDoc
12
13USAGE:
14 python sample_agents_bing_custom_search.py
15
16 Before running the sample:
17
18 pip install azure-identity
19 pip install --pre azure-ai-projects
20
21 Set this environment variables with your own values:
22 1) PROJECT_ENDPOINT - The Azure AI Project endpoint, as found in the Overview
23 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 in
25 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 is
27 listed in Azure AI Foundry connected resources.
28 4) BING_CONFIGURATION_NAME - the name of a search configuration in Grounding with Bing Custom Search
29 resource.
30"""
31
32import os
33from azure.ai.projects import AIProjectClient
34from azure.identity import DefaultAzureCredential
35from azure.ai.agents.models import BingCustomSearchTool, ListSortOrder
36
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"]).id
43
44# Initialize Bing Custom Search tool with connection id and instance name
45bing_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 run
48with project_client:
49 agents_client = project_client.agents
50
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 communication
60 thread = agents_client.threads.create()
61 print(f"Created thread, ID: {thread.id}")
62
63 # Create message to thread
64 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 tools
72 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 done
79 agents_client.delete_agent(agent.id)
80 print("Deleted agent")
81
82 # Fetch and log all messages
83 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}")