localize setting

This commit is contained in:
Jicheng Lu 2025-09-29 01:35:08 -05:00
parent e529b36fab
commit 09c008f76f
7 changed files with 28 additions and 27 deletions

View file

@ -24,10 +24,6 @@ public static class BotSharpCoreExtensions
{ {
public static IServiceCollection AddBotSharpCore(this IServiceCollection services, IConfiguration config, Action<BotSharpOptions>? configOptions = null) public static IServiceCollection AddBotSharpCore(this IServiceCollection services, IConfiguration config, Action<BotSharpOptions>? configOptions = null)
{ {
var interpreterSettings = new InterpreterSettings();
config.Bind("Interpreter", interpreterSettings);
services.AddSingleton(x => interpreterSettings);
services.AddSingleton<IDistributedLocker, DistributedLocker>(); services.AddSingleton<IDistributedLocker, DistributedLocker>();
// Register template render // Register template render
services.AddSingleton<ITemplateRender, TemplateRender>(); services.AddSingleton<ITemplateRender, TemplateRender>();

View file

@ -14,13 +14,16 @@ public class PyInterpretationFn : IFunctionCallback
private readonly IServiceProvider _services; private readonly IServiceProvider _services;
private readonly ILogger<PyInterpretationFn> _logger; private readonly ILogger<PyInterpretationFn> _logger;
private readonly PythonInterpreterSettings _settings;
public PyInterpretationFn( public PyInterpretationFn(
IServiceProvider services, IServiceProvider services,
ILogger<PyInterpretationFn> logger) ILogger<PyInterpretationFn> logger,
PythonInterpreterSettings settings)
{ {
_services = services; _services = services;
_logger = logger; _logger = logger;
_settings = settings;
} }
public async Task<bool> Execute(RoleDialogModel message) public async Task<bool> Execute(RoleDialogModel message)
@ -32,7 +35,7 @@ public class PyInterpretationFn : IFunctionCallback
var args = JsonSerializer.Deserialize<LlmContextIn>(message.FunctionArgs); var args = JsonSerializer.Deserialize<LlmContextIn>(message.FunctionArgs);
var agent = await agentService.GetAgent(message.CurrentAgentId); var agent = await agentService.GetAgent(message.CurrentAgentId);
var inst = GetPyCodeGenerationInstruction(message.CurrentAgentId); var inst = GetPyCodeInterpreterInstruction(message.CurrentAgentId);
var innerAgent = new Agent var innerAgent = new Agent
{ {
Id = agent.Id, Id = agent.Id,
@ -95,13 +98,13 @@ public class PyInterpretationFn : IFunctionCallback
} }
catch (Exception ex) catch (Exception ex)
{ {
var error = $"Error when plotting chart. {ex.Message}"; var error = $"Error when generating python code. {ex.Message}";
_logger.LogWarning(ex, error); _logger.LogWarning(ex, error);
return error; return error;
} }
} }
private string GetPyCodeGenerationInstruction(string agentId) private string GetPyCodeInterpreterInstruction(string agentId)
{ {
var db = _services.GetRequiredService<IBotSharpRepository>(); var db = _services.GetRequiredService<IBotSharpRepository>();
var state = _services.GetRequiredService<IConversationStateService>(); var state = _services.GetRequiredService<IConversationStateService>();
@ -128,10 +131,10 @@ public class PyInterpretationFn : IFunctionCallback
var model = "gpt-5"; var model = "gpt-5";
var state = _services.GetRequiredService<IConversationStateService>(); var state = _services.GetRequiredService<IConversationStateService>();
provider = state.GetState("py_generator_llm_provider") provider = state.GetState("py_intepreter_llm_provider")
//.IfNullOrEmptyAs(_settings.ChartPlot?.LlmProvider) //.IfNullOrEmptyAs(_settings.ChartPlot?.LlmProvider)
.IfNullOrEmptyAs(provider); .IfNullOrEmptyAs(provider);
model = state.GetState("py_generator_llm_model") model = state.GetState("py_intepreter_llm_model")
//.IfNullOrEmptyAs(_settings.ChartPlot?.LlmModel) //.IfNullOrEmptyAs(_settings.ChartPlot?.LlmModel)
.IfNullOrEmptyAs(model); .IfNullOrEmptyAs(model);
@ -144,8 +147,8 @@ public class PyInterpretationFn : IFunctionCallback
var reasoningEffortLevel = "minimal"; var reasoningEffortLevel = "minimal";
var state = _services.GetRequiredService<IConversationStateService>(); var state = _services.GetRequiredService<IConversationStateService>();
maxOutputTokens = int.TryParse(state.GetState("py_generator_max_output_tokens"), out var tokens) ? tokens : maxOutputTokens; maxOutputTokens = int.TryParse(state.GetState("py_intepreter_max_output_tokens"), out var tokens) ? tokens : maxOutputTokens;
reasoningEffortLevel = state.GetState("py_generator_reasoning_effort_level").IfNullOrEmptyAs(reasoningEffortLevel); reasoningEffortLevel = state.GetState("py_intepreter_reasoning_effort_level").IfNullOrEmptyAs(reasoningEffortLevel);
return new AgentLlmConfig return new AgentLlmConfig
{ {

View file

@ -1,6 +1,6 @@
namespace BotSharp.Plugin.PythonInterpreter.Hooks; namespace BotSharp.Plugin.PythonInterpreter.Hooks;
public class InterpreterUtilityHook : IAgentUtilityHook public class PythonInterpreterUtilityHook : IAgentUtilityHook
{ {
private const string PY_INTERPRETER_FN = "util-code-python_interpreter"; private const string PY_INTERPRETER_FN = "util-code-python_interpreter";

View file

@ -1,4 +1,3 @@
using BotSharp.Abstraction.Interpreters.Settings;
using BotSharp.Abstraction.Settings; using BotSharp.Abstraction.Settings;
using BotSharp.Plugin.PythonInterpreter.Hooks; using BotSharp.Plugin.PythonInterpreter.Hooks;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
@ -7,7 +6,7 @@ using System.IO;
namespace BotSharp.Plugin.PythonInterpreter; namespace BotSharp.Plugin.PythonInterpreter;
public class InterpreterPlugin : IBotSharpAppPlugin public class PythonInterpreterPlugin : IBotSharpAppPlugin
{ {
public string Id => "23174e08-e866-4173-824a-cf1d97afa8d0"; public string Id => "23174e08-e866-4173-824a-cf1d97afa8d0";
public string Name => "Python Interpreter"; public string Name => "Python Interpreter";
@ -19,26 +18,26 @@ public class InterpreterPlugin : IBotSharpAppPlugin
services.AddSingleton(provider => services.AddSingleton(provider =>
{ {
var settingService = provider.GetRequiredService<ISettingService>(); var settingService = provider.GetRequiredService<ISettingService>();
return settingService.Bind<InterpreterSettings>("Interpreter"); return settingService.Bind<PythonInterpreterSettings>("PythonInterpreter");
}); });
services.AddScoped<IAgentUtilityHook, InterpreterUtilityHook>(); services.AddScoped<IAgentUtilityHook, PythonInterpreterUtilityHook>();
} }
public void Configure(IApplicationBuilder app) public void Configure(IApplicationBuilder app)
{ {
var settings = app.ApplicationServices.GetRequiredService<InterpreterSettings>(); var settings = app.ApplicationServices.GetRequiredService<PythonInterpreterSettings>();
// For Python interpreter plugin // 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.Initialize();
PythonEngine.BeginAllowThreads(); PythonEngine.BeginAllowThreads();
} }
else 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 // Shut down the Python engine

View file

@ -0,0 +1,6 @@
namespace BotSharp.Plugin.PythonInterpreter.Settings;
public class PythonInterpreterSettings
{
public string DllLocation { get; set; }
}

View file

@ -16,10 +16,9 @@ global using BotSharp.Abstraction.Functions.Models;
global using BotSharp.Abstraction.Repositories; global using BotSharp.Abstraction.Repositories;
global using BotSharp.Abstraction.Conversations.Models; global using BotSharp.Abstraction.Conversations.Models;
global using BotSharp.Abstraction.Functions; global using BotSharp.Abstraction.Functions;
global using BotSharp.Abstraction.Interpreters.Models;
global using BotSharp.Core.Infrastructures; global using BotSharp.Core.Infrastructures;
global using BotSharp.Plugin.PythonInterpreter.Enums; global using BotSharp.Plugin.PythonInterpreter.Enums;
global using BotSharp.Plugin.PythonInterpreter.LlmContext; global using BotSharp.Plugin.PythonInterpreter.LlmContext;
global using BotSharp.Plugin.PythonInterpreter.Settings;

View file

@ -564,10 +564,8 @@
} }
}, },
"Interpreter": { "PythonInterpreter": {
"Python": { "DllLocation": "C:/Users/xxx/AppData/Local/Programs/Python/Python313/python313.dll"
"PythonDLL": "C:/Users/xxx/AppData/Local/Programs/Python/Python311/python311.dll"
}
}, },
"RealtimeModel": { "RealtimeModel": {