Skip to content

[AI] [Projects] Add Bing Custom Search sample #49683

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

#nullable disable

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core.TestFramework;
using NUnit.Framework;

namespace Azure.AI.Projects.Tests;

public partial class Sample_Agent_Bing_Custom_Search : SamplesBase<AIProjectsTestEnvironment>
{
[Test]
[AsyncOnly]
public async Task BingCustomSearchExampleAsync()
{
#if SNIPPET
var connectionString = System.Environment.GetEnvironmentVariable("PROJECT_CONNECTION_STRING");
var modelDeploymentName = System.Environment.GetEnvironmentVariable("MODEL_DEPLOYMENT_NAME");
var bingConnectionName = System.Environment.GetEnvironmentVariable("BING_CONNECTION_NAME");
#else
var connectionString = TestEnvironment.AzureAICONNECTIONSTRING;
var modelDeploymentName = TestEnvironment.MODELDEPLOYMENTNAME;
var bingConnectionName = TestEnvironment.BINGCONNECTIONNAME;
#endif

var projectClient = new AIProjectClient(connectionString, new DefaultAzureCredential());

AgentsClient agentClient = projectClient.GetAgentsClient();
ConnectionResponse bingConnection = await projectClient.GetConnectionsClient().GetConnectionAsync(bingConnectionName);
var connectionId = bingConnection.Id;
var instanceName = "<your_config_instance_name>";

SearchConfigurationList searchConfigurationList = new SearchConfigurationList(
new List<SearchConfiguration>
{
new SearchConfiguration(connectionId, instanceName)
});

BingCustomSearchToolDefinition bingGroundingTool = new(searchConfigurationList);
Agent agent = await agentClient.CreateAgentAsync(
model: modelDeploymentName,
name: "my-assistant",
instructions: "You are a helpful assistant.",
tools: [ bingGroundingTool ]);
// Create thread for communication
AgentThread thread = await agentClient.CreateThreadAsync();

// Create message to thread
ThreadMessage message = await agentClient.CreateMessageAsync(
thread.Id,
MessageRole.User,
"How many medals did the USA win in the 2024 summer olympics?");

// Run the agent
ThreadRun run = await agentClient.CreateRunAsync(thread, agent);
do
{
await Task.Delay(TimeSpan.FromMilliseconds(500));
run = await agentClient.GetRunAsync(thread.Id, run.Id);
}
while (run.Status == RunStatus.Queued
|| run.Status == RunStatus.InProgress);

Assert.AreEqual(
RunStatus.Completed,
run.Status,
run.LastError?.Message);

PageableList<ThreadMessage> messages = await agentClient.GetMessagesAsync(
threadId: thread.Id,
order: ListSortOrder.Ascending
);

foreach (ThreadMessage threadMessage in messages)
{
Console.Write($"{threadMessage.CreatedAt:yyyy-MM-dd HH:mm:ss} - {threadMessage.Role,10}: ");
foreach (MessageContent contentItem in threadMessage.ContentItems)
{
if (contentItem is MessageTextContent textItem)
{
string response = textItem.Text;
if (textItem.Annotations != null)
{
foreach (MessageTextAnnotation annotation in textItem.Annotations)
{
if (annotation is MessageTextUrlCitationAnnotation urlAnnotation)
{
response = response.Replace(urlAnnotation.Text, $" [{urlAnnotation.UrlCitation.Title}]({urlAnnotation.UrlCitation.Url})");
}
}
}
Console.Write($"Agent response: {response}");
}
else if (contentItem is MessageImageFileContent imageFileItem)
{
Console.Write($"<image from ID: {imageFileItem.FileId}");
}
Console.WriteLine();
}
}
await agentClient.DeleteThreadAsync(threadId: thread.Id);
await agentClient.DeleteAgentAsync(agentId: agent.Id);
}

[Test]
[SyncOnly]
public void BingCustomSearchExample()
{
#if SNIPPET
var connectionString = System.Environment.GetEnvironmentVariable("PROJECT_CONNECTION_STRING");
var modelDeploymentName = System.Environment.GetEnvironmentVariable("MODEL_DEPLOYMENT_NAME");
var bingConnectionName = System.Environment.GetEnvironmentVariable("BING_CONNECTION_NAME");
#else
var connectionString = TestEnvironment.AzureAICONNECTIONSTRING;
var modelDeploymentName = TestEnvironment.MODELDEPLOYMENTNAME;
var bingConnectionName = TestEnvironment.BINGCONNECTIONNAME;
#endif

var projectClient = new AIProjectClient(connectionString, new DefaultAzureCredential());
AgentsClient agentClient = projectClient.GetAgentsClient();
ConnectionResponse bingConnection = projectClient.GetConnectionsClient().GetConnection(bingConnectionName);
var connectionId = bingConnection.Id;
var instanceName = "<your_config_instance_name>";

SearchConfigurationList searchConfigurationList = new SearchConfigurationList(
new List<SearchConfiguration>
{
new SearchConfiguration(connectionId, instanceName)
});

BingCustomSearchToolDefinition bingGroundingTool = new(searchConfigurationList);
Agent agent = agentClient.CreateAgent(
model: modelDeploymentName,
name: "my-assistant",
instructions: "You are a helpful assistant.",
tools: [bingGroundingTool]);
// Create thread for communication
AgentThread thread = agentClient.CreateThread();

// Create message to thread
ThreadMessage message = agentClient.CreateMessage(
thread.Id,
MessageRole.User,
"How many medals did the USA win in the 2024 summer olympics?");

// Run the agent
ThreadRun run = agentClient.CreateRun(thread, agent);
do
{
Thread.Sleep(TimeSpan.FromMilliseconds(500));
run = agentClient.GetRun(thread.Id, run.Id);
}
while (run.Status == RunStatus.Queued
|| run.Status == RunStatus.InProgress);

Assert.AreEqual(
RunStatus.Completed,
run.Status,
run.LastError?.Message);

PageableList<ThreadMessage> messages = agentClient.GetMessages(
threadId: thread.Id,
order: ListSortOrder.Ascending
);

foreach (ThreadMessage threadMessage in messages)
{
Console.Write($"{threadMessage.CreatedAt:yyyy-MM-dd HH:mm:ss} - {threadMessage.Role,10}: ");
foreach (MessageContent contentItem in threadMessage.ContentItems)
{
if (contentItem is MessageTextContent textItem)
{
string response = textItem.Text;
if (textItem.Annotations != null)
{
foreach (MessageTextAnnotation annotation in textItem.Annotations)
{
if (annotation is MessageTextUrlCitationAnnotation urlAnnotation)
{
response = response.Replace(urlAnnotation.Text, $" [{urlAnnotation.UrlCitation.Title}]({urlAnnotation.UrlCitation.Url})");
}
}
}
Console.Write($"Agent response: {response}");
}
else if (contentItem is MessageImageFileContent imageFileItem)
{
Console.Write($"<image from ID: {imageFileItem.FileId}");
}
Console.WriteLine();
}
}
agentClient.DeleteThread(threadId: thread.Id);
agentClient.DeleteAgent(agentId: agent.Id);
}
}
Loading