Skip to content

Commit ea3ca62

Browse files
committed
updating bing C# samples
1 parent e058346 commit ea3ca62

File tree

1 file changed

+46
-48
lines changed

1 file changed

+46
-48
lines changed

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

Lines changed: 46 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,13 @@ using Azure.Core;
136136
using Azure.Core.TestFramework;
137137
using NUnit.Framework;
138138

139-
var connectionString = TestEnvironment.AzureAICONNECTIONSTRING;
139+
var connectionString = System.Environment.GetEnvironmentVariable("PROJECT_CONNECTION_STRING");
140+
var modelDeploymentName = System.Environment.GetEnvironmentVariable("MODEL_DEPLOYMENT_NAME");
141+
var bingConnectionName = System.Environment.GetEnvironmentVariable("BING_CONNECTION_NAME");
140142

141-
var clientOptions = new AIProjectClientOptions();
142-
143-
// Adding the custom headers policy
144-
clientOptions.AddPolicy(new CustomHeadersPolicy(), HttpPipelinePosition.PerCall);
145-
var projectClient = new AIProjectClient(connectionString, new DefaultAzureCredential(), clientOptions);
143+
var projectClient = new AIProjectClient(connectionString, new DefaultAzureCredential());
146144

145+
AgentsClient agentClient = projectClient.GetAgentsClient();
147146
```
148147

149148
# [JavaScript](#tab/javascript)
@@ -202,23 +201,20 @@ with project_client:
202201
# [C#](#tab/csharp)
203202

204203
```csharp
205-
GetConnectionResponse bingConnection = await projectClient.GetConnectionsClient().GetConnectionAsync(TestEnvironment.BINGCONNECTIONNAME);
204+
ConnectionResponse bingConnection = projectClient.GetConnectionsClient().GetConnection(bingConnectionName);
206205
var connectionId = bingConnection.Id;
207206

208-
AgentsClient agentClient = projectClient.GetAgentsClient();
209-
210-
ToolConnectionList connectionList = new ToolConnectionList
207+
ToolConnectionList connectionList = new()
211208
{
212209
ConnectionList = { new ToolConnection(connectionId) }
213210
};
214-
BingGroundingToolDefinition bingGroundingTool = new BingGroundingToolDefinition(connectionList);
215-
216-
Response<Agent> agentResponse = await agentClient.CreateAgentAsync(
217-
model: "gpt-4o",
218-
name: "my-assistant",
219-
instructions: "You are a helpful assistant.",
220-
tools: new List<ToolDefinition> { bingGroundingTool });
221-
Agent agent = agentResponse.Value;
211+
BingGroundingToolDefinition bingGroundingTool = new(connectionList);
212+
213+
Agent agent = agentClient.CreateAgent(
214+
model: modelDeploymentName,
215+
name: "my-assistant",
216+
instructions: "You are a helpful assistant.",
217+
tools: [bingGroundingTool]);
222218
```
223219

224220
# [JavaScript](#tab/javascript)
@@ -285,30 +281,13 @@ print(f"Created message, ID: {message.id}")
285281
# [C#](#tab/csharp)
286282

287283
```csharp
288-
// Create thread for communication
289-
Response<AgentThread> threadResponse = await agentClient.CreateThreadAsync();
290-
AgentThread thread = threadResponse.Value;
284+
AgentThread thread = agentClient.CreateThread();
291285

292286
// Create message to thread
293-
Response<ThreadMessage> messageResponse = await agentClient.CreateMessageAsync(
287+
ThreadMessage message = agentClient.CreateMessage(
294288
thread.Id,
295289
MessageRole.User,
296290
"How does wikipedia explain Euler's Identity?");
297-
ThreadMessage message = messageResponse.Value;
298-
```
299-
300-
# [JavaScript](#tab/javascript)
301-
302-
```javascript
303-
// create a thread
304-
const thread = await client.agents.createThread();
305-
306-
// add a message to thread
307-
await client.agents.createMessage(
308-
thread.id, {
309-
role: "user",
310-
content: "What is the weather in Seattle?",
311-
});
312291
```
313292

314293
# [REST API](#tab/rest)
@@ -367,30 +346,46 @@ print(f"Messages: {messages}")
367346
# [C#](#tab/csharp)
368347

369348
```csharp
370-
// Run the agent
371-
Response<ThreadRun> runResponse = await agentClient.CreateRunAsync(thread, agent);
372349

350+
// Run the agent
351+
ThreadRun run = agentClient.CreateRun(thread, agent);
373352
do
374353
{
375-
await Task.Delay(TimeSpan.FromMilliseconds(500));
376-
runResponse = await agentClient.GetRunAsync(thread.Id, runResponse.Value.Id);
354+
Thread.Sleep(TimeSpan.FromMilliseconds(500));
355+
run = agentClient.GetRun(thread.Id, run.Id);
377356
}
378-
while (runResponse.Value.Status == RunStatus.Queued
379-
|| runResponse.Value.Status == RunStatus.InProgress);
357+
while (run.Status == RunStatus.Queued
358+
|| run.Status == RunStatus.InProgress);
380359

381-
Response<PageableList<ThreadMessage>> afterRunMessagesResponse
382-
= await agentClient.GetMessagesAsync(thread.Id);
383-
IReadOnlyList<ThreadMessage> messages = afterRunMessagesResponse.Value.Data;
360+
Assert.AreEqual(
361+
RunStatus.Completed,
362+
run.Status,
363+
run.LastError?.Message);
364+
365+
PageableList<ThreadMessage> messages = agentClient.GetMessages(
366+
threadId: thread.Id,
367+
order: ListSortOrder.Ascending
368+
);
384369

385-
// Note: messages iterate from newest to oldest, with the messages[0] being the most recent
386370
foreach (ThreadMessage threadMessage in messages)
387371
{
388372
Console.Write($"{threadMessage.CreatedAt:yyyy-MM-dd HH:mm:ss} - {threadMessage.Role,10}: ");
389373
foreach (MessageContent contentItem in threadMessage.ContentItems)
390374
{
391375
if (contentItem is MessageTextContent textItem)
392376
{
393-
Console.Write(textItem.Text);
377+
string response = textItem.Text;
378+
if (textItem.Annotations != null)
379+
{
380+
foreach (MessageTextAnnotation annotation in textItem.Annotations)
381+
{
382+
if (annotation is MessageTextUrlCitationAnnotation urlAnnotation)
383+
{
384+
response = response.Replace(urlAnnotation.Text, $" [{urlAnnotation.UrlCitation.Title}]({urlAnnotation.UrlCitation.Url})");
385+
}
386+
}
387+
}
388+
Console.Write($"Agent response: {response}");
394389
}
395390
else if (contentItem is MessageImageFileContent imageFileItem)
396391
{
@@ -399,6 +394,9 @@ foreach (ThreadMessage threadMessage in messages)
399394
Console.WriteLine();
400395
}
401396
}
397+
398+
agentClient.DeleteThread(threadId: thread.Id);
399+
agentClient.DeleteAgent(agentId: agent.Id);
402400
```
403401

404402
# [JavaScript](#tab/javascript)

0 commit comments

Comments
 (0)