init code interpret service
This commit is contained in:
parent
b99d0b4ece
commit
2770c4fe22
|
|
@ -0,0 +1,10 @@
|
|||
using BotSharp.Abstraction.CodeInterpreter.Models;
|
||||
|
||||
namespace BotSharp.Abstraction.CodeInterpreter;
|
||||
|
||||
public interface ICodeInterpretService
|
||||
{
|
||||
string Provider { get; }
|
||||
|
||||
Task<CodeInterpretResult> RunCode(string code, List<KeyValue>? arguments = null, CodeInterpretOptions? options = null);
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
namespace BotSharp.Abstraction.CodeInterpreter.Models;
|
||||
|
||||
public class CodeInterpretOptions
|
||||
{
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
namespace BotSharp.Abstraction.CodeInterpreter.Models;
|
||||
|
||||
public class CodeInterpretResult
|
||||
{
|
||||
public object Result { get; set; }
|
||||
public bool Success { get; set; }
|
||||
public string? ErrorMsg { get; set; }
|
||||
}
|
||||
|
|
@ -99,7 +99,7 @@ public class PyProgrammerFn : IFunctionCallback
|
|||
Language = "python"
|
||||
}
|
||||
};
|
||||
//message.StopCompletion = true;
|
||||
message.StopCompletion = true;
|
||||
|
||||
// Restore the original stdout/stderr
|
||||
sys.stdout = sys.__stdout__;
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@ public class PythonInterpreterPlugin : IBotSharpAppPlugin
|
|||
services.AddSingleton(x => settings);
|
||||
|
||||
services.AddScoped<IAgentUtilityHook, PyProgrammerUtilityHook>();
|
||||
services.AddScoped<ICodeInterpretService, PyInterpretService>();
|
||||
}
|
||||
|
||||
public void Configure(IApplicationBuilder app)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,87 @@
|
|||
using BotSharp.Abstraction.Models;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Python.Runtime;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BotSharp.Plugin.PythonInterpreter.Services;
|
||||
|
||||
public class PyInterpretService : ICodeInterpretService
|
||||
{
|
||||
private readonly IServiceProvider _services;
|
||||
private readonly ILogger<PyInterpretService> _logger;
|
||||
|
||||
public PyInterpretService(
|
||||
IServiceProvider services,
|
||||
ILogger<PyInterpretService> logger)
|
||||
{
|
||||
_services = services;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public string Provider => "python-interpreter";
|
||||
|
||||
public async Task<CodeInterpretResult> RunCode(string code, List<KeyValue>? arguments = null, CodeInterpretOptions? options = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (Py.GIL())
|
||||
{
|
||||
// Import necessary Python modules
|
||||
dynamic sys = Py.Import("sys");
|
||||
dynamic io = Py.Import("io");
|
||||
|
||||
// Redirect standard output/error to capture it
|
||||
dynamic stringIO = io.StringIO();
|
||||
sys.stdout = stringIO;
|
||||
sys.stderr = stringIO;
|
||||
|
||||
// Set global items
|
||||
using var globals = new PyDict();
|
||||
if (code.Contains("__main__") == true)
|
||||
{
|
||||
globals.SetItem("__name__", new PyString("__main__"));
|
||||
}
|
||||
|
||||
// Set arguments
|
||||
if (!arguments.IsNullOrEmpty())
|
||||
{
|
||||
sys.argv = new PyList();
|
||||
sys.argv.Append("code.py");
|
||||
|
||||
foreach (var arg in arguments)
|
||||
{
|
||||
sys.argv.Append($"--{arg.Key}");
|
||||
sys.argv.Append($"{arg.Value}");
|
||||
}
|
||||
}
|
||||
|
||||
// Execute Python script
|
||||
PythonEngine.Exec(code, globals);
|
||||
|
||||
// Get result
|
||||
var result = stringIO.getvalue().ToString();
|
||||
|
||||
// Restore the original stdout/stderr
|
||||
sys.stdout = sys.__stdout__;
|
||||
sys.stderr = sys.__stderr__;
|
||||
|
||||
return new CodeInterpretResult
|
||||
{
|
||||
Result = result,
|
||||
Success = true
|
||||
};
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var errorMsg = $"Error when executing python code in {nameof(PyInterpretService)}-{Provider}. {ex.Message}";
|
||||
_logger.LogError(ex, errorMsg);
|
||||
|
||||
return new CodeInterpretResult
|
||||
{
|
||||
Success = false,
|
||||
ErrorMsg = errorMsg
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -20,6 +20,8 @@ global using BotSharp.Abstraction.Messaging;
|
|||
global using BotSharp.Abstraction.Messaging.Models.RichContent;
|
||||
global using BotSharp.Abstraction.Messaging.Models.RichContent.Template;
|
||||
global using BotSharp.Abstraction.Routing;
|
||||
global using BotSharp.Abstraction.CodeInterpreter.Models;
|
||||
global using BotSharp.Abstraction.CodeInterpreter;
|
||||
|
||||
global using BotSharp.Core.Infrastructures;
|
||||
|
||||
|
|
@ -30,3 +32,4 @@ global using BotSharp.Plugin.PythonInterpreter.Functions;
|
|||
global using BotSharp.Plugin.PythonInterpreter.Hooks;
|
||||
global using BotSharp.Plugin.PythonInterpreter.Models;
|
||||
global using BotSharp.Plugin.PythonInterpreter.Helpers;
|
||||
global using BotSharp.Plugin.PythonInterpreter.Services;
|
||||
Loading…
Reference in a new issue