From 29b8ebe283061dbfb6ef8195216c3d2504bb380c Mon Sep 17 00:00:00 2001 From: Marco Minerva Date: Wed, 6 Nov 2024 17:23:12 +0100 Subject: [PATCH] Change ChatService to singleton, update package version - Changed ChatService registration in Program.cs to singleton. - Reformatted ChatHistory initialization in ChatService.cs. - Modified prompt construction to avoid new lines after chunks. - Updated Microsoft.SemanticKernel package to version 1.27.0. --- SqlDatabaseVectorSearch/Program.cs | 2 +- SqlDatabaseVectorSearch/Services/ChatService.cs | 7 +++---- SqlDatabaseVectorSearch/SqlDatabaseVectorSearch.csproj | 2 +- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/SqlDatabaseVectorSearch/Program.cs b/SqlDatabaseVectorSearch/Program.cs index f613a87..e485ddd 100644 --- a/SqlDatabaseVectorSearch/Program.cs +++ b/SqlDatabaseVectorSearch/Program.cs @@ -32,7 +32,7 @@ builder.Services.AddKernel() .AddAzureOpenAITextEmbeddingGeneration(aiSettings.Embedding.Deployment, aiSettings.Embedding.Endpoint, aiSettings.Embedding.ApiKey, dimensions: aiSettings.Embedding.Dimensions) .AddAzureOpenAIChatCompletion(aiSettings.ChatCompletion.Deployment, aiSettings.ChatCompletion.Endpoint, aiSettings.ChatCompletion.ApiKey); -builder.Services.AddScoped(); +builder.Services.AddSingleton(); builder.Services.AddScoped(); builder.Services.AddEndpointsApiExplorer(); diff --git a/SqlDatabaseVectorSearch/Services/ChatService.cs b/SqlDatabaseVectorSearch/Services/ChatService.cs index ecb9ec6..978b3c4 100644 --- a/SqlDatabaseVectorSearch/Services/ChatService.cs +++ b/SqlDatabaseVectorSearch/Services/ChatService.cs @@ -35,13 +35,12 @@ public class ChatService(IMemoryCache cache, IChatCompletionService chatCompleti public async Task AskQuestionAsync(Guid conversationId, IEnumerable chunks, string question) { - var chat = new ChatHistory("""" - """ + var chat = new ChatHistory(""" You can use only the information provided in this chat to answer questions. If you don't know the answer, reply suggesting to refine the question. For example, if the user asks "What is the capital of France?" and in this chat there isn't information about France, you should reply something like "This information isn't available in the given context". Never answer to questions that are not related to this chat. You must answer in the same language of the user's question. - """"); + """); var prompt = new StringBuilder(""" Using the following information: @@ -52,7 +51,7 @@ public class ChatService(IMemoryCache cache, IChatCompletionService chatCompleti // TODO: Ensure that chunks are not too long, according to the model max token. foreach (var result in chunks) { - prompt.AppendLine(result); + prompt.Append(result); prompt.AppendLine("---"); } diff --git a/SqlDatabaseVectorSearch/SqlDatabaseVectorSearch.csproj b/SqlDatabaseVectorSearch/SqlDatabaseVectorSearch.csproj index 9f89279..690ba34 100644 --- a/SqlDatabaseVectorSearch/SqlDatabaseVectorSearch.csproj +++ b/SqlDatabaseVectorSearch/SqlDatabaseVectorSearch.csproj @@ -11,7 +11,7 @@ - +