Skip to content

add sql executor utility #543

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>$(TargetFramework)</TargetFramework>
Expand Down Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ public async Task<Conversation> Execute(string task, EvaluationRequest request)

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;
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/Infrastructure/BotSharp.Core/Plugins/PluginLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void Load(Action<Assembly> loaded, string? plugin = null)

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}.");
}
}
}
Expand All @@ -79,7 +79,7 @@ public void Load(Action<Assembly> loaded, string? plugin = null)
}
else
{
Console.WriteLine($"Can't find assemble {assemblyPath}.", Color.Red);
Console.WriteLine($"Can't find assemble {assemblyPath}.");
}
});
}
Expand All @@ -101,7 +101,7 @@ private void InitModule(string assembly, IBotSharpPlugin module)
AgentIds = module.AgentIds
});
Console.Write($"Loaded plugin ");
Console.Write(name, Color.Green);
Console.Write(name);
Console.WriteLine($" from {assembly}.");
if (!string.IsNullOrEmpty(module.Description))
{
Expand Down Expand Up @@ -258,7 +258,7 @@ public void Configure(IApplicationBuilder app)
{
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 =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<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" />
<PackageReference Include="System.Drawing.Common" Version="8.0.7" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using ImageMagick;
using OpenCvSharp;
using Microsoft.AspNetCore.Http;
using Sdcb.PaddleInference;
using Sdcb.PaddleOCR.Models;
using Sdcb.PaddleOCR.Models.LocalV3;
using Sdcb.PaddleOCR;
using System.Threading.Tasks;
using BotSharp.Abstraction.Knowledges;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down Expand Up @@ -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>
Expand Down
6 changes: 6 additions & 0 deletions src/Plugins/BotSharp.Plugin.SqlDriver/Enum/Utility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace BotSharp.Plugin.SqlDriver.Enum;

public class Utility
{
public const string SqlExecutor = "sql-executor";
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down
84 changes: 84 additions & 0 deletions src/Plugins/BotSharp.Plugin.SqlDriver/Hooks/SqlExecutorHook.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
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 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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace BotSharp.Plugin.SqlDriver.Hooks;

public class SqlExecutorUtilityHook : IAgentUtilityHook
{
public void AddUtilities(List<string> utilities)
{
utilities.Add(Utility.SqlExecutor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,21 @@ public void Enqueue(SqlStatement statement)

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}");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ namespace BotSharp.Plugin.SqlHero.Settings;
public class SqlDriverSetting
{
public string MySqlConnectionString { get; set; }
public string SqlServerConnectionString { get; set; }
public string SqlLiteConnectionString { get; set; }
}
2 changes: 2 additions & 0 deletions src/Plugins/BotSharp.Plugin.SqlDriver/SqlDriverPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ public void RegisterDI(IServiceCollection services, IConfiguration config)

services.AddScoped<SqlDriverService>();
services.AddScoped<IKnowledgeHook, SqlDriverKnowledgeHook>();
services.AddScoped<IAgentHook, SqlExecutorHook>();
services.AddScoped<IAgentUtilityHook, SqlExecutorUtilityHook>();
}
}
1 change: 1 addition & 0 deletions src/Plugins/BotSharp.Plugin.SqlDriver/Using.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@
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;
Original file line number Diff line number Diff line change
@@ -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" ]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
You are connecting to {{ db_type }} database. Please generate SQL statements following {{ db_type }} rules.

Please call function sql_select if user wants to get or retrieve data from data tables.
If there are any parameters, please add them in the WHERE clause, each of which starts with "@".
For example, SELECT * FROM table WHERE Id=@Id AND Name=@Name
6 changes: 6 additions & 0 deletions src/WebStarter/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@
"Origin": ""
},

"SqlDriver": {
"MySqlConnectionString": "",
"SqlServerConnectionString": "",
"SqlLiteConnectionString": ""
},

"Statistics": {
"DataDir": "stats"
},
Expand Down