Skip to content

Add knowledge generation refinement #687

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace BotSharp.Abstraction.Knowledges.Models;

public class GenerateKnowledge
{
[JsonPropertyName("question")]
public string Question { get; set; } = string.Empty;

[JsonPropertyName("answer")]
public string Answer { get; set; } = string.Empty;

[JsonPropertyName("refined_collection")]
public string RefinedCollection { get; set; } = string.Empty;

[JsonPropertyName("refine_answer")]
public Boolean RefineAnswer { get; set; } = false;

[JsonPropertyName("existing_answer")]
public string ExistingAnswer { get; set; } = string.Empty;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<None Remove="data\agents\01acc3e5-0af7-49e6-ad7a-a760bd12dc40\functions\memorize_knowledge.json" />
<None Remove="data\agents\01acc3e5-0af7-49e6-ad7a-a760bd12dc40\instructions\instruction.liquid" />
<None Remove="data\agents\01acc3e5-0af7-49e6-ad7a-a760bd12dc40\templates\knowledge.generation.liquid" />
<None Remove="data\agents\01acc3e5-0af7-49e6-ad7a-a760bd12dc40\templates\knowledge.generation.refine.liquid" />
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\knowledge_retrieval.fn.liquid" />
</ItemGroup>

Expand All @@ -38,6 +39,9 @@
<Content Include="data\agents\01acc3e5-0af7-49e6-ad7a-a760bd12dc40\instructions\instruction.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\01acc3e5-0af7-49e6-ad7a-a760bd12dc40\templates\knowledge.generation.refine.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\01acc3e5-0af7-49e6-ad7a-a760bd12dc40\templates\knowledge.generation.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using BotSharp.Abstraction.Templating;
using BotSharp.Core.Infrastructures;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;

namespace BotSharp.Plugin.KnowledgeBase.Functions;

Expand All @@ -20,14 +21,23 @@ public GenerateKnowledgeFn(IServiceProvider services, KnowledgeBaseSettings sett

public async Task<bool> Execute(RoleDialogModel message)
{
var args = JsonSerializer.Deserialize<ExtractedKnowledge>(message.FunctionArgs ?? "{}");
var args = JsonSerializer.Deserialize<GenerateKnowledge>(message.FunctionArgs ?? "{}");
var agentService = _services.GetRequiredService<IAgentService>();
var llmAgent = await agentService.GetAgent(BuiltInAgentId.Planner);
var generateKnowledgePrompt = await GetGenerateKnowledgePrompt(args.Question, args.Answer);
var refineKnowledge = args.RefinedCollection;
String generateKnowledgePrompt;
if (args.RefineAnswer == true)
{
generateKnowledgePrompt = await GetRefineKnowledgePrompt(args.Question, args.Answer, args.ExistingAnswer);
}
else
{
generateKnowledgePrompt = await GetGenerateKnowledgePrompt(args.Question, args.Answer);
}
var agent = new Agent
{
Id = message.CurrentAgentId ?? string.Empty,
Name = "sqlDriver_DictionarySearch",
Name = "knowledge_generator",
Instruction = generateKnowledgePrompt,
LlmConfig = llmAgent.LlmConfig
};
Expand All @@ -51,6 +61,21 @@ private async Task<string> GetGenerateKnowledgePrompt(string userQuestions, stri
{ "sql_answer", sqlAnswer },
});
}
private async Task<string> GetRefineKnowledgePrompt(string userQuestion, string sqlAnswer, string existionAnswer)
{
var agentService = _services.GetRequiredService<IAgentService>();
var render = _services.GetRequiredService<ITemplateRender>();

var agent = await agentService.GetAgent(BuiltInAgentId.Learner);
var template = agent.Templates.FirstOrDefault(x => x.Name == "knowledge.generation.refine")?.Content ?? string.Empty;

return render.Render(template, new Dictionary<string, object>
{
{ "user_question", userQuestion },
{ "new_answer", sqlAnswer },
{ "existing_answer", existionAnswer}
});
}
private async Task<RoleDialogModel> GetAiResponse(Agent agent)
{
var text = "Generate question and answer pair";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
You are a knowledge generator for knowledge base. Extract the answer in "SQL Answer" to answer the User Questions.
You are a knowledge extractor for knowledge base. Extract the answer in "SQL Answer" to answer the User Questions.
* Replace alias with the actual table name. Output json array only, formatting as [{"question":"string", "answer":""}].
* Skip the question/answer for tmp table.
* Don't include tmp table in the answer.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
You are a knowledge extractor for knowledge base. Utilize the new answer and existing answer to generate the final integrated answer.
Output json array only, formatting as [{"question":"", "answer":""}]. Replace the new line with \r\n.
* Don't loss any knowledge in the existing answer.

=====
User Question:
{{ user_question }}

=====
New Answer:
{{ new_answer }}

=====
Existing Answer:
{{ existing_answer }}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "verify_dictionary_term",
"description": "Get id from dictionary table by keyword. Call this function only if need_lookup_dictionary is True",
"description": "Get id from dictionary table by keyword. Call this function only if need_lookup_dictionary is true and is_insert is false",
"parameters": {
"type": "object",
"properties": {
Expand All @@ -12,6 +12,10 @@
"type": "string",
"description": "the reason why you need to call verify_dictionary_term"
},
"is_insert": {
"type": "boolean",
"description": "if SQL statement is inserting."
},
"tables": {
"type": "array",
"description": "all related tables",
Expand Down