88using Azure . Core ;
99using Microsoft . Azure . Cosmos ;
1010using Microsoft . Extensions . Configuration ;
11- using Microsoft . SemanticKernel ;
12- using Microsoft . SemanticKernel . AI . ChatCompletion ;
13- using Microsoft . SemanticKernel . Connectors . Memory . AzureCognitiveSearch ;
14- using Microsoft . SemanticKernel . Memory ;
1511using Microsoft . SemanticKernel . TemplateEngine ;
1612using static System . Runtime . InteropServices . JavaScript . JSType ;
13+ using Microsoft . Extensions . Logging ;
14+ using System . Runtime ;
15+ using Newtonsoft . Json ;
16+ using Microsoft . SemanticKernel ;
17+ using Microsoft . SemanticKernel . ChatCompletion ;
18+ using Microsoft . SemanticKernel . Memory ;
19+ using Microsoft . SemanticKernel . Connectors . OpenAI ;
20+ using Microsoft . SemanticKernel . Connectors . Memory . AzureAISearch ;
21+
1722
1823namespace CosmosRecipeGuide . Services
1924{
2025 public class SemanticKernelService
2126 {
22- IKernel kernel ;
27+ //following lines supress errors tagged as "Feature is for evaluation purposes only and is subject to change or removal in future updates."
28+ #pragma warning disable SKEXP0021 // Disable the warning for the next line
29+ #pragma warning disable SKEXP0011 // Disable the warning for the next line
30+ #pragma warning disable SKEXP0003 // Disable the warning for the next line
31+ #pragma warning disable SKEXP0052 // Disable the warning for the next line
32+
33+ readonly Kernel kernel ;
2334 private const string MemoryCollectionName = "SKRecipe" ;
2435
36+ ISemanticTextMemory memoryWithACS ;
37+ IChatCompletionService chatCompletionService ;
38+
2539 public SemanticKernelService ( string OpenAIEndpoint , string OpenAIKey , string EmbeddingsDeployment , string CompletionDeployment , string ACSEndpoint , string ACSApiKey )
2640 {
27- // IMPORTANT: Register an embedding generation service and a memory store using Azure Cognitive Service.
28- kernel = new KernelBuilder ( )
29- . WithAzureChatCompletionService (
30- CompletionDeployment ,
31- OpenAIEndpoint ,
32- OpenAIKey )
33- . WithAzureTextEmbeddingGenerationService (
34- EmbeddingsDeployment ,
35- OpenAIEndpoint ,
36- OpenAIKey )
37- . WithMemoryStorage ( new AzureCognitiveSearchMemoryStore ( ACSEndpoint , ACSApiKey ) )
41+
42+ memoryWithACS = new MemoryBuilder ( )
43+ . WithAzureOpenAITextEmbeddingGeneration ( EmbeddingsDeployment , OpenAIEndpoint , OpenAIKey , "" )
44+ . WithMemoryStore ( new AzureAISearchMemoryStore ( ACSEndpoint , ACSApiKey ) )
3845 . Build ( ) ;
46+
47+ chatCompletionService = new AzureOpenAIChatCompletionService (
48+ CompletionDeployment ,
49+ OpenAIEndpoint ,
50+ OpenAIKey ) ;
3951 }
4052
4153
4254 public async Task SaveEmbeddingsAsync ( string data , string id )
4355 {
4456 try
4557 {
46- await kernel . Memory . SaveReferenceAsync (
47- collection : MemoryCollectionName ,
48- externalSourceName : "Recipe" ,
49- externalId : id ,
50- description : data ,
51- text : data ) ;
58+ await memoryWithACS . SaveReferenceAsync (
59+ collection : MemoryCollectionName ,
60+ externalSourceName : "Recipe" ,
61+ externalId : id ,
62+ description : data ,
63+ text : data ) ;
5264
5365 }
5466 catch ( Exception ex )
@@ -59,11 +71,11 @@ await kernel.Memory.SaveReferenceAsync(
5971
6072 public async Task < List < string > > SearchEmbeddingsAsync ( string query )
6173 {
62- var memories = kernel . Memory . SearchAsync ( MemoryCollectionName , query , limit : 2 , minRelevanceScore : 0.5 ) ;
74+
75+ var memoryResults = memoryWithACS . SearchAsync ( MemoryCollectionName , query , limit : 3 , minRelevanceScore : 0.5 ) ;
6376
6477 List < string > result = new List < string > ( ) ;
65- int i = 0 ;
66- await foreach ( MemoryQueryResult memory in memories )
78+ await foreach ( var memory in memoryResults )
6779 {
6880 result . Add ( memory . Metadata . Id ) ;
6981 }
@@ -86,10 +98,8 @@ public async Task<string> GenerateCompletionAsync(string userPrompt, string rec
8698 - Format the content so that it can be printed to the Command Line
8799 - In case there are more than one recipes you find let the user pick the most appropiate recipe." ;
88100
89- // Client used to request answers to gpt-3.5 - turbo
90- var chatCompletion = kernel . GetService < IChatCompletion > ( ) ;
91101
92- var chatHistory = chatCompletion . CreateNewChat ( systemPromptRecipeAssistant ) ;
102+ var chatHistory = new ChatHistory ( systemPromptRecipeAssistant ) ;
93103
94104 // add shortlisted recipes as system message
95105 chatHistory . AddSystemMessage ( recipeData ) ;
@@ -98,10 +108,10 @@ public async Task<string> GenerateCompletionAsync(string userPrompt, string rec
98108 chatHistory . AddUserMessage ( userPrompt ) ;
99109
100110 // Finally, get the response from AI
101- string answer = await chatCompletion . GenerateMessageAsync ( chatHistory ) ;
102-
111+ var completionResults = await chatCompletionService . GetChatMessageContentsAsync ( chatHistory ) ;
112+ string answer = completionResults [ 0 ] . Content ;
103113
104- return answer ;
114+ return answer ! ;
105115 }
106116 }
107117}
0 commit comments