diff --git a/src/Infrastructure/BotSharp.Abstraction/Utilities/StringExtensions.cs b/src/Infrastructure/BotSharp.Abstraction/Utilities/StringExtensions.cs index 0270133c..8e2fd9bb 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Utilities/StringExtensions.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Utilities/StringExtensions.cs @@ -5,7 +5,7 @@ namespace BotSharp.Abstraction.Utilities; public static class StringExtensions { - public static string IfNullOrEmptyAs(this string? str, string defaultValue) + public static string? IfNullOrEmptyAs(this string? str, string? defaultValue) => string.IsNullOrEmpty(str) ? defaultValue : str; public static string SubstringMax(this string str, int maxLength) diff --git a/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs b/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs index c1b16ee5..9ebbd807 100644 --- a/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs +++ b/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs @@ -1,4 +1,3 @@ -using BotSharp.Abstraction.Hooks; using BotSharp.Abstraction.Instructs; using BotSharp.Abstraction.Instructs.Models; using BotSharp.Abstraction.MLTasks; @@ -13,6 +12,15 @@ public partial class InstructService var agentService = _services.GetRequiredService(); Agent agent = await agentService.LoadAgent(agentId); + if (agent == null) + { + return new InstructResult + { + MessageId = message.MessageId, + Text = $"Agent (id: {agentId}) does not exist!" + }; + } + if (agent.Disabled) { var content = $"This agent ({agent.Name}) is disabled, please install the corresponding plugin ({agent.Plugin.Name}) to activate this agent."; @@ -55,6 +63,7 @@ public partial class InstructService { MessageId = message.MessageId }; + if (completer is ITextCompletion textCompleter) { instruction = null; @@ -86,7 +95,7 @@ public partial class InstructService { CurrentAgentId = agentId, MessageId = message.MessageId, - Files = files?.Select(x => new BotSharpFile { FileUrl = x.FileUrl, FileData = x.FileData }).ToList() ?? [] + Files = files?.Select(x => new BotSharpFile { FileUrl = x.FileUrl, FileData = x.FileData, ContentType = x.ContentType }).ToList() ?? [] } }); response.Text = result.Content; diff --git a/src/Plugins/BotSharp.Plugin.ChartHandler/ChartHandlerPlugin.cs b/src/Plugins/BotSharp.Plugin.ChartHandler/ChartHandlerPlugin.cs index 1161f6e4..4469f08a 100644 --- a/src/Plugins/BotSharp.Plugin.ChartHandler/ChartHandlerPlugin.cs +++ b/src/Plugins/BotSharp.Plugin.ChartHandler/ChartHandlerPlugin.cs @@ -12,7 +12,12 @@ public class ChartHandlerPlugin : IBotSharpPlugin public void RegisterDI(IServiceCollection services, IConfiguration config) { + services.AddScoped(provider => + { + var settingService = provider.GetRequiredService(); + return settingService.Bind("ChartHandler"); + }); + services.AddScoped(); } - } diff --git a/src/Plugins/BotSharp.Plugin.ChartHandler/Functions/PlotChartFn.cs b/src/Plugins/BotSharp.Plugin.ChartHandler/Functions/PlotChartFn.cs index ad8de0d7..8e810683 100644 --- a/src/Plugins/BotSharp.Plugin.ChartHandler/Functions/PlotChartFn.cs +++ b/src/Plugins/BotSharp.Plugin.ChartHandler/Functions/PlotChartFn.cs @@ -21,18 +21,22 @@ public class PlotChartFn : IFunctionCallback public async Task Execute(RoleDialogModel message) { - var db = _services.GetRequiredService(); var agentService = _services.GetRequiredService(); var convService = _services.GetRequiredService(); var args = JsonSerializer.Deserialize(message.FunctionArgs); + var agent = await agentService.GetAgent(message.CurrentAgentId); - var inst = db.GetAgentTemplate(BuiltInAgentId.UtilityAssistant, "util-chart-plot_instruction"); + var inst = GetChartPlotInstruction(message.CurrentAgentId); var innerAgent = new Agent { Id = agent.Id, Name = agent.Name, Instruction = inst, + LlmConfig = new AgentLlmConfig + { + MaxOutputTokens = 8192 + }, TemplateDict = new Dictionary { { "plotting_requirement", args?.PlottingRequirement ?? string.Empty }, @@ -67,8 +71,8 @@ public class PlotChartFn : IFunctionCallback { try { - var llmProviderService = _services.GetRequiredService(); - var completion = CompletionProvider.GetChatCompletion(_services, provider: "openai", model: "gpt-4.1"); + var (provider, model) = GetLlmProviderModel(); + var completion = CompletionProvider.GetChatCompletion(_services, provider: provider, model: model); var response = await completion.GetChatCompletions(agent, dialogs); return response.Content; } @@ -79,4 +83,42 @@ public class PlotChartFn : IFunctionCallback return error; } } + + private string GetChartPlotInstruction(string agentId) + { + var db = _services.GetRequiredService(); + var state = _services.GetRequiredService(); + + var templateContent = string.Empty; + var templateName = state.GetState("chart_plot_template"); + + if (!string.IsNullOrEmpty(templateName)) + { + templateContent = db.GetAgentTemplate(agentId, templateName); + } + else + { + templateName = "util-chart-plot_instruction"; + templateContent = db.GetAgentTemplate(BuiltInAgentId.UtilityAssistant, templateName); + } + + return templateContent; + } + + private (string, string) GetLlmProviderModel() + { + var provider = "openai"; + var model = "gpt-5"; + + var state = _services.GetRequiredService(); + var settings = _services.GetRequiredService(); + provider = state.GetState("chart_plot_llm_provider") + .IfNullOrEmptyAs(settings.ChartPlot?.LlmProvider) + .IfNullOrEmptyAs(provider); + model = state.GetState("chart_plot_llm_model") + .IfNullOrEmptyAs(settings.ChartPlot?.LlmModel) + .IfNullOrEmptyAs(model); + + return (provider, model); + } } diff --git a/src/Plugins/BotSharp.Plugin.ChartHandler/Settings/ChartHandlerSettings.cs b/src/Plugins/BotSharp.Plugin.ChartHandler/Settings/ChartHandlerSettings.cs new file mode 100644 index 00000000..61b0a6be --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.ChartHandler/Settings/ChartHandlerSettings.cs @@ -0,0 +1,12 @@ +namespace BotSharp.Plugin.ChartHandler.Settings; + +public class ChartHandlerSettings +{ + public ChartPlotSetting ChartPlot { get; set; } +} + +public class ChartPlotSetting +{ + public string LlmProvider { get; set; } + public string LlmModel { get; set; } +} diff --git a/src/Plugins/BotSharp.Plugin.ChartHandler/Using.cs b/src/Plugins/BotSharp.Plugin.ChartHandler/Using.cs index fa94465c..d2ab32a1 100644 --- a/src/Plugins/BotSharp.Plugin.ChartHandler/Using.cs +++ b/src/Plugins/BotSharp.Plugin.ChartHandler/Using.cs @@ -29,4 +29,5 @@ global using BotSharp.Abstraction.Options; global using BotSharp.Core.Infrastructures; global using BotSharp.Plugin.ChartHandler.Enums; global using BotSharp.Plugin.ChartHandler.LlmContext; -global using BotSharp.Plugin.ChartHandler.Hooks; \ No newline at end of file +global using BotSharp.Plugin.ChartHandler.Hooks; +global using BotSharp.Plugin.ChartHandler.Settings; \ No newline at end of file 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 9bffa73f..0d631b51 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 @@ -1,18 +1,27 @@ Please take a look at "Plotting Requirement" and generate a javascript code that can be used to render the charts on an html element. - === Plotting Requirement === {{ plotting_requirement }} - -***** Important ***** -** Your output must be a single block with everything needed inside. -** You need to import Plotly.js to plot the charts. The script source should be "https://cdn.plot.ly/plotly-3.0.1.min.js". +***** Hard Requirements ***** +** Your output javascript code must be wrapped in one or multiple blocks with everything needed inside. +** You need to import ECharts.js to plot the charts. The script source is "https://cdnjs.cloudflare.com/ajax/libs/echarts/6.0.0/echarts.min.js". ** You need to add the MODE bar for each chart you plot. -** You must render the charts on the div html element with id {{ chart_element_id }}. +** You must render the charts under the div html element with id {{ chart_element_id }}. +** Add a custom mode bar button named "Fullscreen" that toggles the chart container in and out of fullscreen using the Fullscreen API. Requirements for this button: + * Always call the Fullscreen API on the chart container div itself (document.getElementById("{{ chart_element_id }}")), not on the document or on Plotly’s SVG. + * Use el.requestFullscreen() with fallbacks to el.webkitRequestFullscreen || el.msRequestFullscreen. + * Exit fullscreen with document.exitFullscreen() and vendor fallbacks. + * Listen for fullscreenchange, webkitfullscreenchange, and msfullscreenchange to keep the button working across repeated clicks and ESC exits. + * Ensure the chart fully expands and scales to the entire screen when fullscreen is active. + * Provide a simple inline SVG path icon for the button (no external assets). + * Use Plotly.newPlot(container, data, layout, {displayModeBar:true, modeBarButtonsToAdd:[fullscreenBtn]}); + * fullscreenBtn must be a fully-formed object {name, title, icon, click}. ** You must not create any new html element. ** You must not apply any styles on any html element. -** You must generate as less token as possible. +** Keep code compact (few tokens), but fix all errors before returning. +** Do not generate charts with zero height and zero width. +** Please ensure the code can be executed and the charts are rendered correctly. *** Response Format *** You must output the response in the following JSON format: diff --git a/src/Plugins/BotSharp.Plugin.FileHandler/Functions/ReadImageFn.cs b/src/Plugins/BotSharp.Plugin.FileHandler/Functions/ReadImageFn.cs index 0e2c0456..30b945ff 100644 --- a/src/Plugins/BotSharp.Plugin.FileHandler/Functions/ReadImageFn.cs +++ b/src/Plugins/BotSharp.Plugin.FileHandler/Functions/ReadImageFn.cs @@ -98,10 +98,9 @@ public class ReadImageFn : IFunctionCallback { try { - var llmProviderService = _services.GetRequiredService(); - var provider = llmProviderService.GetProviders().FirstOrDefault(x => x == "openai"); - var model = llmProviderService.GetProviderModel(provider: provider, id: "gpt-4o", multiModal: true); - var completion = CompletionProvider.GetChatCompletion(_services, provider: provider, model: model.Name); + var provider = "openai"; + var model = "gpt-5-mini"; + var completion = CompletionProvider.GetChatCompletion(_services, provider: provider, model: model); var response = await completion.GetChatCompletions(agent, dialogs); return response.Content; } diff --git a/src/Plugins/BotSharp.Plugin.FileHandler/Functions/ReadPdfFn.cs b/src/Plugins/BotSharp.Plugin.FileHandler/Functions/ReadPdfFn.cs index 1eafeb85..b1354806 100644 --- a/src/Plugins/BotSharp.Plugin.FileHandler/Functions/ReadPdfFn.cs +++ b/src/Plugins/BotSharp.Plugin.FileHandler/Functions/ReadPdfFn.cs @@ -89,10 +89,9 @@ public class ReadPdfFn : IFunctionCallback { try { - var llmProviderService = _services.GetRequiredService(); - var provider = llmProviderService.GetProviders().FirstOrDefault(x => x == "openai"); - var model = llmProviderService.GetProviderModel(provider: provider, id: "gpt-4o", multiModal: true); - var completion = CompletionProvider.GetChatCompletion(_services, provider: provider, model: model.Name); + var provider = "openai"; + var model = "gpt-5-mini"; + var completion = CompletionProvider.GetChatCompletion(_services, provider: provider, model: model); var response = await completion.GetChatCompletions(agent, dialogs); return response.Content; } diff --git a/src/Plugins/BotSharp.Plugin.Qdrant/QdrantDb.cs b/src/Plugins/BotSharp.Plugin.Qdrant/QdrantDb.cs index cd14e375..2ffbdb53 100644 --- a/src/Plugins/BotSharp.Plugin.Qdrant/QdrantDb.cs +++ b/src/Plugins/BotSharp.Plugin.Qdrant/QdrantDb.cs @@ -230,8 +230,13 @@ public class QdrantDb : IVectorDb { foreach (var item in payload) { + if (item.Value == null || item.Key.IsEqualTo(KnowledgePayloadName.Text)) + { + continue; + } + var value = item.Value.DataValue?.ConvertToString(); - if (value == null || item.Key.IsEqualTo(KnowledgePayloadName.Text)) + if (string.IsNullOrEmpty(value)) { continue; } diff --git a/src/WebStarter/appsettings.json b/src/WebStarter/appsettings.json index f7f4d342..8124a4a8 100644 --- a/src/WebStarter/appsettings.json +++ b/src/WebStarter/appsettings.json @@ -317,6 +317,13 @@ "Origin": "" }, + "ChartHandler": { + "ChartPlot": { + "LlmProvider": "openai", + "LlmModel": "gpt-5" + } + }, + "SqlDriver": { "MySqlConnectionString": "", "SqlServerConnectionString": "",