2023-09-06 12:58:02 +00:00
|
|
|
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
|
|
|
|
2023-09-06 12:58:02 +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;
|
|
|
|
|
|
2023-08-25 15:33:21 +00:00
|
|
|
protected readonly IServiceProvider _services;
|
|
|
|
|
protected readonly AgentSettings _settings;
|
2023-08-18 02:44:49 +00:00
|
|
|
|
2023-08-25 15:33:21 +00:00
|
|
|
public AgentHookBase(IServiceProvider services, AgentSettings settings)
|
2023-08-18 02:44:49 +00:00
|
|
|
{
|
|
|
|
|
_services = services;
|
2023-08-25 15:33:21 +00:00
|
|
|
_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)
|
|
|
|
|
{
|
2023-09-06 12:58:02 +00:00
|
|
|
dict["current_date"] = $"{DateTime.Now:MMM dd, yyyy}";
|
|
|
|
|
dict["current_time"] = $"{DateTime.Now:hh:mm tt}";
|
|
|
|
|
dict["current_weekday"] = $"{DateTime.Now:dddd}";
|
2023-08-25 15:33:21 +00:00
|
|
|
return true;
|
2023-08-18 02:44:49 +00:00
|
|
|
}
|
|
|
|
|
|
2023-09-29 18:08:42 +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 16:13:40 +00:00
|
|
|
public virtual void OnLoadAgentUtility(Agent agent, IEnumerable<AgentUtility> utilities)
|
2024-11-20 08:24:26 +00:00
|
|
|
{
|
|
|
|
|
if (agent.Type == AgentType.Routing || utilities.IsNullOrEmpty()) return;
|
|
|
|
|
|
|
|
|
|
var conv = _services.GetRequiredService<IConversationService>();
|
|
|
|
|
var isConvMode = conv.IsConversationMode();
|
|
|
|
|
if (!isConvMode) return;
|
|
|
|
|
|
|
|
|
|
var render = _services.GetRequiredService<ITemplateRender>();
|
|
|
|
|
|
|
|
|
|
agent.Functions ??= [];
|
|
|
|
|
var agentUtilities = agent.Utilities ?? [];
|
|
|
|
|
|
2024-11-20 16:13:40 +00:00
|
|
|
foreach (var utillity in utilities)
|
2024-11-20 08:24:26 +00:00
|
|
|
{
|
2024-11-20 16:13:40 +00:00
|
|
|
if (utillity.Name.IsNullOrEmpty() || utillity.Content == null) continue;
|
2024-11-20 08:24:26 +00:00
|
|
|
|
2024-11-20 16:13:40 +00:00
|
|
|
var isEnabled = agentUtilities.Contains(utillity.Name);
|
2024-11-20 08:24:26 +00:00
|
|
|
if (!isEnabled) continue;
|
|
|
|
|
|
2024-11-20 16:13:40 +00:00
|
|
|
var (fns, prompts) = GetUtilityContent(utillity.Content);
|
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";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private (IEnumerable<FunctionDef>, IEnumerable<string>) GetUtilityContent(UtilityContent content)
|
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!content.Functions.IsNullOrEmpty())
|
|
|
|
|
{
|
|
|
|
|
var functionNames = content.Functions?.Select(x => x.Name)?.ToList() ?? [];
|
|
|
|
|
fns = agent?.Functions?.Where(x => functionNames.Contains(x.Name, StringComparer.OrdinalIgnoreCase))?.ToList() ?? [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!content.Templates.IsNullOrEmpty())
|
|
|
|
|
{
|
|
|
|
|
foreach (var template in content.Templates)
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
}
|