commit
9cbd8125f2
|
|
@ -1,11 +0,0 @@
|
|||
using BotSharp.Abstraction.CodeInterpreter.Models;
|
||||
|
||||
namespace BotSharp.Abstraction.CodeInterpreter;
|
||||
|
||||
public interface ICodeInterpretService
|
||||
{
|
||||
string Provider { get; }
|
||||
|
||||
Task<CodeInterpretResult> RunCode(string codeScript, CodeInterpretOptions? options = null)
|
||||
=> throw new NotImplementedException();
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
using BotSharp.Abstraction.Coding.Options;
|
||||
using BotSharp.Abstraction.Coding.Responses;
|
||||
|
||||
namespace BotSharp.Abstraction.Coding;
|
||||
|
||||
public interface ICodeProcessor
|
||||
{
|
||||
string Provider { get; }
|
||||
|
||||
Task<CodeInterpretResponse> RunAsync(string codeScript, CodeInterpretOptions? options = null)
|
||||
=> throw new NotImplementedException();
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
using System.Threading;
|
||||
|
||||
namespace BotSharp.Abstraction.CodeInterpreter.Models;
|
||||
namespace BotSharp.Abstraction.Coding.Options;
|
||||
|
||||
public class CodeInterpretOptions
|
||||
{
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
namespace BotSharp.Abstraction.CodeInterpreter.Models;
|
||||
namespace BotSharp.Abstraction.Coding.Responses;
|
||||
|
||||
public class CodeInterpretResult
|
||||
public class CodeInterpretResponse
|
||||
{
|
||||
public string Result { get; set; } = string.Empty;
|
||||
public bool Success { get; set; }
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
namespace BotSharp.Abstraction.Files.Options;
|
||||
|
||||
public class FileLlmProcessOptions
|
||||
public class FileHandleOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// Llm provider
|
||||
|
|
@ -3,10 +3,10 @@ using BotSharp.Abstraction.Files.Responses;
|
|||
|
||||
namespace BotSharp.Abstraction.Files.Proccessors;
|
||||
|
||||
public interface IFileLlmProcessor
|
||||
public interface IFileProcessor
|
||||
{
|
||||
public string Provider { get; }
|
||||
|
||||
Task<FileLlmInferenceResponse> GetFileLlmInferenceAsync(Agent agent, string text, IEnumerable<InstructFileModel> files, FileLlmProcessOptions? options = null)
|
||||
Task<FileHandleResponse> HandleFilesAsync(Agent agent, string text, IEnumerable<InstructFileModel> files, FileHandleOptions? options = null)
|
||||
=> throw new NotImplementedException();
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
namespace BotSharp.Abstraction.Files.Responses;
|
||||
|
||||
public class FileLlmInferenceResponse
|
||||
public class FileHandleResponse
|
||||
{
|
||||
public string Result { get; set; } = string.Empty;
|
||||
public bool Success { get; set; }
|
||||
|
|
@ -2,7 +2,7 @@ namespace BotSharp.Abstraction.Instructs.Options;
|
|||
|
||||
public class CodeInstructOptions
|
||||
{
|
||||
public string? Processor { get; set; }
|
||||
public string? CodeScriptName { get; set; }
|
||||
public string? CodeInterpretProvider { get; set; }
|
||||
public List<KeyValue>? Arguments { get; set; }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,5 +2,5 @@ namespace BotSharp.Abstraction.Instructs.Options;
|
|||
|
||||
public class FileInstructOptions
|
||||
{
|
||||
public string? FileLlmProcessorProvider { get; set; }
|
||||
public string? Processor { get; set; }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,4 @@
|
|||
using BotSharp.Abstraction.CodeInterpreter.Models;
|
||||
|
||||
namespace BotSharp.Core.CodeInterpreter;
|
||||
namespace BotSharp.Core.Coding;
|
||||
|
||||
public class CodeScriptExecutor
|
||||
{
|
||||
|
|
@ -13,7 +11,7 @@ public class CodeScriptExecutor
|
|||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<T> Execute<T>(Func<Task<T>> func, CancellationToken cancellationToken = default)
|
||||
public async Task<T> ExecuteAsync<T>(Func<Task<T>> func, CancellationToken cancellationToken = default)
|
||||
{
|
||||
await _semLock.WaitAsync(cancellationToken);
|
||||
|
||||
|
|
@ -1,6 +1,5 @@
|
|||
using BotSharp.Abstraction.Google.Settings;
|
||||
using BotSharp.Abstraction.Instructs;
|
||||
using BotSharp.Abstraction.MessageHub;
|
||||
using BotSharp.Abstraction.MessageHub.Observers;
|
||||
using BotSharp.Abstraction.MessageHub.Services;
|
||||
using BotSharp.Abstraction.Messaging;
|
||||
|
|
@ -8,7 +7,7 @@ using BotSharp.Abstraction.Planning;
|
|||
using BotSharp.Abstraction.Plugins.Models;
|
||||
using BotSharp.Abstraction.Settings;
|
||||
using BotSharp.Abstraction.Templating;
|
||||
using BotSharp.Core.CodeInterpreter;
|
||||
using BotSharp.Core.Coding;
|
||||
using BotSharp.Core.Instructs;
|
||||
using BotSharp.Core.MessageHub;
|
||||
using BotSharp.Core.MessageHub.Observers;
|
||||
|
|
@ -18,7 +17,6 @@ using BotSharp.Core.Routing.Reasoning;
|
|||
using BotSharp.Core.Templating;
|
||||
using BotSharp.Core.Translation;
|
||||
using BotSharp.Core.WebSearch.Hooks;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace BotSharp.Core.Conversations;
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
using BotSharp.Abstraction.CodeInterpreter;
|
||||
using BotSharp.Abstraction.Coding;
|
||||
using BotSharp.Abstraction.Files.Options;
|
||||
using BotSharp.Abstraction.Files.Proccessors;
|
||||
using BotSharp.Abstraction.Instructs;
|
||||
|
|
@ -47,7 +47,7 @@ public partial class InstructService
|
|||
|
||||
|
||||
// Run code template
|
||||
var codeResponse = await GetCodeResponse(agent, message, templateName, codeOptions);
|
||||
var codeResponse = await RunCode(agent, message, templateName, codeOptions);
|
||||
if (codeResponse != null)
|
||||
{
|
||||
return codeResponse;
|
||||
|
|
@ -105,16 +105,16 @@ public partial class InstructService
|
|||
prompt = message.Content;
|
||||
}
|
||||
|
||||
IFileLlmProcessor? fileProcessor = null;
|
||||
IFileProcessor? fileProcessor = null;
|
||||
if (!files.IsNullOrEmpty() && fileOptions != null)
|
||||
{
|
||||
fileProcessor = _services.GetServices<IFileLlmProcessor>()
|
||||
.FirstOrDefault(x => x.Provider.IsEqualTo(fileOptions.FileLlmProcessorProvider));
|
||||
fileProcessor = _services.GetServices<IFileProcessor>()
|
||||
.FirstOrDefault(x => x.Provider.IsEqualTo(fileOptions.Processor));
|
||||
}
|
||||
|
||||
if (fileProcessor != null)
|
||||
{
|
||||
var inference = await fileProcessor.GetFileLlmInferenceAsync(agent, prompt, files, new FileLlmProcessOptions
|
||||
var fileResponse = await fileProcessor.HandleFilesAsync(agent, prompt, files, new FileHandleOptions
|
||||
{
|
||||
Provider = provider,
|
||||
Model = model,
|
||||
|
|
@ -124,7 +124,7 @@ public partial class InstructService
|
|||
InvokeFrom = $"{nameof(InstructService)}.{nameof(Execute)}",
|
||||
Data = state.GetStates().ToDictionary(x => x.Key, x => (object)x.Value)
|
||||
});
|
||||
result = inference.Result.IfNullOrEmptyAs(string.Empty);
|
||||
result = fileResponse.Result.IfNullOrEmptyAs(string.Empty);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -160,7 +160,7 @@ public partial class InstructService
|
|||
/// <param name="templateName"></param>
|
||||
/// <param name="codeOptions"></param>
|
||||
/// <returns></returns>
|
||||
private async Task<InstructResult?> GetCodeResponse(
|
||||
private async Task<InstructResult?> RunCode(
|
||||
Agent agent,
|
||||
RoleDialogModel message,
|
||||
string templateName,
|
||||
|
|
@ -177,14 +177,14 @@ public partial class InstructService
|
|||
var state = _services.GetRequiredService<IConversationStateService>();
|
||||
var hooks = _services.GetHooks<IInstructHook>(agent.Id);
|
||||
|
||||
var codeProvider = codeOptions?.CodeInterpretProvider ?? "botsharp-py-interpreter";
|
||||
var codeInterpreter = _services.GetServices<ICodeInterpretService>()
|
||||
var codeProvider = codeOptions?.Processor ?? "botsharp-py-interpreter";
|
||||
var codeProcessor = _services.GetServices<ICodeProcessor>()
|
||||
.FirstOrDefault(x => x.Provider.IsEqualTo(codeProvider));
|
||||
|
||||
if (codeInterpreter == null)
|
||||
if (codeProcessor == null)
|
||||
{
|
||||
#if DEBUG
|
||||
_logger.LogWarning($"No code interpreter found. (Agent: {agent.Id}, Code interpreter: {codeProvider})");
|
||||
_logger.LogWarning($"No code processor found. (Agent: {agent.Id}, Code processor: {codeProvider})");
|
||||
#endif
|
||||
return response;
|
||||
}
|
||||
|
|
@ -248,7 +248,7 @@ public partial class InstructService
|
|||
}
|
||||
|
||||
// Run code script
|
||||
var result = await codeInterpreter.RunCode(context.CodeScript, options: new()
|
||||
var codeResponse = await codeProcessor.RunAsync(context.CodeScript, options: new()
|
||||
{
|
||||
ScriptName = scriptName,
|
||||
Arguments = context.Arguments
|
||||
|
|
@ -258,7 +258,7 @@ public partial class InstructService
|
|||
{
|
||||
MessageId = message.MessageId,
|
||||
Template = scriptName,
|
||||
Text = result?.Result ?? result?.ErrorMsg
|
||||
Text = codeResponse?.Result ?? codeResponse?.ErrorMsg
|
||||
};
|
||||
|
||||
if (context?.Arguments != null)
|
||||
|
|
@ -273,7 +273,7 @@ public partial class InstructService
|
|||
await hook.OnResponseGenerated(new InstructResponseModel
|
||||
{
|
||||
AgentId = agent.Id,
|
||||
Provider = codeInterpreter.Provider,
|
||||
Provider = codeProcessor.Provider,
|
||||
Model = string.Empty,
|
||||
TemplateName = scriptName,
|
||||
UserMessage = message.Content,
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ public class PythonInterpreterPlugin : IBotSharpAppPlugin
|
|||
services.AddSingleton(x => settings);
|
||||
|
||||
services.AddScoped<IAgentUtilityHook, PyProgrammerUtilityHook>();
|
||||
services.AddScoped<ICodeInterpretService, PyInterpretService>();
|
||||
services.AddScoped<ICodeProcessor, PyCodeInterpreter>();
|
||||
}
|
||||
|
||||
public void Configure(IApplicationBuilder app)
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
using BotSharp.Core.CodeInterpreter;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Python.Runtime;
|
||||
using System.Threading;
|
||||
|
|
@ -6,15 +5,15 @@ using System.Threading.Tasks;
|
|||
|
||||
namespace BotSharp.Plugin.PythonInterpreter.Services;
|
||||
|
||||
public class PyInterpretService : ICodeInterpretService
|
||||
public class PyCodeInterpreter : ICodeProcessor
|
||||
{
|
||||
private readonly IServiceProvider _services;
|
||||
private readonly ILogger<PyInterpretService> _logger;
|
||||
private readonly ILogger<PyCodeInterpreter> _logger;
|
||||
private readonly CodeScriptExecutor _executor;
|
||||
|
||||
public PyInterpretService(
|
||||
public PyCodeInterpreter(
|
||||
IServiceProvider services,
|
||||
ILogger<PyInterpretService> logger,
|
||||
ILogger<PyCodeInterpreter> logger,
|
||||
CodeScriptExecutor executor)
|
||||
{
|
||||
_services = services;
|
||||
|
|
@ -24,11 +23,11 @@ public class PyInterpretService : ICodeInterpretService
|
|||
|
||||
public string Provider => "botsharp-py-interpreter";
|
||||
|
||||
public async Task<CodeInterpretResult> RunCode(string codeScript, CodeInterpretOptions? options = null)
|
||||
public async Task<CodeInterpretResponse> RunAsync(string codeScript, CodeInterpretOptions? options = null)
|
||||
{
|
||||
if (options?.UseMutex == true)
|
||||
{
|
||||
return await _executor.Execute(async () =>
|
||||
return await _executor.ExecuteAsync(async () =>
|
||||
{
|
||||
return InnerRunCode(codeScript, options);
|
||||
}, cancellationToken: options?.CancellationToken ?? CancellationToken.None);
|
||||
|
|
@ -36,7 +35,7 @@ public class PyInterpretService : ICodeInterpretService
|
|||
return InnerRunCode(codeScript, options);
|
||||
}
|
||||
|
||||
private CodeInterpretResult InnerRunCode(string codeScript, CodeInterpretOptions? options = null)
|
||||
private CodeInterpretResponse InnerRunCode(string codeScript, CodeInterpretOptions? options = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
|
@ -44,10 +43,10 @@ public class PyInterpretService : ICodeInterpretService
|
|||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var errorMsg = $"Error when executing inner python code in {nameof(PyInterpretService)}: {Provider}.";
|
||||
var errorMsg = $"Error when executing inner python code in {nameof(PyCodeInterpreter)}: {Provider}.";
|
||||
_logger.LogError(ex, errorMsg);
|
||||
|
||||
return new CodeInterpretResult
|
||||
return new CodeInterpretResponse
|
||||
{
|
||||
Success = false,
|
||||
ErrorMsg = errorMsg
|
||||
|
|
@ -55,7 +54,7 @@ public class PyInterpretService : ICodeInterpretService
|
|||
}
|
||||
}
|
||||
|
||||
private CodeInterpretResult CoreRun(string codeScript, CodeInterpretOptions? options = null)
|
||||
private CodeInterpretResponse CoreRun(string codeScript, CodeInterpretOptions? options = null)
|
||||
{
|
||||
using (Py.GIL())
|
||||
{
|
||||
|
|
@ -100,7 +99,7 @@ public class PyInterpretService : ICodeInterpretService
|
|||
// Get result
|
||||
var result = stringIO.getvalue()?.ToString() as string;
|
||||
|
||||
return new CodeInterpretResult
|
||||
return new CodeInterpretResponse
|
||||
{
|
||||
Result = result?.TrimEnd('\r', '\n'),
|
||||
Success = true
|
||||
|
|
@ -108,10 +107,10 @@ public class PyInterpretService : ICodeInterpretService
|
|||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var errorMsg = $"Error when executing core python code in {nameof(PyInterpretService)}: {Provider}. {ex.Message}";
|
||||
var errorMsg = $"Error when executing core python code in {nameof(PyCodeInterpreter)}: {Provider}. {ex.Message}";
|
||||
_logger.LogError(ex, errorMsg);
|
||||
|
||||
return new CodeInterpretResult
|
||||
return new CodeInterpretResponse
|
||||
{
|
||||
Success = false,
|
||||
ErrorMsg = errorMsg
|
||||
|
|
@ -20,9 +20,10 @@ 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.Abstraction.Coding;
|
||||
global using BotSharp.Abstraction.Coding.Options;
|
||||
global using BotSharp.Abstraction.Coding.Responses;
|
||||
global using BotSharp.Core.Coding;
|
||||
global using BotSharp.Core.Infrastructures;
|
||||
|
||||
global using BotSharp.Plugin.PythonInterpreter.Enums;
|
||||
|
|
|
|||
Loading…
Reference in a new issue