init agent code
This commit is contained in:
parent
2e0069d4e5
commit
1be9ffebaa
|
|
@ -0,0 +1,22 @@
|
|||
namespace BotSharp.Abstraction.Agents.Models;
|
||||
|
||||
public class AgentCodeScript
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string Content { get; set; }
|
||||
|
||||
public AgentCodeScript()
|
||||
{
|
||||
}
|
||||
|
||||
public AgentCodeScript(string name, string content)
|
||||
{
|
||||
Name = name;
|
||||
Content = content;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return Name;
|
||||
}
|
||||
}
|
||||
|
|
@ -6,5 +6,5 @@ public interface ICodeInterpretService
|
|||
{
|
||||
string Provider { get; }
|
||||
|
||||
Task<CodeInterpretResult> RunCode(string code, IEnumerable<KeyValue>? arguments = null, CodeInterpretOptions? options = null);
|
||||
Task<CodeInterpretResult> RunCode(string codeScript, IEnumerable<KeyValue>? arguments = null, CodeInterpretOptions? options = null);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,12 +10,12 @@ public interface IInstructService
|
|||
/// <param name="agentId"></param>
|
||||
/// <param name="message"></param>
|
||||
/// <param name="instruction"></param>
|
||||
/// <param name="llmTemplateName"></param>
|
||||
/// <param name="templateName"></param>
|
||||
/// <param name="files"></param>
|
||||
/// <param name="codeOptions"></param>
|
||||
/// <returns></returns>
|
||||
Task<InstructResult> Execute(string agentId, RoleDialogModel message,
|
||||
string? instruction = null, string? llmTemplateName = null,
|
||||
string? instruction = null, string? templateName = null,
|
||||
IEnumerable<InstructFileModel>? files = null, CodeInstructOptions? codeOptions = null);
|
||||
|
||||
/// <summary>
|
||||
|
|
|
|||
|
|
@ -2,6 +2,6 @@ namespace BotSharp.Abstraction.Instructs.Models;
|
|||
|
||||
public class CodeInstructOptions
|
||||
{
|
||||
public string? CodeTemplateName { get; set; }
|
||||
public string? CodeScriptName { get; set; }
|
||||
public string? CodeInterpretProvider { get; set; }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -85,7 +85,6 @@ public interface IBotSharpRepository : IHaveServiceProvider
|
|||
=> throw new NotImplementedException();
|
||||
bool PatchAgentTemplate(string agentId, AgentTemplate template)
|
||||
=> throw new NotImplementedException();
|
||||
|
||||
bool UpdateAgentLabels(string agentId, List<string> labels)
|
||||
=> throw new NotImplementedException();
|
||||
bool AppendAgentLabels(string agentId, List<string> labels)
|
||||
|
|
@ -109,6 +108,14 @@ public interface IBotSharpRepository : IHaveServiceProvider
|
|||
=> throw new NotImplementedException();
|
||||
#endregion
|
||||
|
||||
#region Agent Code
|
||||
string? GetAgentCodeScript(string agentId, string scriptName)
|
||||
=> throw new NotImplementedException();
|
||||
|
||||
bool UpdateAgentCodeScript(string agentId, AgentCodeScript script)
|
||||
=> throw new NotImplementedException();
|
||||
#endregion
|
||||
|
||||
#region Conversation
|
||||
void CreateNewConversation(Conversation conversation)
|
||||
=> throw new NotImplementedException();
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ public partial class InstructService
|
|||
string agentId,
|
||||
RoleDialogModel message,
|
||||
string? instruction = null,
|
||||
string? llmTemplateName = null,
|
||||
string? templateName = null,
|
||||
IEnumerable<InstructFileModel>? files = null,
|
||||
CodeInstructOptions? codeOptions = null)
|
||||
{
|
||||
|
|
@ -22,7 +22,7 @@ public partial class InstructService
|
|||
var response = new InstructResult
|
||||
{
|
||||
MessageId = message.MessageId,
|
||||
Template = codeOptions?.CodeTemplateName ?? llmTemplateName
|
||||
Template = codeOptions?.CodeScriptName ?? templateName
|
||||
};
|
||||
|
||||
|
||||
|
|
@ -40,23 +40,36 @@ public partial class InstructService
|
|||
}
|
||||
|
||||
// Run code template
|
||||
if (!string.IsNullOrWhiteSpace(codeOptions?.CodeTemplateName))
|
||||
if (!string.IsNullOrWhiteSpace(codeOptions?.CodeScriptName))
|
||||
{
|
||||
var codeInterpreter = _services.GetServices<ICodeInterpretService>()
|
||||
.FirstOrDefault(x => x.Provider.IsEqualTo(codeOptions?.CodeInterpretProvider.IfNullOrEmptyAs("python-interpreter")));
|
||||
|
||||
if (codeInterpreter == null)
|
||||
{
|
||||
var error = "No code interpreter found.";
|
||||
var error = $"No code interpreter found. (Agent: {agentId}, Code interpreter: {codeOptions.CodeInterpretProvider})";
|
||||
_logger.LogError(error);
|
||||
response.Text = error;
|
||||
}
|
||||
else
|
||||
{
|
||||
var db = _services.GetRequiredService<IBotSharpRepository>();
|
||||
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();
|
||||
var codeScript = db.GetAgentCodeScript(agentId, codeOptions.CodeScriptName);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(codeScript))
|
||||
{
|
||||
var error = $"Empty code script. (Agent: {agentId}, Code script: {codeOptions.CodeScriptName})";
|
||||
_logger.LogError(error);
|
||||
response.Text = error;
|
||||
}
|
||||
else
|
||||
{
|
||||
var result = await codeInterpreter.RunCode(codeScript, arguments);
|
||||
response.Text = result?.Result?.ToString();
|
||||
}
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
|
@ -83,9 +96,9 @@ public partial class InstructService
|
|||
var model = string.Empty;
|
||||
|
||||
// Render prompt
|
||||
var prompt = string.IsNullOrEmpty(llmTemplateName) ?
|
||||
var prompt = string.IsNullOrEmpty(templateName) ?
|
||||
agentService.RenderInstruction(agent) :
|
||||
agentService.RenderTemplate(agent, llmTemplateName);
|
||||
agentService.RenderTemplate(agent, templateName);
|
||||
|
||||
var completer = CompletionProvider.GetCompletion(_services,
|
||||
agentConfig: agent.LlmConfig);
|
||||
|
|
@ -136,7 +149,7 @@ public partial class InstructService
|
|||
AgentId = agentId,
|
||||
Provider = provider,
|
||||
Model = model,
|
||||
TemplateName = llmTemplateName,
|
||||
TemplateName = templateName,
|
||||
UserMessage = prompt,
|
||||
SystemInstruction = instruction,
|
||||
CompletionText = response.Text
|
||||
|
|
|
|||
|
|
@ -0,0 +1,61 @@
|
|||
using System.IO;
|
||||
|
||||
namespace BotSharp.Core.Repository;
|
||||
|
||||
public partial class FileRepository
|
||||
{
|
||||
#region Code
|
||||
public string? GetAgentCodeScript(string agentId, string scriptName)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(agentId)
|
||||
|| string.IsNullOrWhiteSpace(scriptName))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var dir = Path.Combine(_dbSettings.FileRepository, _agentSettings.DataDir, agentId, AGENT_CODE_FOLDER);
|
||||
if (!Directory.Exists(dir))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
foreach (var file in Directory.GetFiles(dir))
|
||||
{
|
||||
var fileName = Path.GetFileName(file);
|
||||
if (scriptName.IsEqualTo(fileName))
|
||||
{
|
||||
return File.ReadAllText(file);
|
||||
}
|
||||
}
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
public bool UpdateAgentCodeScript(string agentId, AgentCodeScript script)
|
||||
{
|
||||
if (string.IsNullOrEmpty(agentId) || script == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var dir = Path.Combine(_dbSettings.FileRepository, _agentSettings.DataDir, agentId, AGENT_CODE_FOLDER);
|
||||
if (!Directory.Exists(dir))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var found = Directory.GetFiles(dir).FirstOrDefault(f =>
|
||||
{
|
||||
var fileName = Path.GetFileName(f);
|
||||
return fileName.IsEqualTo(script.Name);
|
||||
});
|
||||
|
||||
if (found == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
File.WriteAllText(found, script.Content);
|
||||
return true;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
|
@ -26,6 +26,7 @@ public partial class FileRepository : IBotSharpRepository
|
|||
private const string AGENT_FUNCTIONS_FOLDER = "functions";
|
||||
private const string AGENT_TEMPLATES_FOLDER = "templates";
|
||||
private const string AGENT_RESPONSES_FOLDER = "responses";
|
||||
private const string AGENT_CODE_FOLDER = "codes";
|
||||
private const string AGENT_TASKS_FOLDER = "tasks";
|
||||
private const string AGENT_TASK_PREFIX = "#metadata";
|
||||
private const string AGENT_TASK_SUFFIX = "/metadata";
|
||||
|
|
|
|||
|
|
@ -42,7 +42,6 @@ public class InstructModeController : ControllerBase
|
|||
codeOptions: input.CodeOptions);
|
||||
|
||||
result.States = state.GetStates();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ public class PyInterpretService : ICodeInterpretService
|
|||
|
||||
public string Provider => "python-interpreter";
|
||||
|
||||
public async Task<CodeInterpretResult> RunCode(string code,
|
||||
public async Task<CodeInterpretResult> RunCode(string codeScript,
|
||||
IEnumerable<KeyValue>? arguments = null, CodeInterpretOptions? options = null)
|
||||
{
|
||||
try
|
||||
|
|
@ -38,7 +38,7 @@ public class PyInterpretService : ICodeInterpretService
|
|||
|
||||
// Set global items
|
||||
using var globals = new PyDict();
|
||||
if (code.Contains("__main__") == true)
|
||||
if (codeScript.Contains("__main__") == true)
|
||||
{
|
||||
globals.SetItem("__name__", new PyString("__main__"));
|
||||
}
|
||||
|
|
@ -61,7 +61,7 @@ public class PyInterpretService : ICodeInterpretService
|
|||
}
|
||||
|
||||
// Execute Python script
|
||||
PythonEngine.Exec(code, globals);
|
||||
PythonEngine.Exec(codeScript, globals);
|
||||
|
||||
// Get result
|
||||
var result = stringIO.getvalue().ToString();
|
||||
|
|
|
|||
Loading…
Reference in a new issue