{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# BingSearch using Kernel \n", "\n", "This notebook explains how to integrate Bing Search with the Semantic Kernel to get the latest information from the internet.\n", "\n", "To use Bing Search you simply need a Bing Search API key. You can get the API key by creating a [Bing Search resource](https://learn.microsoft.com/en-us/bing/search-apis/bing-web-search/create-bing-search-service-resource) in Azure. \n", "\n", "To learn more read the following [documentation](https://learn.microsoft.com/en-us/bing/search-apis/bing-web-search/overview).\n" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Prepare a Semantic Kernel instance first, loading also the AI backend settings defined in the [Setup notebook](0-AI-settings.ipynb):" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [], "source": [ "#r \"nuget: Microsoft.SemanticKernel, 1.0.0-beta1\"\n", "#r \"nuget: Microsoft.SemanticKernel.Plugins.Web, 1.0.0-beta1\"\n", "\n", "#!import config/Settings.cs\n", "#!import config/Utils.cs\n", "\n", "using Microsoft.SemanticKernel;\n", "using Microsoft.SemanticKernel.Plugins.Core;\n", "using Microsoft.SemanticKernel.Orchestration;\n", "using Microsoft.SemanticKernel.Planning;\n", "using Microsoft.SemanticKernel.Planners;\n", "using Microsoft.SemanticKernel.TemplateEngine;\n", "using InteractiveKernel = Microsoft.DotNet.Interactive.Kernel;\n", "\n", "var builder = new KernelBuilder();\n", "\n", "// Configure AI backend used by the kernel\n", "var (useAzureOpenAI, model, azureEndpoint, apiKey, orgId) = Settings.LoadFromFile();\n", "\n", "if (useAzureOpenAI)\n", " builder.WithAzureChatCompletionService(model, azureEndpoint, apiKey);\n", "else\n", " builder.WithOpenAIChatCompletionService(model, apiKey, orgId);\n", "\n", "var kernel = builder.Build();" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Add the following namespaces to use the Bing Search connector." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [], "source": [ "using Microsoft.SemanticKernel.Plugins.Web;\n", "using Microsoft.SemanticKernel.Plugins.Web.Bing;" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Enter your Bing Search Key in BING_KEY using `InteractiveKernel` method as introduced in [`.NET Interactive`](https://github.com/dotnet/interactive/blob/main/docs/kernels-overview.md)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [], "source": [ "using InteractiveKernel = Microsoft.DotNet.Interactive.Kernel;\n", "\n", "string BING_KEY = (await InteractiveKernel.GetPasswordAsync(\"Please enter your Bing Search Key\")).GetClearTextPassword();" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Below are some examples on how [`WebSearchEnginePlugin`](../src/Plugins/Plugins.Web/WebSearchEnginePlugin.cs) can be used in SK. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [], "source": [ "private static async Task Example1Async(IKernel kernel)\n", "{\n", " Console.WriteLine(\"Example 1\");\n", "\n", " // Run \n", " var question = \"What is quantum tunnelling\";\n", " var function = kernel.Functions.GetFunction(\"bing\", \"search\");\n", " var bingResult = await kernel.RunAsync(question, function);\n", " var bingResultString = bingResult.GetValue();\n", "\n", " Console.WriteLine(question);\n", " Console.WriteLine(\"----\");\n", " Console.WriteLine(bingResultString);\n", " Console.WriteLine();\n", "\n", " /* OUTPUT:\n", "\n", " What is quantum tunnelling\n", " ----\n", " In physics, quantum tunnelling, barrier penetration, or simply tunnelling is a quantum mechanical\n", " phenomenon in which an object such as an electron or atom passes through a potential energy \n", " barrier that, according to classical mechanics, the object does not have sufficient energy to\n", " enter or surmount.\n", " \n", " */\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [], "source": [ "private static async Task Example2Async(IKernel kernel)\n", "{\n", " Console.WriteLine(\"Example 2\");\n", "\n", " //The following function only works in interactive notebooks\n", " string question = await InteractiveKernel.GetInputAsync(\"Please ask your question\"); \n", "\n", " var function = kernel.Functions.GetFunction(\"bing\", \"search\");\n", " var bingResult = await kernel.RunAsync(question, function);\n", " var bingResultString = bingResult.GetValue();\n", "\n", " Console.WriteLine(bingResultString);\n", "}" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Just uncomment the examples to run them in the following code:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [], "source": [ "// Load Bing plugin\n", "var bingConnector = new BingConnector(BING_KEY);\n", "\n", "kernel.ImportFunctions(new WebSearchEnginePlugin(bingConnector), \"bing\");\n", "\n", "//await Example1Async(kernel);\n", "//await Example2Async(kernel);" ] } ], "metadata": { "kernelspec": { "display_name": ".NET (C#)", "language": "C#", "name": ".net-csharp" }, "language_info": { "name": "polyglot-notebook" }, "orig_nbformat": 4, "polyglot_notebook": { "kernelInfo": { "defaultKernelName": "csharp", "items": [ { "aliases": [], "name": "csharp" } ] } } }, "nbformat": 4, "nbformat_minor": 2 }