add sql executor utility

This commit is contained in:
Jicheng Lu 2024-07-15 15:06:22 -05:00
parent 2d15f2996b
commit 772aec54bd
15 changed files with 155 additions and 23 deletions

View file

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>$(TargetFramework)</TargetFramework>
@ -191,7 +191,6 @@
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="Nanoid" Version="3.0.0" />
<PackageReference Include="RedLock.net" Version="2.3.2" />
<PackageReference Include="System.Drawing.Common" Version="8.0.6" />
</ItemGroup>
<ItemGroup>

View file

@ -65,14 +65,14 @@ public class EvaluatingService : IEvaluatingService
if (roundCount > 10)
{
Console.WriteLine($"Conversation ended due to execced max round count {roundCount}", Color.Red);
Console.WriteLine($"Conversation ended due to execced max round count {roundCount}");
break;
}
if (response.FunctionName == "conversation_end" ||
response.FunctionName == "human_intervention_needed")
{
Console.WriteLine($"Conversation ended by function {response.FunctionName}", Color.Green);
Console.WriteLine($"Conversation ended by function {response.FunctionName}");
break;
}
}

View file

@ -67,7 +67,7 @@ public class PluginLoader
if (!_plugins.Any(x => x.Assembly == plugin))
{
Console.WriteLine($"Load dependent plugin {plugin} failed by {module.Name}.", Color.Red);
Console.WriteLine($"Load dependent plugin {plugin} failed by {module.Name}.");
}
}
}
@ -79,7 +79,7 @@ public class PluginLoader
}
else
{
Console.WriteLine($"Can't find assemble {assemblyPath}.", Color.Red);
Console.WriteLine($"Can't find assemble {assemblyPath}.");
}
});
}
@ -101,7 +101,7 @@ public class PluginLoader
AgentIds = module.AgentIds
});
Console.Write($"Loaded plugin ");
Console.Write(name, Color.Green);
Console.Write(name);
Console.WriteLine($" from {assembly}.");
if (!string.IsNullOrEmpty(module.Description))
{
@ -258,7 +258,7 @@ public class PluginLoader
{
if (_modules.Count == 0)
{
Console.WriteLine($"No plugin loaded. Please check whether the Load() method is called.", Color.Yellow);
Console.WriteLine($"No plugin loaded. Please check whether the Load() method is called.");
}
_modules.ForEach(module =>

View file

@ -88,7 +88,7 @@ public partial class RoutingService
message.FunctionArgs = AppendPropertyToArgs(message.FunctionArgs, "redirect_to", record.Name);
agentId = routingRule.RedirectTo;
#if DEBUG
Console.WriteLine($"*** Routing redirect to {record.Name.ToUpper()} ***", Color.Yellow);
Console.WriteLine($"*** Routing redirect to {record.Name.ToUpper()} ***");
#else
logger.LogInformation($"*** Routing redirect to {record.Name.ToUpper()} ***");
#endif

View file

@ -19,7 +19,6 @@
<PackageReference Include="Sdcb.PaddleInference.runtime.win64.mkl" Version="2.5.1" />
<PackageReference Include="Sdcb.PaddleOCR" Version="2.7.0.1" />
<PackageReference Include="Sdcb.PaddleOCR.Models.LocalV3" Version="2.7.0.1" />
<PackageReference Include="System.Drawing.Common" Version="8.0.5" />
</ItemGroup>
<ItemGroup>

View file

@ -11,6 +11,8 @@
</PropertyGroup>
<ItemGroup>
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\sql_select.json" />
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\sql_executor.fn.liquid" />
<None Remove="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\agent.json" />
<None Remove="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\functions\get_table_columns.json" />
<None Remove="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\functions\lookup_dictionary.json" />
@ -42,6 +44,12 @@
<Content Include="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\functions\sql_select.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\sql_select.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\sql_executor.fn.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>

View file

@ -0,0 +1,6 @@
namespace BotSharp.Plugin.SqlDriver.Enum;
public class Utility
{
public const string SqlExecutor = "sql-executor";
}

View file

@ -1,12 +1,3 @@
using BotSharp.Abstraction.Conversations.Models;
using BotSharp.Abstraction.Functions;
using BotSharp.Plugin.SqlDriver.Models;
using BotSharp.Plugin.SqlHero.Settings;
using Dapper;
using MySqlConnector;
using System.Text.Json;
using System.Threading.Tasks;
namespace BotSharp.Plugin.SqlDriver.Functions;
public class ExecuteQueryFn : IFunctionCallback

View file

@ -0,0 +1,60 @@
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"
};
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 prompt = agent?.Templates?.FirstOrDefault(x => x.Name.IsEqualTo(SQL_EXECUTOR_TEMPLATE))?.Content ?? string.Empty;
var fns = agent?.Functions?.Where(x => _targetSqlExecutorFunctions.Contains(x.Name))?.ToList();
return (prompt, fns);
}
}

