chore: import upstream snapshot with attribution
CodeQL / Analyze (csharp) (push) Waiting to run
CodeQL / Analyze (python) (push) Waiting to run

This commit is contained in:
wehub-resource-sync
2026-07-13 13:21:23 +08:00
commit b957a53def
5423 changed files with 863745 additions and 0 deletions
@@ -0,0 +1,55 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AssemblyName>GettingStartedWithTextSearch</AssemblyName>
<RootNamespace></RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<TargetFramework>net10.0</TargetFramework>
<IsTestProject>true</IsTestProject>
<IsPackable>false</IsPackable>
<!-- Suppress: "Declare types in namespaces", "Require ConfigureAwait", "Experimental" -->
<NoWarn>$(NoWarn);CS8618,IDE0009,IDE1006,CA1051,CA1050,CA1707,CA1054,CA2007,VSTHRD111,CS1591,RCS1110,RCS1243,CA5394,SKEXP0001,SKEXP0010,SKEXP0040,SKEXP0050,SKEXP0060,SKEXP0101</NoWarn>
<OutputType>Library</OutputType>
<UserSecretsId>5ee045b0-aea3-4f08-8d31-32d1a6f8fed0</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="HtmlAgilityPack" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="xRetry" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.abstractions" />
<PackageReference Include="xunit.runner.visualstudio">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Azure.Identity" />
<PackageReference Include="Microsoft.Extensions.Configuration" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
<PackageReference Include="Microsoft.Extensions.Http" />
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" />
<PackageReference Include="Microsoft.Extensions.Http.Resilience" />
<PackageReference Include="Microsoft.Extensions.Logging" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" />
<PackageReference Include="Microsoft.ML.Tokenizers" />
<PackageReference Include="System.Numerics.Tensors" />
<PackageReference Include="Microsoft.SemanticKernel.Connectors.InMemory" />
</ItemGroup>
<Import Project="$(RepoRoot)/dotnet/src/InternalUtilities/samples/SamplesInternalUtilities.props" />
<ItemGroup>
<ProjectReference Include="..\..\src\Connectors\Connectors.AzureOpenAI\Connectors.AzureOpenAI.csproj" />
<ProjectReference Include="..\..\src\Connectors\Connectors.OpenAI\Connectors.OpenAI.csproj" />
<ProjectReference Include="..\..\src\Extensions\PromptTemplates.Handlebars\PromptTemplates.Handlebars.csproj" />
<ProjectReference Include="..\..\src\Plugins\Plugins.Web\Plugins.Web.csproj" />
<ProjectReference Include="..\..\src\SemanticKernel.Abstractions\SemanticKernel.Abstractions.csproj" />
<ProjectReference Include="..\..\src\SemanticKernel.Core\SemanticKernel.Core.csproj" />
</ItemGroup>
<ItemGroup>
<Using Include="Xunit" />
<Using Include="Xunit.Abstractions" />
</ItemGroup>
</Project>
@@ -0,0 +1,8 @@
// Copyright (c) Microsoft. All rights reserved.
namespace GettingStartedWithTextSearch;
[CollectionDefinition("InMemoryVectorStoreCollection")]
public class InMemoryVectorStoreCollectionFixture : ICollectionFixture<InMemoryVectorStoreFixture>
{
}
@@ -0,0 +1,187 @@
// Copyright (c) Microsoft. All rights reserved.
using System.Reflection;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.VectorData;
using Microsoft.SemanticKernel.Connectors.InMemory;
using Microsoft.SemanticKernel.Data;
using OpenAI;
namespace GettingStartedWithTextSearch;
/// <summary>
/// Helper class for setting up and tearing down a <see cref="InMemoryVectorStore"/> for testing purposes.
/// </summary>
public class InMemoryVectorStoreFixture : IAsyncLifetime
{
/// <summary>
/// Gets the embedding generator used for creating vector embeddings.
/// </summary>
public IEmbeddingGenerator<string, Embedding<float>> EmbeddingGenerator { get; private set; }
/// <summary>
/// Gets the in-memory vector store instance.
/// </summary>
public InMemoryVectorStore InMemoryVectorStore { get; private set; }
/// <summary>
/// Gets the vector store record collection for data models.
/// </summary>
public VectorStoreCollection<Guid, DataModel> VectorStoreRecordCollection { get; private set; }
/// <summary>
/// Gets the name of the collection used for storing records.
/// </summary>
public string CollectionName => "records";
/// <summary>
/// Initializes a new instance of the <see cref="InMemoryVectorStoreFixture"/> class.
/// </summary>
public InMemoryVectorStoreFixture()
{
IConfigurationRoot configRoot = new ConfigurationBuilder()
.AddJsonFile("appsettings.Development.json", true)
.AddEnvironmentVariables()
.AddUserSecrets(Assembly.GetExecutingAssembly())
.Build();
TestConfiguration.Initialize(configRoot);
// Create an embedding generation service.
this.EmbeddingGenerator = new OpenAIClient(TestConfiguration.OpenAI.ApiKey)
.GetEmbeddingClient(TestConfiguration.OpenAI.EmbeddingModelId)
.AsIEmbeddingGenerator();
// Create an InMemory vector store.
this.InMemoryVectorStore = new InMemoryVectorStore(new() { EmbeddingGenerator = this.EmbeddingGenerator });
}
/// <inheritdoc/>
public async Task DisposeAsync()
{
await this.VectorStoreRecordCollection.EnsureCollectionDeletedAsync().ConfigureAwait(false);
}
/// <inheritdoc/>
public async Task InitializeAsync()
{
this.VectorStoreRecordCollection = await InitializeRecordCollectionAsync();
}
#region private
/// <summary>
/// Initialize a <see cref="VectorStoreCollection{TKey, TRecord}"/> with a list of strings.
/// </summary>
private async Task<VectorStoreCollection<Guid, DataModel>> InitializeRecordCollectionAsync()
{
// Delegate which will create a record.
static DataModel CreateRecord(int index, string text, ReadOnlyMemory<float> embedding)
{
var guid = Guid.NewGuid();
return new()
{
Key = guid,
Text = text,
Link = $"guid://{guid}",
Tag = index % 2 == 0 ? "Even" : "Odd",
};
}
// Create a record collection from a list of strings using the provided delegate.
string[] lines =
[
"Semantic Kernel is a lightweight, open-source development kit that lets you easily build AI agents and integrate the latest AI models into your C#, Python, or Java codebase. It serves as an efficient middleware that enables rapid delivery of enterprise-grade solutions.",
"Semantic Kernel is a new AI SDK, and a simple and yet powerful programming model that lets you add large language capabilities to your app in just a matter of minutes. It uses natural language prompting to create and execute semantic kernel AI tasks across multiple languages and platforms.",
"In this guide, you learned how to quickly get started with Semantic Kernel by building a simple AI agent that can interact with an AI service and run your code. To see more examples and learn how to build more complex AI agents, check out our in-depth samples.",
"The Semantic Kernel extension for Visual Studio Code makes it easy to design and test semantic functions.The extension provides an interface for designing semantic functions and allows you to test them with the push of a button with your existing models and data.",
"The kernel is the central component of Semantic Kernel.At its simplest, the kernel is a Dependency Injection container that manages all of the services and plugins necessary to run your AI application.",
"Semantic Kernel (SK) is a lightweight SDK that lets you mix conventional programming languages, like C# and Python, with the latest in Large Language Model (LLM) AI “prompts” with prompt templating, chaining, and planning capabilities.",
"Semantic Kernel is a lightweight, open-source development kit that lets you easily build AI agents and integrate the latest AI models into your C#, Python, or Java codebase. It serves as an efficient middleware that enables rapid delivery of enterprise-grade solutions. Enterprise ready.",
"With Semantic Kernel, you can easily build agents that can call your existing code.This power lets you automate your business processes with models from OpenAI, Azure OpenAI, Hugging Face, and more! We often get asked though, “How do I architect my solution?” and “How does it actually work?”"
];
var vectorizedSearch = await CreateCollectionFromListAsync<Guid, DataModel>(lines, CreateRecord);
return vectorizedSearch;
}
/// <summary>
/// Delegate to create a record.
/// </summary>
/// <typeparam name="TKey">Type of the record key.</typeparam>
/// <typeparam name="TRecord">Type of the record.</typeparam>
internal delegate TRecord CreateRecord<TKey, TRecord>(int index, string text, ReadOnlyMemory<float> vector) where TKey : notnull;
/// <summary>
/// Create a <see cref="VectorStoreCollection{TKey, TRecord}"/> from a list of strings by:
/// 1. Creating an instance of <see cref="VectorStoreCollection{TKey, TRecord}"/>
/// 2. Generating embeddings for each string.
/// 3. Creating a record with a valid key for each string and it's embedding.
/// 4. Insert the records into the collection.
/// </summary>
/// <param name="entries">A list of strings.</param>
/// <param name="createRecord">A delegate which can create a record with a valid key for each string and it's embedding.</param>
private async Task<VectorStoreCollection<TKey, TRecord>> CreateCollectionFromListAsync<TKey, TRecord>(
string[] entries,
CreateRecord<TKey, TRecord> createRecord)
where TKey : notnull
where TRecord : class
{
// Get and create collection if it doesn't exist.
var collection = this.InMemoryVectorStore.GetCollection<TKey, TRecord>(this.CollectionName);
await collection.EnsureCollectionExistsAsync().ConfigureAwait(false);
// Create records and generate embeddings for them.
var tasks = entries.Select((entry, i) => Task.Run(async () =>
{
var record = createRecord(i, entry, (await this.EmbeddingGenerator.GenerateAsync(entry).ConfigureAwait(false)).Vector);
await collection.UpsertAsync(record).ConfigureAwait(false);
}));
await Task.WhenAll(tasks).ConfigureAwait(false);
return collection;
}
/// <summary>
/// Sample model class that represents a record entry.
/// </summary>
/// <remarks>
/// Note that each property is decorated with an attribute that specifies how the property should be treated by the vector store.
/// This allows us to create a collection in the vector store and upsert and retrieve instances of this class without any further configuration.
/// </remarks>
public sealed class DataModel
{
/// <summary>
/// Gets or sets the unique identifier for this record.
/// </summary>
[VectorStoreKey]
[TextSearchResultName]
public Guid Key { get; init; }
/// <summary>
/// Gets or sets the text content of this record.
/// </summary>
[VectorStoreData]
[TextSearchResultValue]
public string Text { get; init; }
/// <summary>
/// Gets or sets the link associated with this record.
/// </summary>
[VectorStoreData]
[TextSearchResultLink]
public string Link { get; init; }
/// <summary>
/// Gets or sets the tag for categorizing this record.
/// </summary>
[VectorStoreData(IsIndexed = true)]
public required string Tag { get; init; }
/// <summary>
/// Gets the embedding representation of the text content.
/// </summary>
[VectorStoreVector(1536)]
public string Embedding => Text;
}
#endregion
}
@@ -0,0 +1,46 @@
# Starting With Semantic Kernel
This project contains a step by step guide to get started using Text Search with the Semantic Kernel.
The examples can be run as integration tests but their code can also be copied to stand-alone programs.
## Configuring Secrets
Most of the examples will require secrets and credentials, to access OpenAI, Azure OpenAI,
Bing and other resources. We suggest using .NET
[Secret Manager](https://learn.microsoft.com/aspnet/core/security/app-secrets)
to avoid the risk of leaking secrets into the repository, branches and pull requests.
You can also use environment variables if you prefer.
**NOTE**
The `Step2_Search_For_RAG.RagWithBingTextSearchUsingFullPagesAsync` sample requires a large context window so we recommend using `gpt-4o` or `gpt-4o-mini` models.
To set your secrets with Secret Manager:
```
cd dotnet/samples/Concepts
dotnet user-secrets init
dotnet user-secrets set "OpenAI:EmbeddingModelId" "..."
dotnet user-secrets set "OpenAI:ChatModelId" "..."
dotnet user-secrets set "OpenAI:ApiKey" "..."
dotnet user-secrets set "Bing:ApiKey" "..."
dotnet user-secrets set "Google:SearchEngineId" "..."
dotnet user-secrets set "Google:ApiKey" "..."
```
To set your secrets with environment variables, use these names:
```
OpenAI__EmbeddingModelId
OpenAI__ChatModelId
OpenAI__ApiKey
Bing__ApiKey
Google__SearchEngineId
Google__ApiKey
```
@@ -0,0 +1,203 @@
// Copyright (c) Microsoft. All rights reserved.
#pragma warning disable CS0618 // ITextSearch is obsolete - Sample demonstrates legacy interface usage
using Microsoft.SemanticKernel.Data;
using Microsoft.SemanticKernel.Plugins.Web.Bing;
using Microsoft.SemanticKernel.Plugins.Web.Google;
namespace GettingStartedWithTextSearch;
/// <summary>
/// This example shows how to create and use a <see cref="ITextSearch"/>.
/// </summary>
public class Step1_Web_Search(ITestOutputHelper output) : BaseTest(output)
{
/// <summary>
/// Show how to create a <see cref="BingTextSearch"/> and use it to perform a search.
/// </summary>
[Fact]
public async Task BingSearchAsync()
{
// Create an ITextSearch instance using Bing search
var textSearch = new BingTextSearch(apiKey: TestConfiguration.Bing.ApiKey);
var query = "What is the Semantic Kernel?";
// Search and return results
KernelSearchResults<string> searchResults = await textSearch.SearchAsync(query, new TextSearchOptions { Top = 4 });
await foreach (string result in searchResults.Results)
{
Console.WriteLine(result);
}
}
/// <summary>
/// Show how to create a <see cref="GoogleTextSearch"/> and use it to perform a search.
/// </summary>
[Fact]
public async Task GoogleSearchAsync()
{
// Create an ITextSearch instance using Google search
var textSearch = new GoogleTextSearch(
searchEngineId: TestConfiguration.Google.SearchEngineId,
apiKey: TestConfiguration.Google.ApiKey);
var query = "What is the Semantic Kernel?";
// Search and return results
KernelSearchResults<string> searchResults = await textSearch.SearchAsync(query, new TextSearchOptions { Top = 4 });
await foreach (string result in searchResults.Results)
{
Console.WriteLine(result);
}
}
/// <summary>
/// Show how to use <see cref="BingTextSearch"/> with the new generic <see cref="ITextSearch{TRecord}"/>
/// interface and LINQ filtering for type-safe searches.
/// </summary>
[Fact]
public async Task BingSearchWithLinqFilteringAsync()
{
#pragma warning disable CA1859 // Use concrete types when possible for improved performance - Sample intentionally demonstrates interface usage
// Create an ITextSearch<BingWebPage> instance for type-safe LINQ filtering
ITextSearch<BingWebPage> textSearch = new BingTextSearch(apiKey: TestConfiguration.Bing.ApiKey);
#pragma warning restore CA1859
var query = "What is the Semantic Kernel?";
// Use LINQ filtering for type-safe search with compile-time validation
var options = new TextSearchOptions<BingWebPage>
{
Top = 4,
Filter = page => page.Language == "en" && page.IsFamilyFriendly == true
};
// Search and return strongly-typed results
Console.WriteLine("\n--- Bing Search with LINQ Filtering ---\n");
KernelSearchResults<BingWebPage> searchResults = await textSearch.GetSearchResultsAsync(query, options);
await foreach (BingWebPage page in searchResults.Results)
{
Console.WriteLine($"Name: {page.Name}");
Console.WriteLine($"Snippet: {page.Snippet}");
Console.WriteLine($"Url: {page.Url}");
Console.WriteLine($"Language: {page.Language}");
Console.WriteLine($"Family Friendly: {page.IsFamilyFriendly}");
Console.WriteLine("---");
}
}
/// <summary>
/// Show how to use <see cref="GoogleTextSearch"/> with the new generic <see cref="ITextSearch{TRecord}"/>
/// interface and LINQ filtering for type-safe searches.
/// </summary>
[Fact]
public async Task GoogleSearchWithLinqFilteringAsync()
{
#pragma warning disable CA1859 // Use concrete types when possible for improved performance - Sample intentionally demonstrates interface usage
// Create an ITextSearch<GoogleWebPage> instance for type-safe LINQ filtering
ITextSearch<GoogleWebPage> textSearch = new GoogleTextSearch(
searchEngineId: TestConfiguration.Google.SearchEngineId,
apiKey: TestConfiguration.Google.ApiKey);
#pragma warning restore CA1859
var query = "What is the Semantic Kernel?";
// Use LINQ filtering for type-safe search with compile-time validation
var options = new TextSearchOptions<GoogleWebPage>
{
Top = 4,
Filter = page => page.Title != null && page.Title.Contains("Semantic") && page.DisplayLink != null && page.DisplayLink.EndsWith(".com")
};
// Search and return strongly-typed results
Console.WriteLine("\n--- Google Search with LINQ Filtering ---\n");
KernelSearchResults<GoogleWebPage> searchResults = await textSearch.GetSearchResultsAsync(query, options);
await foreach (GoogleWebPage page in searchResults.Results)
{
Console.WriteLine($"Title: {page.Title}");
Console.WriteLine($"Snippet: {page.Snippet}");
Console.WriteLine($"Link: {page.Link}");
Console.WriteLine($"Display Link: {page.DisplayLink}");
Console.WriteLine("---");
}
}
/// <summary>
/// Show how to create a <see cref="BingTextSearch"/> and use it to perform a search
/// and return results as a collection of <see cref="BingWebPage"/> instances.
/// </summary>
[Fact]
public async Task SearchForWebPagesAsync()
{
// Create an ITextSearch instance
ITextSearch textSearch = this.UseBingSearch ?
new BingTextSearch(
apiKey: TestConfiguration.Bing.ApiKey) :
new GoogleTextSearch(
searchEngineId: TestConfiguration.Google.SearchEngineId,
apiKey: TestConfiguration.Google.ApiKey);
var query = "What is the Semantic Kernel?";
// Search and return results using the implementation specific data model
KernelSearchResults<object> objectResults = await textSearch.GetSearchResultsAsync(query, new() { Top = 4 });
if (this.UseBingSearch)
{
Console.WriteLine("\n--- Bing Web Page Results ---\n");
await foreach (BingWebPage webPage in objectResults.Results)
{
Console.WriteLine($"Name: {webPage.Name}");
Console.WriteLine($"Snippet: {webPage.Snippet}");
Console.WriteLine($"Url: {webPage.Url}");
Console.WriteLine($"DisplayUrl: {webPage.DisplayUrl}");
Console.WriteLine($"DateLastCrawled: {webPage.DateLastCrawled}");
}
}
else
{
Console.WriteLine("\n--- Google Web Page Results ---\n");
await foreach (Google.Apis.CustomSearchAPI.v1.Data.Result result in objectResults.Results)
{
Console.WriteLine($"Title: {result.Title}");
Console.WriteLine($"Snippet: {result.Snippet}");
Console.WriteLine($"Link: {result.Link}");
Console.WriteLine($"DisplayLink: {result.DisplayLink}");
Console.WriteLine($"Kind: {result.Kind}");
}
}
}
/// <summary>
/// Show how to create a <see cref="BingTextSearch"/> and use it to perform a search
/// and return results as a collection of <see cref="TextSearchResult"/> instances.
/// </summary>
/// <remarks>
/// Having a normalized format for search results is useful when you want to process the results
/// for different search services in a consistent way.
/// </remarks>
[Fact]
public async Task SearchForTextSearchResultsAsync()
{
// Create an ITextSearch instance
ITextSearch textSearch = this.UseBingSearch ?
new BingTextSearch(
apiKey: TestConfiguration.Bing.ApiKey) :
new GoogleTextSearch(
searchEngineId: TestConfiguration.Google.SearchEngineId,
apiKey: TestConfiguration.Google.ApiKey);
var query = "What is the Semantic Kernel?";
// Search and return results as TextSearchResult items
KernelSearchResults<TextSearchResult> textResults = await textSearch.GetTextSearchResultsAsync(query, new() { Top = 4 });
Console.WriteLine("\n--- Text Search Results ---\n");
await foreach (TextSearchResult result in textResults.Results)
{
Console.WriteLine($"Name: {result.Name}");
Console.WriteLine($"Value: {result.Value}");
Console.WriteLine($"Link: {result.Link}");
}
}
}
@@ -0,0 +1,377 @@
// Copyright (c) Microsoft. All rights reserved.
#pragma warning disable CS0618 // ITextSearch is obsolete - Sample demonstrates legacy interface usage
using System.Text.RegularExpressions;
using HtmlAgilityPack;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Data;
using Microsoft.SemanticKernel.Plugins.Web.Bing;
using Microsoft.SemanticKernel.Plugins.Web.Google;
using Microsoft.SemanticKernel.PromptTemplates.Handlebars;
namespace GettingStartedWithTextSearch;
/// <summary>
/// This example shows how to use <see cref="ITextSearch"/> for Retrieval Augmented Generation (RAG).
/// </summary>
public class Step2_Search_For_RAG(ITestOutputHelper output) : BaseTest(output)
{
/// <summary>
/// Show how to create a default <see cref="KernelPlugin"/> from a <see cref="BingTextSearch"/> and use it to
/// add grounding context to a prompt.
/// </summary>
[Fact]
public async Task RagWithTextSearchAsync()
{
// Create a kernel with OpenAI chat completion
IKernelBuilder kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.AddOpenAIChatCompletion(
modelId: TestConfiguration.OpenAI.ChatModelId,
apiKey: TestConfiguration.OpenAI.ApiKey);
Kernel kernel = kernelBuilder.Build();
// Create a text search using Bing search
ITextSearch textSearch = this.UseBingSearch ?
new BingTextSearch(
apiKey: TestConfiguration.Bing.ApiKey) :
new GoogleTextSearch(
searchEngineId: TestConfiguration.Google.SearchEngineId,
apiKey: TestConfiguration.Google.ApiKey);
// Build a text search plugin with web 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));
}
/// <summary>
/// Show how to create a default <see cref="KernelPlugin"/> from an <see cref="ITextSearch"/> and use it to
/// add grounding context to a Handlebars prompt and include citations in the response.
/// </summary>
[Fact]
public async Task RagWithBingTextSearchIncludingCitationsAsync()
{
// Create a kernel with OpenAI chat completion
IKernelBuilder kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.AddOpenAIChatCompletion(
modelId: TestConfiguration.OpenAI.ChatModelId,
apiKey: TestConfiguration.OpenAI.ApiKey);
Kernel kernel = kernelBuilder.Build();
// Create a text search using Bing search
var textSearch = new BingTextSearch(new(TestConfiguration.Bing.ApiKey));
// 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
));
}
/// <summary>
/// Show how to create a default <see cref="KernelPlugin"/> from an <see cref="ITextSearch"/> and use it to
/// add grounding context to a Handlebars prompt and include citations in the response.
/// </summary>
[Fact]
public async Task RagWithBingTextSearchIncludingTimeStampedCitationsAsync()
{
// Create a kernel with OpenAI chat completion
IKernelBuilder kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.AddOpenAIChatCompletion(
modelId: TestConfiguration.OpenAI.ChatModelId,
apiKey: TestConfiguration.OpenAI.ApiKey);
Kernel kernel = kernelBuilder.Build();
// Create a text search using Bing search
var textSearch = new BingTextSearch(new(TestConfiguration.Bing.ApiKey));
// Build a text search plugin with Bing search and add to the kernel
var searchPlugin = textSearch.CreateWithGetSearchResults("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-GetSearchResults query)}}
{{#each this}}
Name: {{Name}}
Snippet: {{Snippet}}
Link: {{DisplayUrl}}
Date Last Crawled: {{DateLastCrawled}}
-----------------
{{/each}}
{{/with}}
{{query}}
Include citations to and the date of 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
));
}
/// <summary>
/// Show how to create a default <see cref="KernelPlugin"/> from an <see cref="ITextSearch"/> and use it to
/// add grounding context to a Handlebars prompt that includes results from the Microsoft Developer Blogs site.
/// </summary>
[Fact]
public async Task RagWithBingTextSearchUsingDevBlogsSiteAsync()
{
// Create a kernel with OpenAI chat completion
IKernelBuilder kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.AddOpenAIChatCompletion(
modelId: TestConfiguration.OpenAI.ChatModelId,
apiKey: TestConfiguration.OpenAI.ApiKey);
Kernel kernel = kernelBuilder.Build();
// Create a text search using Bing search
var textSearch = new BingTextSearch(new(TestConfiguration.Bing.ApiKey));
// Create a filter to search only the Microsoft Developer Blogs site
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
));
}
/// <summary>
/// Show how to create a default <see cref="KernelPlugin"/> from an <see cref="ITextSearch"/> and use it to
/// add grounding context to a Handlebars prompt that include results for the specified web site.
/// </summary>
[Fact]
public async Task RagWithBingTextSearchUsingCustomSiteAsync()
{
// Create a kernel with OpenAI chat completion
IKernelBuilder kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.AddOpenAIChatCompletion(
modelId: TestConfiguration.OpenAI.ChatModelId,
apiKey: TestConfiguration.OpenAI.ApiKey);
Kernel kernel = kernelBuilder.Build();
// Create a text search using Bing search
var textSearch = new BingTextSearch(new(TestConfiguration.Bing.ApiKey));
// Build a text search plugin with Bing search and add to the kernel
var options = new KernelFunctionFromMethodOptions()
{
FunctionName = "GetSiteResults",
Description = "Perform a search for content related to the specified query and optionally from the specified domain.",
Parameters =
[
new KernelParameterMetadata("query") { Description = "What to search for", IsRequired = true },
new KernelParameterMetadata("top") { Description = "Number of results", IsRequired = false, DefaultValue = 5 },
new KernelParameterMetadata("skip") { Description = "Number of results to skip", IsRequired = false, DefaultValue = 0 },
new KernelParameterMetadata("site") { Description = "Only return results from this domain", IsRequired = false },
],
ReturnParameter = new() { ParameterType = typeof(KernelSearchResults<string>) },
};
var searchPlugin = KernelPluginFactory.CreateFromFunctions("SearchPlugin", "Search specified site", [textSearch.CreateGetTextSearchResults(options)]);
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-GetSiteResults query)}}
{{#each this}}
Name: {{Name}}
Value: {{Value}}
Link: {{Link}}
-----------------
{{/each}}
{{/with}}
{{query}}
Only include results from techcommunity.microsoft.com.
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
));
}
/// <summary>
/// Show how to create a default <see cref="KernelPlugin"/> from an <see cref="ITextSearch"/> and use it to
/// add grounding context to a Handlebars prompt that include full web pages.
/// </summary>
[Fact]
public async Task RagWithBingTextSearchUsingFullPagesAsync()
{
// Create a kernel with OpenAI chat completion
IKernelBuilder kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.AddOpenAIChatCompletion(
modelId: TestConfiguration.OpenAI.ChatModelId, // Requires a large context window e.g. gpt-4o or gpt-4o-mini
apiKey: TestConfiguration.OpenAI.ApiKey);
Kernel kernel = kernelBuilder.Build();
// Create a text search using Bing search
var textSearch = new TextSearchWithFullValues(new BingTextSearch(new(TestConfiguration.Bing.ApiKey)));
// Create a filter to search only the Microsoft Developer Blogs site
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
));
}
}
/// <summary>
/// Wraps a <see cref="ITextSearch"/> to provide full web pages as search results.
/// </summary>
public partial class TextSearchWithFullValues(ITextSearch searchDelegate) : ITextSearch
{
/// <inheritdoc/>
public Task<KernelSearchResults<object>> GetSearchResultsAsync(string query, TextSearchOptions? searchOptions = null, CancellationToken cancellationToken = default)
{
return searchDelegate.GetSearchResultsAsync(query, searchOptions, cancellationToken);
}
/// <inheritdoc/>
public async Task<KernelSearchResults<TextSearchResult>> GetTextSearchResultsAsync(string query, TextSearchOptions? searchOptions = null, CancellationToken cancellationToken = default)
{
var results = await searchDelegate.GetTextSearchResultsAsync(query, searchOptions, cancellationToken);
var resultList = new List<TextSearchResult>();
using HttpClient client = new();
await foreach (var item in results.Results.WithCancellation(cancellationToken).ConfigureAwait(false))
{
string? value = item.Value;
try
{
if (item.Link is not null)
{
value = await client.GetStringAsync(new Uri(item.Link), cancellationToken);
value = ConvertHtmlToPlainText(value);
}
}
catch (HttpRequestException)
{
}
resultList.Add(new(value) { Name = item.Name, Link = item.Link });
}
return new KernelSearchResults<TextSearchResult>(resultList.ToAsyncEnumerable<TextSearchResult>(), results.TotalCount, results.Metadata);
}
/// <inheritdoc/>
public Task<KernelSearchResults<string>> SearchAsync(string query, TextSearchOptions? searchOptions = null, CancellationToken cancellationToken = default)
{
return searchDelegate.SearchAsync(query, searchOptions, cancellationToken);
}
/// <summary>
/// Convert HTML to plain text.
/// </summary>
private static string ConvertHtmlToPlainText(string html)
{
HtmlDocument doc = new();
doc.LoadHtml(html);
string text = doc.DocumentNode.InnerText;
text = MyRegex().Replace(text, " "); // Remove unnecessary whitespace
return text.Trim();
}
[GeneratedRegex(@"\s+")]
private static partial Regex MyRegex();
}
@@ -0,0 +1,166 @@
// Copyright (c) Microsoft. All rights reserved.
using System.Text.Json;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using Microsoft.SemanticKernel.Data;
using Microsoft.SemanticKernel.Plugins.Web.Bing;
namespace GettingStartedWithTextSearch;
/// <summary>
/// This example shows how to use <see cref="ITextSearch"/> for Function Calling.
/// </summary>
public class Step3_Search_With_FunctionCalling(ITestOutputHelper output) : BaseTest(output)
{
/// <summary>
/// Show how to create a default <see cref="KernelPlugin"/> from an <see cref="BingTextSearch"/> and use it with
/// function calling to have the LLM include grounding context in it's response.
/// </summary>
[Fact]
public async Task FunctionCallingWithBingTextSearchAsync()
{
// Create a kernel with OpenAI chat completion
IKernelBuilder kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.AddOpenAIChatCompletion(
modelId: TestConfiguration.OpenAI.ChatModelId,
apiKey: TestConfiguration.OpenAI.ApiKey);
kernelBuilder.Services.AddSingleton<ITestOutputHelper>(this.Output);
kernelBuilder.Services.AddSingleton<IFunctionInvocationFilter, FunctionInvocationFilter>();
Kernel kernel = kernelBuilder.Build();
// Create a search service with Bing search
var textSearch = new BingTextSearch(new(TestConfiguration.Bing.ApiKey));
// 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
OpenAIPromptExecutionSettings settings = new() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() };
KernelArguments arguments = new(settings);
Console.WriteLine(await kernel.InvokePromptAsync("What is the Semantic Kernel?", arguments));
}
/// <summary>
/// Show how to create a default <see cref="KernelPlugin"/> from an <see cref="BingTextSearch"/> and use it with
/// function calling and have the LLM include links in the final response.
/// </summary>
[Fact]
public async Task FunctionCallingWithBingTextSearchIncludingCitationsAsync()
{
// Create a kernel with OpenAI chat completion
IKernelBuilder kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.AddOpenAIChatCompletion(
modelId: TestConfiguration.OpenAI.ChatModelId,
apiKey: TestConfiguration.OpenAI.ApiKey);
Kernel kernel = kernelBuilder.Build();
// Create a search service with Bing search
var textSearch = new BingTextSearch(new(TestConfiguration.Bing.ApiKey));
// 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
OpenAIPromptExecutionSettings settings = new() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() };
KernelArguments arguments = new(settings);
Console.WriteLine(await kernel.InvokePromptAsync("What is the Semantic Kernel? Include citations to the relevant information where it is referenced in the response.", arguments));
}
#pragma warning disable CS0618 // Suppress obsolete warnings for legacy TextSearchOptions/TextSearchFilter usage
/// <summary>
/// Show how to create a default <see cref="KernelPlugin"/> from an <see cref="BingTextSearch"/> and use it with
/// function calling to have the LLM include grounding context from the Microsoft Dev Blogs site in it's response.
/// </summary>
[Fact]
public async Task FunctionCallingWithBingTextSearchUsingDevBlogsSiteAsync()
{
// Create a kernel with OpenAI chat completion
IKernelBuilder kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.AddOpenAIChatCompletion(
modelId: TestConfiguration.OpenAI.ChatModelId,
apiKey: TestConfiguration.OpenAI.ApiKey);
Kernel kernel = kernelBuilder.Build();
// Create a search service with Bing search
var textSearch = new BingTextSearch(new(TestConfiguration.Bing.ApiKey));
// Build a text search plugin with Bing search and add to the kernel
var filter = new TextSearchFilter().Equality("site", "devblogs.microsoft.com");
var searchOptions = new TextSearchOptions() { Filter = filter };
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
OpenAIPromptExecutionSettings settings = new() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() };
KernelArguments arguments = new(settings);
Console.WriteLine(await kernel.InvokePromptAsync("What is the Semantic Kernel? Include citations to the relevant information where it is referenced in the response.", arguments));
}
/// <summary>
/// Show how to create a default <see cref="KernelPlugin"/> from an <see cref="BingTextSearch"/> and use it with
/// function calling to have the LLM include grounding context from the Microsoft Dev Blogs site in it's response.
/// </summary>
[Fact]
public async Task FunctionCallingWithBingTextSearchUsingSiteArgumentAsync()
{
// Create a kernel with OpenAI chat completion
IKernelBuilder kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.AddOpenAIChatCompletion(
modelId: TestConfiguration.OpenAI.ChatModelId,
apiKey: TestConfiguration.OpenAI.ApiKey);
Kernel kernel = kernelBuilder.Build();
// Create a search service with Bing search
var textSearch = new BingTextSearch(new(TestConfiguration.Bing.ApiKey));
// Build a text search plugin with Bing search and add to the kernel
var searchPlugin = KernelPluginFactory.CreateFromFunctions("SearchPlugin", "Search specified site", [CreateSearchBySite(textSearch)]);
kernel.Plugins.Add(searchPlugin);
// Invoke prompt and use text search plugin to provide grounding information
OpenAIPromptExecutionSettings settings = new() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() };
KernelArguments arguments = new(settings);
Console.WriteLine(await kernel.InvokePromptAsync("What is the Semantic Kernel? Only include results from techcommunity.microsoft.com. Include citations to the relevant information where it is referenced in the response.", arguments));
}
#region private
private sealed class FunctionInvocationFilter(ITestOutputHelper output) : IFunctionInvocationFilter
{
public async Task OnFunctionInvocationAsync(FunctionInvocationContext context, Func<FunctionInvocationContext, Task> next)
{
if (context.Function.PluginName == "SearchPlugin")
{
output.WriteLine($"{context.Function.Name}:{JsonSerializer.Serialize(context.Arguments)}\n");
}
await next(context);
}
}
private static KernelFunction CreateSearchBySite(BingTextSearch textSearch, TextSearchFilter? filter = null)
{
var options = new KernelFunctionFromMethodOptions()
{
FunctionName = "Search",
Description = "Perform a search for content related to the specified query and optionally from the specified domain.",
Parameters =
[
new KernelParameterMetadata("query") { Description = "What to search for", IsRequired = true },
new KernelParameterMetadata("count") { Description = "Number of results", IsRequired = false, DefaultValue = 2 },
new KernelParameterMetadata("skip") { Description = "Number of results to skip", IsRequired = false, DefaultValue = 0 },
new KernelParameterMetadata("site") { Description = "Only return results from this domain", IsRequired = false, DefaultValue = 2 },
],
ReturnParameter = new() { ParameterType = typeof(KernelSearchResults<string>) },
};
return textSearch.CreateSearch(options);
}
#pragma warning restore CS0618
#endregion
}
@@ -0,0 +1,124 @@
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.Extensions.VectorData;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using Microsoft.SemanticKernel.Data;
using Microsoft.SemanticKernel.PromptTemplates.Handlebars;
using static GettingStartedWithTextSearch.InMemoryVectorStoreFixture;
namespace GettingStartedWithTextSearch;
/// <summary>
/// This example shows how to create a <see cref="ITextSearch"/> from a
/// <see cref="VectorStore"/>.
/// </summary>
[Collection("InMemoryVectorStoreCollection")]
public class Step4_Search_With_VectorStore(ITestOutputHelper output, InMemoryVectorStoreFixture fixture) : BaseTest(output)
{
/// <summary>
/// Show how to create a <see cref="VectorStoreTextSearch{TRecord}"/> and use it to perform a search.
/// </summary>
[Fact]
public async Task UsingInMemoryVectorStoreRecordTextSearchAsync()
{
// Use embedding generation service and record collection for the fixture.
var collection = fixture.VectorStoreRecordCollection;
// Create a text search instance using the InMemory vector store.
var textSearch = new VectorStoreTextSearch<DataModel>(collection);
// Search and return results as TextSearchResult items
var query = "What is the Semantic Kernel?";
KernelSearchResults<TextSearchResult> textResults = await textSearch.GetTextSearchResultsAsync(query, new() { Top = 2, Skip = 0 });
Console.WriteLine("\n--- Text Search Results ---\n");
await foreach (TextSearchResult result in textResults.Results)
{
Console.WriteLine($"Name: {result.Name}");
Console.WriteLine($"Value: {result.Value}");
Console.WriteLine($"Link: {result.Link}");
}
}
/// <summary>
/// Show how to create a default <see cref="KernelPlugin"/> from an <see cref="ITextSearch"/> and use it to
/// add grounding context to a Handlebars prompt.
/// </summary>
[Fact]
public async Task RagWithInMemoryVectorStoreTextSearchAsync()
{
// Create a kernel with OpenAI chat completion
IKernelBuilder kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.AddOpenAIChatCompletion(
modelId: TestConfiguration.OpenAI.ChatModelId,
apiKey: TestConfiguration.OpenAI.ApiKey);
Kernel kernel = kernelBuilder.Build();
// Use embedding generation service and record collection for the fixture.
var embeddingGenerator = fixture.EmbeddingGenerator;
var collection = fixture.VectorStoreRecordCollection;
// Create a text search instance using the InMemory vector store.
var textSearch = new VectorStoreTextSearch<DataModel>(collection);
// Build a text search plugin with vector store 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
));
}
/// <summary>
/// Show how to create a default <see cref="KernelPlugin"/> from an <see cref="VectorStoreTextSearch{TRecord}"/> and use it with
/// function calling to have the LLM include grounding context in it's response.
/// </summary>
[Fact]
public async Task FunctionCallingWithInMemoryVectorStoreTextSearchAsync()
{
// Create a kernel with OpenAI chat completion
IKernelBuilder kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.AddOpenAIChatCompletion(
modelId: TestConfiguration.OpenAI.ChatModelId,
apiKey: TestConfiguration.OpenAI.ApiKey);
Kernel kernel = kernelBuilder.Build();
// Use embedding generation service and record collection for the fixture.
var embeddingGenerator = fixture.EmbeddingGenerator;
var collection = fixture.VectorStoreRecordCollection;
// Create a text search instance using the InMemory vector store.
var textSearch = new VectorStoreTextSearch<DataModel>(collection);
// Build a text search plugin with vector store 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
OpenAIPromptExecutionSettings settings = new() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() };
KernelArguments arguments = new(settings);
Console.WriteLine(await kernel.InvokePromptAsync("What is the Semantic Kernel?", arguments));
}
}