Skip to content

Commit 83d4f63

Browse files
committed
introduced context in query
1 parent 477c591 commit 83d4f63

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

src/KernelMemory.Extensions.ConsoleTest/Samples/CustomSearchPipelineBase.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ public async Task RunSample2()
136136
{
137137
var options = new UserQueryOptions("default");
138138
userQuestion = new UserQuestion(options, question);
139+
userQuestion.Context = "Computer security and IT Security";
139140
}
140141
else
141142
{
@@ -316,7 +317,7 @@ private static IKernelMemoryBuilder CreateBasicKernelMemoryBuilder(
316317
services.AddSingleton<HyDeQueryHandler>();
317318
var hydeConfig = new HiDeQueryHandlerConfiguration()
318319
{
319-
Prompt = "Given a question, generate a paragraph of text that answers the question in the context of computer security and IT security"
320+
Prompt = "Given a question, generate a paragraph of text that answers the question"
320321
};
321322
services.AddSingleton(hydeConfig);
322323
services.AddSingleton<KeywordSearchQueryHandler>();

src/KernelMemory.Extensions/QueryPipeline/HyDeQueryHandler.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ protected override async Task OnHandleAsync(UserQuestion userQuestion, Cancellat
4747
// Perform a vector search in default memory
4848
StringBuilder prompt = new StringBuilder(_configuration.Prompt.Length + userQuestion.Question.Length + "Question: ".Length + "Paragraph: ".Length + 20);
4949
prompt.AppendLine(_configuration.Prompt);
50+
if (!string.IsNullOrEmpty(userQuestion.Context))
51+
{
52+
prompt.AppendLine("The context of the question is: " + userQuestion.Context);
53+
}
54+
5055
prompt.AppendLine("Question: " + userQuestion.Question);
5156
prompt.AppendLine("Paragraph: ");
5257

src/KernelMemory.Extensions/QueryPipeline/UserQuestion.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ internal void AddReRanker(IReRanker reRanker)
3333

3434
public string Question { get; private set; }
3535

36+
/// <summary>
37+
/// If we know the context in which the query live, we can
38+
/// use it during the search, especially with query enhancer
39+
/// like HyDe or similar techniques.
40+
/// </summary>
41+
public string Context { get; set; }
42+
3643
/// <summary>
3744
/// if the question was rewritten, this stores the original question made by the user.
3845
/// </summary>

0 commit comments

Comments
 (0)