Skip to content

Commit ed09ad7

Browse files
author
Jill Grant
authored
Merge pull request #1832 from aahill/agents
updating tool docs
2 parents ca91da9 + cca8df2 commit ed09ad7

File tree

4 files changed

+617
-524
lines changed

4 files changed

+617
-524
lines changed

articles/ai-services/agents/how-to/tools/bing-grounding.md

Lines changed: 149 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -112,99 +112,13 @@ Your use of Grounding with Bing Search will be governed by the Terms of Use. By
112112

113113
::: zone-end
114114

115-
::: zone pivot="csharp-example"
115+
::: zone pivot="code-example"
116116

117-
```csharp
118-
using System;
119-
using System.Collections.Generic;
120-
using System.Threading.Tasks;
121-
using Azure.Core;
122-
using Azure.Core.TestFramework;
123-
using NUnit.Framework;
124-
125-
namespace Azure.AI.Projects.Tests;
126-
127-
public partial class Sample_Agent_Bing_Grounding : SamplesBase<AIProjectsTestEnvironment>
128-
{
129-
[Test]
130-
public async Task BingGroundingExample()
131-
{
132-
var connectionString = TestEnvironment.AzureAICONNECTIONSTRING;
133-
134-
var clientOptions = new AIProjectClientOptions();
135-
136-
// Adding the custom headers policy
137-
clientOptions.AddPolicy(new CustomHeadersPolicy(), HttpPipelinePosition.PerCall);
138-
var projectClient = new AIProjectClient(connectionString, new DefaultAzureCredential(), clientOptions);
139-
140-
GetConnectionResponse bingConnection = await projectClient.GetConnectionsClient().GetConnectionAsync(TestEnvironment.BINGCONNECTIONNAME);
141-
var connectionId = bingConnection.Id;
142-
143-
AgentsClient agentClient = projectClient.GetAgentsClient();
144-
145-
ToolConnectionList connectionList = new ToolConnectionList
146-
{
147-
ConnectionList = { new ToolConnection(connectionId) }
148-
};
149-
BingGroundingToolDefinition bingGroundingTool = new BingGroundingToolDefinition(connectionList);
150-
151-
Response<Agent> agentResponse = await agentClient.CreateAgentAsync(
152-
model: "gpt-4-1106-preview",
153-
name: "my-assistant",
154-
instructions: "You are a helpful assistant.",
155-
tools: new List<ToolDefinition> { bingGroundingTool });
156-
Agent agent = agentResponse.Value;
157-
158-
// Create thread for communication
159-
Response<AgentThread> threadResponse = await agentClient.CreateThreadAsync();
160-
AgentThread thread = threadResponse.Value;
161-
162-
// Create message to thread
163-
Response<ThreadMessage> messageResponse = await agentClient.CreateMessageAsync(
164-
thread.Id,
165-
MessageRole.User,
166-
"How does wikipedia explain Euler's Identity?");
167-
ThreadMessage message = messageResponse.Value;
168-
169-
// Run the agent
170-
Response<ThreadRun> runResponse = await agentClient.CreateRunAsync(thread, agent);
171-
172-
do
173-
{
174-
await Task.Delay(TimeSpan.FromMilliseconds(500));
175-
runResponse = await agentClient.GetRunAsync(thread.Id, runResponse.Value.Id);
176-
}
177-
while (runResponse.Value.Status == RunStatus.Queued
178-
|| runResponse.Value.Status == RunStatus.InProgress);
179-
180-
Response<PageableList<ThreadMessage>> afterRunMessagesResponse
181-
= await agentClient.GetMessagesAsync(thread.Id);
182-
IReadOnlyList<ThreadMessage> messages = afterRunMessagesResponse.Value.Data;
183-
184-
// Note: messages iterate from newest to oldest, with the messages[0] being the most recent
185-
foreach (ThreadMessage threadMessage in messages)
186-
{
187-
Console.Write($"{threadMessage.CreatedAt:yyyy-MM-dd HH:mm:ss} - {threadMessage.Role,10}: ");
188-
foreach (MessageContent contentItem in threadMessage.ContentItems)
189-
{
190-
if (contentItem is MessageTextContent textItem)
191-
{
192-
Console.Write(textItem.Text);
193-
}
194-
else if (contentItem is MessageImageFileContent imageFileItem)
195-
{
196-
Console.Write($"<image from ID: {imageFileItem.FileId}");
197-
}
198-
Console.WriteLine();
199-
}
200-
}
201-
}
202-
}
203-
```
204-
::: zone-end
117+
## Step 1: Create an agent with bing grounding
205118

