-
Notifications
You must be signed in to change notification settings - Fork 1.2k
.NET: Add Foundry Agents Tool Sample - Bing Custom Search #3701
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
Merged
rogerbarreto
merged 7 commits into
microsoft:main
from
rogerbarreto:feature/3674-bing-custom-search
Feb 24, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
04f5b67
.NET: Add Bing Custom Search sample #3674
rogerbarreto 0862776
Apply format fixes
rogerbarreto 7c82676
Merge branch 'main' into feature/3674-bing-custom-search
rogerbarreto a2badca
.NET: Improve Bing Custom Search sample with dual MEAI/Native SDK opt…
rogerbarreto dd5cac5
Merge branch 'main' of https://github.com/microsoft/agent-framework i…
rogerbarreto cf1e56d
Merge branch 'main' into feature/3674-bing-custom-search
rogerbarreto 1101b37
Address PR review comments for Bing Custom Search sample
rogerbarreto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...Agents/FoundryAgents_Step21_BingCustomSearch/FoundryAgents_Step21_BingCustomSearch.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFrameworks>net10.0</TargetFrameworks> | ||
|
|
||
| <Nullable>enable</Nullable> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <NoWarn>$(NoWarn);CA1812;CS8321</NoWarn> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.Extensions.Logging.Console" /> | ||
| <PackageReference Include="Azure.Identity" /> | ||
| <PackageReference Include="Azure.AI.Projects" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\..\..\src\Microsoft.Agents.AI.AzureAI\Microsoft.Agents.AI.AzureAI.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
76 changes: 76 additions & 0 deletions
76
dotnet/samples/GettingStarted/FoundryAgents/FoundryAgents_Step21_BingCustomSearch/Program.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| // This sample shows how to use Bing Custom Search Tool with AI Agents. | ||
|
|
||
| using Azure.AI.Projects; | ||
| using Azure.AI.Projects.OpenAI; | ||
| using Azure.Identity; | ||
| using Microsoft.Agents.AI; | ||
| using OpenAI.Responses; | ||
|
|
||
| string endpoint = Environment.GetEnvironmentVariable("AZURE_FOUNDRY_PROJECT_ENDPOINT") ?? throw new InvalidOperationException("AZURE_FOUNDRY_PROJECT_ENDPOINT is not set."); | ||
| string deploymentName = Environment.GetEnvironmentVariable("AZURE_FOUNDRY_PROJECT_DEPLOYMENT_NAME") ?? "gpt-4o-mini"; | ||
| string connectionId = Environment.GetEnvironmentVariable("BING_CUSTOM_SEARCH_PROJECT_CONNECTION_ID") ?? throw new InvalidOperationException("BING_CUSTOM_SEARCH_PROJECT_CONNECTION_ID is not set."); | ||
| string instanceName = Environment.GetEnvironmentVariable("BING_CUSTOM_SEARCH_INSTANCE_NAME") ?? throw new InvalidOperationException("BING_CUSTOM_SEARCH_INSTANCE_NAME is not set."); | ||
|
|
||
| const string AgentInstructions = """ | ||
| You are a helpful agent that can use Bing Custom Search tools to assist users. | ||
| Use the available Bing Custom Search tools to answer questions and perform tasks. | ||
| """; | ||
|
|
||
| // Get a client to create/retrieve/delete server side agents with Azure Foundry Agents. | ||
| // WARNING: DefaultAzureCredential is convenient for development but requires careful consideration in production. | ||
| // In production, consider using a specific credential (e.g., ManagedIdentityCredential) to avoid | ||
| // latency issues, unintended credential probing, and potential security risks from fallback mechanisms. | ||
| AIProjectClient aiProjectClient = new(new Uri(endpoint), new DefaultAzureCredential()); | ||
|
|
||
| // Bing Custom Search tool parameters shared by both options | ||
| BingCustomSearchToolParameters bingCustomSearchToolParameters = new([ | ||
| new BingCustomSearchConfiguration(connectionId, instanceName) | ||
| ]); | ||
|
|
||
| AIAgent agent = await CreateAgentWithMEAIAsync(); | ||
| // AIAgent agent = await CreateAgentWithNativeSDKAsync(); | ||
|
|
||
| Console.WriteLine($"Created agent: {agent.Name}"); | ||
|
|
||
| // Run the agent with a search query | ||
| AgentResponse response = await agent.RunAsync("Search for the latest news about Microsoft AI"); | ||
|
|
||
| Console.WriteLine("\n=== Agent Response ==="); | ||
| foreach (var message in response.Messages) | ||
| { | ||
| Console.WriteLine(message.Text); | ||
| } | ||
|
|
||
| // Cleanup by deleting the agent | ||
| await aiProjectClient.Agents.DeleteAgentAsync(agent.Name); | ||
| Console.WriteLine($"\nDeleted agent: {agent.Name}"); | ||
|
|
||
| // --- Agent Creation Options --- | ||
|
|
||
| // Option 1 - Using AsAITool wrapping for the ResponseTool returned by AgentTool.CreateBingCustomSearchTool (MEAI + AgentFramework) | ||
| async Task<AIAgent> CreateAgentWithMEAIAsync() | ||
| { | ||
| return await aiProjectClient.CreateAIAgentAsync( | ||
| model: deploymentName, | ||
| name: "BingCustomSearchAgent-MEAI", | ||
| instructions: AgentInstructions, | ||
| tools: [((ResponseTool)AgentTool.CreateBingCustomSearchTool(bingCustomSearchToolParameters)).AsAITool()]); | ||
| } | ||
|
|
||
| // Option 2 - Using PromptAgentDefinition with AgentTool.CreateBingCustomSearchTool (Native SDK) | ||
| async Task<AIAgent> CreateAgentWithNativeSDKAsync() | ||
| { | ||
| return await aiProjectClient.CreateAIAgentAsync( | ||
| name: "BingCustomSearchAgent-NATIVE", | ||
| creationOptions: new AgentVersionCreationOptions( | ||
| new PromptAgentDefinition(model: deploymentName) | ||
| { | ||
| Instructions = AgentInstructions, | ||
| Tools = { | ||
| (ResponseTool)AgentTool.CreateBingCustomSearchTool(bingCustomSearchToolParameters), | ||
| } | ||
| }) | ||
| ); | ||
| } |
63 changes: 63 additions & 0 deletions
63
...es/GettingStarted/FoundryAgents/FoundryAgents_Step21_BingCustomSearch/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| # Using Bing Custom Search with AI Agents | ||
|
|
||
| This sample demonstrates how to use the Bing Custom Search tool with AI agents to perform customized web searches. | ||
|
|
||
| ## What this sample demonstrates | ||
|
|
||
| - Creating agents with Bing Custom Search capabilities | ||
| - Configuring custom search instances via connection ID and instance name | ||
| - Two agent creation approaches: MEAI abstraction (Option 1) and Native SDK (Option 2) | ||
| - Running search queries through the agent | ||
| - Managing agent lifecycle (creation and deletion) | ||
|
|
||
| ## Agent creation options | ||
|
|
||
| This sample provides two approaches for creating agents with Bing Custom Search: | ||
|
|
||
| - **Option 1 - MEAI + AgentFramework**: Uses the Agent Framework `ResponseTool` wrapped with `AsAITool()` to call the `CreateAIAgentAsync` overload that accepts `tools:[]`, while still relying on the same underlying Azure AI Projects SDK types as Option 2. | ||
| - **Option 2 - Native SDK**: Uses `PromptAgentDefinition` with `AgentVersionCreationOptions` to create the agent directly with the Azure AI Projects SDK types. | ||
|
|
||
| Both options produce the same result. Toggle between them by commenting/uncommenting the corresponding `CreateAgentWith*Async` call in `Program.cs`. | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| Before you begin, ensure you have the following prerequisites: | ||
|
|
||
| - .NET 10 SDK or later | ||
| - Azure Foundry service endpoint and deployment configured | ||
| - Azure CLI installed and authenticated (for Azure credential authentication) | ||
| - A Bing Custom Search resource configured in Azure and connected to your Foundry project | ||
|
|
||
| **Note**: This demo uses Azure Default credentials for authentication. Make sure you're logged in with `az login` and have access to the Azure Foundry resource. | ||
|
|
||
| Set the following environment variables: | ||
|
|
||
| ```powershell | ||
| $env:AZURE_FOUNDRY_PROJECT_ENDPOINT="https://your-foundry-service.services.ai.azure.com/api/projects/your-foundry-project" | ||
| $env:AZURE_FOUNDRY_PROJECT_DEPLOYMENT_NAME="gpt-4o-mini" # Optional, defaults to gpt-4o-mini | ||
| $env:BING_CUSTOM_SEARCH_PROJECT_CONNECTION_ID="/subscriptions/<sub-id>/resourceGroups/<rg>/providers/Microsoft.CognitiveServices/accounts/<account>/projects/<project>/connections/<connection-name>" | ||
| $env:BING_CUSTOM_SEARCH_INSTANCE_NAME="your-configuration-name" | ||
| ``` | ||
|
|
||
| ### Finding the connection ID and instance name | ||
|
|
||
| - **Connection ID**: The full ARM resource path including the `/projects/<name>/connections/<connection-name>` segment. Find the connection name in your Foundry project under **Management center** → **Connected resources**. | ||
| - **Instance Name**: The **configuration name** from the Bing Custom Search resource (Azure portal → your Bing Custom Search resource → **Configurations**). This is _not_ the Azure resource name. | ||
|
|
||
| ## Run the sample | ||
|
|
||
| Navigate to the FoundryAgents sample directory and run: | ||
|
|
||
| ```powershell | ||
| cd dotnet/samples/GettingStarted/FoundryAgents | ||
| dotnet run --project .\FoundryAgents_Step21_BingCustomSearch | ||
| ``` | ||
|
|
||
| ## Expected behavior | ||
|
|
||
rogerbarreto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| The sample will: | ||
|
|
||
| 1. Create an agent with Bing Custom Search tool capabilities | ||
| 2. Run the agent with a search query about Microsoft AI | ||
| 3. Display the search results returned by the agent | ||
| 4. Clean up resources by deleting the agent | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.