refine agent utility
This commit is contained in:
parent
7ae750723a
commit
e70cd70e46
|
|
@ -1,5 +1,9 @@
|
|||
using BotSharp.Abstraction.Agents.Settings;
|
||||
using BotSharp.Abstraction.Conversations;
|
||||
using BotSharp.Abstraction.Functions.Models;
|
||||
using BotSharp.Abstraction.Repositories;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Data;
|
||||
|
||||
namespace BotSharp.Abstraction.Agents;
|
||||
|
||||
|
|
@ -52,4 +56,79 @@ public abstract class AgentHookBase : IAgentHook
|
|||
public virtual void OnAgentLoaded(Agent agent)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void OnLoadAgentUtility(Agent agent, IEnumerable<AgentUtilityLoadModel> utilities)
|
||||
{
|
||||
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 ?? [];
|
||||
|
||||
foreach (var item in utilities)
|
||||
{
|
||||
if (item.UtilityName.IsNullOrEmpty() || item.Content == null) continue;
|
||||
|
||||
var isEnabled = agentUtilities.Contains(item.UtilityName);
|
||||
if (!isEnabled) continue;
|
||||
|
||||
var (fns, prompts) = GetUtilityContent(item.Content);
|
||||
|
||||
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;
|
||||
|
||||
if (!template.Data.IsNullOrEmpty())
|
||||
{
|
||||
prompt = render.Render(prompt, template.Data);
|
||||
}
|
||||
prompts.Add(prompt);
|
||||
}
|
||||
}
|
||||
|
||||
return (fns, prompts);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,4 +31,6 @@ public interface IAgentHook
|
|||
/// <param name="agent"></param>
|
||||
/// <returns></returns>
|
||||
void OnAgentLoaded(Agent agent);
|
||||
|
||||
void OnLoadAgentUtility(Agent agent, IEnumerable<AgentUtilityLoadModel> utilities);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,64 @@
|
|||
namespace BotSharp.Abstraction.Agents.Models;
|
||||
|
||||
public class AgentUtilityLoadModel
|
||||
{
|
||||
public string UtilityName { get; set; }
|
||||
public UtilityContent Content { get; set; }
|
||||
|
||||
public AgentUtilityLoadModel()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public AgentUtilityLoadModel(string utilityName, UtilityContent content)
|
||||
{
|
||||
UtilityName = utilityName;
|
||||
Content = content;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class UtilityContent
|
||||
{
|
||||
public IEnumerable<UtilityFunction> Functions { get; set; } = [];
|
||||
public IEnumerable<UtilityTemplate> Templates { get; set; } = [];
|
||||
|
||||
public UtilityContent()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public class UtilityFunction : UtilityBase
|
||||
{
|
||||
public UtilityFunction()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public UtilityFunction(string name)
|
||||
{
|
||||
Name = name;
|
||||
}
|
||||
}
|
||||
|
||||
public class UtilityTemplate : UtilityBase
|
||||
{
|
||||
public Dictionary<string, object>? Data { get; set; }
|
||||
|
||||
public UtilityTemplate()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public UtilityTemplate(string name, Dictionary<string, object>? data = null)
|
||||
{
|
||||
Name = name;
|
||||
Data = data;
|
||||
}
|
||||
}
|
||||
|
||||
public class UtilityBase
|
||||
{
|
||||
public string Name { get; set; }
|
||||
}
|
||||
|
|
@ -1,6 +1,5 @@
|
|||
using BotSharp.Abstraction.Conversations.Enums;
|
||||
using BotSharp.Abstraction.Routing.Models;
|
||||
using System.Drawing;
|
||||
|
||||
namespace BotSharp.Core.Routing;
|
||||
|
||||
|
|
|
|||
|
|
@ -4,13 +4,11 @@ namespace BotSharp.Core.Routing;
|
|||
|
||||
public partial class RoutingService
|
||||
{
|
||||
//private int _currentRecursionDepth = 0;
|
||||
public async Task<bool> InvokeAgent(string agentId, List<RoleDialogModel> dialogs)
|
||||
{
|
||||
var agentService = _services.GetRequiredService<IAgentService>();
|
||||
var agent = await agentService.LoadAgent(agentId);
|
||||
|
||||
//_currentRecursionDepth++;
|
||||
Context.IncreaseRecursiveCounter();
|
||||
if (Context.CurrentRecursionDepth > agent.LlmConfig.MaxRecursionDepth)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -16,21 +16,6 @@ public partial class RoutingService : IRoutingService
|
|||
public IRoutingContext Context => _context;
|
||||
public Agent Router => _router;
|
||||
|
||||
//public int GetRecursiveCounter()
|
||||
//{
|
||||
// return _currentRecursionDepth;
|
||||
//}
|
||||
|
||||
//public void SetRecursiveCounter(int counter)
|
||||
//{
|
||||
// _currentRecursionDepth = counter;
|
||||
//}
|
||||
|
||||
//public void ResetRecursiveCounter()
|
||||
//{
|
||||
// _currentRecursionDepth = 0;
|
||||
//}
|
||||
|
||||
public RoutingService(
|
||||
IServiceProvider services,
|
||||
RoutingSettings settings,
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
using BotSharp.Abstraction.Agents.Settings;
|
||||
using BotSharp.Abstraction.Functions.Models;
|
||||
|
||||
namespace BotSharp.Plugin.AudioHandler.Hooks;
|
||||
|
||||
|
|
@ -9,52 +8,24 @@ public class AudioHandlerHook : AgentHookBase, IAgentHook
|
|||
|
||||
public override string SelfId => string.Empty;
|
||||
|
||||
public AudioHandlerHook(IServiceProvider services, AgentSettings settings) : base(services, settings)
|
||||
public AudioHandlerHook(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(UtilityName.AudioHandler);
|
||||
|
||||
if (isEnabled && isConvMode)
|
||||
var utilityLoad = new AgentUtilityLoadModel
|
||||
{
|
||||
AddUtility(agent, HANDLER_AUDIO);
|
||||
}
|
||||
UtilityName = UtilityName.AudioHandler,
|
||||
Content = new UtilityContent
|
||||
{
|
||||
Functions = [new(HANDLER_AUDIO)],
|
||||
Templates = [new($"{HANDLER_AUDIO}.fn")]
|
||||
}
|
||||
};
|
||||
|
||||
base.OnLoadAgentUtility(agent, [utilityLoad]);
|
||||
base.OnAgentLoaded(agent);
|
||||
}
|
||||
|
||||
private void AddUtility(Agent agent, string functionName)
|
||||
{
|
||||
var (prompt, fn) = GetPromptAndFunction(functionName);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private (string, FunctionDef?) GetPromptAndFunction(string functionName)
|
||||
{
|
||||
var db = _services.GetRequiredService<IBotSharpRepository>();
|
||||
var agent = db.GetAgent(BuiltInAgentId.UtilityAssistant);
|
||||
var prompt = agent?.Templates?.FirstOrDefault(x => x.Name.IsEqualTo($"{functionName}.fn"))?.Content ?? string.Empty;
|
||||
var loadAttachmentFn = agent?.Functions?.FirstOrDefault(x => x.Name.IsEqualTo(functionName));
|
||||
return (prompt, loadAttachmentFn);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,8 +20,7 @@ namespace BotSharp.Plugin.EmailHandler
|
|||
return settingService.Bind<EmailSenderSettings>("EmailSender");
|
||||
});
|
||||
|
||||
services.AddScoped<IAgentHook, EmailSenderHook>();
|
||||
services.AddScoped<IAgentHook, EmailReaderHook>();
|
||||
services.AddScoped<IAgentHook, EmailHandlerHook>();
|
||||
services.AddScoped<IAgentUtilityHook, EmailHandlerUtilityHook>();
|
||||
|
||||
var emailReaderSettings = new EmailReaderSettings();
|
||||
|
|
|
|||
|
|
@ -0,0 +1,34 @@
|
|||
using BotSharp.Abstraction.Agents;
|
||||
using BotSharp.Abstraction.Agents.Settings;
|
||||
using BotSharp.Plugin.EmailHandler.Enums;
|
||||
|
||||
namespace BotSharp.Plugin.EmailHandler.Hooks;
|
||||
|
||||
public class EmailHandlerHook : AgentHookBase
|
||||
{
|
||||
private static string EMAIL_READER_FN = "handle_email_reader";
|
||||
private static string EMAIL_SENDER_FN = "handle_email_sender";
|
||||
|
||||
public override string SelfId => string.Empty;
|
||||
|
||||
public EmailHandlerHook(IServiceProvider services, AgentSettings settings)
|
||||
: base(services, settings)
|
||||
{
|
||||
}
|
||||
|
||||
public override void OnAgentLoaded(Agent agent)
|
||||
{
|
||||
var utilityLoad = new AgentUtilityLoadModel
|
||||
{
|
||||
UtilityName = UtilityName.EmailHandler,
|
||||
Content = new UtilityContent
|
||||
{
|
||||
Functions = [new(EMAIL_READER_FN), new(EMAIL_SENDER_FN)],
|
||||
Templates = [new($"{EMAIL_READER_FN}.fn"), new($"{EMAIL_SENDER_FN}.fn")]
|
||||
}
|
||||
};
|
||||
|
||||
base.OnLoadAgentUtility(agent, [utilityLoad]);
|
||||
base.OnAgentLoaded(agent);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,18 +1,12 @@
|
|||
using BotSharp.Abstraction.Agents;
|
||||
using BotSharp.Plugin.EmailHandler.Enums;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BotSharp.Plugin.EmailHandler.Hooks
|
||||
namespace BotSharp.Plugin.EmailHandler.Hooks;
|
||||
|
||||
public class EmailHandlerUtilityHook : IAgentUtilityHook
|
||||
{
|
||||
public class EmailHandlerUtilityHook : IAgentUtilityHook
|
||||
public void AddUtilities(List<string> utilities)
|
||||
{
|
||||
public void AddUtilities(List<string> utilities)
|
||||
{
|
||||
utilities.Add(UtilityName.EmailHandler);
|
||||
}
|
||||
utilities.Add(UtilityName.EmailHandler);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,56 +0,0 @@
|
|||
using BotSharp.Abstraction.Agents;
|
||||
using BotSharp.Abstraction.Agents.Settings;
|
||||
using BotSharp.Abstraction.Functions.Models;
|
||||
using BotSharp.Plugin.EmailHandler.Enums;
|
||||
|
||||
namespace BotSharp.Plugin.EmailHandler.Hooks;
|
||||
|
||||
public class EmailReaderHook : AgentHookBase
|
||||
{
|
||||
private static string FUNCTION_NAME = "handle_email_reader";
|
||||
|
||||
public override string SelfId => string.Empty;
|
||||
|
||||
public EmailReaderHook(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(UtilityName.EmailHandler);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,56 +0,0 @@
|
|||
using BotSharp.Abstraction.Agents;
|
||||
using BotSharp.Abstraction.Agents.Settings;
|
||||
using BotSharp.Abstraction.Functions.Models;
|
||||
using BotSharp.Plugin.EmailHandler.Enums;
|
||||
|
||||
namespace BotSharp.Plugin.EmailHandler.Hooks;
|
||||
|
||||
public class EmailSenderHook : AgentHookBase
|
||||
{
|
||||
private static string FUNCTION_NAME = "handle_email_sender";
|
||||
|
||||
public override string SelfId => string.Empty;
|
||||
|
||||
public EmailSenderHook(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(UtilityName.EmailHandler);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,12 +1,5 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using BotSharp.Plugin.SqlHero.Settings;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using BotSharp.Plugin.SqlDriver.Settings;
|
||||
using MySql.Data.MySqlClient;
|
||||
|
||||
namespace BotSharp.Plugin.ExcelHandler.Helpers.MySql
|
||||
|
|
|
|||
|
|
@ -1,11 +1,5 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using BotSharp.Plugin.SqlDriver.Settings;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using BotSharp.Plugin.SqlDriver.Models;
|
||||
using BotSharp.Plugin.SqlHero.Settings;
|
||||
|
||||
namespace BotSharp.Plugin.ExcelHandler.Helpers.Sqlite;
|
||||
|
||||
|
|
|
|||
|
|
@ -12,47 +12,18 @@ public class ExcelHandlerHook : AgentHookBase, IAgentHook
|
|||
|
||||
public override void OnAgentLoaded(Agent agent)
|
||||
{
|
||||
var conv = _services.GetRequiredService<IConversationService>();
|
||||
var isConvMode = conv.IsConversationMode();
|
||||
var isEnabled = !agent.Utilities.IsNullOrEmpty() && agent.Utilities.Contains(UtilityName.ExcelHandler);
|
||||
|
||||
if (isEnabled && isConvMode)
|
||||
var utilityLoad = new AgentUtilityLoadModel
|
||||
{
|
||||
AddUtility(agent, HANDLER_EXCEL);
|
||||
}
|
||||
UtilityName = UtilityName.ExcelHandler,
|
||||
Content = new UtilityContent
|
||||
{
|
||||
Functions = [new(HANDLER_EXCEL)],
|
||||
Templates = [new($"{HANDLER_EXCEL}.fn")]
|
||||
}
|
||||
};
|
||||
|
||||
base.OnLoadAgentUtility(agent, [utilityLoad]);
|
||||
base.OnAgentLoaded(agent);
|
||||
}
|
||||
|
||||
private void AddUtility(Agent agent, string functionName)
|
||||
{
|
||||
var (prompt, fn) = GetPromptAndFunction(functionName);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private (string, FunctionDef?) GetPromptAndFunction(string functionName)
|
||||
{
|
||||
var db = _services.GetRequiredService<IBotSharpRepository>();
|
||||
var agent = db.GetAgent(BuiltInAgentId.UtilityAssistant);
|
||||
var prompt = agent?.Templates?.FirstOrDefault(x => x.Name.IsEqualTo($"{functionName}.fn"))?.Content ?? string.Empty;
|
||||
var loadAttachmentFn = agent?.Functions?.FirstOrDefault(x => x.Name.IsEqualTo(functionName));
|
||||
return (prompt, loadAttachmentFn);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -15,55 +15,47 @@ public class FileHandlerHook : AgentHookBase, IAgentHook
|
|||
|
||||
public override void OnAgentLoaded(Agent agent)
|
||||
{
|
||||
var conv = _services.GetRequiredService<IConversationService>();
|
||||
var isConvMode = conv.IsConversationMode();
|
||||
|
||||
if (isConvMode)
|
||||
var utilityLoads = new List<AgentUtilityLoadModel>
|
||||
{
|
||||
AddUtility(agent, UtilityName.ImageGenerator, GENERATE_IMAGE_FN);
|
||||
AddUtility(agent, UtilityName.ImageReader, READ_IMAGE_FN);
|
||||
AddUtility(agent, UtilityName.ImageEditor, EDIT_IMAGE_FN);
|
||||
AddUtility(agent, UtilityName.PdfReader, READ_PDF_FN);
|
||||
|
||||
}
|
||||
new AgentUtilityLoadModel
|
||||
{
|
||||
UtilityName = UtilityName.ImageGenerator,
|
||||
Content = new UtilityContent
|
||||
{
|
||||
Functions = [new(GENERATE_IMAGE_FN)],
|
||||
Templates = [new($"{GENERATE_IMAGE_FN}.fn")]
|
||||
}
|
||||
},
|
||||
new AgentUtilityLoadModel
|
||||
{
|
||||
UtilityName = UtilityName.ImageReader,
|
||||
Content = new UtilityContent
|
||||
{
|
||||
Functions = [new(READ_IMAGE_FN)],
|
||||
Templates = [new($"{READ_IMAGE_FN}.fn")]
|
||||
}
|
||||
},
|
||||
new AgentUtilityLoadModel
|
||||
{
|
||||
UtilityName = UtilityName.ImageEditor,
|
||||
Content = new UtilityContent
|
||||
{
|
||||
Functions = [new(EDIT_IMAGE_FN)],
|
||||
Templates = [new($"{EDIT_IMAGE_FN}.fn")]
|
||||
}
|
||||
},
|
||||
new AgentUtilityLoadModel
|
||||
{
|
||||
UtilityName = UtilityName.PdfReader,
|
||||
Content = new UtilityContent
|
||||
{
|
||||
Functions = [new(READ_PDF_FN)],
|
||||
Templates = [new($"{READ_PDF_FN}.fn")]
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
base.OnLoadAgentUtility(agent, utilityLoads);
|
||||
base.OnAgentLoaded(agent);
|
||||
}
|
||||
|
||||
private void AddUtility(Agent agent, string utility, string functionName)
|
||||
{
|
||||
if (!IsEnableUtility(agent, utility)) return;
|
||||
|
||||
var (prompt, fn) = GetPromptAndFunction(functionName);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsEnableUtility(Agent agent, string utility)
|
||||
{
|
||||
return !agent.Utilities.IsNullOrEmpty() && agent.Utilities.Contains(utility);
|
||||
}
|
||||
|
||||
private (string, FunctionDef?) GetPromptAndFunction(string functionName)
|
||||
{
|
||||
var db = _services.GetRequiredService<IBotSharpRepository>();
|
||||
var agent = db.GetAgent(BuiltInAgentId.UtilityAssistant);
|
||||
var prompt = agent?.Templates?.FirstOrDefault(x => x.Name.IsEqualTo($"{functionName}.fn"))?.Content ?? string.Empty;
|
||||
var loadAttachmentFn = agent?.Functions?.FirstOrDefault(x => x.Name.IsEqualTo(functionName));
|
||||
return (prompt, loadAttachmentFn);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,14 +1,11 @@
|
|||
using BotSharp.Abstraction.Agents;
|
||||
using BotSharp.Abstraction.Agents.Enums;
|
||||
using BotSharp.Abstraction.Agents.Settings;
|
||||
using BotSharp.Abstraction.Functions.Models;
|
||||
using BotSharp.Abstraction.Repositories;
|
||||
|
||||
namespace BotSharp.Plugin.HttpHandler.Hooks;
|
||||
|
||||
public class HttpHandlerHook : AgentHookBase
|
||||
{
|
||||
private static string FUNCTION_NAME = "handle_http_request";
|
||||
private static string HTTP_HANDLER_FN = "handle_http_request";
|
||||
|
||||
public override string SelfId => string.Empty;
|
||||
|
||||
|
|
@ -19,40 +16,17 @@ public class HttpHandlerHook : AgentHookBase
|
|||
|
||||
public override void OnAgentLoaded(Agent agent)
|
||||
{
|
||||
var conv = _services.GetRequiredService<IConversationService>();
|
||||
var isConvMode = conv.IsConversationMode();
|
||||
var isEnabled = !agent.Utilities.IsNullOrEmpty() && agent.Utilities.Contains(UtilityName.HttpHandler);
|
||||
|
||||
if (isConvMode && isEnabled)
|
||||
var utilityLoad = new AgentUtilityLoadModel
|
||||
{
|
||||
var (prompt, fn) = GetPromptAndFunction(FUNCTION_NAME);
|
||||
if (fn != null)
|
||||
UtilityName = UtilityName.HttpHandler,
|
||||
Content = new UtilityContent
|
||||
{
|
||||
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);
|
||||
}
|
||||
Functions = [new(HTTP_HANDLER_FN)],
|
||||
Templates = [new($"{HTTP_HANDLER_FN}.fn")]
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
base.OnLoadAgentUtility(agent, [utilityLoad]);
|
||||
base.OnAgentLoaded(agent);
|
||||
}
|
||||
|
||||
private (string, FunctionDef?) GetPromptAndFunction(string functionName)
|
||||
{
|
||||
var db = _services.GetRequiredService<IBotSharpRepository>();
|
||||
var agent = db.GetAgent(BuiltInAgentId.UtilityAssistant);
|
||||
var prompt = agent?.Templates?.FirstOrDefault(x => x.Name.IsEqualTo($"{functionName}.fn"))?.Content ?? string.Empty;
|
||||
var loadAttachmentFn = agent?.Functions?.FirstOrDefault(x => x.Name.IsEqualTo(functionName));
|
||||
return (prompt, loadAttachmentFn);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,59 +2,29 @@ namespace BotSharp.Plugin.KnowledgeBase.Hooks;
|
|||
|
||||
public class KnowledgeBaseAgentHook : AgentHookBase, IAgentHook
|
||||
{
|
||||
private const string KNOWLEDGE_RETRIEVAL_FN = "knowledge_retrieval";
|
||||
|
||||
public override string SelfId => string.Empty;
|
||||
public KnowledgeBaseAgentHook(IServiceProvider services, AgentSettings settings) : base(services, settings)
|
||||
|
||||
public KnowledgeBaseAgentHook(IServiceProvider services, AgentSettings settings)
|
||||
: base(services, settings)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override void OnAgentLoaded(Agent agent)
|
||||
{
|
||||
var conv = _services.GetRequiredService<IConversationService>();
|
||||
var isConvMode = conv.IsConversationMode();
|
||||
|
||||
if (isConvMode)
|
||||
var utilityLoad = new AgentUtilityLoadModel
|
||||
{
|
||||
AddUtility(agent, UtilityName.KnowledgeRetrieval, "knowledge_retrieval");
|
||||
}
|
||||
UtilityName = UtilityName.KnowledgeRetrieval,
|
||||
Content = new UtilityContent
|
||||
{
|
||||
Functions = [new(KNOWLEDGE_RETRIEVAL_FN)],
|
||||
Templates = [new($"{KNOWLEDGE_RETRIEVAL_FN}.fn")]
|
||||
}
|
||||
};
|
||||
|
||||
base.OnLoadAgentUtility(agent, [utilityLoad]);
|
||||
base.OnAgentLoaded(agent);
|
||||
}
|
||||
|
||||
private void AddUtility(Agent agent, string utility, string functionName)
|
||||
{
|
||||
if (!IsEnableUtility(agent, utility)) return;
|
||||
|
||||
var (prompt, fn) = GetPromptAndFunction(functionName);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsEnableUtility(Agent agent, string utility)
|
||||
{
|
||||
return !agent.Utilities.IsNullOrEmpty() && agent.Utilities.Contains(utility);
|
||||
}
|
||||
|
||||
private (string, FunctionDef?) GetPromptAndFunction(string functionName)
|
||||
{
|
||||
var db = _services.GetRequiredService<IBotSharpRepository>();
|
||||
var agent = db.GetAgent(BuiltInAgentId.UtilityAssistant);
|
||||
var prompt = agent?.Templates?.FirstOrDefault(x => x.Name.IsEqualTo($"{functionName}.fn"))?.Content ?? string.Empty;
|
||||
var fn = agent?.Functions?.FirstOrDefault(x => x.Name.IsEqualTo(functionName));
|
||||
return (prompt, fn);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,10 @@ namespace BotSharp.Plugin.Planner.Hooks;
|
|||
|
||||
public class PlannerAgentHook : AgentHookBase
|
||||
{
|
||||
private const string PRIMARY_STAGE_FN = "plan_primary_stage";
|
||||
private const string SECONDARY_STAGE_FN = "plan_secondary_stage";
|
||||
private const string SUMMARY_FN = "plan_summary";
|
||||
|
||||
public override string SelfId => BuiltInAgentId.Planner;
|
||||
|
||||
public PlannerAgentHook(IServiceProvider services, AgentSettings settings)
|
||||
|
|
@ -30,76 +34,25 @@ public class PlannerAgentHook : AgentHookBase
|
|||
|
||||
public override void OnAgentLoaded(Agent agent)
|
||||
{
|
||||
var conv = _services.GetRequiredService<IConversationService>();
|
||||
var isConvMode = conv.IsConversationMode();
|
||||
var isEnabled = !agent.Utilities.IsNullOrEmpty() && agent.Utilities.Contains(UtilityName.TwoStagePlanner);
|
||||
|
||||
if (isConvMode && isEnabled)
|
||||
var utilityLoad = new AgentUtilityLoadModel
|
||||
{
|
||||
var (prompt, fn) = GetPromptAndFunction("plan_primary_stage");
|
||||
if (fn != null)
|
||||
UtilityName = UtilityName.TwoStagePlanner,
|
||||
Content = new UtilityContent
|
||||
{
|
||||
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);
|
||||
}
|
||||
Functions = [
|
||||
new(PRIMARY_STAGE_FN),
|
||||
new(SECONDARY_STAGE_FN),
|
||||
new(SUMMARY_FN)
|
||||
],
|
||||
Templates = [
|
||||
new($"{PRIMARY_STAGE_FN}.fn"),
|
||||
new($"{SECONDARY_STAGE_FN}.fn"),
|
||||
new($"{SUMMARY_FN}.fn")
|
||||
]
|
||||
}
|
||||
};
|
||||
|
||||
(prompt, fn) = GetPromptAndFunction("plan_secondary_stage");
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
(prompt, fn) = GetPromptAndFunction("plan_summary");
|
||||
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.OnLoadAgentUtility(agent, [utilityLoad]);
|
||||
base.OnAgentLoaded(agent);
|
||||
}
|
||||
|
||||
private (string, FunctionDef?) GetPromptAndFunction(string functionName)
|
||||
{
|
||||
var db = _services.GetRequiredService<IBotSharpRepository>();
|
||||
var agent = db.GetAgent(BuiltInAgentId.UtilityAssistant);
|
||||
var prompt = agent?.Templates?.FirstOrDefault(x => x.Name.IsEqualTo($"{functionName}.fn"))?.Content ?? string.Empty;
|
||||
var loadAttachmentFn = agent?.Functions?.FirstOrDefault(x => x.Name.IsEqualTo(functionName));
|
||||
return (prompt, loadAttachmentFn);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>$(TargetFramework)</TargetFramework>
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
namespace BotSharp.Plugin.SqlDriver.Enum;
|
||||
|
||||
public class Utility
|
||||
public class UtilityName
|
||||
{
|
||||
public const string SqlExecutor = "sql-executor";
|
||||
public const string SqlDictionaryLookup = "sql-dictionary-lookup";
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
namespace BotSharp.Plugin.SqlDriver.Helpers;
|
||||
|
||||
internal static class SqlDriverHelper
|
||||
{
|
||||
internal static string GetDatabaseType(IServiceProvider services)
|
||||
{
|
||||
var settings = services.GetRequiredService<SqlDriverSetting>();
|
||||
var dbType = "MySQL";
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(settings?.SqlServerConnectionString))
|
||||
{
|
||||
dbType = "SQL Server";
|
||||
}
|
||||
else if (!string.IsNullOrWhiteSpace(settings?.SqlLiteConnectionString))
|
||||
{
|
||||
dbType = "SQL Lite";
|
||||
}
|
||||
return dbType;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,84 +0,0 @@
|
|||
using BotSharp.Abstraction.Agents.Enums;
|
||||
using BotSharp.Abstraction.Agents.Settings;
|
||||
using BotSharp.Abstraction.Functions.Models;
|
||||
using BotSharp.Abstraction.Repositories;
|
||||
|
||||
namespace BotSharp.Plugin.SqlDriver.Hooks;
|
||||
|
||||
public class GetTableDefinitionHook : AgentHookBase, IAgentHook
|
||||
{
|
||||
private const string SQL_EXECUTOR_TEMPLATE = "sql_table_definition.fn";
|
||||
private IEnumerable<string> _targetSqlExecutorFunctions = new List<string>
|
||||
{
|
||||
"sql_table_definition",
|
||||
};
|
||||
|
||||
public override string SelfId => BuiltInAgentId.Planner;
|
||||
|
||||
public GetTableDefinitionHook(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(Utility.SqlTableDefinition);
|
||||
|
||||
if (isConvMode && isEnabled)
|
||||
{
|
||||
var (prompt, fns) = GetPromptAndFunctions();
|
||||
if (!fns.IsNullOrEmpty())
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(prompt))
|
||||
{
|
||||
agent.Instruction += $"\r\n\r\n{prompt}\r\n\r\n";
|
||||
}
|
||||
|
||||
if (agent.Functions == null)
|
||||
{
|
||||
agent.Functions = fns;
|
||||
}
|
||||
else
|
||||
{
|
||||
agent.Functions.AddRange(fns);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
base.OnAgentLoaded(agent);
|
||||
}
|
||||
|
||||
private (string, List<FunctionDef>?) GetPromptAndFunctions()
|
||||
{
|
||||
var db = _services.GetRequiredService<IBotSharpRepository>();
|
||||
var agent = db.GetAgent(BuiltInAgentId.UtilityAssistant);
|
||||
var fns = agent?.Functions?.Where(x => _targetSqlExecutorFunctions.Contains(x.Name))?.ToList();
|
||||
|
||||
var prompt = agent?.Templates?.FirstOrDefault(x => x.Name.IsEqualTo(SQL_EXECUTOR_TEMPLATE))?.Content ?? string.Empty;
|
||||
var dbType = GetDatabaseType();
|
||||
var render = _services.GetRequiredService<ITemplateRender>();
|
||||
prompt = render.Render(prompt, new Dictionary<string, object>
|
||||
{
|
||||
{ "db_type", dbType }
|
||||
});
|
||||
|
||||
return (prompt, fns);
|
||||
}
|
||||
|
||||
private string GetDatabaseType()
|
||||
{
|
||||
var settings = _services.GetRequiredService<SqlDriverSetting>();
|
||||
var dbType = "MySQL";
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(settings?.SqlServerConnectionString))
|
||||
{
|
||||
dbType = "SQL Server";
|
||||
}
|
||||
else if (!string.IsNullOrWhiteSpace(settings?.SqlLiteConnectionString))
|
||||
{
|
||||
dbType = "SQL Lite";
|
||||
}
|
||||
return dbType;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,84 +0,0 @@
|
|||
using BotSharp.Abstraction.Agents.Enums;
|
||||
using BotSharp.Abstraction.Agents.Settings;
|
||||
using BotSharp.Abstraction.Functions.Models;
|
||||
using BotSharp.Abstraction.Repositories;
|
||||
|
||||
namespace BotSharp.Plugin.SqlDriver.Hooks;
|
||||
|
||||
public class SqlDictionaryLookupHook : AgentHookBase, IAgentHook
|
||||
{
|
||||
private const string SQL_EXECUTOR_TEMPLATE = "verify_dictionary_term.fn";
|
||||
private IEnumerable<string> _targetSqlExecutorFunctions = new List<string>
|
||||
{
|
||||
"verify_dictionary_term",
|
||||
};
|
||||
|
||||
public override string SelfId => BuiltInAgentId.Planner;
|
||||
|
||||
public SqlDictionaryLookupHook(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(Utility.SqlDictionaryLookup);
|
||||
|
||||
if (isConvMode && isEnabled)
|
||||
{
|
||||
var (prompt, fns) = GetPromptAndFunctions();
|
||||
if (!fns.IsNullOrEmpty())
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(prompt))
|
||||
{
|
||||
agent.Instruction += $"\r\n\r\n{prompt}\r\n\r\n";
|
||||
}
|
||||
|
||||
if (agent.Functions == null)
|
||||
{
|
||||
agent.Functions = fns;
|
||||
}
|
||||
else
|
||||
{
|
||||
agent.Functions.AddRange(fns);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
base.OnAgentLoaded(agent);
|
||||
}
|
||||
|
||||
private (string, List<FunctionDef>?) GetPromptAndFunctions()
|
||||
{
|
||||
var db = _services.GetRequiredService<IBotSharpRepository>();
|
||||
var agent = db.GetAgent(BuiltInAgentId.UtilityAssistant);
|
||||
var fns = agent?.Functions?.Where(x => _targetSqlExecutorFunctions.Contains(x.Name))?.ToList();
|
||||
|
||||
var prompt = agent?.Templates?.FirstOrDefault(x => x.Name.IsEqualTo(SQL_EXECUTOR_TEMPLATE))?.Content ?? string.Empty;
|
||||
var dbType = GetDatabaseType();
|
||||
var render = _services.GetRequiredService<ITemplateRender>();
|
||||
prompt = render.Render(prompt, new Dictionary<string, object>
|
||||
{
|
||||
{ "db_type", dbType }
|
||||
});
|
||||
|
||||
return (prompt, fns);
|
||||
}
|
||||
|
||||
private string GetDatabaseType()
|
||||
{
|
||||
var settings = _services.GetRequiredService<SqlDriverSetting>();
|
||||
var dbType = "MySQL";
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(settings?.SqlServerConnectionString))
|
||||
{
|
||||
dbType = "SQL Server";
|
||||
}
|
||||
else if (!string.IsNullOrWhiteSpace(settings?.SqlLiteConnectionString))
|
||||
{
|
||||
dbType = "SQL Lite";
|
||||
}
|
||||
return dbType;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
using BotSharp.Abstraction.Agents.Enums;
|
||||
using BotSharp.Abstraction.Agents.Settings;
|
||||
|
||||
namespace BotSharp.Plugin.SqlDriver.Hooks;
|
||||
|
||||
public class SqlDriverAgentHook : AgentHookBase, IAgentHook
|
||||
{
|
||||
private const string SQL_TABLE_DEFINITION_FN = "sql_table_definition";
|
||||
private const string VERIFY_DICTIONARY_TERM_FN = "verify_dictionary_term";
|
||||
private const string SQL_SELECT_FN = "sql_select";
|
||||
|
||||
public override string SelfId => BuiltInAgentId.Planner;
|
||||
|
||||
public SqlDriverAgentHook(IServiceProvider services, AgentSettings settings)
|
||||
: base(services, settings)
|
||||
{
|
||||
}
|
||||
|
||||
public override void OnAgentLoaded(Agent agent)
|
||||
{
|
||||
var dbType = SqlDriverHelper.GetDatabaseType(_services);
|
||||
var promptData = new Dictionary<string, object>
|
||||
{
|
||||
{ "db_type", dbType }
|
||||
};
|
||||
|
||||
var utilityLoads = new List<AgentUtilityLoadModel>
|
||||
{
|
||||
new AgentUtilityLoadModel
|
||||
{
|
||||
UtilityName = UtilityName.SqlTableDefinition,
|
||||
Content = new UtilityContent
|
||||
{
|
||||
Functions = new List<UtilityFunction> { new(SQL_TABLE_DEFINITION_FN) },
|
||||
Templates = new List<UtilityTemplate> { new($"{SQL_TABLE_DEFINITION_FN}.fn", promptData) }
|
||||
}
|
||||
},
|
||||
new AgentUtilityLoadModel
|
||||
{
|
||||
UtilityName = UtilityName.SqlDictionaryLookup,
|
||||
Content = new UtilityContent
|
||||
{
|
||||
Functions = new List<UtilityFunction> { new(VERIFY_DICTIONARY_TERM_FN) },
|
||||
Templates = new List<UtilityTemplate> { new($"{VERIFY_DICTIONARY_TERM_FN}.fn", promptData) }
|
||||
}
|
||||
},
|
||||
new AgentUtilityLoadModel
|
||||
{
|
||||
UtilityName = UtilityName.SqlExecutor,
|
||||
Content = new UtilityContent
|
||||
{
|
||||
Functions = new List<UtilityFunction> { new(SQL_SELECT_FN), new(SQL_TABLE_DEFINITION_FN) },
|
||||
Templates = new List<UtilityTemplate> { new($"sql_executor.fn", promptData) }
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
base.OnLoadAgentUtility(agent, utilityLoads);
|
||||
base.OnAgentLoaded(agent);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,85 +0,0 @@
|
|||
using BotSharp.Abstraction.Agents.Enums;
|
||||
using BotSharp.Abstraction.Agents.Settings;
|
||||
using BotSharp.Abstraction.Functions.Models;
|
||||
using BotSharp.Abstraction.Repositories;
|
||||
|
||||
namespace BotSharp.Plugin.SqlDriver.Hooks;
|
||||
|
||||
public class SqlExecutorHook : AgentHookBase, IAgentHook
|
||||
{
|
||||
private const string SQL_EXECUTOR_TEMPLATE = "sql_executor.fn";
|
||||
private IEnumerable<string> _targetSqlExecutorFunctions = new List<string>
|
||||
{
|
||||
"sql_select",
|
||||
"sql_table_definition",
|
||||
};
|
||||
|
||||
public override string SelfId => string.Empty;
|
||||
|
||||
public SqlExecutorHook(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(Utility.SqlExecutor);
|
||||
|
||||
if (isConvMode && isEnabled)
|
||||
{
|
||||
var (prompt, fns) = GetPromptAndFunctions();
|
||||
if (!fns.IsNullOrEmpty())
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(prompt))
|
||||
{
|
||||
agent.Instruction += $"\r\n\r\n{prompt}\r\n\r\n";
|
||||
}
|
||||
|
||||
if (agent.Functions == null)
|
||||
{
|
||||
agent.Functions = fns;
|
||||
}
|
||||
else
|
||||
{
|
||||
agent.Functions.AddRange(fns);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
base.OnAgentLoaded(agent);
|
||||
}
|
||||
|
||||
private (string, List<FunctionDef>?) GetPromptAndFunctions()
|
||||
{
|
||||
var db = _services.GetRequiredService<IBotSharpRepository>();
|
||||
var agent = db.GetAgent(BuiltInAgentId.UtilityAssistant);
|
||||
var fns = agent?.Functions?.Where(x => _targetSqlExecutorFunctions.Contains(x.Name))?.ToList();
|
||||
|
||||
var prompt = agent?.Templates?.FirstOrDefault(x => x.Name.IsEqualTo(SQL_EXECUTOR_TEMPLATE))?.Content ?? string.Empty;
|
||||
var dbType = GetDatabaseType(); //need change-> using hook?
|
||||
var render = _services.GetRequiredService<ITemplateRender>();
|
||||
prompt = render.Render(prompt, new Dictionary<string, object>
|
||||
{
|
||||
{ "db_type", dbType }
|
||||
});
|
||||
|
||||
return (prompt, fns);
|
||||
}
|
||||
|
||||
private string GetDatabaseType()
|
||||
{
|
||||
var settings = _services.GetRequiredService<SqlDriverSetting>();
|
||||
var dbType = "MySQL";
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(settings?.SqlServerConnectionString))
|
||||
{
|
||||
dbType = "SQL Server";
|
||||
}
|
||||
else if (!string.IsNullOrWhiteSpace(settings?.SqlLiteConnectionString))
|
||||
{
|
||||
dbType = "SQL Lite";
|
||||
}
|
||||
return dbType;
|
||||
}
|
||||
}
|
||||
|
|
@ -4,8 +4,8 @@ public class SqlUtilityHook : IAgentUtilityHook
|
|||
{
|
||||
public void AddUtilities(List<string> utilities)
|
||||
{
|
||||
utilities.Add(Utility.SqlExecutor);
|
||||
utilities.Add(Utility.SqlDictionaryLookup);
|
||||
utilities.Add(Utility.SqlTableDefinition);
|
||||
utilities.Add(UtilityName.SqlExecutor);
|
||||
utilities.Add(UtilityName.SqlDictionaryLookup);
|
||||
utilities.Add(UtilityName.SqlTableDefinition);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
namespace BotSharp.Plugin.SqlHero.Settings;
|
||||
namespace BotSharp.Plugin.SqlDriver.Settings;
|
||||
|
||||
public class SqlDriverSetting
|
||||
{
|
||||
|
|
|
|||
|
|
@ -25,12 +25,10 @@ public class SqlDriverPlugin : IBotSharpPlugin
|
|||
|
||||
services.AddScoped<SqlDriverService>();
|
||||
services.AddScoped<DbKnowledgeService>();
|
||||
services.AddScoped<IKnowledgeHook, SqlDriverKnowledgeHook>();
|
||||
services.AddScoped<IAgentHook, SqlExecutorHook>();
|
||||
services.AddScoped<IAgentUtilityHook, SqlUtilityHook>();
|
||||
services.AddScoped<IAgentHook, SqlDriverAgentHook>();
|
||||
services.AddScoped<IPlanningHook, SqlDriverPlanningHook>();
|
||||
services.AddScoped<IAgentHook, SqlDictionaryLookupHook>();
|
||||
services.AddScoped<IAgentHook, GetTableDefinitionHook>();
|
||||
services.AddScoped<IKnowledgeHook, SqlDriverKnowledgeHook>();
|
||||
services.AddScoped<IConversationHook, SqlDriverConversationHook>();
|
||||
services.AddScoped<IAgentUtilityHook, SqlUtilityHook>();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,5 +20,5 @@ global using BotSharp.Abstraction.Settings;
|
|||
global using BotSharp.Plugin.SqlDriver.Hooks;
|
||||
global using BotSharp.Plugin.SqlDriver.Services;
|
||||
global using BotSharp.Plugin.SqlDriver.Enum;
|
||||
global using BotSharp.Plugin.SqlHero.Settings;
|
||||
global using System.Drawing;
|
||||
global using BotSharp.Plugin.SqlDriver.Helpers;
|
||||
global using BotSharp.Plugin.SqlDriver.Settings;
|
||||
|
|
|
|||
|
|
@ -1,14 +1,12 @@
|
|||
using BotSharp.Abstraction.Agents.Models;
|
||||
using BotSharp.Abstraction.Agents.Settings;
|
||||
using BotSharp.Abstraction.Repositories;
|
||||
using BotSharp.Abstraction.Utilities;
|
||||
using BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.Enums;
|
||||
|
||||
namespace BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.Hooks
|
||||
{
|
||||
internal class OutboundPhoneCallHandlerHook : AgentHookBase
|
||||
{
|
||||
private static string FUNCTION_NAME = "twilio_outbound_phone_call";
|
||||
private static string OUTBOUND_PHONE_CALL_FN = "twilio_outbound_phone_call";
|
||||
|
||||
public override string SelfId => string.Empty;
|
||||
|
||||
|
|
@ -18,41 +16,18 @@ namespace BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.Hooks
|
|||
|
||||
public override void OnAgentLoaded(Agent agent)
|
||||
{
|
||||
var conv = _services.GetRequiredService<IConversationService>();
|
||||
var isConvMode = conv.IsConversationMode();
|
||||
var isEnabled = !agent.Utilities.IsNullOrEmpty() && agent.Utilities.Contains(UtilityName.OutboundPhoneCall);
|
||||
|
||||
if (isConvMode && isEnabled)
|
||||
var utilityLoad = new AgentUtilityLoadModel
|
||||
{
|
||||
var (prompt, fn) = GetPromptAndFunction();
|
||||
if (fn != null)
|
||||
UtilityName = UtilityName.OutboundPhoneCall,
|
||||
Content = new UtilityContent
|
||||
{
|
||||
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);
|
||||
}
|
||||
Functions = [new(OUTBOUND_PHONE_CALL_FN)],
|
||||
Templates = [new($"{OUTBOUND_PHONE_CALL_FN}.fn")]
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
base.OnLoadAgentUtility(agent, [utilityLoad]);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue