@@ -5,7 +5,7 @@ description: Find samples to ground Azure AI Foundry Agents using Custom Bing Se
5
5
author : aahill
6
6
ms.author : aahi
7
7
manager : nitinme
8
- ms.date : 04/15 /2025
8
+ ms.date : 07/09 /2025
9
9
ms.service : azure-ai-agent-service
10
10
ms.topic : how-to
11
11
ms.custom :
@@ -36,7 +36,6 @@ zone_pivot_groups: selection-bing-custom-grounding
36
36
37
37
::: zone-end
38
38
39
- <!--
40
39
::: zone pivot="python"
41
40
42
41
## Create a project client
@@ -46,8 +45,8 @@ Create a client object, which will contain the connection string for connecting
46
45
``` python
47
46
import os
48
47
from azure.ai.projects import AIProjectClient
49
- from azure.ai.projects.models import MessageRole, BingCustomSearchTool
50
48
from azure.identity import DefaultAzureCredential
49
+ from azure.ai.agents.models import BingCustomSearchTool
51
50
52
51
53
52
# 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
56
55
57
56
# Create an AIProjectClient instance
58
57
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(),
62
60
)
63
61
```
64
62
@@ -73,16 +71,18 @@ conn_id = bing_custom_connection.id
73
71
74
72
print (conn_id)
75
73
76
- # Initialize agent bing custom search tool and add the connection id
74
+ # Initialize Bing Custom Search tool with connection id and instance name
77
75
bing_custom_tool = BingCustomSearchTool(connection_id = conn_id, instance_name = " <config_instance_name>" )
78
76
79
77
# Create agent with the bing custom search tool and process assistant run
80
78
with project_client:
81
- agent = project_client.agents.create_agent(
79
+ agents_client = project_client.agents
80
+
81
+ agent = agents_client.create_agent(
82
82
model = os.environ[" MODEL_DEPLOYMENT_NAME" ],
83
83
name = " my-agent" ,
84
84
instructions = " You are a helpful agent" ,
85
- tools=bing_custom_tool.definitions
85
+ tools = bing_custom_tool.definitions,
86
86
)
87
87
print (f " Created agent, ID: { agent.id} " )
88
88
```
@@ -91,13 +91,13 @@ with project_client:
91
91
92
92
``` python
93
93
# Create thread for communication
94
- thread = project_client.agents.create_thread ()
94
+ thread = agents_client.threads.create ()
95
95
print (f " Created thread, ID: { thread.id} " )
96
96
97
97
# Create message to thread
98
- message = project_client.agents.create_message (
98
+ message = agents_client.messages.create (
99
99
thread_id = thread.id,
100
- role=MessageRole.USER ,
100
+ role = " user " ,
101
101
content = " How many medals did the USA win in the 2024 summer olympics?" ,
102
102
)
103
103
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
109
109
110
110
111
111
``` 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)
114
114
print (f " Run finished with status: { run.status} " )
115
115
116
116
if run.status == " failed" :
117
117
print (f " Run failed: { run.last_error} " )
118
118
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} ) " )
132
131
```
133
132
134
133
135
134
::: zone-end
136
- -->
137
135
138
136
<!--
139
137
::: zone pivot="csharp"
0 commit comments