Merge pull request #687 from Joannall/master
Add knowledge generation refinement
This commit is contained in:
commit
e8b7e4361c
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -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>
|
||||
|
||||
|
|
@ -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>
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
using BotSharp.Abstraction.Templating;
|
||||
using BotSharp.Core.Infrastructures;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
namespace BotSharp.Plugin.KnowledgeBase.Functions;
|
||||
|
||||
|
|
@ -20,14 +21,23 @@ public class GenerateKnowledgeFn : IFunctionCallback
|
|||
|
||||
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
|
||||
};
|
||||
|
|
@ -51,6 +61,21 @@ public class GenerateKnowledgeFn : IFunctionCallback
|
|||
{ "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";
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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 }}
|
||||
|
|
@ -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": {
|
||||
|
|
@ -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",
|
||||
|
|
|
|||
Loading…
Reference in a new issue