@@ -5,44 +5,65 @@ namespace BotSharp.Plugin.Planner.Functions;
5
5
public class SummaryPlanFn : IFunctionCallback
6
6
{
7
7
public string Name => "plan_summary" ;
8
+
8
9
private readonly IServiceProvider _services ;
9
- private readonly ILogger _logger ;
10
- private object aiAssistant ;
10
+ private readonly ILogger < SummaryPlanFn > _logger ;
11
11
12
- public SummaryPlanFn ( IServiceProvider services , ILogger < PrimaryStagePlanFn > logger )
12
+ public SummaryPlanFn (
13
+ IServiceProvider services ,
14
+ ILogger < SummaryPlanFn > logger )
13
15
{
14
16
_services = services ;
15
17
_logger = logger ;
16
18
}
17
19
18
20
public async Task < bool > Execute ( RoleDialogModel message )
19
21
{
20
- //debug
22
+ var fn = _services . GetRequiredService < IRoutingService > ( ) ;
23
+ var agentService = _services . GetRequiredService < IAgentService > ( ) ;
21
24
var state = _services . GetRequiredService < IConversationStateService > ( ) ;
25
+
26
+ var currentAgent = await agentService . LoadAgent ( message . CurrentAgentId ) ;
22
27
state . SetState ( "max_tokens" , "4096" ) ;
23
28
24
29
var task = state . GetState ( "requirement_detail" ) ;
25
30
26
- // summarize and generate query
27
- var summaryPlanningPrompt = await GetPlanSummaryPrompt ( task , message ) ;
28
- _logger . LogInformation ( summaryPlanningPrompt ) ;
31
+ // Get DDL
32
+ var steps = message . Content . JsonArrayContent < SecondStagePlan > ( ) ;
33
+
34
+ // Get all the related tables
35
+ var allTables = new List < string > ( ) ;
36
+ foreach ( var step in steps )
37
+ {
38
+ allTables . AddRange ( step . Tables ) ;
39
+ }
40
+ message . Data = allTables . Distinct ( ) . ToList ( ) ;
41
+
42
+ // Get table DDL and stores in content
43
+ var msgCopy = RoleDialogModel . From ( message ) ;
44
+ await fn . InvokeFunction ( "get_table_definition" , msgCopy ) ;
45
+ var ddlStatements = msgCopy . Content ;
46
+
47
+ // Summarize and generate query
48
+ var summaryPlanPrompt = await GetPlanSummaryPrompt ( task , message . Content , ddlStatements ) ;
49
+ _logger . LogInformation ( $ "Summary plan prompt:\r \n { summaryPlanPrompt } ") ;
29
50
30
51
var plannerAgent = new Agent
31
52
{
32
53
Id = BuiltInAgentId . Planner ,
33
- Name = "planner_summary " ,
34
- Instruction = summaryPlanningPrompt ,
35
- TemplateDict = new Dictionary < string , object > ( )
54
+ Name = "Planner Summary " ,
55
+ Instruction = summaryPlanPrompt ,
56
+ LlmConfig = currentAgent . LlmConfig
36
57
} ;
37
- var response_summary = await GetAiResponse ( plannerAgent ) ;
38
58
39
- message . Content = response_summary . Content ;
59
+ var summary = await GetAiResponse ( plannerAgent ) ;
60
+ message . Content = summary . Content ;
40
61
message . StopCompletion = true ;
41
62
42
63
return true ;
43
64
}
44
65
45
- private async Task < string > GetPlanSummaryPrompt ( string task , RoleDialogModel message )
66
+ private async Task < string > GetPlanSummaryPrompt ( string task , string knowledge , string ddlStatement )
46
67
{
47
68
// save to knowledge base
48
69
var agentService = _services . GetRequiredService < IAgentService > ( ) ;
@@ -58,9 +79,9 @@ private async Task<string> GetPlanSummaryPrompt(string task, RoleDialogModel mes
58
79
59
80
return render . Render ( template , new Dictionary < string , object >
60
81
{
61
- { "table_structure" , message . SecondaryContent } , ////check
62
- { "task_description" , task } ,
63
- { "relevant_knowledges" , message . Content } ,
82
+ { "table_structure" , ddlStatement } ,
83
+ { "task_description" , task } ,
84
+ { "relevant_knowledges" , knowledge } ,
64
85
{ "response_format" , responseFormat }
65
86
} ) ;
66
87
}
@@ -69,17 +90,17 @@ private async Task<RoleDialogModel> GetAiResponse(Agent plannerAgent)
69
90
var conv = _services . GetRequiredService < IConversationService > ( ) ;
70
91
var wholeDialogs = conv . GetDialogHistory ( ) ;
71
92
72
- //add "test" to wholeDialogs' last element
93
+ // Add "test" to wholeDialogs' last element
73
94
if ( plannerAgent . Name == "planner_summary" )
74
95
{
75
- //add "test" to wholeDialogs' last element in a new paragraph
96
+ // Add "test" to wholeDialogs' last element in a new paragraph
76
97
wholeDialogs . Last ( ) . Content += "\n \n If the table structure didn't mention auto incremental, the data field id needs to insert id manually and you need to use max(id) instead of LAST_INSERT_ID function.\n For example, you should use SET @id = select max(id) from table;" ;
77
98
wholeDialogs . Last ( ) . Content += "\n \n Try if you can generate a single query to fulfill the needs" ;
78
99
}
79
100
80
101
if ( plannerAgent . Name == "planning_1st" )
81
102
{
82
- //add "test" to wholeDialogs' last element in a new paragraph
103
+ // Add "test" to wholeDialogs' last element in a new paragraph
83
104
wholeDialogs . Last ( ) . Content += "\n \n You must analyze the table description to infer the table relations." ;
84
105
}
85
106
0 commit comments