Skip to content

Commit e8b7e43

Browse files
authored
Merge pull request #687 from Joannall/master
Add knowledge generation refinement
2 parents 008dede + 5350f59 commit e8b7e43

File tree

6 files changed

+72
-5
lines changed

6 files changed

+72
-5
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace BotSharp.Abstraction.Knowledges.Models;
2+
3+
public class GenerateKnowledge
4+
{
5+
[JsonPropertyName("question")]
6+
public string Question { get; set; } = string.Empty;
7+
8+
[JsonPropertyName("answer")]
9+
public string Answer { get; set; } = string.Empty;
10+
11+
[JsonPropertyName("refined_collection")]
12+
public string RefinedCollection { get; set; } = string.Empty;
13+
14+
[JsonPropertyName("refine_answer")]
15+
public Boolean RefineAnswer { get; set; } = false;
16+
17+
[JsonPropertyName("existing_answer")]
18+
public string ExistingAnswer { get; set; } = string.Empty;
19+
}

src/Plugins/BotSharp.Plugin.KnowledgeBase/BotSharp.Plugin.KnowledgeBase.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<None Remove="data\agents\01acc3e5-0af7-49e6-ad7a-a760bd12dc40\functions\memorize_knowledge.json" />
2323
<None Remove="data\agents\01acc3e5-0af7-49e6-ad7a-a760bd12dc40\instructions\instruction.liquid" />
2424
<None Remove="data\agents\01acc3e5-0af7-49e6-ad7a-a760bd12dc40\templates\knowledge.generation.liquid" />
25+
<None Remove="data\agents\01acc3e5-0af7-49e6-ad7a-a760bd12dc40\templates\knowledge.generation.refine.liquid" />
2526
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\knowledge_retrieval.fn.liquid" />
2627
</ItemGroup>
2728

@@ -38,6 +39,9 @@
3839
<Content Include="data\agents\01acc3e5-0af7-49e6-ad7a-a760bd12dc40\instructions\instruction.liquid">
3940
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
4041
</Content>
42+
<Content Include="data\agents\01acc3e5-0af7-49e6-ad7a-a760bd12dc40\templates\knowledge.generation.refine.liquid">
43+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
44+
</Content>
4145
<Content Include="data\agents\01acc3e5-0af7-49e6-ad7a-a760bd12dc40\templates\knowledge.generation.liquid">
4246
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
4347
</Content>

src/Plugins/BotSharp.Plugin.KnowledgeBase/Functions/GenerateKnowledgeFn.cs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using BotSharp.Abstraction.Templating;
22
using BotSharp.Core.Infrastructures;
3+
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
34

45
namespace BotSharp.Plugin.KnowledgeBase.Functions;
56

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

2122
public async Task<bool> Execute(RoleDialogModel message)
2223
{
23-
var args = JsonSerializer.Deserialize<ExtractedKnowledge>(message.FunctionArgs ?? "{}");
24+
var args = JsonSerializer.Deserialize<GenerateKnowledge>(message.FunctionArgs ?? "{}");
2425
var agentService = _services.GetRequiredService<IAgentService>();
2526
var llmAgent = await agentService.GetAgent(BuiltInAgentId.Planner);
26-
var generateKnowledgePrompt = await GetGenerateKnowledgePrompt(args.Question, args.Answer);
27+
var refineKnowledge = args.RefinedCollection;
28+
String generateKnowledgePrompt;
29+
if (args.RefineAnswer == true)
30+
{
31+
generateKnowledgePrompt = await GetRefineKnowledgePrompt(args.Question, args.Answer, args.ExistingAnswer);
32+
}
33+
else
34+
{
35+
generateKnowledgePrompt = await GetGenerateKnowledgePrompt(args.Question, args.Answer);
36+
}
2737
var agent = new Agent
2838
{
2939
Id = message.CurrentAgentId ?? string.Empty,
30-
Name = "sqlDriver_DictionarySearch",
40+
Name = "knowledge_generator",
3141
Instruction = generateKnowledgePrompt,
3242
LlmConfig = llmAgent.LlmConfig
3343
};
@@ -51,6 +61,21 @@ private async Task<string> GetGenerateKnowledgePrompt(string userQuestions, stri
5161
{ "sql_answer", sqlAnswer },
5262
});
5363
}
64+
private async Task<string> GetRefineKnowledgePrompt(string userQuestion, string sqlAnswer, string existionAnswer)
65+
{
66+
var agentService = _services.GetRequiredService<IAgentService>();
67+
var render = _services.GetRequiredService<ITemplateRender>();
68+
69+
var agent = await agentService.GetAgent(BuiltInAgentId.Learner);
70+
var template = agent.Templates.FirstOrDefault(x => x.Name == "knowledge.generation.refine")?.Content ?? string.Empty;
71+
72+
return render.Render(template, new Dictionary<string, object>
73+
{
74+
{ "user_question", userQuestion },
75+
{ "new_answer", sqlAnswer },
76+
{ "existing_answer", existionAnswer}
77+
});
78+
}
5479
private async Task<RoleDialogModel> GetAiResponse(Agent agent)
5580
{
5681
var text = "Generate question and answer pair";

src/Plugins/BotSharp.Plugin.KnowledgeBase/data/agents/01acc3e5-0af7-49e6-ad7a-a760bd12dc40/templates/knowledge.generation.liquid

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
You are a knowledge generator for knowledge base. Extract the answer in "SQL Answer" to answer the User Questions.
1+
You are a knowledge extractor for knowledge base. Extract the answer in "SQL Answer" to answer the User Questions.
22
* Replace alias with the actual table name. Output json array only, formatting as [{"question":"string", "answer":""}].
33
* Skip the question/answer for tmp table.
44
* Don't include tmp table in the answer.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
You are a knowledge extractor for knowledge base. Utilize the new answer and existing answer to generate the final integrated answer.
2+
Output json array only, formatting as [{"question":"", "answer":""}]. Replace the new line with \r\n.
3+
* Don't loss any knowledge in the existing answer.
4+
5+
=====
6+
User Question:
7+
{{ user_question }}
8+
9+
=====
10+
New Answer:
11+
{{ new_answer }}
12+
13+
=====
14+
Existing Answer:
15+
{{ existing_answer }}

src/Plugins/BotSharp.Plugin.SqlDriver/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/functions/sql_dictionary_lookup.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "verify_dictionary_term",
3-
"description": "Get id from dictionary table by keyword. Call this function only if need_lookup_dictionary is True",
3+
"description": "Get id from dictionary table by keyword. Call this function only if need_lookup_dictionary is true and is_insert is false",
44
"parameters": {
55
"type": "object",
66
"properties": {
@@ -12,6 +12,10 @@
1212
"type": "string",
1313
"description": "the reason why you need to call verify_dictionary_term"
1414
},
15+
"is_insert": {
16+
"type": "boolean",
17+
"description": "if SQL statement is inserting."
18+
},
1519
"tables": {
1620
"type": "array",
1721
"description": "all related tables",

0 commit comments

Comments
 (0)