diff --git a/src/Plugins/BotSharp.Plugin.ChartHandler/Functions/PlotChartFn.cs b/src/Plugins/BotSharp.Plugin.ChartHandler/Functions/PlotChartFn.cs index bd4feb0a..d1fc5943 100644 --- a/src/Plugins/BotSharp.Plugin.ChartHandler/Functions/PlotChartFn.cs +++ b/src/Plugins/BotSharp.Plugin.ChartHandler/Functions/PlotChartFn.cs @@ -1,4 +1,5 @@ using BotSharp.Abstraction.Messaging.Models.RichContent.Template; +using BotSharp.Abstraction.Routing; namespace BotSharp.Plugin.ChartHandler.Functions; @@ -26,6 +27,7 @@ public class PlotChartFn : IFunctionCallback { var agentService = _services.GetRequiredService(); var convService = _services.GetRequiredService(); + var routingCtx = _services.GetRequiredService(); var args = JsonSerializer.Deserialize(message.FunctionArgs); @@ -44,15 +46,21 @@ public class PlotChartFn : IFunctionCallback } }; - var response = await GetChatCompletion(innerAgent, - [ - new RoleDialogModel(AgentRole.User, "Please follow the instruction to generate the javascript code.") - { - CurrentAgentId = message.CurrentAgentId, - MessageId = message.MessageId - } - ]); + var dialogs = routingCtx.GetDialogs(); + if (dialogs.IsNullOrEmpty()) + { + dialogs = convService.GetDialogHistory(); + } + var messageLimit = _settings.ChartPlot?.MessageLimit > 0 ? _settings.ChartPlot.MessageLimit.Value : 50; + dialogs = dialogs.TakeLast(messageLimit).ToList(); + dialogs.Add(new RoleDialogModel(AgentRole.User, "Please follow the instruction and chat context to generate valid javascript code.") + { + CurrentAgentId = message.CurrentAgentId, + MessageId = message.MessageId + }); + + var response = await GetChatCompletion(innerAgent, dialogs); var obj = response.JsonContent(); message.Content = obj?.GreetingMessage ?? "Here is the chart you ask for:"; message.RichContent = new RichContent diff --git a/src/Plugins/BotSharp.Plugin.ChartHandler/Settings/ChartHandlerSettings.cs b/src/Plugins/BotSharp.Plugin.ChartHandler/Settings/ChartHandlerSettings.cs index ad13e0ef..1218b397 100644 --- a/src/Plugins/BotSharp.Plugin.ChartHandler/Settings/ChartHandlerSettings.cs +++ b/src/Plugins/BotSharp.Plugin.ChartHandler/Settings/ChartHandlerSettings.cs @@ -11,4 +11,5 @@ public class ChartPlotSetting public string? LlmModel { get; set; } public int? MaxOutputTokens { get; set; } public string? ReasoningEffortLevel { get; set; } + public int? MessageLimit { get; set; } } diff --git a/src/Plugins/BotSharp.Plugin.ChartHandler/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/templates/util-chart-plot_instruction.liquid b/src/Plugins/BotSharp.Plugin.ChartHandler/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/templates/util-chart-plot_instruction.liquid index e89ace07..af0a04a9 100644 --- a/src/Plugins/BotSharp.Plugin.ChartHandler/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/templates/util-chart-plot_instruction.liquid +++ b/src/Plugins/BotSharp.Plugin.ChartHandler/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/templates/util-chart-plot_instruction.liquid @@ -42,5 +42,5 @@ You must output the response in the following JSON format: { "greeting_message": "A short polite message that informs user that the charts have been generated.", "js_code": "The javascript code that can generate the charts as requested.", - "report_summary": "Generate an insightful summary report in markdown format based on the data. Bold all key findings and use level-4 headings or smaller (####, #####, etc.) to structure the content with clear titles." + "report_summary": "Generate an insightful summary report in markdown format based on the data. You can summarize using one or multiple titles and list the key findings under each title. Bold all key findings and use level-4 headings or smaller (####, #####, etc.). DO NOT make everything in one line." } \ No newline at end of file