init code template in instruct service

This commit is contained in:
Jicheng Lu 2025-09-30 15:03:06 -05:00
parent 2770c4fe22
commit 2e0069d4e5
9 changed files with 91 additions and 35 deletions

View file

@ -6,5 +6,5 @@ public interface ICodeInterpretService
{
string Provider { get; }
Task<CodeInterpretResult> RunCode(string code, List<KeyValue>? arguments = null, CodeInterpretOptions? options = null);
Task<CodeInterpretResult> RunCode(string code, IEnumerable<KeyValue>? arguments = null, CodeInterpretOptions? options = null);
}

View file

@ -7,13 +7,16 @@ public interface IInstructService
/// <summary>
/// Execute completion by using specified instruction or template
/// </summary>
/// <param name="agentId">Agent (static agent)</param>
/// <param name="message">Additional message provided by user</param>
/// <param name="templateName">Template name</param>
/// <param name="instruction">System prompt</param>
/// <param name="agentId"></param>
/// <param name="message"></param>
/// <param name="instruction"></param>
/// <param name="llmTemplateName"></param>
/// <param name="files"></param>
/// <param name="codeOptions"></param>
/// <returns></returns>
Task<InstructResult> Execute(string agentId, RoleDialogModel message,
string? templateName = null, string? instruction = null, IEnumerable<InstructFileModel>? files = null);
string? instruction = null, string? llmTemplateName = null,
IEnumerable<InstructFileModel>? files = null, CodeInstructOptions? codeOptions = null);
/// <summary>
/// A generic way to execute completion by using specified instruction or template

View file

@ -0,0 +1,7 @@
namespace BotSharp.Abstraction.Instructs.Models;
public class CodeInstructOptions
{
public string? CodeTemplateName { get; set; }
public string? CodeInterpretProvider { get; set; }
}

View file

@ -6,7 +6,8 @@ public class InstructResult : ITrackableMessage
public string MessageId { get; set; }
public string Text { get; set; } = string.Empty;
public object? Data { get; set; }
[JsonPropertyName("template")]
public string? Template{ get; set; }
public string? Template { get; set; }
public Dictionary<string, string>? States { get; set; } = new();
}

View file

