This notebook explains how to integrate Bing Search with the Semantic Kernel to get the latest information from the internet.
To use Bing Search you simply need a Bing Search API key. You can get the API key by creating a Bing Search resource in Azure.
To learn more read the following documentation.
Prepare a Semantic Kernel instance first, loading also the AI backend settings defined in the Setup notebook:
#r "nuget: Microsoft.SemanticKernel, 1.23.0"
#r "nuget: Microsoft.SemanticKernel.Plugins.Web, 1.23.0-alpha"
#r "nuget: Microsoft.SemanticKernel.Plugins.Core, 1.23.0-alpha"
#r "nuget: Microsoft.SemanticKernel.PromptTemplates.Handlebars, 1.23.0-alpha"
#!import config/Settings.cs
#!import config/Utils.cs
Enter your Bing Search Key in BING_KEY using InteractiveKernel
method as introduced in .NET Interactive
using InteractiveKernel = Microsoft.DotNet.Interactive.Kernel;
string BING_KEY = (await InteractiveKernel.GetPasswordAsync("Please enter your Bing Search Key")).GetClearTextPassword();
The sample below shows how to create a plugin named SearchPlugin from an instance of BingTextSearch
. Using CreateWithSearch
creates a new plugin with a single Search function that calls the underlying text search implementation. The SearchPlugin is added to the Kernel which makes it available to be called during prompt rendering. The prompt template includes a call to {{SearchPlugin.Search $query}}
which will invoke the SearchPlugin to retrieve results related to the current query. The results are then inserted into the rendered prompt before it is sent to the AI model.
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Data;
using Microsoft.SemanticKernel.Plugins.Web.Bing;
using Kernel = Microsoft.SemanticKernel.Kernel;
// Create a kernel with OpenAI chat completion
var builder = Kernel.CreateBuilder();
// Configure AI backend used by the kernel
var (useAzureOpenAI, model, azureEndpoint, apiKey, orgId) = Settings.LoadFromFile();
if (useAzureOpenAI)
builder.AddAzureOpenAIChatCompletion(model, azureEndpoint, apiKey);
else
builder.AddOpenAIChatCompletion(model, apiKey, orgId);
var kernel = builder.Build();
// Create a text search using Bing search
#pragma warning disable SKEXP0050
var textSearch = new BingTextSearch(apiKey: BING_KEY);
// Build a text search plugin with Bing search and add to the kernel
var searchPlugin = textSearch.CreateWithSearch("SearchPlugin");
kernel.Plugins.Add(searchPlugin);
// Invoke prompt and use text search plugin to provide grounding information
var query = "What is the Semantic Kernel?";
var prompt = "{{SearchPlugin.Search $query}}. {{$query}}";
KernelArguments arguments = new() { { "query", query } };
Console.WriteLine(await kernel.InvokePromptAsync(prompt, arguments));
The sample below repeats the pattern described in the previous section with a few notable changes:
CreateWithGetTextSearchResults
is used to create a SearchPlugin
which calls the GetTextSearchResults
method from the underlying text search implementation.using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Data;
using Microsoft.SemanticKernel.Plugins.Web.Bing;
using Microsoft.SemanticKernel.PromptTemplates.Handlebars;
using Kernel = Microsoft.SemanticKernel.Kernel;
// Create a kernel with OpenAI chat completion
var builder = Kernel.CreateBuilder();
// Configure AI backend used by the kernel
var (useAzureOpenAI, model, azureEndpoint, apiKey, orgId) = Settings.LoadFromFile();
if (useAzureOpenAI)
builder.AddAzureOpenAIChatCompletion(model, azureEndpoint, apiKey);
else
builder.AddOpenAIChatCompletion(model, apiKey, orgId);
var kernel = builder.Build();
// Create a text search using Bing search
#pragma warning disable SKEXP0050
var textSearch = new BingTextSearch(apiKey: BING_KEY);
// Build a text search plugin with Bing search and add to the kernel
var searchPlugin = textSearch.CreateWithGetTextSearchResults("SearchPlugin");
kernel.Plugins.Add(searchPlugin);
// Invoke prompt and use text search plugin to provide grounding information
var query = "What is the Semantic Kernel?";
string promptTemplate = """
{{#with (SearchPlugin-GetTextSearchResults query)}}
{{#each this}}
Name: {{Name}}
Value: {{Value}}
Link: {{Link}}
-----------------
{{/each}}
{{/with}}
{{query}}
Include citations to the relevant information where it is referenced in the response.
""";
KernelArguments arguments = new() { { "query", query } };
HandlebarsPromptTemplateFactory promptTemplateFactory = new();
Console.WriteLine(await kernel.InvokePromptAsync(
promptTemplate,
arguments,
templateFormat: HandlebarsPromptTemplateFactory.HandlebarsTemplateFormat,
promptTemplateFactory: promptTemplateFactory
));
The samples shown so far will use the top ranked web search results to provide the grounding data. To provide more reliability in the data the web search can be restricted to only return results from a specified site.
The sample below builds on the previous one to add filtering of the search results.
A TextSearchFilter
with an equality clause is used to specify that only results from the Microsoft Developer Blogs site (site == 'devblogs.microsoft.com'
) are to be included in the search results.
The sample uses KernelPluginFactory.CreateFromFunctions
to create the SearchPlugin
.
A custom description is provided for the plugin.
The ITextSearch.CreateGetTextSearchResults
extension method is used to create the KernelFunction
which invokes the text search service.
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Data;
using Microsoft.SemanticKernel.PromptTemplates.Handlebars;
using Microsoft.SemanticKernel.Plugins.Web.Bing;
// Create a kernel with OpenAI chat completion
var builder = Kernel.CreateBuilder();
// Configure AI backend used by the kernel
var (useAzureOpenAI, model, azureEndpoint, apiKey, orgId) = Settings.LoadFromFile();
if (useAzureOpenAI)
builder.AddAzureOpenAIChatCompletion(model, azureEndpoint, apiKey);
else
builder.AddOpenAIChatCompletion(model, apiKey, orgId);
var kernel = builder.Build();
// Create a text search using Bing search
#pragma warning disable SKEXP0050
var textSearch = new BingTextSearch(apiKey: BING_KEY);
// Create a filter to search only the Microsoft Developer Blogs site
#pragma warning disable SKEXP0001
var filter = new TextSearchFilter().Equality("site", "devblogs.microsoft.com");
var searchOptions = new TextSearchOptions() { Filter = filter };
// Build a text search plugin with Bing search and add to the kernel
var searchPlugin = KernelPluginFactory.CreateFromFunctions(
"SearchPlugin", "Search Microsoft Developer Blogs site only",
[textSearch.CreateGetTextSearchResults(searchOptions: searchOptions)]);
kernel.Plugins.Add(searchPlugin);
// Invoke prompt and use text search plugin to provide grounding information
var query = "What is the Semantic Kernel?";
string promptTemplate = """
{{#with (SearchPlugin-GetTextSearchResults query)}}
{{#each this}}
Name: {{Name}}
Value: {{Value}}
Link: {{Link}}
-----------------
{{/each}}
{{/with}}
{{query}}
Include citations to the relevant information where it is referenced in the response.
""";
KernelArguments arguments = new() { { "query", query } };
HandlebarsPromptTemplateFactory promptTemplateFactory = new();
Console.WriteLine(await kernel.InvokePromptAsync(
promptTemplate,
arguments,
templateFormat: HandlebarsPromptTemplateFactory.HandlebarsTemplateFormat,
promptTemplateFactory: promptTemplateFactory
));