Merge branch 'SciSharp:master' into master
This commit is contained in:
commit
8e66dc49c7
|
|
@ -17,7 +17,7 @@ public enum AgentField
|
|||
Response,
|
||||
Sample,
|
||||
LlmConfig,
|
||||
Tool
|
||||
Utility
|
||||
}
|
||||
|
||||
public enum AgentTaskField
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
namespace BotSharp.Abstraction.Agents.Enums;
|
||||
|
||||
public class AgentTool
|
||||
public class AgentUtility
|
||||
{
|
||||
public const string FileAnalyzer = "file-analyzer";
|
||||
public const string ImageGenerator = "image-generator";
|
||||
|
|
@ -52,5 +52,5 @@ public interface IAgentService
|
|||
|
||||
PluginDef GetPlugin(string agentId);
|
||||
|
||||
IEnumerable<string> GetAgentTools();
|
||||
IEnumerable<string> GetAgentUtilities();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +0,0 @@
|
|||
namespace BotSharp.Abstraction.Agents;
|
||||
|
||||
public interface IAgentToolHook
|
||||
{
|
||||
void AddTools(List<string> tools);
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
namespace BotSharp.Abstraction.Agents;
|
||||
|
||||
public interface IAgentUtilityHook
|
||||
{
|
||||
void AddUtilities(List<string> utilities);
|
||||
}
|
||||
|
|
@ -91,9 +91,9 @@ public class Agent
|
|||
= new List<string>();
|
||||
|
||||
/// <summary>
|
||||
/// Useful tools
|
||||
/// Agent utilities
|
||||
/// </summary>
|
||||
public List<string> Tools { get; set; }
|
||||
public List<string> Utilities { get; set; }
|
||||
= new List<string>();
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -127,7 +127,7 @@ public class Agent
|
|||
Functions = agent.Functions,
|
||||
Responses = agent.Responses,
|
||||
Samples = agent.Samples,
|
||||
Tools = agent.Tools,
|
||||
Utilities = agent.Utilities,
|
||||
Knowledges = agent.Knowledges,
|
||||
IsPublic = agent.IsPublic,
|
||||
Disabled = agent.Disabled,
|
||||
|
|
@ -169,9 +169,9 @@ public class Agent
|
|||
return this;
|
||||
}
|
||||
|
||||
public Agent SetTools(List<string> tools)
|
||||
public Agent SetUtilities(List<string> utilities)
|
||||
{
|
||||
Tools = tools ?? new List<string>();
|
||||
Utilities = utilities ?? new List<string>();
|
||||
return this;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ public class AgentSettings
|
|||
public string TemplateFormat { get; set; } = "liquid";
|
||||
public string HostAgentId { get; set; } = string.Empty;
|
||||
public bool EnableTranslator { get; set; } = false;
|
||||
public bool EnableHttpHandler { get; set; } = false;
|
||||
|
||||
/// <summary>
|
||||
/// This is the default LLM config for agent
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ public interface IBotSharpFileService
|
|||
Task<IEnumerable<MessageFileModel>> GetChatImages(string conversationId, string source, IEnumerable<string> fileTypes, List<RoleDialogModel> conversations, int? offset = null);
|
||||
IEnumerable<MessageFileModel> GetMessageFiles(string conversationId, IEnumerable<string> messageIds, string source, bool imageOnly = false);
|
||||
string GetMessageFile(string conversationId, string messageId, string source, string index, string fileName);
|
||||
bool HasConversationUserFiles(string conversationId);
|
||||
bool SaveMessageFiles(string conversationId, string messageId, string source, List<BotSharpFile> files);
|
||||
|
||||
string GetUserAvatar();
|
||||
|
|
|
|||
|
|
@ -3,8 +3,14 @@ namespace BotSharp.Abstraction.Files.Models;
|
|||
public class LlmFileContext
|
||||
{
|
||||
[JsonPropertyName("user_request")]
|
||||
public string UserRequest { get; set; }
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
public string? UserRequest { get; set; }
|
||||
|
||||
[JsonPropertyName("file_types")]
|
||||
public string FileTypes { get; set; }
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
public string? FileTypes { get; set; }
|
||||
|
||||
[JsonPropertyName("image_description")]
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
public string? ImageDescription { get; set; }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ namespace BotSharp.Abstraction.Utilities;
|
|||
|
||||
public static class ListExtenstions
|
||||
{
|
||||
public static bool IsNullOrEmpty<T>(this IEnumerable<T> list)
|
||||
public static bool IsNullOrEmpty<T>(this IEnumerable<T>? list)
|
||||
{
|
||||
return list == null || !list.Any();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ public partial class AgentService
|
|||
record.Templates = agent.Templates ?? new List<AgentTemplate>();
|
||||
record.Responses = agent.Responses ?? new List<AgentResponse>();
|
||||
record.Samples = agent.Samples ?? new List<string>();
|
||||
record.Tools = agent.Tools ?? new List<string>();
|
||||
record.Utilities = agent.Utilities ?? new List<string>();
|
||||
if (agent.LlmConfig != null && !agent.LlmConfig.IsInherit)
|
||||
{
|
||||
record.LlmConfig = agent.LlmConfig;
|
||||
|
|
@ -94,7 +94,7 @@ public partial class AgentService
|
|||
.SetFunctions(foundAgent.Functions)
|
||||
.SetResponses(foundAgent.Responses)
|
||||
.SetSamples(foundAgent.Samples)
|
||||
.SetTools(foundAgent.Tools)
|
||||
.SetUtilities(foundAgent.Utilities)
|
||||
.SetLlmConfig(foundAgent.LlmConfig);
|
||||
|
||||
_db.UpdateAgent(clonedAgent, AgentField.All);
|
||||
|
|
|
|||
|
|
@ -55,15 +55,14 @@ public partial class AgentService : IAgentService
|
|||
return agents;
|
||||
}
|
||||
|
||||
public IEnumerable<string> GetAgentTools()
|
||||
public IEnumerable<string> GetAgentUtilities()
|
||||
{
|
||||
var tools = new List<string>();
|
||||
|
||||
var hooks = _services.GetServices<IAgentToolHook>();
|
||||
var utilities = new List<string>();
|
||||
var hooks = _services.GetServices<IAgentUtilityHook>();
|
||||
foreach (var hook in hooks)
|
||||
{
|
||||
hook.AddTools(tools);
|
||||
hook.AddUtilities(utilities);
|
||||
}
|
||||
return tools.Where(x => !string.IsNullOrWhiteSpace(x)).Distinct().OrderBy(x => x).ToList();
|
||||
return utilities.Where(x => !string.IsNullOrWhiteSpace(x)).Distinct().OrderBy(x => x).ToList();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -47,8 +47,10 @@
|
|||
|
||||
<ItemGroup>
|
||||
<None Remove="data\agents\00000000-0000-0000-0000-000000000000\agent.json" />
|
||||
<None Remove="data\agents\00000000-0000-0000-0000-000000000000\functions\generate_image.json" />
|
||||
<None Remove="data\agents\00000000-0000-0000-0000-000000000000\instruction.liquid" />
|
||||
<None Remove="data\agents\00000000-0000-0000-0000-000000000000\functions\load_attachment.json" />
|
||||
<None Remove="data\agents\00000000-0000-0000-0000-000000000000\templates\generate_image.fn.liquid" />
|
||||
<None Remove="data\agents\00000000-0000-0000-0000-000000000000\templates\load_attachment.fn.liquid" />
|
||||
<None Remove="data\agents\01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b\agent.json" />
|
||||
<None Remove="data\agents\01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b\functions.json" />
|
||||
|
|
@ -163,6 +165,12 @@
|
|||
<Content Include="data\agents\00000000-0000-0000-0000-000000000000\templates\load_attachment.fn.liquid">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="data\agents\00000000-0000-0000-0000-000000000000\functions\generate_image.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="data\agents\00000000-0000-0000-0000-000000000000\templates\generate_image.fn.liquid">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="data\plugins\config.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
|
|
|
|||
|
|
@ -18,6 +18,8 @@ public class FilePlugin : IBotSharpPlugin
|
|||
services.AddScoped<IBotSharpFileService, BotSharpFileService>();
|
||||
|
||||
services.AddScoped<IAgentHook, FileAnalyzerHook>();
|
||||
services.AddScoped<IAgentToolHook, FileAnalyzerToolHook>();
|
||||
services.AddScoped<IAgentUtilityHook, FileAnalyzerUtilityHook>();
|
||||
services.AddScoped<IAgentHook, ImageGeneratorHook>();
|
||||
services.AddScoped<IAgentUtilityHook, ImageGeneratorUtilityHook>();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,125 @@
|
|||
using BotSharp.Abstraction.Functions;
|
||||
using System.Net.Http;
|
||||
|
||||
namespace BotSharp.Core.Files.Functions;
|
||||
|
||||
public class GenerateImageFn : IFunctionCallback
|
||||
{
|
||||
public string Name => "generate_image";
|
||||
public string Indication => "Generating image";
|
||||
|
||||
private readonly IServiceProvider _services;
|
||||
private readonly ILogger<GenerateImageFn> _logger;
|
||||
private static string UTILITY_ASSISTANT = Guid.Empty.ToString();
|
||||
private string _conversationId;
|
||||
private string _messageId;
|
||||
|
||||
public GenerateImageFn(
|
||||
IServiceProvider services,
|
||||
ILogger<GenerateImageFn> logger)
|
||||
{
|
||||
_services = services;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
|
||||
public async Task<bool> Execute(RoleDialogModel message)
|
||||
{
|
||||
var args = JsonSerializer.Deserialize<LlmFileContext>(message.FunctionArgs);
|
||||
Init(message);
|
||||
SetImageOptions();
|
||||
|
||||
var agentService = _services.GetRequiredService<IAgentService>();
|
||||
var agent = await agentService.LoadAgent(UTILITY_ASSISTANT);
|
||||
var imageAgent = new Agent
|
||||
{
|
||||
Id = agent?.Id ?? Guid.Empty.ToString(),
|
||||
Name = agent?.Name ?? "Unkown",
|
||||
Instruction = args?.ImageDescription,
|
||||
TemplateDict = new Dictionary<string, object>()
|
||||
};
|
||||
|
||||
var response = await GetImageGeneration(imageAgent, message, args?.ImageDescription);
|
||||
message.Content = response;
|
||||
message.StopCompletion = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
private void Init(RoleDialogModel message)
|
||||
{
|
||||
var convService = _services.GetRequiredService<IConversationService>();
|
||||
_conversationId = convService.ConversationId;
|
||||
_messageId = message.MessageId;
|
||||
}
|
||||
|
||||
private void SetImageOptions()
|
||||
{
|
||||
var state = _services.GetRequiredService<IConversationStateService>();
|
||||
var size = state.SetState("image_size", "1024x1024");
|
||||
var quality = state.SetState("image_quality", "standard");
|
||||
var style = state.SetState("image_style", "natural");
|
||||
var format = state.SetState("image_format", "bytes");
|
||||
var count = state.SetState("image_count", "1");
|
||||
}
|
||||
|
||||
private async Task<string> GetImageGeneration(Agent agent, RoleDialogModel message, string? description)
|
||||
{
|
||||
try
|
||||
{
|
||||
var completion = CompletionProvider.GetImageGeneration(_services, provider: "openai", model: "dall-e-3", imageGenerate: true);
|
||||
var text = !string.IsNullOrWhiteSpace(description) ? description : message.Content;
|
||||
var dialog = RoleDialogModel.From(message, AgentRole.User, text);
|
||||
var result = await completion.GetImageGeneration(agent, new List<RoleDialogModel> { dialog });
|
||||
await SaveGeneratedImages(result?.GeneratedImages);
|
||||
return result?.Content ?? string.Empty;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var error = $"Error when generating image.";
|
||||
_logger.LogWarning($"{error} {ex.Message}");
|
||||
return error;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task SaveGeneratedImages(List<ImageGeneration>? images)
|
||||
{
|
||||
if (images.IsNullOrEmpty()) return;
|
||||
|
||||
var files = new List<BotSharpFile>();
|
||||
foreach (var image in images)
|
||||
{
|
||||
if (string.IsNullOrEmpty(image?.ImageUrl)
|
||||
&& string.IsNullOrEmpty(image?.ImageData))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var data = image.ImageData;
|
||||
if (!string.IsNullOrEmpty(image.ImageUrl))
|
||||
{
|
||||
var http = _services.GetRequiredService<IHttpClientFactory>();
|
||||
using var client = http.CreateClient();
|
||||
var bytes = await client.GetByteArrayAsync(image.ImageUrl);
|
||||
data = Convert.ToBase64String(bytes);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(data))
|
||||
{
|
||||
var imageName = $"{Guid.NewGuid().ToString()}.png";
|
||||
var imageData = $"data:image/png;base64,{data}";
|
||||
files.Add(new BotSharpFile { FileName = imageName, FileData = imageData });
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning($"Error when saving generated image: {image.ImageUrl ?? image.ImageData}\r\n{ex.Message}");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
var fileService = _services.GetRequiredService<IBotSharpFileService>();
|
||||
fileService.SaveMessageFiles(_conversationId, _messageId, FileSourceType.Bot, files);
|
||||
}
|
||||
}
|
||||
|
|
@ -12,7 +12,7 @@ public class LoadAttachmentFn : IFunctionCallback
|
|||
private readonly ILogger<LoadAttachmentFn> _logger;
|
||||
private readonly IEnumerable<string> _imageTypes = new List<string> { "image", "images", "png", "jpg", "jpeg" };
|
||||
private readonly IEnumerable<string> _pdfTypes = new List<string> { "pdf" };
|
||||
private static string TOOL_ASSISTANT = Guid.Empty.ToString();
|
||||
private static string UTILITY_ASSISTANT = Guid.Empty.ToString();
|
||||
|
||||
public LoadAttachmentFn(
|
||||
IServiceProvider services,
|
||||
|
|
@ -31,7 +31,7 @@ public class LoadAttachmentFn : IFunctionCallback
|
|||
var wholeDialogs = conv.GetDialogHistory();
|
||||
var fileTypes = args?.FileTypes?.Split(",", StringSplitOptions.RemoveEmptyEntries)?.ToList() ?? new List<string>();
|
||||
var dialogs = await AssembleFiles(conv.ConversationId, wholeDialogs, fileTypes);
|
||||
var agent = await agentService.LoadAgent(TOOL_ASSISTANT);
|
||||
var agent = await agentService.LoadAgent(UTILITY_ASSISTANT);
|
||||
var fileAgent = new Agent
|
||||
{
|
||||
Id = agent?.Id ?? Guid.Empty.ToString(),
|
||||
|
|
|
|||
|
|
@ -2,7 +2,8 @@ namespace BotSharp.Core.Files.Hooks;
|
|||
|
||||
public class FileAnalyzerHook : AgentHookBase
|
||||
{
|
||||
private static string TOOL_ASSISTANT = Guid.Empty.ToString();
|
||||
private static string UTILITY_ASSISTANT = Guid.Empty.ToString();
|
||||
private static string FUNCTION_NAME = "load_attachment";
|
||||
|
||||
public override string SelfId => string.Empty;
|
||||
|
||||
|
|
@ -15,7 +16,7 @@ public class FileAnalyzerHook : AgentHookBase
|
|||
{
|
||||
var conv = _services.GetRequiredService<IConversationService>();
|
||||
var isConvMode = conv.IsConversationMode();
|
||||
var isEnabled = !agent.Tools.IsNullOrEmpty() && agent.Tools.Contains(AgentTool.FileAnalyzer);
|
||||
var isEnabled = !agent.Utilities.IsNullOrEmpty() && agent.Utilities.Contains(AgentUtility.FileAnalyzer);
|
||||
|
||||
if (isConvMode && isEnabled)
|
||||
{
|
||||
|
|
@ -43,11 +44,10 @@ public class FileAnalyzerHook : AgentHookBase
|
|||
|
||||
private (string, FunctionDef?) GetPromptAndFunction()
|
||||
{
|
||||
var fn = "load_attachment";
|
||||
var db = _services.GetRequiredService<IBotSharpRepository>();
|
||||
var agent = db.GetAgent(TOOL_ASSISTANT);
|
||||
var prompt = agent?.Templates?.FirstOrDefault(x => x.Name.IsEqualTo($"{fn}.fn"))?.Content ?? string.Empty;
|
||||
var loadAttachmentFn = agent?.Functions?.FirstOrDefault(x => x.Name.IsEqualTo(fn));
|
||||
var agent = db.GetAgent(UTILITY_ASSISTANT);
|
||||
var prompt = agent?.Templates?.FirstOrDefault(x => x.Name.IsEqualTo($"{FUNCTION_NAME}.fn"))?.Content ?? string.Empty;
|
||||
var loadAttachmentFn = agent?.Functions?.FirstOrDefault(x => x.Name.IsEqualTo(FUNCTION_NAME));
|
||||
return (prompt, loadAttachmentFn);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +0,0 @@
|
|||
|
||||
namespace BotSharp.Core.Files.Hooks;
|
||||
|
||||
public class FileAnalyzerToolHook : IAgentToolHook
|
||||
{
|
||||
public void AddTools(List<string> tools)
|
||||
{
|
||||
tools.Add(AgentTool.FileAnalyzer);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
namespace BotSharp.Core.Files.Hooks;
|
||||
|
||||
public class FileAnalyzerUtilityHook : IAgentUtilityHook
|
||||
{
|
||||
public void AddUtilities(List<string> utilities)
|
||||
{
|
||||
utilities.Add(AgentUtility.FileAnalyzer);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
namespace BotSharp.Core.Files.Hooks;
|
||||
|
||||
public class ImageGeneratorHook : AgentHookBase
|
||||
{
|
||||
private static string UTILITY_ASSISTANT = Guid.Empty.ToString();
|
||||
private static string FUNCTION_NAME = "generate_image";
|
||||
|
||||
public override string SelfId => string.Empty;
|
||||
|
||||
public ImageGeneratorHook(IServiceProvider services, AgentSettings settings)
|
||||
: base(services, settings)
|
||||
{
|
||||
}
|
||||
|
||||
public override void OnAgentLoaded(Agent agent)
|
||||
{
|
||||
var conv = _services.GetRequiredService<IConversationService>();
|
||||
var isConvMode = conv.IsConversationMode();
|
||||
var isEnabled = !agent.Utilities.IsNullOrEmpty() && agent.Utilities.Contains(AgentUtility.ImageGenerator);
|
||||
|
||||
if (isConvMode && isEnabled)
|
||||
{
|
||||
var (prompt, fn) = GetPromptAndFunction();
|
||||
if (fn != null)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(prompt))
|
||||
{
|
||||
agent.Instruction += $"\r\n\r\n{prompt}\r\n\r\n";
|
||||
}
|
||||
|
||||
if (agent.Functions == null)
|
||||
{
|
||||
agent.Functions = new List<FunctionDef> { fn };
|
||||
}
|
||||
else
|
||||
{
|
||||
agent.Functions.Add(fn);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
base.OnAgentLoaded(agent);
|
||||
}
|
||||
|
||||
private (string, FunctionDef?) GetPromptAndFunction()
|
||||
{
|
||||
var db = _services.GetRequiredService<IBotSharpRepository>();
|
||||
var agent = db.GetAgent(UTILITY_ASSISTANT);
|
||||
var prompt = agent?.Templates?.FirstOrDefault(x => x.Name.IsEqualTo($"{FUNCTION_NAME}.fn"))?.Content ?? string.Empty;
|
||||
var loadAttachmentFn = agent?.Functions?.FirstOrDefault(x => x.Name.IsEqualTo(FUNCTION_NAME));
|
||||
return (prompt, loadAttachmentFn);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
namespace BotSharp.Core.Files.Hooks;
|
||||
|
||||
internal class ImageGeneratorUtilityHook : IAgentUtilityHook
|
||||
{
|
||||
public void AddUtilities(List<string> utilities)
|
||||
{
|
||||
utilities.Add(AgentUtility.ImageGenerator);
|
||||
}
|
||||
}
|
||||
|
|
@ -181,16 +181,6 @@ public partial class BotSharpFileService
|
|||
return found;
|
||||
}
|
||||
|
||||
public bool HasConversationUserFiles(string conversationId)
|
||||
{
|
||||
if (string.IsNullOrEmpty(conversationId)) return false;
|
||||
|
||||
var dir = Path.Combine(_baseDir, CONVERSATION_FOLDER, conversationId, FILE_FOLDER);
|
||||
if (!ExistDirectory(dir)) return false;
|
||||
|
||||
return Directory.GetDirectories(dir).Any();
|
||||
}
|
||||
|
||||
public bool SaveMessageFiles(string conversationId, string messageId, string source, List<BotSharpFile> files)
|
||||
{
|
||||
if (files.IsNullOrEmpty()) return false;
|
||||
|
|
|
|||
|
|
@ -54,8 +54,8 @@ namespace BotSharp.Core.Repository
|
|||
case AgentField.LlmConfig:
|
||||
UpdateAgentLlmConfig(agent.Id, agent.LlmConfig);
|
||||
break;
|
||||
case AgentField.Tool:
|
||||
UpdateAgentTools(agent.Id, agent.Tools);
|
||||
case AgentField.Utility:
|
||||
UpdateAgentUtilities(agent.Id, agent.Utilities);
|
||||
break;
|
||||
case AgentField.All:
|
||||
UpdateAgentAllFields(agent);
|
||||
|
|
@ -149,14 +149,14 @@ namespace BotSharp.Core.Repository
|
|||
File.WriteAllText(agentFile, json);
|
||||
}
|
||||
|
||||
private void UpdateAgentTools(string agentId, List<string> tools)
|
||||
private void UpdateAgentUtilities(string agentId, List<string> utilities)
|
||||
{
|
||||
if (tools == null) return;
|
||||
if (utilities == null) return;
|
||||
|
||||
var (agent, agentFile) = GetAgentFromFile(agentId);
|
||||
if (agent == null) return;
|
||||
|
||||
agent.Tools = tools;
|
||||
agent.Utilities = utilities;
|
||||
agent.UpdatedDateTime = DateTime.UtcNow;
|
||||
var json = JsonSerializer.Serialize(agent, _options);
|
||||
File.WriteAllText(agentFile, json);
|
||||
|
|
@ -301,7 +301,7 @@ namespace BotSharp.Core.Repository
|
|||
agent.Disabled = inputAgent.Disabled;
|
||||
agent.Type = inputAgent.Type;
|
||||
agent.Profiles = inputAgent.Profiles;
|
||||
agent.Tools = inputAgent.Tools;
|
||||
agent.Utilities = inputAgent.Utilities;
|
||||
agent.RoutingRules = inputAgent.RoutingRules;
|
||||
agent.LlmConfig = inputAgent.LlmConfig;
|
||||
agent.UpdatedDateTime = DateTime.UtcNow;
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"id": "00000000-0000-0000-0000-000000000000",
|
||||
"name": "Tool Assistant",
|
||||
"description": "Tool assistant that can be used to complete many different tasks",
|
||||
"name": "Utility Assistant",
|
||||
"description": "Utility assistant that can be used to complete many different tasks",
|
||||
"type": "static",
|
||||
"createdDateTime": "2023-06-24T10:39:32.2349685Z",
|
||||
"updatedDateTime": "2023-06-24T14:39:32.2349686Z",
|
||||
|
|
|
|||
|
|
@ -0,0 +1,14 @@
|
|||
{
|
||||
"name": "generate_image",
|
||||
"description": "If the user requests you providing or generating image or picture, you can call this function to generate image.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"image_description": {
|
||||
"type": "string",
|
||||
"description": "The image description that user requests."
|
||||
}
|
||||
},
|
||||
"required": [ "image_description" ]
|
||||
}
|
||||
}
|
||||
|
|
@ -1 +1 @@
|
|||
You are a tool agent.
|
||||
You are a utility agent.
|
||||
|
|
@ -0,0 +1 @@
|
|||
Please call generate_image if user wants you to provide or generate an image or picture.
|
||||
|
|
@ -141,9 +141,9 @@ public class AgentController : ControllerBase
|
|||
return await _agentService.DeleteAgent(agentId);
|
||||
}
|
||||
|
||||
[HttpGet("/agent/tools")]
|
||||
public IEnumerable<string> GetAgentTools()
|
||||
[HttpGet("/agent/utilities")]
|
||||
public IEnumerable<string> GetAgentUtilities()
|
||||
{
|
||||
return _agentService.GetAgentTools();
|
||||
return _agentService.GetAgentUtilities();
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
using BotSharp.Abstraction.Agents.Models;
|
||||
using BotSharp.Abstraction.Functions.Models;
|
||||
using BotSharp.Abstraction.Routing.Models;
|
||||
using BotSharp.Core.Infrastructures;
|
||||
|
||||
namespace BotSharp.OpenAPI.ViewModels.Agents;
|
||||
|
||||
|
|
@ -43,7 +44,7 @@ public class AgentCreationModel
|
|||
/// Combine different Agents together to form a Profile.
|
||||
/// </summary>
|
||||
public List<string> Profiles { get; set; } = new List<string>();
|
||||
public List<string> Tools { get; set; } = new List<string>();
|
||||
public List<string> Utilities { get; set; } = new List<string>();
|
||||
public List<RoutingRuleUpdateModel> RoutingRules { get; set; } = new List<RoutingRuleUpdateModel>();
|
||||
public AgentLlmConfig? LlmConfig { get; set; }
|
||||
|
||||
|
|
@ -58,7 +59,7 @@ public class AgentCreationModel
|
|||
Functions = Functions,
|
||||
Responses = Responses,
|
||||
Samples = Samples,
|
||||
Tools = Tools,
|
||||
Utilities = Utilities,
|
||||
IsPublic = IsPublic,
|
||||
Type = Type,
|
||||
Disabled = Disabled,
|
||||
|
|
|
|||
|
|
@ -26,9 +26,9 @@ public class AgentUpdateModel
|
|||
public List<string>? Samples { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Tools
|
||||
/// Utilities
|
||||
/// </summary>
|
||||
public List<string>? Tools { get; set; }
|
||||
public List<string>? Utilities { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Functions
|
||||
|
|
@ -76,7 +76,7 @@ public class AgentUpdateModel
|
|||
Templates = Templates ?? new List<AgentTemplate>(),
|
||||
Functions = Functions ?? new List<FunctionDef>(),
|
||||
Responses = Responses ?? new List<AgentResponse>(),
|
||||
Tools = Tools ?? new List<string>(),
|
||||
Utilities = Utilities ?? new List<string>(),
|
||||
LlmConfig = LlmConfig
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ public class AgentViewModel
|
|||
public List<FunctionDef> Functions { get; set; }
|
||||
public List<AgentResponse> Responses { get; set; }
|
||||
public List<string> Samples { get; set; }
|
||||
public List<string> Tools { get; set; }
|
||||
public List<string> Utilities { get; set; }
|
||||
|
||||
[JsonPropertyName("is_public")]
|
||||
public bool IsPublic { get; set; }
|
||||
|
|
@ -64,7 +64,7 @@ public class AgentViewModel
|
|||
Functions = agent.Functions,
|
||||
Responses = agent.Responses,
|
||||
Samples = agent.Samples,
|
||||
Tools = agent.Tools,
|
||||
Utilities = agent.Utilities,
|
||||
IsPublic= agent.IsPublic,
|
||||
Disabled = agent.Disabled,
|
||||
IconUrl = agent.IconUrl,
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
namespace BotSharp.Plugin.HttpHandler.Enums;
|
||||
|
||||
public class Tool
|
||||
public class Utility
|
||||
{
|
||||
public const string HttpHandler = "http-handler";
|
||||
}
|
||||
|
|
@ -8,7 +8,8 @@ namespace BotSharp.Plugin.HttpHandler.Hooks;
|
|||
|
||||
public class HttpHandlerHook : AgentHookBase
|
||||
{
|
||||
private static string TOOL_ASSISTANT = Guid.Empty.ToString();
|
||||
private static string UTILITY_ASSISTANT = Guid.Empty.ToString();
|
||||
private static string FUNCTION_NAME = "handle_http_request";
|
||||
|
||||
public override string SelfId => string.Empty;
|
||||
|
||||
|
|
@ -21,7 +22,7 @@ public class HttpHandlerHook : AgentHookBase
|
|||
{
|
||||
var conv = _services.GetRequiredService<IConversationService>();
|
||||
var isConvMode = conv.IsConversationMode();
|
||||
var isEnabled = !agent.Tools.IsNullOrEmpty() && agent.Tools.Contains(Tool.HttpHandler);
|
||||
var isEnabled = !agent.Utilities.IsNullOrEmpty() && agent.Utilities.Contains(Utility.HttpHandler);
|
||||
|
||||
if (isConvMode && isEnabled)
|
||||
{
|
||||
|
|
@ -49,11 +50,10 @@ public class HttpHandlerHook : AgentHookBase
|
|||
|
||||
private (string, FunctionDef?) GetPromptAndFunction()
|
||||
{
|
||||
var fn = "handle_http_request";
|
||||
var db = _services.GetRequiredService<IBotSharpRepository>();
|
||||
var agent = db.GetAgent(TOOL_ASSISTANT);
|
||||
var prompt = agent?.Templates?.FirstOrDefault(x => x.Name.IsEqualTo($"{fn}.fn"))?.Content ?? string.Empty;
|
||||
var loadAttachmentFn = agent?.Functions?.FirstOrDefault(x => x.Name.IsEqualTo(fn));
|
||||
var agent = db.GetAgent(UTILITY_ASSISTANT);
|
||||
var prompt = agent?.Templates?.FirstOrDefault(x => x.Name.IsEqualTo($"{FUNCTION_NAME}.fn"))?.Content ?? string.Empty;
|
||||
var loadAttachmentFn = agent?.Functions?.FirstOrDefault(x => x.Name.IsEqualTo(FUNCTION_NAME));
|
||||
return (prompt, loadAttachmentFn);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +0,0 @@
|
|||
using BotSharp.Abstraction.Agents;
|
||||
using BotSharp.Plugin.HttpHandler.Enums;
|
||||
|
||||
namespace BotSharp.Plugin.HttpHandler.Hooks;
|
||||
|
||||
public class HttpHandlerToolHook : IAgentToolHook
|
||||
{
|
||||
public void AddTools(List<string> tools)
|
||||
{
|
||||
tools.Add(Tool.HttpHandler);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
using BotSharp.Abstraction.Agents;
|
||||
using BotSharp.Plugin.HttpHandler.Enums;
|
||||
|
||||
namespace BotSharp.Plugin.HttpHandler.Hooks;
|
||||
|
||||
public class HttpHandlerUtilityHook : IAgentUtilityHook
|
||||
{
|
||||
public void AddUtilities(List<string> utilities)
|
||||
{
|
||||
utilities.Add(Utility.HttpHandler);
|
||||
}
|
||||
}
|
||||
|
|
@ -22,6 +22,6 @@ public class HttpHandlerPlugin : IBotSharpPlugin
|
|||
});
|
||||
|
||||
services.AddScoped<IAgentHook, HttpHandlerHook>();
|
||||
services.AddScoped<IAgentToolHook, HttpHandlerToolHook>();
|
||||
services.AddScoped<IAgentUtilityHook, HttpHandlerUtilityHook>();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ public class AgentDocument : MongoBase
|
|||
public List<FunctionDefMongoElement> Functions { get; set; }
|
||||
public List<AgentResponseMongoElement> Responses { get; set; }
|
||||
public List<string> Samples { get; set; }
|
||||
public List<string> Tools { get; set; }
|
||||
public List<string> Utilities { get; set; }
|
||||
public bool IsPublic { get; set; }
|
||||
public bool Disabled { get; set; }
|
||||
public List<string> Profiles { get; set; }
|
||||
|
|
|
|||
|
|
@ -55,8 +55,8 @@ public partial class MongoRepository
|
|||
case AgentField.LlmConfig:
|
||||
UpdateAgentLlmConfig(agent.Id, agent.LlmConfig);
|
||||
break;
|
||||
case AgentField.Tool:
|
||||
UpdateAgentTools(agent.Id, agent.Tools);
|
||||
case AgentField.Utility:
|
||||
UpdateAgentUtilities(agent.Id, agent.Utilities);
|
||||
break;
|
||||
case AgentField.All:
|
||||
UpdateAgentAllFields(agent);
|
||||
|
|
@ -219,13 +219,13 @@ public partial class MongoRepository
|
|||
_dc.Agents.UpdateOne(filter, update);
|
||||
}
|
||||
|
||||
private void UpdateAgentTools(string agentId, List<string> tools)
|
||||
private void UpdateAgentUtilities(string agentId, List<string> utilities)
|
||||
{
|
||||
if (tools == null) return;
|
||||
if (utilities == null) return;
|
||||
|
||||
var filter = Builders<AgentDocument>.Filter.Eq(x => x.Id, agentId);
|
||||
var update = Builders<AgentDocument>.Update
|
||||
.Set(x => x.Tools, tools)
|
||||
.Set(x => x.Utilities, utilities)
|
||||
.Set(x => x.UpdatedTime, DateTime.UtcNow);
|
||||
|
||||
_dc.Agents.UpdateOne(filter, update);
|
||||
|
|
@ -257,7 +257,7 @@ public partial class MongoRepository
|
|||
.Set(x => x.Functions, agent.Functions.Select(f => FunctionDefMongoElement.ToMongoElement(f)).ToList())
|
||||
.Set(x => x.Responses, agent.Responses.Select(r => AgentResponseMongoElement.ToMongoElement(r)).ToList())
|
||||
.Set(x => x.Samples, agent.Samples)
|
||||
.Set(x => x.Tools, agent.Tools)
|
||||
.Set(x => x.Utilities, agent.Utilities)
|
||||
.Set(x => x.LlmConfig, AgentLlmConfigMongoElement.ToMongoElement(agent.LlmConfig))
|
||||
.Set(x => x.IsPublic, agent.IsPublic)
|
||||
.Set(x => x.UpdatedTime, DateTime.UtcNow);
|
||||
|
|
@ -383,7 +383,7 @@ public partial class MongoRepository
|
|||
.Select(r => AgentResponseMongoElement.ToMongoElement(r))?
|
||||
.ToList() ?? new List<AgentResponseMongoElement>(),
|
||||
Samples = x.Samples ?? new List<string>(),
|
||||
Tools = x.Tools ?? new List<string>(),
|
||||
Utilities = x.Utilities ?? new List<string>(),
|
||||
IsPublic = x.IsPublic,
|
||||
Type = x.Type,
|
||||
InheritAgentId = x.InheritAgentId,
|
||||
|
|
@ -473,7 +473,7 @@ public partial class MongoRepository
|
|||
.Select(r => AgentResponseMongoElement.ToDomainElement(r))
|
||||
.ToList() : new List<AgentResponse>(),
|
||||
Samples = agentDoc.Samples ?? new List<string>(),
|
||||
Tools = agentDoc.Tools ?? new List<string>(),
|
||||
Utilities = agentDoc.Utilities ?? new List<string>(),
|
||||
IsPublic = agentDoc.IsPublic,
|
||||
Disabled = agentDoc.Disabled,
|
||||
Type = agentDoc.Type,
|
||||
|
|
|
|||
|
|
@ -50,7 +50,7 @@ public partial class MongoRepository
|
|||
.Select(r => AgentResponseMongoElement.ToMongoElement(r))?
|
||||
.ToList() ?? new List<AgentResponseMongoElement>(),
|
||||
Samples = x.Samples ?? new List<string>(),
|
||||
Tools = x.Tools ?? new List<string>(),
|
||||
Utilities = x.Utilities ?? new List<string>(),
|
||||
IsPublic = x.IsPublic,
|
||||
Type = x.Type,
|
||||
InheritAgentId = x.InheritAgentId,
|
||||
|
|
@ -75,7 +75,7 @@ public partial class MongoRepository
|
|||
.Set(x => x.Functions, agent.Functions)
|
||||
.Set(x => x.Responses, agent.Responses)
|
||||
.Set(x => x.Samples, agent.Samples)
|
||||
.Set(x => x.Tools, agent.Tools)
|
||||
.Set(x => x.Utilities, agent.Utilities)
|
||||
.Set(x => x.IsPublic, agent.IsPublic)
|
||||
.Set(x => x.Type, agent.Type)
|
||||
.Set(x => x.InheritAgentId, agent.InheritAgentId)
|
||||
|
|
|
|||
Loading…
Reference in a new issue