@@ -5,44 +5,65 @@ namespace BotSharp.Plugin.Planner.Functions;
55public class SummaryPlanFn : IFunctionCallback
66{
77 public string Name => "plan_summary" ;
8+
89 private readonly IServiceProvider _services ;
9- private readonly ILogger _logger ;
10- private object aiAssistant ;
10+ private readonly ILogger < SummaryPlanFn > _logger ;
1111
12- public SummaryPlanFn ( IServiceProvider services , ILogger < PrimaryStagePlanFn > logger )
12+ public SummaryPlanFn (
13+ IServiceProvider services ,
14+ ILogger < SummaryPlanFn > logger )
1315 {
1416 _services = services ;
1517 _logger = logger ;
1618 }
1719
1820 public async Task < bool > Execute ( RoleDialogModel message )
1921 {
20- //debug
22+ var fn = _services . GetRequiredService < IRoutingService > ( ) ;
23+ var agentService = _services . GetRequiredService < IAgentService > ( ) ;
2124 var state = _services . GetRequiredService < IConversationStateService > ( ) ;
25+
26+ var currentAgent = await agentService . LoadAgent ( message . CurrentAgentId ) ;
2227 state . SetState ( "max_tokens" , "4096" ) ;
2328
2429 var task = state . GetState ( "requirement_detail" ) ;
2530
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 } ") ;
2950
3051 var plannerAgent = new Agent
3152 {
3253 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
3657 } ;
37- var response_summary = await GetAiResponse ( plannerAgent ) ;
3858
39- message . Content = response_summary . Content ;
59+ var summary = await GetAiResponse ( plannerAgent ) ;
60+ message . Content = summary . Content ;
4061 message . StopCompletion = true ;
4162
4263 return true ;
4364 }
4465
45- private async Task < string > GetPlanSummaryPrompt ( string task , RoleDialogModel message )
66+ private async Task < string > GetPlanSummaryPrompt ( string task , string knowledge , string ddlStatement )
4667 {
4768 // save to knowledge base
4869 var agentService = _services . GetRequiredService < IAgentService > ( ) ;
@@ -58,9 +79,9 @@ private async Task<string> GetPlanSummaryPrompt(string task, RoleDialogModel mes
5879
5980 return render . Render ( template , new Dictionary < string , object >
6081 {
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 } ,
6485 { "response_format" , responseFormat }
6586 } ) ;
6687 }
@@ -69,17 +90,17 @@ private async Task<RoleDialogModel> GetAiResponse(Agent plannerAgent)
6990 var conv = _services . GetRequiredService < IConversationService > ( ) ;
7091 var wholeDialogs = conv . GetDialogHistory ( ) ;
7192
72- //add "test" to wholeDialogs' last element
93+ // Add "test" to wholeDialogs' last element
7394 if ( plannerAgent . Name == "planner_summary" )
7495 {
75- //add "test" to wholeDialogs' last element in a new paragraph
96+ // Add "test" to wholeDialogs' last element in a new paragraph
7697 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;" ;
7798 wholeDialogs . Last ( ) . Content += "\n \n Try if you can generate a single query to fulfill the needs" ;
7899 }
79100
80101 if ( plannerAgent . Name == "planning_1st" )
81102 {
82- //add "test" to wholeDialogs' last element in a new paragraph
103+ // Add "test" to wholeDialogs' last element in a new paragraph
83104 wholeDialogs . Last ( ) . Content += "\n \n You must analyze the table description to infer the table relations." ;
84105 }
85106
0 commit comments