View file

@ -0,0 +1,9 @@
namespace BotSharp.Plugin.SqlDriver.Hooks;
public class SqlExecutorUtilityHook : IAgentUtilityHook
{
public void AddUtilities(List<string> utilities)
{
utilities.Add(Utility.SqlExecutor);
}
}

View file

@ -25,21 +25,21 @@ public class SqlDriverService
Console.WriteLine($"{sql.Reason}");
Console.WriteLine(sql.Statement, Color.Yellow);
Console.WriteLine(sql.Statement);
foreach (var p in sql.Parameters)
{
Console.WriteLine($"@{p.Name} = '{p.Value}'", Color.Green);
Console.WriteLine($"@{p.Name} = '{p.Value}'");
}
if (sql.Return != null)
{
Console.Write($"Return: ");
if (!string.IsNullOrEmpty(sql.Return.Value))
{
Console.WriteLine($" {sql.Return.Value}", Color.Red);
Console.WriteLine($" {sql.Return.Value}");
}
else
{
Console.WriteLine($"{sql.Return.Name} as @{sql.Return.Alias}", Color.Green);
Console.WriteLine($"{sql.Return.Name} as @{sql.Return.Alias}");
}
}
}

View file

@ -17,5 +17,7 @@ public class SqlDriverPlugin : IBotSharpPlugin
services.AddScoped<SqlDriverService>();
services.AddScoped<IKnowledgeHook, SqlDriverKnowledgeHook>();
services.AddScoped<IAgentHook, SqlExecutorHook>();
services.AddScoped<IAgentUtilityHook, SqlExecutorUtilityHook>();
}
}

View file

@ -19,5 +19,6 @@ global using BotSharp.Abstraction.Knowledges.Models;
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;

View file

@ -0,0 +1,56 @@
{
"name": "sql_select",
"description": "Get the specific value from table",
"parameters": {
"type": "object",
"properties": {
"sql_statement": {
"type": "string",
"description": "SQL statement with SELECT"
},
"reason": {
"type": "string",
"description": "reason"
},
"table": {
"type": "string",
"description": "related table"
},
"parameters": {
"type": "array",
"description": "data criteria for the query",
"items": {
"type": "object",
"description": "the name and value for the parameter",
"properties": {
"name": {
"type": "string",
"description": "field name"
},
"value": {
"type": "string",
"description": "real value inferred by the context"
}
},
"required": [ "name", "value" ]
}
},
"return_field": {
"type": "object",
"description": "the name and alias for the return field",
"properties": {
"name": {
"type": "string",
"description": "field in the table"
},
"alias": {
"type": "string",
"description": "meaningful field alias"
}
},
"required": [ "name", "value" ]
}
},
"required": [ "sql_statement", "reason", "table", "parameters", "return_field" ]
}
}

View file

@ -0,0 +1 @@
Please call function sql_select if user wants to get or retrieve data from data tables.