2024-08-29 21:33:32 +00:00
using BotSharp.Plugin.Planner.TwoStaging.Models ;
namespace BotSharp.Plugin.Planner.Functions ;
public class SummaryPlanFn : IFunctionCallback
{
public string Name = > "plan_summary" ;
private readonly IServiceProvider _services ;
private readonly ILogger _logger ;
private object aiAssistant ;
public SummaryPlanFn ( IServiceProvider services , ILogger < PrimaryStagePlanFn > logger )
{
_services = services ;
_logger = logger ;
}
public async Task < bool > Execute ( RoleDialogModel message )
{
//debug
var state = _services . GetRequiredService < IConversationStateService > ( ) ;
state . SetState ( "max_tokens" , "4096" ) ;
var task = state . GetState ( "requirement_detail" ) ;
// summarize and generate query
var summaryPlanningPrompt = await GetPlanSummaryPrompt ( task , message ) ;
_logger . LogInformation ( summaryPlanningPrompt ) ;
2024-08-30 21:12:20 +00:00
2024-08-29 21:33:32 +00:00
var plannerAgent = new Agent
{
Id = BuiltInAgentId . Planner ,
Name = "planner_summary" ,
Instruction = summaryPlanningPrompt ,
TemplateDict = new Dictionary < string , object > ( )
} ;
2024-08-30 21:12:20 +00:00
var response_summary = await GetAiResponse ( plannerAgent ) ;
2024-08-29 21:33:32 +00:00
message . Content = response_summary . Content ;
message . StopCompletion = true ;
return true ;
}
private async Task < string > GetPlanSummaryPrompt ( string task , RoleDialogModel message )
{
// save to knowledge base
var agentService = _services . GetRequiredService < IAgentService > ( ) ;
var render = _services . GetRequiredService < ITemplateRender > ( ) ;
2024-08-30 21:12:20 +00:00
var aiAssistant = await agentService . GetAgent ( BuiltInAgentId . Planner ) ;
var template = aiAssistant . Templates . FirstOrDefault ( x = > x . Name = = "two_stage.summarize" ) ? . Content ? ? string . Empty ;
2024-08-29 21:33:32 +00:00
var responseFormat = JsonSerializer . Serialize ( new FirstStagePlan
{
Parameters = [ JsonDocument . Parse ( "{}" ) ] ,
Results = [ "" ]
} ) ;
return render . Render ( template , new Dictionary < string , object >
{
{ "table_structure" , message . SecondaryContent } , ////check
{ "task_description" , task } ,
{ "relevant_knowledges" , message . Content } ,
{ "response_format" , responseFormat }
} ) ;
}
2024-08-30 21:12:20 +00:00
private async Task < RoleDialogModel > GetAiResponse ( Agent plannerAgent )
2024-08-29 21:33:32 +00:00
{
var conv = _services . GetRequiredService < IConversationService > ( ) ;
var wholeDialogs = conv . GetDialogHistory ( ) ;
2024-08-30 21:12:20 +00:00
2024-08-29 21:33:32 +00:00
//add "test" to wholeDialogs' last element
2024-08-30 21:12:20 +00:00
if ( plannerAgent . Name = = "planner_summary" )
2024-08-29 21:33:32 +00:00
{
//add "test" to wholeDialogs' last element in a new paragraph
wholeDialogs . Last ( ) . Content + = "\n\nIf 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.\nFor example, you should use SET @id = select max(id) from table;" ;
wholeDialogs . Last ( ) . Content + = "\n\nTry if you can generate a single query to fulfill the needs" ;
}
2024-08-30 21:12:20 +00:00
2024-08-29 21:33:32 +00:00
if ( plannerAgent . Name = = "planning_1st" )
{
//add "test" to wholeDialogs' last element in a new paragraph
wholeDialogs . Last ( ) . Content + = "\n\nYou must analyze the table description to infer the table relations." ;
}
var completion = CompletionProvider . GetChatCompletion ( _services ,
provider : plannerAgent . LlmConfig . Provider ,
model : plannerAgent . LlmConfig . Model ) ;
return await completion . GetChatCompletions ( plannerAgent , wholeDialogs ) ;
}
}