206-
::: zone pivot="python-example"
119+
Create a client object, which will contain the connection string for connecting to your AI project and other resources.
207120

121+
# [Python](#tab/python)
208122

209123
```python
210124
import os
@@ -213,15 +127,45 @@ from azure.identity import DefaultAzureCredential
213127
from azure.ai.projects.models import BingGroundingTool
214128

215129

216-
# Create an Azure AI Client from a connection string, copied from your Azure AI project.
130+
# Create an Azure AI Client from a connection string, copied from your AI Studio project.
217131
# At the moment, it should be in the format "<HostName>;<AzureSubscriptionId>;<ResourceGroup>;<HubName>"
218132
# Customer needs to login to Azure subscription via Azure CLI and set the environment variables
219133

220134
project_client = AIProjectClient.from_connection_string(
221135
credential=DefaultAzureCredential(),
222136
conn_str=os.environ["PROJECT_CONNECTION_STRING"],
223137
)
138+
```
139+
140+
# [C#](#tab/csharp)
141+
142+
```csharp
143+
using System;
144+
using System.Collections.Generic;
145+
using System.Threading.Tasks;
146+
using Azure.Core;
147+
using Azure.Core.TestFramework;
148+
using NUnit.Framework;
149+
150+
var connectionString = TestEnvironment.AzureAICONNECTIONSTRING;
151+
152+
var clientOptions = new AIProjectClientOptions();
153+
154+
// Adding the custom headers policy
155+
clientOptions.AddPolicy(new CustomHeadersPolicy(), HttpPipelinePosition.PerCall);
156+
var projectClient = new AIProjectClient(connectionString, new DefaultAzureCredential(), clientOptions);
157+
158+
```
159+
160+
---
161+
162+
## Step 2: Enable the Bing search tool
163+
164+
To make the Bing search tool available to your agent, create a Bing connection to initialize the Bing search tool and attach it to the agent.
224165

166+
# [Python](#tab/python)
167+
168+
```python
225169
bing_connection = project_client.connections.get(
226170
connection_name=os.environ["BING_CONNECTION_NAME"]
227171
)
@@ -242,35 +186,128 @@ with project_client:
242186
headers={"x-ms-enable-preview": "true"}
243187
)
244188
print(f"Created agent, ID: {agent.id}")
189+
```
245190

246-
# Create thread for communication
247-
thread = project_client.agents.create_thread()
248-
print(f"Created thread, ID: {thread.id}")
191+
# [C#](#tab/csharp)
249192

250-
# Create message to thread
251-
message = project_client.agents.create_message(
252-
thread_id=thread.id,
253-
role="user",
254-
content="How is the weather in Seattle today?",
255-
)
256-
print(f"Created message, ID: {message.id}")
193+
```csharp
194+
GetConnectionResponse bingConnection = await projectClient.GetConnectionsClient().GetConnectionAsync(TestEnvironment.BINGCONNECTIONNAME);
195+
var connectionId = bingConnection.Id;
196+
197+
AgentsClient agentClient = projectClient.GetAgentsClient();
257198

258-
# Create and process agent run in thread with tools
259-
run = project_client.agents.create_and_process_run(thread_id=thread.id, assistant_id=agent.id)
260-
print(f"Run finished with status: {run.status}")
199+
ToolConnectionList connectionList = new ToolConnectionList
200+
{
201+
ConnectionList = { new ToolConnection(connectionId) }
202+
};
203+
BingGroundingToolDefinition bingGroundingTool = new BingGroundingToolDefinition(connectionList);
204+
205+
Response<Agent> agentResponse = await agentClient.CreateAgentAsync(
206+
model: "gpt-4o-mini",
207+
name: "my-assistant",
208+
instructions: "You are a helpful assistant.",
209+
tools: new List<ToolDefinition> { bingGroundingTool });
210+
Agent agent = agentResponse.Value;
211+
```
212+
---
261213

