diff --git a/sdk/ai/Azure.AI.Projects/tests/Samples/Agent/Sample_Agent_Bing_Custom_Search.cs b/sdk/ai/Azure.AI.Projects/tests/Samples/Agent/Sample_Agent_Bing_Custom_Search.cs new file mode 100644 index 000000000000..b455a5f85b4e --- /dev/null +++ b/sdk/ai/Azure.AI.Projects/tests/Samples/Agent/Sample_Agent_Bing_Custom_Search.cs @@ -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 +{ + [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 = ""; + + SearchConfigurationList searchConfigurationList = new SearchConfigurationList( + new List + { + 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 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($" + { + 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 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($"