BotSharp/src/Infrastructure/BotSharp.Core/Files/Hooks/AttachmentProcessingHook.cs

72 lines
2.6 KiB
C#
Raw Normal View History

2024-06-18 18:18:24 +00:00
2024-06-06 18:30:39 +00:00
namespace BotSharp.Core.Files.Hooks;
public class AttachmentProcessingHook : AgentHookBase
{
private readonly IServiceProvider _services;
private readonly AgentSettings _agentSettings;
public override string SelfId => string.Empty;
public AttachmentProcessingHook(IServiceProvider services, AgentSettings settings)
: base(services, settings)
{
_services = services;
_agentSettings = settings;
}
2024-06-18 18:18:24 +00:00
public override void OnAgentLoaded(Agent agent)
{
var fileService = _services.GetRequiredService<IBotSharpFileService>();
var conv = _services.GetRequiredService<IConversationService>();
var hasConvFiles = fileService.HasConversationUserFiles(conv.ConversationId);
if (hasConvFiles)
{
agent.Instruction += "\r\n\r\nIf user wants to describe images or pdf files, please call load_attachment.";
}
base.OnAgentLoaded(agent);
}
2024-06-06 18:30:39 +00:00
public override bool OnFunctionsLoaded(List<FunctionDef> functions)
{
var fileService = _services.GetRequiredService<IBotSharpFileService>();
var conv = _services.GetRequiredService<IConversationService>();
2024-06-06 19:57:41 +00:00
var hasConvFiles = fileService.HasConversationUserFiles(conv.ConversationId);
2024-06-06 18:30:39 +00:00
if (hasConvFiles)
{
var json = JsonSerializer.Serialize(new
{
2024-06-07 04:02:10 +00:00
user_request = new
2024-06-06 18:30:39 +00:00
{
type = "string",
2024-06-07 04:02:10 +00:00
description = "The request posted by user, which is related to analyzing requested files. User can request for multiple files to process at one time."
},
file_types = new
{
type = "string",
description = "The file types requested by user to analyze, such as image, png, jpeg, and pdf. There can be multiple file types in a single request. An example output is, 'image,pdf'"
2024-06-06 18:30:39 +00:00
}
});
functions.Add(new FunctionDef
{
Name = "load_attachment",
2024-06-07 04:02:10 +00:00
Description = "If the user's request is related to analyzing files and/or images, you can call this function to analyze files and images.",
2024-06-06 18:30:39 +00:00
Parameters =
{
Properties = JsonSerializer.Deserialize<JsonDocument>(json),
Required = new List<string>
{
2024-06-07 04:02:10 +00:00
"user_request",
"file_types"
2024-06-06 18:30:39 +00:00
}
}
});
}
2024-06-07 04:02:10 +00:00
return base.OnFunctionsLoaded(functions); ;
2024-06-06 18:30:39 +00:00
}
}