Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

12 changes: 12 additions & 0 deletions src/Infrastructure/BotSharp.Abstraction/Coding/ICodeProcessor.cs
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Threading;

namespace BotSharp.Abstraction.CodeInterpreter.Models;
namespace BotSharp.Abstraction.Coding.Options;

public class CodeInterpretOptions
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace BotSharp.Abstraction.Files.Options;

public class FileLlmProcessOptions
public class FileHandleOptions
{
/// <summary>
/// Llm provider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

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();
}
Original file line number Diff line number Diff line change
@@ -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; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ namespace BotSharp.Abstraction.Instructs.Options;

public class FileInstructOptions
{
public string? FileLlmProcessorProvider { get; set; }
public string? Processor { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using BotSharp.Abstraction.CodeInterpreter.Models;

namespace BotSharp.Core.CodeInterpreter;
namespace BotSharp.Core.Coding;

public class CodeScriptExecutor
{
Expand All @@ -13,7 +11,7 @@ public 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);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
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;
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;
Expand All @@ -18,7 +17,6 @@
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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -47,7 +47,7 @@ public async Task<InstructResult> Execute(


// Run code template
var codeResponse = await GetCodeResponse(agent, message, templateName, codeOptions);
var codeResponse = await RunCode(agent, message, templateName, codeOptions);
if (codeResponse != null)
{
return codeResponse;
Expand Down Expand Up @@ -105,16 +105,16 @@ public async Task<InstructResult> Execute(
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,
Expand All @@ -124,7 +124,7 @@ public async Task<InstructResult> Execute(
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
{
Expand Down Expand Up @@ -160,7 +160,7 @@ await hook.OnResponseGenerated(new InstructResponseModel
/// <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,
Expand All @@ -177,14 +177,14 @@ await hook.OnResponseGenerated(new InstructResponseModel
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;
}
Expand Down Expand Up @@ -248,7 +248,7 @@ await hook.OnResponseGenerated(new InstructResponseModel
}

// 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
Expand All @@ -258,7 +258,7 @@ await hook.OnResponseGenerated(new InstructResponseModel
{
MessageId = message.MessageId,
Template = scriptName,
Text = result?.Result ?? result?.ErrorMsg
Text = codeResponse?.Result ?? codeResponse?.ErrorMsg
};

if (context?.Arguments != null)
Expand All @@ -273,7 +273,7 @@ await hook.OnResponseGenerated(new InstructResponseModel
await hook.OnResponseGenerated(new InstructResponseModel
{
AgentId = agent.Id,
Provider = codeInterpreter.Provider,
Provider = codeProcessor.Provider,
Model = string.Empty,
TemplateName = scriptName,
UserMessage = message.Content,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void RegisterDI(IServiceCollection services, IConfiguration config)
services.AddSingleton(x => settings);

services.AddScoped<IAgentUtilityHook, PyProgrammerUtilityHook>();
services.AddScoped<ICodeInterpretService, PyInterpretService>();
services.AddScoped<ICodeProcessor, PyCodeInterpreter>();
}

public void Configure(IApplicationBuilder app)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
using BotSharp.Core.CodeInterpreter;
using Microsoft.Extensions.Logging;
using Python.Runtime;
using System.Threading;
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;
Expand All @@ -24,38 +23,38 @@ public PyInterpretService(

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);
}
return InnerRunCode(codeScript, options);
}

private CodeInterpretResult InnerRunCode(string codeScript, CodeInterpretOptions? options = null)
private CodeInterpretResponse InnerRunCode(string codeScript, CodeInterpretOptions? options = null)
{
try
{
return CoreRun(codeScript, options);
}
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
};
}
}

private CodeInterpretResult CoreRun(string codeScript, CodeInterpretOptions? options = null)
private CodeInterpretResponse CoreRun(string codeScript, CodeInterpretOptions? options = null)
{
using (Py.GIL())
{
Expand Down Expand Up @@ -100,18 +99,18 @@ private CodeInterpretResult CoreRun(string codeScript, CodeInterpretOptions? opt
// Get result
var result = stringIO.getvalue()?.ToString() as string;

return new CodeInterpretResult
return new CodeInterpretResponse
{
Result = result?.TrimEnd('\r', '\n'),
Success = true
};
}
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
Expand Down
7 changes: 4 additions & 3 deletions src/Plugins/BotSharp.Plugin.PythonInterpreter/Using.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
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;
Expand Down
Loading