Skip to content

Commit

Permalink
introduced context in query
Browse files Browse the repository at this point in the history
  • Loading branch information
alkampfergit committed Nov 24, 2024
1 parent 477c591 commit 83d4f63
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ public async Task RunSample2()
{
var options = new UserQueryOptions("default");
userQuestion = new UserQuestion(options, question);
userQuestion.Context = "Computer security and IT Security";
}
else
{
Expand Down Expand Up @@ -316,7 +317,7 @@ private static IKernelMemoryBuilder CreateBasicKernelMemoryBuilder(
services.AddSingleton<HyDeQueryHandler>();
var hydeConfig = new HiDeQueryHandlerConfiguration()
{
Prompt = "Given a question, generate a paragraph of text that answers the question in the context of computer security and IT security"
Prompt = "Given a question, generate a paragraph of text that answers the question"
};
services.AddSingleton(hydeConfig);
services.AddSingleton<KeywordSearchQueryHandler>();
Expand Down
5 changes: 5 additions & 0 deletions src/KernelMemory.Extensions/QueryPipeline/HyDeQueryHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ protected override async Task OnHandleAsync(UserQuestion userQuestion, Cancellat
// Perform a vector search in default memory
StringBuilder prompt = new StringBuilder(_configuration.Prompt.Length + userQuestion.Question.Length + "Question: ".Length + "Paragraph: ".Length + 20);
prompt.AppendLine(_configuration.Prompt);
if (!string.IsNullOrEmpty(userQuestion.Context))
{
prompt.AppendLine("The context of the question is: " + userQuestion.Context);
}

prompt.AppendLine("Question: " + userQuestion.Question);
prompt.AppendLine("Paragraph: ");

Expand Down
7 changes: 7 additions & 0 deletions src/KernelMemory.Extensions/QueryPipeline/UserQuestion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ internal void AddReRanker(IReRanker reRanker)

public string Question { get; private set; }

/// <summary>
/// If we know the context in which the query live, we can
/// use it during the search, especially with query enhancer
/// like HyDe or similar techniques.
/// </summary>
public string Context { get; set; }

/// <summary>
/// if the question was rewritten, this stores the original question made by the user.
/// </summary>
Expand Down

0 comments on commit 83d4f63

Please sign in to comment.