Skip to content

Commit 1ee29fd

Browse files
committed
updating custom search tool
1 parent 0325feb commit 1ee29fd

File tree

2 files changed

+28
-28
lines changed

2 files changed

+28
-28
lines changed

articles/ai-foundry/agents/how-to/tools/bing-custom-search-samples.md

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: Find samples to ground Azure AI Foundry Agents using Custom Bing Se
55
author: aahill
66
ms.author: aahi
77
manager: nitinme
8-
ms.date: 04/15/2025
8+
ms.date: 07/09/2025
99
ms.service: azure-ai-agent-service
1010
ms.topic: how-to
1111
ms.custom:
@@ -36,7 +36,6 @@ zone_pivot_groups: selection-bing-custom-grounding
3636

3737
:::zone-end
3838

39-
<!--
4039
::: zone pivot="python"
4140

4241
## Create a project client
@@ -46,8 +45,8 @@ Create a client object, which will contain the connection string for connecting
4645
```python
4746
import os
4847
from azure.ai.projects import AIProjectClient
49-
from azure.ai.projects.models import MessageRole, BingCustomSearchTool
5048
from azure.identity import DefaultAzureCredential
49+
from azure.ai.agents.models import BingCustomSearchTool
5150

5251

5352
# Create an Azure AI Client from an endpoint, copied from your Azure AI Foundry project.
@@ -56,9 +55,8 @@ project_endpoint = os.environ["PROJECT_ENDPOINT"] # Ensure the PROJECT_ENDPOINT
5655

5756
# Create an AIProjectClient instance
5857
project_client = AIProjectClient(
59-
endpoint=project_endpoint,
60-
credential=DefaultAzureCredential(), # Use Azure Default Credential for authentication
61-
api_version="latest",
58+
endpoint=os.environ["PROJECT_ENDPOINT"],
59+
credential=DefaultAzureCredential(),
6260
)
6361
```
6462

@@ -73,16 +71,18 @@ conn_id = bing_custom_connection.id
7371

7472
print(conn_id)
7573

76-
# Initialize agent bing custom search tool and add the connection id
74+
# Initialize Bing Custom Search tool with connection id and instance name
7775
bing_custom_tool = BingCustomSearchTool(connection_id=conn_id, instance_name="<config_instance_name>")
7876

7977
# Create agent with the bing custom search tool and process assistant run
8078
with project_client:
81-
agent = project_client.agents.create_agent(
79+
agents_client = project_client.agents
80+
81+
agent = agents_client.create_agent(
8282
model=os.environ["MODEL_DEPLOYMENT_NAME"],
8383
name="my-agent",
8484
instructions="You are a helpful agent",
85-
tools=bing_custom_tool.definitions
85+
tools=bing_custom_tool.definitions,
8686
)
8787
print(f"Created agent, ID: {agent.id}")
8888
```
@@ -91,13 +91,13 @@ with project_client:
9191

9292
```python
9393
# Create thread for communication
94-
thread = project_client.agents.create_thread()
94+
thread = agents_client.threads.create()
9595
print(f"Created thread, ID: {thread.id}")
9696

9797
# Create message to thread
98-
message = project_client.agents.create_message(
98+
message = agents_client.messages.create(
9999
thread_id=thread.id,
100-
role=MessageRole.USER,
100+
role="user",
101101
content="How many medals did the USA win in the 2024 summer olympics?",
102102
)
103103
print(f"Created message, ID: {message.id}")
@@ -109,31 +109,29 @@ Create a run and observe that the model uses the Grounding with Bing Search tool
109109

110110

111111
```python
112-
# Create and process agent run in thread with tools
113-
run = project_client.agents.create_and_process_run(thread_id=thread.id, agent_id=agent.id)
112+
# Create and process Agent run in thread with tools
113+
run = agents_client.runs.create_and_process(thread_id=thread.id, agent_id=agent.id)
114114
print(f"Run finished with status: {run.status}")
115115

116116
if run.status == "failed":
117117
print(f"Run failed: {run.last_error}")
118118

119-
# Delete the assistant when done
120-
project_client.agents.delete_agent(agent.id)
121-
print("Deleted agent")
122-
123-
# Print the Agent's response message with optional citation
124-
response_message = project_client.agents.list_messages(thread_id=thread.id).get_last_message_by_role(
125-
MessageRole.AGENT
126-
)
127-
if response_message:
128-
for text_message in response_message.text_messages:
129-
print(f"Agent response: {text_message.text.value}")
130-
for annotation in response_message.url_citation_annotations:
131-
print(f"URL Citation: [{annotation.url_citation.title}]({annotation.url_citation.url})")
119+
# Uncomment these lines to delete the Agent when done
120+
#agents_client.delete_agent(agent.id)
121+
#print("Deleted agent")
122+
123+
# Fetch and log all messages
124+
messages = agents_client.messages.list(thread_id=thread.id)
125+
for msg in messages:
126+
if msg.text_messages:
127+
for text_message in msg.text_messages:
128+
print(f"Agent response: {text_message.text.value}")
129+
for annotation in msg.url_citation_annotations:
130+
print(f"URL Citation: [{annotation.url_citation.title}]({annotation.url_citation.url})")
132131
```
133132

134133

135134
:::zone-end
136-
-->
137135

138136
<!--
139137
::: zone pivot="csharp"

zone-pivots/zone-pivot-groups.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,6 +1175,8 @@ groups:
11751175
pivots:
11761176
- id: portal
11771177
title: Azure AI Foundry portal
1178+
- id: python
1179+
title: Python
11781180
- id: rest
11791181
title: REST API
11801182
- id: selection-openapi-function

0 commit comments

Comments
 (0)