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.
This commit is contained in:
Marco Minerva
2025-11-17 15:49:42 +01:00
parent dba2fb1c41
commit d6c0e6630f
2 changed files with 5 additions and 5 deletions
@@ -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<ChatResponse> CreateQuestionAsync(Guid conversationId, string question, CancellationToken cancellationToken = default)
public async Task<ChatResponse> 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);
@@ -143,7 +143,7 @@ public partial class VectorSearchService(IServiceProvider serviceProvider, Appli
private async Task<(ChatResponse ReformulatedQuestion, int EmbeddingTokenCount, IEnumerable<Entities.DocumentChunk> 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);