@ -8,6 +8,17 @@ public class KeyValue
[JsonPropertyName("value")]
public string? Value { get; set; }
public KeyValue()
{
}
public KeyValue(string key, string? value)
{
Key = key;
Value = value;
}
public override string ToString()
{
return $"Key: {Key}, Value: {Value}";

View file

@ -1,36 +1,67 @@
using BotSharp.Abstraction.CodeInterpreter;
using BotSharp.Abstraction.Instructs;
using BotSharp.Abstraction.Instructs.Models;
using BotSharp.Abstraction.MLTasks;
using BotSharp.Abstraction.Models;
namespace BotSharp.Core.Instructs;
public partial class InstructService
{
public async Task<InstructResult> Execute(string agentId, RoleDialogModel message,
string? templateName = null, string? instruction = null, IEnumerable<InstructFileModel>? files = null)
public async Task<InstructResult> Execute(
string agentId,
RoleDialogModel message,
string? instruction = null,
string? llmTemplateName = null,
IEnumerable<InstructFileModel>? files = null,
CodeInstructOptions? codeOptions = null)
{
var agentService = _services.GetRequiredService<IAgentService>();
Agent agent = await agentService.LoadAgent(agentId);
var response = new InstructResult
{
MessageId = message.MessageId,
Template = codeOptions?.CodeTemplateName ?? llmTemplateName
};
if (agent == null)
{
return new InstructResult
{
MessageId = message.MessageId,
Text = $"Agent (id: {agentId}) does not exist!"
};
response.Text = $"Agent (id: {agentId}) does not exist!";
return response;
}
if (agent.Disabled)
{
var content = $"This agent ({agent.Name}) is disabled, please install the corresponding plugin ({agent.Plugin.Name}) to activate this agent.";
return new InstructResult
{
MessageId = message.MessageId,
Text = content
};
response.Text = content;
return response;
}
// Run code template
if (!string.IsNullOrWhiteSpace(codeOptions?.CodeTemplateName))
{
var codeInterpreter = _services.GetServices<ICodeInterpretService>()
.FirstOrDefault(x => x.Provider.IsEqualTo(codeOptions?.CodeInterpretProvider.IfNullOrEmptyAs("python-interpreter")));
if (codeInterpreter == null)
{
var error = "No code interpreter found.";
_logger.LogError(error);
response.Text = error;
}
else
{
var state = _services.GetRequiredService<IConversationStateService>();
var arguments = state.GetStates().Select(x => new KeyValue(x.Key, x.Value));
var result = await codeInterpreter.RunCode("", arguments);
response.Text = result?.Result?.ToString();
}
return response;
}
// Trigger before completion hooks
var hooks = _services.GetHooks<IInstructHook>(agentId);
foreach (var hook in hooks)
@ -52,19 +83,13 @@ public partial class InstructService
var model = string.Empty;
// Render prompt
var prompt = string.IsNullOrEmpty(templateName) ?
var prompt = string.IsNullOrEmpty(llmTemplateName) ?
agentService.RenderInstruction(agent) :
agentService.RenderTemplate(agent, templateName);
agentService.RenderTemplate(agent, llmTemplateName);
var completer = CompletionProvider.GetCompletion(_services,
agentConfig: agent.LlmConfig);
var response = new InstructResult
{
MessageId = message.MessageId,
Template = templateName,
};
if (completer is ITextCompletion textCompleter)
{
instruction = null;
@ -111,7 +136,7 @@ public partial class InstructService
AgentId = agentId,
Provider = provider,
Model = model,
TemplateName = templateName,
TemplateName = llmTemplateName,
UserMessage = prompt,
SystemInstruction = instruction,
CompletionText = response.Text

View file

@ -36,9 +36,10 @@ public class InstructModeController : ControllerBase
var instructor = _services.GetRequiredService<IInstructService>();
var result = await instructor.Execute(agentId,
new RoleDialogModel(AgentRole.User, input.Text),
templateName: input.Template,
instruction: input.Instruction,
files: input.Files);
llmTemplateName: input.Template,
files: input.Files,
codeOptions: input.CodeOptions);
result.States = state.GetStates();

View file

@ -1,3 +1,5 @@
using BotSharp.Abstraction.Instructs.Models;
namespace BotSharp.OpenAPI.ViewModels.Instructs;
public class InstructMessageModel : IncomingMessageModel
@ -9,6 +11,7 @@ public class InstructMessageModel : IncomingMessageModel
public override string Channel { get; set; } = ConversationChannel.OpenAPI;
public string? Template { get; set; }
public List<InstructFileModel> Files { get; set; } = [];
public CodeInstructOptions? CodeOptions { get; set; }
}

View file

@ -20,7 +20,8 @@ public class PyInterpretService : ICodeInterpretService
public string Provider => "python-interpreter";
public async Task<CodeInterpretResult> RunCode(string code, List<KeyValue>? arguments = null, CodeInterpretOptions? options = null)
public async Task<CodeInterpretResult> RunCode(string code,
IEnumerable<KeyValue>? arguments = null, CodeInterpretOptions? options = null)
{
try
{
@ -45,14 +46,18 @@ public class PyInterpretService : ICodeInterpretService
// Set arguments
if (!arguments.IsNullOrEmpty())
{
sys.argv = new PyList();
sys.argv.Append("code.py");
var list = new PyList();
list.Append(new PyString("code.py"));
foreach (var arg in arguments)
{
sys.argv.Append($"--{arg.Key}");
sys.argv.Append($"{arg.Value}");
if (!string.IsNullOrWhiteSpace(arg.Key) && !string.IsNullOrWhiteSpace(arg.Value))
{
list.Append(new PyString($"--{arg.Key}"));
list.Append(new PyString($"{arg.Value}"));
}
}
sys.argv = list;
}
// Execute Python script