From d6c0e6630f9906d6c874ff7d83df12e9985e2a99 Mon Sep 17 00:00:00 2001 From: Marco Minerva Date: Mon, 17 Nov 2025 15:49:42 +0100 Subject: [PATCH] Rename and enhance ChatService methods Renamed `CreateQuestionAsync` to `CreateReformulateQuestionAsync` for clearer intent. Updated `CreateChatAsync` to include a new `ChatSystemPrompt` property in `AzureOpenAIPromptExecutionSettings` and modified `ChatHistory` initialization to simplify setup. Removed redundant `prompt` string construction and added user messages directly to `ChatHistory`. Updated `VectorSearchService` to use the renamed `CreateReformulateQuestionAsync` method for consistency. These changes improve code clarity, maintainability, and functionality. --- SqlDatabaseVectorSearch/Services/ChatService.cs | 8 ++++---- SqlDatabaseVectorSearch/Services/VectorSearchService.cs | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/SqlDatabaseVectorSearch/Services/ChatService.cs b/SqlDatabaseVectorSearch/Services/ChatService.cs index 00eafa7..d0c68b3 100644 --- a/SqlDatabaseVectorSearch/Services/ChatService.cs +++ b/SqlDatabaseVectorSearch/Services/ChatService.cs @@ -86,7 +86,7 @@ public class ChatService(IChatCompletionService chatCompletionService, Tokenizer Remember to ALWAYS end your answer with a period followed by a space before adding citations. """; - public async Task CreateQuestionAsync(Guid conversationId, string question, CancellationToken cancellationToken = default) + public async Task CreateReformulateQuestionAsync(Guid conversationId, string question, CancellationToken cancellationToken = default) { var chat = await GetChatHistoryAsync(conversationId, cancellationToken); @@ -170,11 +170,10 @@ public class ChatService(IChatCompletionService chatCompletionService, Tokenizer { var settings = new AzureOpenAIPromptExecutionSettings { - MaxTokens = appSettings.MaxOutputTokens + MaxTokens = appSettings.MaxOutputTokens, + ChatSystemPrompt = systemPromptForAnswering }; - var chat = new ChatHistory(systemPromptForAnswering); - var prompt = new StringBuilder($""" Answer the following question: --- @@ -210,6 +209,7 @@ public class ChatService(IChatCompletionService chatCompletionService, Tokenizer } } + var chat = new ChatHistory(); chat.AddUserMessage(prompt.ToString()); return (chat, settings); diff --git a/SqlDatabaseVectorSearch/Services/VectorSearchService.cs b/SqlDatabaseVectorSearch/Services/VectorSearchService.cs index cc49944..f18d39e 100644 --- a/SqlDatabaseVectorSearch/Services/VectorSearchService.cs +++ b/SqlDatabaseVectorSearch/Services/VectorSearchService.cs @@ -143,7 +143,7 @@ public partial class VectorSearchService(IServiceProvider serviceProvider, Appli private async Task<(ChatResponse ReformulatedQuestion, int EmbeddingTokenCount, IEnumerable Chunks)> CreateContextAsync(Question question, bool reformulate, CancellationToken cancellationToken) { // Reformulate the question taking into account the context of the chat to perform keyword search and embeddings. - var reformulatedQuestion = reformulate ? await chatService.CreateQuestionAsync(question.ConversationId, question.Text, cancellationToken) : new(question.Text); + var reformulatedQuestion = reformulate ? await chatService.CreateReformulateQuestionAsync(question.ConversationId, question.Text, cancellationToken) : new(question.Text); var embeddingTokenCount = tokenizerService.CountEmbeddingTokens(reformulatedQuestion.Text!); logger.LogDebug("Embedding Token Count: {EmbeddingTokenCount}", embeddingTokenCount);