From 084177346f914d482762c958b9d5f8fdc0d66b8e Mon Sep 17 00:00:00 2001 From: Marco Minerva Date: Wed, 6 Nov 2024 17:26:09 +0100 Subject: [PATCH] Change ChatService to singleton, update package version - Changed ChatService registration in Program.cs from scoped to singleton. - Modified ChatHistory initialization in ChatService.cs by removing unnecessary line breaks. - Renamed loop variable in ChatService.cs from 'result' to 'text' for better readability. - Updated Microsoft.SemanticKernel package version from 1.26.0 to 1.27.0 in SqlDatabaseVectorSearch.csproj. --- SqlDatabaseVectorSearch/Program.cs | 2 +- SqlDatabaseVectorSearch/Services/ChatService.cs | 9 ++++----- SqlDatabaseVectorSearch/SqlDatabaseVectorSearch.csproj | 2 +- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/SqlDatabaseVectorSearch/Program.cs b/SqlDatabaseVectorSearch/Program.cs index a798f73..044b34f 100644 --- a/SqlDatabaseVectorSearch/Program.cs +++ b/SqlDatabaseVectorSearch/Program.cs @@ -35,7 +35,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..f6b1628 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: @@ -50,9 +49,9 @@ 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) + foreach (var text in chunks) { - prompt.AppendLine(result); + prompt.Append(text); prompt.AppendLine("---"); } diff --git a/SqlDatabaseVectorSearch/SqlDatabaseVectorSearch.csproj b/SqlDatabaseVectorSearch/SqlDatabaseVectorSearch.csproj index 7fc93f7..97a4b8e 100644 --- a/SqlDatabaseVectorSearch/SqlDatabaseVectorSearch.csproj +++ b/SqlDatabaseVectorSearch/SqlDatabaseVectorSearch.csproj @@ -12,7 +12,7 @@ - +