1010using Microsoft . Extensions . AI ;
1111using Microsoft . Extensions . Options ;
1212using Microsoft . Extensions . VectorData ;
13+ using Microsoft . SemanticKernel ;
1314using Microsoft . SemanticKernel . ChatCompletion ;
1415
1516namespace BuildingBlocks . VectorDB . SearchServices ;
@@ -18,14 +19,15 @@ public sealed class SemanticSearch(
1819 IChatCompletionService chatCompletionService ,
1920 IEmbeddingGenerator < string , Embedding < float > > embeddingGenerator ,
2021 VectorStore vectorStore ,
22+ Kernel kernel ,
2123 IOptions < SemanticKernelOptions > semanticKernelOptions
2224) : ISemanticSearch
2325{
2426 private readonly SemanticKernelOptions _semanticKernelOptions = semanticKernelOptions . Value ;
2527
2628 private const string InitPrompt =
2729 @"
28- You are an intelligent, friendly e-commerce assistant designed to help shoppers understand their search results.
30+ You are an intelligent, friendly GenAI-Eshop assistant designed to help users understand their search results.
2931
3032Follow these guidelines in every response:
3133- Always respond in a warm, professional, and conversational tone.
@@ -58,19 +60,20 @@ public async Task<VectorSearchResult<TEntity>> HybridSearchAsync<TVectorEntity,
5860
5961 var collection = vectorStore . GetCollection < Guid , TVectorEntity > ( typeof ( TVectorEntity ) . Name . Underscore ( ) ) ;
6062 await collection . EnsureCollectionExistsAsync ( cancellationToken ) ;
61- var vectorCollection = ( IKeywordHybridSearchable < TVectorEntity > ) collection ;
6263
6364 // https://learn.microsoft.com/en-us/semantic-kernel/concepts/vector-store-connectors/hybrid-search?pivots=programming-language-csharp#top-and-skip
6465 var options = new HybridSearchOptions < TVectorEntity >
6566 {
6667 VectorProperty = r => r . Vector ,
68+ // field to search on the full-text search column with `IsFullTextIndexed=true` attribute
6769 AdditionalProperty = fullTextSearchFiled ?? ( r => r . Description ) ,
6870 Skip = skip ,
6971 IncludeVectors = false ,
7072 // https://learn.microsoft.com/en-us/semantic-kernel/concepts/vector-store-connectors/vector-search?pivots=programming-language-csharp#filter
7173 Filter = filter ,
7274 } ;
7375
76+ var vectorCollection = ( IKeywordHybridSearchable < TVectorEntity > ) collection ;
7477 // https://learn.microsoft.com/en-us/semantic-kernel/concepts/vector-store-connectors/hybrid-search?pivots=programming-language-csharp
7578 // keywords will use to search on full-text search column with `IsFullTextIndexed=true` attribute
7679 var searchResults = vectorCollection . HybridSearchAsync (
@@ -146,7 +149,8 @@ public async Task<VectorSearchResult<TEntity>> HybridSearchAsync<TVectorEntity,
146149 var aiResponse = await chatCompletionService . GetChatMessageContentAsync (
147150 chatHistory : history ,
148151 // https://ollama.com/blog/thinking
149- executionSettings : SemanticKernelExecutionSettings . GetDefaultSettings ( _semanticKernelOptions ) ,
152+ executionSettings : SemanticKernelExecutionSettings . GetProviderExecutionSettings ( _semanticKernelOptions ) ,
153+ kernel : kernel ,
150154 cancellationToken : cancellationToken
151155 ) ;
152156
@@ -173,9 +177,6 @@ public async Task<VectorSearchResult<TEntity>> SemanticSearchAsync<TVectorEntity
173177 where TVectorEntity : VectorEntityBase
174178 where TEntity : Entity
175179 {
176- // https://learn.microsoft.com/en-us/semantic-kernel/concepts/ai-services/embedding-generation/?tabs=csharp-Ollama&pivots=programming-language-csharp#using-text-embedding-generation-services
177- var vector = await embeddingGenerator . GenerateVectorAsync ( searchTerm , cancellationToken : cancellationToken ) ;
178-
179180 var collection = vectorStore . GetCollection < Guid , TVectorEntity > ( typeof ( TVectorEntity ) . Name . Underscore ( ) ) ;
180181 await collection . EnsureCollectionExistsAsync ( cancellationToken ) ;
181182
@@ -188,6 +189,8 @@ public async Task<VectorSearchResult<TEntity>> SemanticSearchAsync<TVectorEntity
188189 Filter = filter ,
189190 } ;
190191
192+ // https://learn.microsoft.com/en-us/semantic-kernel/concepts/ai-services/embedding-generation/?tabs=csharp-Ollama&pivots=programming-language-csharp#using-text-embedding-generation-services
193+ var vector = await embeddingGenerator . GenerateVectorAsync ( searchTerm , cancellationToken : cancellationToken ) ;
191194 var searchResults = collection . SearchAsync (
192195 searchValue : vector ,
193196 top : take ,
@@ -265,7 +268,8 @@ Make the response sound natural and helpful to the user.
265268 // https://ollama.com/blog/thinking
266269 // - in ollama cli we can `ollama run qwen3:0.6b --think=false` to turn off thinking
267270 // - in ollama api with passing `think=false` as parameter
268- executionSettings : SemanticKernelExecutionSettings . GetDefaultSettings ( _semanticKernelOptions ) ,
271+ executionSettings : SemanticKernelExecutionSettings . GetProviderExecutionSettings ( _semanticKernelOptions ) ,
272+ kernel : kernel ,
269273 cancellationToken : cancellationToken
270274 ) ;
271275
0 commit comments