From 09c008f76f8723ab72f096cf886b2461ab6a020e Mon Sep 17 00:00:00 2001 From: Jicheng Lu Date: Mon, 29 Sep 2025 01:35:08 -0500 Subject: [PATCH] localize setting --- .../BotSharp.Core/BotSharpCoreExtensions.cs | 4 ---- .../Functions/PyInterpretationFn.cs | 19 +++++++++++-------- ...ook.cs => PythonInterpreterUtilityHook.cs} | 2 +- ...erPlugin.cs => PythonInterpreterPlugin.cs} | 15 +++++++-------- .../Settings/PythonInterpreterSettings.cs | 6 ++++++ .../Using.cs | 3 +-- src/WebStarter/appsettings.json | 6 ++---- 7 files changed, 28 insertions(+), 27 deletions(-) rename src/Plugins/BotSharp.Plugin.PythonInterpreter/Hooks/{InterpreterUtilityHook.cs => PythonInterpreterUtilityHook.cs} (90%) rename src/Plugins/BotSharp.Plugin.PythonInterpreter/{InterpreterPlugin.cs => PythonInterpreterPlugin.cs} (75%) create mode 100644 src/Plugins/BotSharp.Plugin.PythonInterpreter/Settings/PythonInterpreterSettings.cs diff --git a/src/Infrastructure/BotSharp.Core/BotSharpCoreExtensions.cs b/src/Infrastructure/BotSharp.Core/BotSharpCoreExtensions.cs index bfae45ba..a3fa3965 100644 --- a/src/Infrastructure/BotSharp.Core/BotSharpCoreExtensions.cs +++ b/src/Infrastructure/BotSharp.Core/BotSharpCoreExtensions.cs @@ -24,10 +24,6 @@ public static class BotSharpCoreExtensions { public static IServiceCollection AddBotSharpCore(this IServiceCollection services, IConfiguration config, Action? configOptions = null) { - var interpreterSettings = new InterpreterSettings(); - config.Bind("Interpreter", interpreterSettings); - services.AddSingleton(x => interpreterSettings); - services.AddSingleton(); // Register template render services.AddSingleton(); diff --git a/src/Plugins/BotSharp.Plugin.PythonInterpreter/Functions/PyInterpretationFn.cs b/src/Plugins/BotSharp.Plugin.PythonInterpreter/Functions/PyInterpretationFn.cs index 7706f984..f3876e8f 100644 --- a/src/Plugins/BotSharp.Plugin.PythonInterpreter/Functions/PyInterpretationFn.cs +++ b/src/Plugins/BotSharp.Plugin.PythonInterpreter/Functions/PyInterpretationFn.cs @@ -14,13 +14,16 @@ public class PyInterpretationFn : IFunctionCallback private readonly IServiceProvider _services; private readonly ILogger _logger; + private readonly PythonInterpreterSettings _settings; public PyInterpretationFn( IServiceProvider services, - ILogger logger) + ILogger logger, + PythonInterpreterSettings settings) { _services = services; _logger = logger; + _settings = settings; } public async Task Execute(RoleDialogModel message) @@ -32,7 +35,7 @@ public class PyInterpretationFn : IFunctionCallback var args = JsonSerializer.Deserialize(message.FunctionArgs); var agent = await agentService.GetAgent(message.CurrentAgentId); - var inst = GetPyCodeGenerationInstruction(message.CurrentAgentId); + var inst = GetPyCodeInterpreterInstruction(message.CurrentAgentId); var innerAgent = new Agent { Id = agent.Id, @@ -95,13 +98,13 @@ public class PyInterpretationFn : IFunctionCallback } catch (Exception ex) { - var error = $"Error when plotting chart. {ex.Message}"; + var error = $"Error when generating python code. {ex.Message}"; _logger.LogWarning(ex, error); return error; } } - private string GetPyCodeGenerationInstruction(string agentId) + private string GetPyCodeInterpreterInstruction(string agentId) { var db = _services.GetRequiredService(); var state = _services.GetRequiredService(); @@ -128,10 +131,10 @@ public class PyInterpretationFn : IFunctionCallback var model = "gpt-5"; var state = _services.GetRequiredService(); - provider = state.GetState("py_generator_llm_provider") + provider = state.GetState("py_intepreter_llm_provider") //.IfNullOrEmptyAs(_settings.ChartPlot?.LlmProvider) .IfNullOrEmptyAs(provider); - model = state.GetState("py_generator_llm_model") + model = state.GetState("py_intepreter_llm_model") //.IfNullOrEmptyAs(_settings.ChartPlot?.LlmModel) .IfNullOrEmptyAs(model); @@ -144,8 +147,8 @@ public class PyInterpretationFn : IFunctionCallback var reasoningEffortLevel = "minimal"; var state = _services.GetRequiredService(); - maxOutputTokens = int.TryParse(state.GetState("py_generator_max_output_tokens"), out var tokens) ? tokens : maxOutputTokens; - reasoningEffortLevel = state.GetState("py_generator_reasoning_effort_level").IfNullOrEmptyAs(reasoningEffortLevel); + maxOutputTokens = int.TryParse(state.GetState("py_intepreter_max_output_tokens"), out var tokens) ? tokens : maxOutputTokens; + reasoningEffortLevel = state.GetState("py_intepreter_reasoning_effort_level").IfNullOrEmptyAs(reasoningEffortLevel); return new AgentLlmConfig { diff --git a/src/Plugins/BotSharp.Plugin.PythonInterpreter/Hooks/InterpreterUtilityHook.cs b/src/Plugins/BotSharp.Plugin.PythonInterpreter/Hooks/PythonInterpreterUtilityHook.cs similarity index 90% rename from src/Plugins/BotSharp.Plugin.PythonInterpreter/Hooks/InterpreterUtilityHook.cs rename to src/Plugins/BotSharp.Plugin.PythonInterpreter/Hooks/PythonInterpreterUtilityHook.cs index 97a0f482..2d243e35 100644 --- a/src/Plugins/BotSharp.Plugin.PythonInterpreter/Hooks/InterpreterUtilityHook.cs +++ b/src/Plugins/BotSharp.Plugin.PythonInterpreter/Hooks/PythonInterpreterUtilityHook.cs @@ -1,6 +1,6 @@ namespace BotSharp.Plugin.PythonInterpreter.Hooks; -public class InterpreterUtilityHook : IAgentUtilityHook +public class PythonInterpreterUtilityHook : IAgentUtilityHook { private const string PY_INTERPRETER_FN = "util-code-python_interpreter"; diff --git a/src/Plugins/BotSharp.Plugin.PythonInterpreter/InterpreterPlugin.cs b/src/Plugins/BotSharp.Plugin.PythonInterpreter/PythonInterpreterPlugin.cs similarity index 75% rename from src/Plugins/BotSharp.Plugin.PythonInterpreter/InterpreterPlugin.cs rename to src/Plugins/BotSharp.Plugin.PythonInterpreter/PythonInterpreterPlugin.cs index 2e1e723b..e1c1bd63 100644 --- a/src/Plugins/BotSharp.Plugin.PythonInterpreter/InterpreterPlugin.cs +++ b/src/Plugins/BotSharp.Plugin.PythonInterpreter/PythonInterpreterPlugin.cs @@ -1,4 +1,3 @@ -using BotSharp.Abstraction.Interpreters.Settings; using BotSharp.Abstraction.Settings; using BotSharp.Plugin.PythonInterpreter.Hooks; using Microsoft.AspNetCore.Builder; @@ -7,7 +6,7 @@ using System.IO; namespace BotSharp.Plugin.PythonInterpreter; -public class InterpreterPlugin : IBotSharpAppPlugin +public class PythonInterpreterPlugin : IBotSharpAppPlugin { public string Id => "23174e08-e866-4173-824a-cf1d97afa8d0"; public string Name => "Python Interpreter"; @@ -19,26 +18,26 @@ public class InterpreterPlugin : IBotSharpAppPlugin services.AddSingleton(provider => { var settingService = provider.GetRequiredService(); - return settingService.Bind("Interpreter"); + return settingService.Bind("PythonInterpreter"); }); - services.AddScoped(); + services.AddScoped(); } public void Configure(IApplicationBuilder app) { - var settings = app.ApplicationServices.GetRequiredService(); + var settings = app.ApplicationServices.GetRequiredService(); // For Python interpreter plugin - if (File.Exists(settings.Python.PythonDLL)) + if (File.Exists(settings.DllLocation)) { - Runtime.PythonDLL = settings.Python.PythonDLL; + Runtime.PythonDLL = settings.DllLocation; PythonEngine.Initialize(); PythonEngine.BeginAllowThreads(); } else { - Serilog.Log.Error("Python DLL found at {PythonDLL}", settings.Python.PythonDLL); + Serilog.Log.Error("Python DLL found at {PythonDLL}", settings.DllLocation); } // Shut down the Python engine diff --git a/src/Plugins/BotSharp.Plugin.PythonInterpreter/Settings/PythonInterpreterSettings.cs b/src/Plugins/BotSharp.Plugin.PythonInterpreter/Settings/PythonInterpreterSettings.cs new file mode 100644 index 00000000..01f5a9d1 --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.PythonInterpreter/Settings/PythonInterpreterSettings.cs @@ -0,0 +1,6 @@ +namespace BotSharp.Plugin.PythonInterpreter.Settings; + +public class PythonInterpreterSettings +{ + public string DllLocation { get; set; } +} diff --git a/src/Plugins/BotSharp.Plugin.PythonInterpreter/Using.cs b/src/Plugins/BotSharp.Plugin.PythonInterpreter/Using.cs index 00a172fa..88ed52e6 100644 --- a/src/Plugins/BotSharp.Plugin.PythonInterpreter/Using.cs +++ b/src/Plugins/BotSharp.Plugin.PythonInterpreter/Using.cs @@ -16,10 +16,9 @@ global using BotSharp.Abstraction.Functions.Models; global using BotSharp.Abstraction.Repositories; global using BotSharp.Abstraction.Conversations.Models; global using BotSharp.Abstraction.Functions; -global using BotSharp.Abstraction.Interpreters.Models; - global using BotSharp.Core.Infrastructures; global using BotSharp.Plugin.PythonInterpreter.Enums; global using BotSharp.Plugin.PythonInterpreter.LlmContext; +global using BotSharp.Plugin.PythonInterpreter.Settings; diff --git a/src/WebStarter/appsettings.json b/src/WebStarter/appsettings.json index 81ac7091..f00c4899 100644 --- a/src/WebStarter/appsettings.json +++ b/src/WebStarter/appsettings.json @@ -564,10 +564,8 @@ } }, - "Interpreter": { - "Python": { - "PythonDLL": "C:/Users/xxx/AppData/Local/Programs/Python/Python311/python311.dll" - } + "PythonInterpreter": { + "DllLocation": "C:/Users/xxx/AppData/Local/Programs/Python/Python313/python313.dll" }, "RealtimeModel": {