Skip to content

Commit d7dc007

Browse files
authored
Merge pull request #23 from vtjc2002/main
refactor to allowKeywordExtractingHandler to read from prompt txt
2 parents 929229f + 766ff86 commit d7dc007

File tree

3 files changed

+10
-24
lines changed

3 files changed

+10
-24
lines changed

App/kernel-memory/service/Abstractions/Constants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,5 @@ public static class Summary
163163
// Standard prompt names
164164
public const string PromptNamesSummarize = "summarize";
165165
public const string PromptNamesAnswerWithFacts = "answer-with-facts";
166+
public const string PromptNamesExtractKeywords = "extract-keywords";
166167
}

App/kernel-memory/service/Core/Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
<ItemGroup>
4343
<EmbeddedResource Include="Prompts\summarize.txt" />
4444
<EmbeddedResource Include="Prompts\answer-with-facts.txt" />
45+
<EmbeddedResource Include="Prompts\extract-keywords.txt" />
4546
</ItemGroup>
4647

4748
<ItemGroup>

App/kernel-memory/service/Core/Handlers/KeywordExtractingHandler.cs

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using Microsoft.SemanticKernel;
1212
using Microsoft.SemanticKernel.ChatCompletion;
1313
using static Microsoft.KernelMemory.Pipeline.DataPipeline;
14+
using Microsoft.KernelMemory.Prompts;
1415

1516
namespace Microsoft.KernelMemory.Handlers;
1617

@@ -21,11 +22,13 @@ public sealed class KeywordExtractingHandler : IPipelineStepHandler
2122
private readonly IPipelineOrchestrator _orchestrator;
2223
private readonly Kernel _kernel;
2324
private readonly KernelMemoryConfig? _config = null;
25+
private readonly string _extractKeywordPrompt;
2426

2527
public KeywordExtractingHandler(
2628
string stepName,
2729
IPipelineOrchestrator orchestrator,
2830
KernelMemoryConfig config = null,
31+
IPromptProvider? promptProvider = null,
2932
ILoggerFactory? loggerFactory = null
3033
)
3134
{
@@ -34,6 +37,10 @@ public KeywordExtractingHandler(
3437
this._orchestrator = orchestrator;
3538
this._config = config;
3639

40+
promptProvider ??= new EmbeddedPromptProvider();
41+
42+
this._extractKeywordPrompt = promptProvider.ReadPrompt(Constants.PromptNamesExtractKeywords);
43+
3744
//init Semantic Kernel
3845
this._kernel = Kernel.CreateBuilder()
3946
.AddAzureOpenAIChatCompletion(deploymentName: (string)this._config.Services["AzureOpenAIText"]["Deployment"],
@@ -79,30 +86,7 @@ public KeywordExtractingHandler(
7986
var chat = this._kernel.GetRequiredService<IChatCompletionService>();
8087
var chatHistory = new ChatHistory();
8188

82-
var systemMessage = """
83-
You are an assistant to analyze Content and Extract Tags by Content.
84-
[EXTRACT TAGS RULES]
85-
IT SHOULD BE A LIST OF DICTIONARIES WITH CATEGORY AND TAGS
86-
TAGS SHOULD BE CATEGORY SPECIFIC
87-
TAGS SHOULD BE A LIST OF STRINGS
88-
TAGS COUNT CAN BE UP TO 10 UNDER A CATEGORY
89-
CATEGORY COUNT CAN BE UP TO 10
90-
DON'T ADD ANY MARKDOWN EXPRESSION IN YOUR RESPONSE
91-
[END RULES]
92-
93-
[EXAMPLE]
94-
[
95-
{
96-
[category1": ["tag1", "tag2", "tag3"]
97-
},
98-
{
99-
"category2": ["tag1", "tag2", "tag3"]
100-
}
101-
]
102-
[END EXAMPLE]
103-
""";
104-
105-
chatHistory.AddSystemMessage(systemMessage);
89+
chatHistory.AddSystemMessage(this._extractKeywordPrompt);
10690
chatHistory.AddUserMessage($"Extract tags from this content : {extactedFileContent} \n The format should be Json but Markdown expression.");
10791

10892
var executionParam = new PromptExecutionSettings()

0 commit comments

Comments
 (0)