This commit is contained in:
Jicheng Lu 2025-09-29 15:34:52 -05:00
parent 9abe315952
commit a5bf44a44a
5 changed files with 31 additions and 17 deletions

View file

@ -1,6 +1,6 @@
using BotSharp.Plugin.PythonInterpreter.Hooks;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Python.Runtime;
using System.IO;
@ -26,24 +26,33 @@ public class PythonInterpreterPlugin : IBotSharpAppPlugin
public void Configure(IApplicationBuilder app)
{
var settings = app.ApplicationServices.GetRequiredService<PythonInterpreterSettings>();
var sp = app.ApplicationServices;
var settings = sp.GetRequiredService<PythonInterpreterSettings>();
var logger = sp.GetRequiredService<ILogger<PyProgrammerFn>>();
var pyLoc = settings.InstallLocation;
// For Python interpreter plugin
if (File.Exists(settings.DllLocation))
try
{
Runtime.PythonDLL = settings.DllLocation;
PythonEngine.Initialize();
_pyState = PythonEngine.BeginAllowThreads();
if (File.Exists(pyLoc))
{
Runtime.PythonDLL = pyLoc;
PythonEngine.Initialize();
_pyState = PythonEngine.BeginAllowThreads();
var lifetime = app.ApplicationServices.GetRequiredService<IHostApplicationLifetime>();
lifetime.ApplicationStopping.Register(() => {
PythonEngine.EndAllowThreads(_pyState);
PythonEngine.Shutdown();
});
var lifetime = app.ApplicationServices.GetRequiredService<IHostApplicationLifetime>();
lifetime.ApplicationStopping.Register(() => {
PythonEngine.EndAllowThreads(_pyState);
PythonEngine.Shutdown();
});
}
else
{
logger.LogError($"Python dll not found at {pyLoc}");
}
}
else
catch (Exception ex)
{
Serilog.Log.Error($"Python DLL found at {settings.DllLocation}");
logger.LogError(ex, $"Error when loading python dll {pyLoc}");
}
}
}

View file

@ -4,7 +4,10 @@ namespace BotSharp.Plugin.PythonInterpreter.Settings;
public class PythonInterpreterSettings
{
public string DllLocation { get; set; }
/// <summary>
/// Python installation path to .dll or .so
/// </summary>
public string InstallLocation { get; set; }
public string PythonVersion { get; set; }
public CodeGenerationSetting? CodeGeneration { get; set; }
}

View file

@ -26,3 +26,5 @@ global using BotSharp.Core.Infrastructures;
global using BotSharp.Plugin.PythonInterpreter.Enums;
global using BotSharp.Plugin.PythonInterpreter.LlmContext;
global using BotSharp.Plugin.PythonInterpreter.Settings;
global using BotSharp.Plugin.PythonInterpreter.Functions;
global using BotSharp.Plugin.PythonInterpreter.Hooks;

View file

@ -1,5 +1,5 @@
{
"name": "util-code-python_interpreter",
"name": "util-code-python_programmer",
"description": "If user's requirement can be fulfilled by python code, you can call this function to generate python code to execute.",
"parameters": {
"type": "object",

View file

@ -566,7 +566,7 @@
},
"PythonInterpreter": {
"DllLocation": "C:/Users/xxx/AppData/Local/Programs/Python/Python313/python313.dll",
"InstallLocation": "C:/Users/xxx/AppData/Local/Programs/Python/Python313/python313.dll",
"PythonVersion": "3.13",
"CodeGeneration": {
"LlmProvider": "openai",