Merge pull request #551 from iceljc/features/relocate-generate_image

move generate image to file handler
This commit is contained in:
iceljc 2024-07-17 22:11:27 -05:00 committed by GitHub
commit 418a7132be
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 22 additions and 93 deletions

View file

@ -1,7 +0,0 @@
namespace BotSharp.Abstraction.Agents.Enums;
public class AgentUtility
{
public const string FileReader = "file-reader";
public const string ImageGenerator = "image-generator";
}

View file

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>$(TargetFramework)</TargetFramework>
@ -47,9 +47,7 @@
<ItemGroup>
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\agent.json" />
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\generate_image.json" />
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\instruction.liquid" />
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\generate_image.fn.liquid" />
<None Remove="data\agents\01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b\agent.json" />
<None Remove="data\agents\01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b\functions.json" />
<None Remove="data\agents\01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b\functions\human_intervention_needed.json" />
@ -157,12 +155,6 @@
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\instruction.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\generate_image.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\generate_image.fn.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\plugins\config.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>

View file

@ -1,4 +1,3 @@
using BotSharp.Core.Files.Hooks;
using BotSharp.Core.Files.Services;
using Microsoft.Extensions.Configuration;
@ -16,8 +15,5 @@ public class FilePlugin : IBotSharpPlugin
public void RegisterDI(IServiceCollection services, IConfiguration config)
{
services.AddScoped<IBotSharpFileService, BotSharpFileService>();
services.AddScoped<IAgentHook, ImageGeneratorHook>();
services.AddScoped<IAgentUtilityHook, ImageGeneratorUtilityHook>();
}
}

View file

@ -1,52 +0,0 @@
namespace BotSharp.Core.Files.Hooks;
public class ImageGeneratorHook : AgentHookBase
{
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(BuiltInAgentId.UtilityAssistant);
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);
}
}

View file

@ -1,9 +0,0 @@
namespace BotSharp.Core.Files.Hooks;
internal class ImageGeneratorUtilityHook : IAgentUtilityHook
{
public void AddUtilities(List<string> utilities)
{
utilities.Add(AgentUtility.ImageGenerator);
}
}

View file

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>$(TargetFramework)</TargetFramework>
@ -12,8 +12,8 @@
<ItemGroup>
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\handle_email_request.json" />
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\email_attachment_prompt.liquid" />
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\handle_email_request.fn.liquid" />
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\select_attachment_prompt.liquid" />
</ItemGroup>
<ItemGroup>
@ -23,7 +23,7 @@
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\handle_email_request.fn.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\email_attachment_prompt.liquid">
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\select_attachment_prompt.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

View file

@ -56,7 +56,7 @@ public class HandleEmailRequestFn : IFunctionCallback
}
mailMessage.Body = bodyBuilder.ToMessageBody();
var response = await HandleSendEmailBySMTP(mailMessage);
var response = await SendEmailBySMTP(mailMessage);
message.Content = response;
_logger.LogWarning($"Email successfully send over to {recipient}. Email Subject: {subject} [{response}]");
@ -96,7 +96,7 @@ public class HandleEmailRequestFn : IFunctionCallback
{
return $"id: {idx + 1}, file_name: {x.FileName}.{x.FileType}, content_type: {x.ContentType}";
}).ToList();
var prompt = db.GetAgentTemplate(BuiltInAgentId.UtilityAssistant, "email_attachment_prompt");
var prompt = db.GetAgentTemplate(BuiltInAgentId.UtilityAssistant, "select_attachment_prompt");
prompt = render.Render(prompt, new Dictionary<string, object>
{
{ "file_list", promptFiles }
@ -140,7 +140,7 @@ public class HandleEmailRequestFn : IFunctionCallback
}
}
private async Task<string> HandleSendEmailBySMTP(MimeMessage mailMessage)
private async Task<string> SendEmailBySMTP(MimeMessage mailMessage)
{
using var smtpClient = new SmtpClient();
await smtpClient.ConnectAsync(_emailSettings.SMTPServer, _emailSettings.SMTPPort, SecureSocketOptions.StartTls);

View file

@ -1,6 +1,6 @@
{
"name": "handle_email_request",
"description": "If the user requests to send an email, you need to capture the email content and the recipient email address. If the user explicitly enter email subject use the same if not intelligently capture the email subject from the content. Then call this function to send out email.",
"description": "If the user requests to send an email with or without attachments or files, you need to capture the email content and the recipient email address. If the user explicitly enter email subject use the same if not intelligently capture the email subject from the content. Then call this function to send out email.",
"parameters": {
"type": "object",
"properties": {
@ -18,7 +18,7 @@
},
"is_need_attachments": {
"type": "boolean",
"description": "If the user request to send email with attachemnt(s), then this value should be true. Otherwise, this value should be false."
"description": "If the user request to send email with attachemnt(s) or file(s), then this value should be True. Otherwise, this value should be False."
}
},
"required": [ "to_address", "email_content", "subject", "is_need_attachments" ]

View file

@ -1,2 +1 @@
Suppose user has uploaded some attachments.
Please call handle_email_request if user wants to send out an email.

View file

@ -11,16 +11,24 @@
</PropertyGroup>
<ItemGroup>
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\generate_image.json" />
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\read_image.json" />
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\read_pdf.json" />
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\generate_image.fn.liquid" />
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\read_image.fn.liquid" />
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\read_pdf.fn.liquid" />
</ItemGroup>
<ItemGroup>
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\generate_image.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\read_image.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\generate_image.fn.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\read_image.fn.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>

View file

@ -2,6 +2,7 @@ namespace BotSharp.Plugin.FileHandler.Enums;
public class UtilityName
{
public const string ImageGenerator = "image-generator";
public const string ImageReader = "image-reader";
public const string PdfReader = "pdf-reader";
}

View file

@ -1,6 +1,4 @@
using BotSharp.Abstraction.Functions;
namespace BotSharp.Core.Files.Functions;
namespace BotSharp.Plugin.FileHandler.Functions;
public class GenerateImageFn : IFunctionCallback
{

View file

@ -4,6 +4,7 @@ public class FileHandlerHook : AgentHookBase, IAgentHook
{
private const string READ_IMAGE_FN = "read_image";
private const string READ_PDF_FN = "read_pdf";
private const string GENERATE_IMAGE_FN = "generate_image";
public override string SelfId => string.Empty;
@ -20,6 +21,7 @@ public class FileHandlerHook : AgentHookBase, IAgentHook
{
AddUtility(agent, UtilityName.ImageReader, READ_IMAGE_FN);
AddUtility(agent, UtilityName.PdfReader, READ_PDF_FN);
AddUtility(agent, UtilityName.ImageGenerator, GENERATE_IMAGE_FN);
}
base.OnAgentLoaded(agent);

View file

@ -6,5 +6,6 @@ public class FileHandlerUtilityHook : IAgentUtilityHook
{
utilities.Add(UtilityName.ImageReader);
utilities.Add(UtilityName.PdfReader);
utilities.Add(UtilityName.ImageGenerator);
}
}