262-
if run.status == "failed":
263-
print(f"Run failed: {run.last_error}")
264214

265-
# Delete the assistant when done
266-
project_client.agents.delete_agent(agent.id)
267-
print("Deleted agent")
215+
## Step 3: Create a thread
268216

269-
# Fetch and log all messages
270-
messages = project_client.agents.list_messages(thread_id=thread.id)
271-
print(f"Messages: {messages}")
217+
# [Python](#tab/python)
218+
219+
```python
220+
# Create thread for communication
221+
thread = project_client.agents.create_thread()
222+
print(f"Created thread, ID: {thread.id}")
223+
224+
# Create message to thread
225+
message = project_client.agents.create_message(
226+
thread_id=thread.id,
227+
role="user",
228+
content="How is the weather in Seattle today?",
229+
)
230+
print(f"Created message, ID: {message.id}")
272231
```
273232

233+
# [C#](#tab/csharp)
234+
235+
```csharp
236+
// Create thread for communication
237+
Response<AgentThread> threadResponse = await agentClient.CreateThreadAsync();
238+
AgentThread thread = threadResponse.Value;
239+
240+
// Create message to thread
241+
Response<ThreadMessage> messageResponse = await agentClient.CreateMessageAsync(
242+
thread.Id,
243+
MessageRole.User,
244+
"How does wikipedia explain Euler's Identity?");
245+
ThreadMessage message = messageResponse.Value;
246+
```
247+
248+
---
249+
250+
## Step 4: Create a run and check the output
251+
252+
Create a run and observe that the model uses the file search tool to provide a response to the user's question.
253+
254+
# [Python](#tab/python)
255+
256+
```python
257+
# Create and process agent run in thread with tools
258+
run = project_client.agents.create_and_process_run(thread_id=thread.id, assistant_id=agent.id)
259+
print(f"Run finished with status: {run.status}")
260+
261+
if run.status == "failed":
262+
print(f"Run failed: {run.last_error}")
263+
264+
# Delete the assistant when done
265+
project_client.agents.delete_agent(agent.id)
266+
print("Deleted agent")
267+
268+
# Fetch and log all messages
269+
messages = project_client.agents.list_messages(thread_id=thread.id)
270+
print(f"Messages: {messages}")
271+
```
272+
273+
# [C#](#tab/csharp)
274+
275+
```csharp
276+
// Run the agent
277+
Response<ThreadRun> runResponse = await agentClient.CreateRunAsync(thread, agent);
278+
279+
do
280+
{
281+
await Task.Delay(TimeSpan.FromMilliseconds(500));
282+
runResponse = await agentClient.GetRunAsync(thread.Id, runResponse.Value.Id);
283+
}
284+
while (runResponse.Value.Status == RunStatus.Queued
285+
|| runResponse.Value.Status == RunStatus.InProgress);
286+
287+
Response<PageableList<ThreadMessage>> afterRunMessagesResponse
288+
= await agentClient.GetMessagesAsync(thread.Id);
289+
IReadOnlyList<ThreadMessage> messages = afterRunMessagesResponse.Value.Data;
290+
291+
// Note: messages iterate from newest to oldest, with the messages[0] being the most recent
292+
foreach (ThreadMessage threadMessage in messages)
293+
{
294+
Console.Write($"{threadMessage.CreatedAt:yyyy-MM-dd HH:mm:ss} - {threadMessage.Role,10}: ");
295+
foreach (MessageContent contentItem in threadMessage.ContentItems)
296+
{
297+
if (contentItem is MessageTextContent textItem)
298+
{
299+
Console.Write(textItem.Text);
300+
}
301+
else if (contentItem is MessageImageFileContent imageFileItem)
302+
{
303+
Console.Write($"<image from ID: {imageFileItem.FileId}");
304+
}
305+
Console.WriteLine();
306+
}
307+
}
308+
```
309+
---
310+
274311
::: zone-end
275312

276313
## Next steps

0 commit comments

Comments
 (0)