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.
This commit is contained in:
Marco Minerva
2024-11-06 17:26:09 +01:00
parent c18a6b4e03
commit 084177346f
3 changed files with 6 additions and 7 deletions
+1 -1
View File
@@ -35,7 +35,7 @@ builder.Services.AddKernel()
.AddAzureOpenAITextEmbeddingGeneration(aiSettings.Embedding.Deployment, aiSettings.Embedding.Endpoint, aiSettings.Embedding.ApiKey, dimensions: aiSettings.Embedding.Dimensions) .AddAzureOpenAITextEmbeddingGeneration(aiSettings.Embedding.Deployment, aiSettings.Embedding.Endpoint, aiSettings.Embedding.ApiKey, dimensions: aiSettings.Embedding.Dimensions)
.AddAzureOpenAIChatCompletion(aiSettings.ChatCompletion.Deployment, aiSettings.ChatCompletion.Endpoint, aiSettings.ChatCompletion.ApiKey); .AddAzureOpenAIChatCompletion(aiSettings.ChatCompletion.Deployment, aiSettings.ChatCompletion.Endpoint, aiSettings.ChatCompletion.ApiKey);
builder.Services.AddScoped<ChatService>(); builder.Services.AddSingleton<ChatService>();
builder.Services.AddScoped<VectorSearchService>(); builder.Services.AddScoped<VectorSearchService>();
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
@@ -35,13 +35,12 @@ public class ChatService(IMemoryCache cache, IChatCompletionService chatCompleti
public async Task<string> AskQuestionAsync(Guid conversationId, IEnumerable<string> chunks, string question) public async Task<string> AskQuestionAsync(Guid conversationId, IEnumerable<string> 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. 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". 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. Never answer to questions that are not related to this chat.
You must answer in the same language of the user's question. You must answer in the same language of the user's question.
""""); """);
var prompt = new StringBuilder(""" var prompt = new StringBuilder("""
Using the following information: 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. // 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("---"); prompt.AppendLine("---");
} }
@@ -12,7 +12,7 @@
<PackageReference Include="EntityFrameworkCore.Exceptions.SqlServer" Version="8.1.3" /> <PackageReference Include="EntityFrameworkCore.Exceptions.SqlServer" Version="8.1.3" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.10" /> <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.10" /> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.10" />
<PackageReference Include="Microsoft.SemanticKernel" Version="1.26.0" /> <PackageReference Include="Microsoft.SemanticKernel" Version="1.27.0" />
<PackageReference Include="MinimalHelpers.OpenApi" Version="2.0.17" /> <PackageReference Include="MinimalHelpers.OpenApi" Version="2.0.17" />
<PackageReference Include="PdfPig" Version="0.1.9" /> <PackageReference Include="PdfPig" Version="0.1.9" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.9.0" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.9.0" />