BotSharp/src/Infrastructure/BotSharp.Abstraction/Agents/AgentHookBase.cs

128 lines
3.7 KiB
C#
Raw Normal View History

using BotSharp.Abstraction.Agents.Settings;
2024-11-20 08:24:26 +00:00
using BotSharp.Abstraction.Conversations;
2023-09-27 20:49:44 +00:00
using BotSharp.Abstraction.Functions.Models;
2024-11-20 08:24:26 +00:00
using BotSharp.Abstraction.Repositories;
using Microsoft.Extensions.DependencyInjection;
using System.Data;
2023-08-18 02:44:49 +00:00
namespace BotSharp.Abstraction.Agents;
2023-08-18 02:44:49 +00:00
public abstract class AgentHookBase : IAgentHook
{
2023-10-24 21:38:34 +00:00
public virtual string SelfId => throw new NotImplementedException("Please set SelfId as agent id!");
2023-08-18 02:44:49 +00:00
protected Agent _agent;
public Agent Agent => _agent;
protected readonly IServiceProvider _services;
protected readonly AgentSettings _settings;
2023-08-18 02:44:49 +00:00
public AgentHookBase(IServiceProvider services, AgentSettings settings)
2023-08-18 02:44:49 +00:00
{
_services = services;
_settings = settings;
2023-08-18 02:44:49 +00:00
}
public void SetAget(Agent agent)
{
_agent = agent;
}
public virtual bool OnAgentLoading(ref string id)
{
return true;
}
public virtual bool OnInstructionLoaded(string template, Dictionary<string, object> dict)
{
dict["current_date"] = $"{DateTime.Now:MMM dd, yyyy}";
dict["current_time"] = $"{DateTime.Now:hh:mm tt}";
dict["current_weekday"] = $"{DateTime.Now:dddd}";
return true;
2023-08-18 02:44:49 +00:00
}
public virtual bool OnFunctionsLoaded(List<FunctionDef> functions)
2023-08-18 02:44:49 +00:00
{
_agent.Functions = functions;
return true;
}
2023-10-19 17:24:00 +00:00
public virtual bool OnSamplesLoaded(List<string> samples)
2023-08-18 02:44:49 +00:00
{
_agent.Samples = samples;
return true;
}
public virtual void OnAgentLoaded(Agent agent)
{
}
2024-11-20 08:24:26 +00:00
2024-11-20 23:10:38 +00:00
public virtual void OnAgentUtilityloaded(Agent agent)
2024-11-20 08:24:26 +00:00
{
2024-11-20 23:10:38 +00:00
if (agent.Type == AgentType.Routing) return;
2024-11-20 08:24:26 +00:00
var conv = _services.GetRequiredService<IConversationService>();
var isConvMode = conv.IsConversationMode();
if (!isConvMode) return;
var render = _services.GetRequiredService<ITemplateRender>();
agent.Functions ??= [];
2024-11-20 23:10:38 +00:00
agent.Utilities ??= [];
2024-11-20 08:24:26 +00:00
2024-11-20 23:10:38 +00:00
foreach (var utillity in agent.Utilities)
2024-11-20 08:24:26 +00:00
{
2024-11-20 23:10:38 +00:00
if (utillity == null || string.IsNullOrWhiteSpace(utillity.Name)) continue;
2024-11-20 08:24:26 +00:00
2024-11-20 23:10:38 +00:00
var (fns, prompts) = GetUtilityContent(utillity);
2024-11-20 08:24:26 +00:00
if (!fns.IsNullOrEmpty())
{
agent.Functions.AddRange(fns);
}
if (!prompts.IsNullOrEmpty())
{
foreach (var prompt in prompts)
{
agent.Instruction += $"\r\n\r\n{prompt}\r\n\r\n";
}
}
}
}
2024-11-20 23:10:38 +00:00
private (IEnumerable<FunctionDef>, IEnumerable<string>) GetUtilityContent(AgentUtility utility)
2024-11-20 08:24:26 +00:00
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var render = _services.GetRequiredService<ITemplateRender>();
var fns = new List<FunctionDef>();
var prompts = new List<string>();
var agent = db.GetAgent(BuiltInAgentId.UtilityAssistant);
if (agent == null)
{
return (fns, prompts);
}
2024-11-20 23:10:38 +00:00
if (!utility.Functions.IsNullOrEmpty())
2024-11-20 08:24:26 +00:00
{
2024-11-20 23:10:38 +00:00
var functionNames = utility.Functions?.Select(x => x.Name)?.ToList() ?? [];
2024-11-20 08:24:26 +00:00
fns = agent?.Functions?.Where(x => functionNames.Contains(x.Name, StringComparer.OrdinalIgnoreCase))?.ToList() ?? [];
}
2024-11-20 23:10:38 +00:00
if (!utility.Templates.IsNullOrEmpty())
2024-11-20 08:24:26 +00:00
{
2024-11-20 23:10:38 +00:00
foreach (var template in utility.Templates)
2024-11-20 08:24:26 +00:00
{
var prompt = agent?.Templates?.FirstOrDefault(x => x.Name.IsEqualTo(template.Name))?.Content ?? string.Empty;
if (string.IsNullOrWhiteSpace(prompt)) continue;
prompts.Add(prompt);
}
}
return (fns, prompts);
}
2023-08-18 02:44:49 +00:00
}