Skip to content

Commit 4b6945d

Browse files
authored
Merge pull request #32 from sandeepsnairms/main
Updating SK version to 1.0.0-rc4
2 parents da7e043 + 9e20b71 commit 4b6945d

File tree

5 files changed

+62
-39
lines changed

5 files changed

+62
-39
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
################################################################################
2+
# This .gitignore file was automatically created by Microsoft(R) Visual Studio.
3+
################################################################################
4+
5+
/appsettings.Development.json
6+
/obj
7+
/.vs
8+
/bin

C#/CosmosDB-NoSQL_CognitiveSearch_SemanticKernel/CosmosRecipeGuide.csproj

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@
2323
</ItemGroup>
2424

2525
<ItemGroup>
26-
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.35.1" />
27-
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
28-
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
29-
<PackageReference Include="Microsoft.SemanticKernel" Version="0.18.230725.3-preview" />
30-
<PackageReference Include="Microsoft.SemanticKernel.Connectors.Memory.AzureCognitiveSearch" Version="0.18.230725.3-preview" />
26+
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.37.0" />
27+
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
28+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
29+
<PackageReference Include="Microsoft.SemanticKernel" Version="1.0.0-rc4" />
30+
<PackageReference Include="Microsoft.SemanticKernel.Connectors.Memory.AzureAISearch" Version="1.0.0-rc3" />
3131
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
32-
<PackageReference Include="Spectre.Console" Version="0.47.0" />
32+
<PackageReference Include="Spectre.Console" Version="0.48.1-preview.0.5" />
3333
</ItemGroup>
3434

3535

C#/CosmosDB-NoSQL_CognitiveSearch_SemanticKernel/CosmosRecipeGuide.sln

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ VisualStudioVersion = 17.6.33801.468
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CosmosRecipeGuide", "CosmosRecipeGuide.csproj", "{536B20AF-59DD-41E6-B026-04604B027E16}"
77
EndProject
8+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{9C994D0D-A1C7-4881-94F6-C1C120308CCB}"
9+
ProjectSection(SolutionItems) = preProject
10+
.editorconfig = .editorconfig
11+
EndProjectSection
12+
EndProject
813
Global
914
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1015
Debug|Any CPU = Debug|Any CPU

C#/CosmosDB-NoSQL_CognitiveSearch_SemanticKernel/Service/SemanticKernelService.cs

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,47 +8,59 @@
88
using Azure.Core;
99
using Microsoft.Azure.Cosmos;
1010
using 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;
1511
using Microsoft.SemanticKernel.TemplateEngine;
1612
using 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

1823
namespace 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
}

C#/CosmosDB-NoSQL_CognitiveSearch_SemanticKernel/appsettings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"CosmosDatabase": "ContosoRecipes_CS",
1313
"CosmosContainer": "Recipe",
1414
"RecipeLocalFolder": "..\\..\\DataSet\\Recipe",
15-
15+
1616
"OpenAIEndpoint": "https://{Account Name}.openai.azure.com/",
1717
"OpenAIKey": "",
1818
"OpenAIEmbeddingDeployment": "text-embedding-ada-002",
@@ -23,4 +23,4 @@
2323
"SearchIndexName": "recipesindex",
2424
"SearchServiceAdminApiKey": "",
2525
"SearchServiceQueryApiKey": ""
26-
}
26+
}

0 commit comments

Comments
 (0)