add chat context to chart

This commit is contained in:
Jicheng Lu 2025-09-10 23:38:45 -05:00
parent c00391c18c
commit c736186a9b
3 changed files with 19 additions and 9 deletions

View file

@ -1,4 +1,6 @@
using BotSharp.Abstraction.Messaging.Models.RichContent.Template; using BotSharp.Abstraction.Messaging.Models.RichContent.Template;
using BotSharp.Abstraction.Routing;
using System.Linq;
namespace BotSharp.Plugin.ChartHandler.Functions; namespace BotSharp.Plugin.ChartHandler.Functions;
@ -26,6 +28,7 @@ public class PlotChartFn : IFunctionCallback
{ {
var agentService = _services.GetRequiredService<IAgentService>(); var agentService = _services.GetRequiredService<IAgentService>();
var convService = _services.GetRequiredService<IConversationService>(); var convService = _services.GetRequiredService<IConversationService>();
var routingCtx = _services.GetRequiredService<IRoutingContext>();
var args = JsonSerializer.Deserialize<LlmContextIn>(message.FunctionArgs); var args = JsonSerializer.Deserialize<LlmContextIn>(message.FunctionArgs);
@ -44,15 +47,21 @@ public class PlotChartFn : IFunctionCallback
} }
}; };
var response = await GetChatCompletion(innerAgent, var dialogs = routingCtx.GetDialogs();
[ if (dialogs.IsNullOrEmpty())
new RoleDialogModel(AgentRole.User, "Please follow the instruction to generate the javascript code.") {
{ dialogs = convService.GetDialogHistory();
CurrentAgentId = message.CurrentAgentId, }
MessageId = message.MessageId
}
]);
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<LlmContextOut>(); var obj = response.JsonContent<LlmContextOut>();
message.Content = obj?.GreetingMessage ?? "Here is the chart you ask for:"; message.Content = obj?.GreetingMessage ?? "Here is the chart you ask for:";
message.RichContent = new RichContent<IRichMessage> message.RichContent = new RichContent<IRichMessage>

View file

@ -11,4 +11,5 @@ public class ChartPlotSetting
public string? LlmModel { get; set; } public string? LlmModel { get; set; }
public int? MaxOutputTokens { get; set; } public int? MaxOutputTokens { get; set; }
public string? ReasoningEffortLevel { get; set; } public string? ReasoningEffortLevel { get; set; }
public int? MessageLimit { get; set; }
} }

View file

@ -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.", "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.", "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."
} }