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

54 lines
1.7 KiB
C#
Raw Normal View History

2024-06-06 18:30:39 +00:00
namespace BotSharp.Core.Files.Hooks;
2024-06-28 16:58:19 +00:00
public class FileAnalyzerHook : AgentHookBase
2024-06-06 18:30:39 +00:00
{
2024-07-02 18:36:45 +00:00
private static string UTILITY_ASSISTANT = Guid.Empty.ToString();
2024-06-06 18:30:39 +00:00
public override string SelfId => string.Empty;
2024-06-28 16:58:19 +00:00
public FileAnalyzerHook(IServiceProvider services, AgentSettings settings)
2024-06-06 18:30:39 +00:00
: base(services, settings)
{
}
2024-06-18 18:18:24 +00:00
public override void OnAgentLoaded(Agent agent)
{
var conv = _services.GetRequiredService<IConversationService>();
2024-06-25 03:46:13 +00:00
var isConvMode = conv.IsConversationMode();
2024-07-02 18:36:45 +00:00
var isEnabled = !agent.Utilities.IsNullOrEmpty() && agent.Utilities.Contains(AgentUtility.FileAnalyzer);
2024-06-18 18:18:24 +00:00
2024-06-25 03:46:13 +00:00
if (isConvMode && isEnabled)
2024-06-18 18:18:24 +00:00
{
2024-06-26 03:38:01 +00:00
var (prompt, fn) = GetPromptAndFunction();
if (fn != null)
2024-06-06 18:30:39 +00:00
{
2024-06-24 21:39:08 +00:00
if (!string.IsNullOrWhiteSpace(prompt))
{
agent.Instruction += $"\r\n\r\n{prompt}\r\n\r\n";
}
if (agent.Functions == null)
2024-06-07 04:02:10 +00:00
{
2024-06-26 03:38:01 +00:00
agent.Functions = new List<FunctionDef> { fn };
2024-06-24 21:39:08 +00:00
}
else
2024-06-19 09:24:49 +00:00
{
2024-06-26 03:38:01 +00:00
agent.Functions.Add(fn);
2024-06-24 21:39:08 +00:00
}
2024-06-19 09:24:49 +00:00
}
2024-06-06 18:30:39 +00:00
}
2024-06-19 09:24:49 +00:00
base.OnAgentLoaded(agent);
2024-06-06 18:30:39 +00:00
}
2024-06-24 21:39:08 +00:00
2024-06-26 03:38:01 +00:00
private (string, FunctionDef?) GetPromptAndFunction()
2024-06-24 21:39:08 +00:00
{
2024-06-26 03:38:01 +00:00
var fn = "load_attachment";
2024-06-24 21:39:08 +00:00
var db = _services.GetRequiredService<IBotSharpRepository>();
2024-07-02 18:36:45 +00:00
var agent = db.GetAgent(UTILITY_ASSISTANT);
2024-06-26 03:38:01 +00:00
var prompt = agent?.Templates?.FirstOrDefault(x => x.Name.IsEqualTo($"{fn}.fn"))?.Content ?? string.Empty;
var loadAttachmentFn = agent?.Functions?.FirstOrDefault(x => x.Name.IsEqualTo(fn));
2024-06-24 21:39:08 +00:00
return (prompt, loadAttachmentFn);
}
2024-06-06 18:30:39 +00:00
}