diff --git a/src/Plugins/BotSharp.Plugin.SqlDriver/BotSharp.Plugin.SqlDriver.csproj b/src/Plugins/BotSharp.Plugin.SqlDriver/BotSharp.Plugin.SqlDriver.csproj index fcc88874..171ba4cd 100644 --- a/src/Plugins/BotSharp.Plugin.SqlDriver/BotSharp.Plugin.SqlDriver.csproj +++ b/src/Plugins/BotSharp.Plugin.SqlDriver/BotSharp.Plugin.SqlDriver.csproj @@ -14,6 +14,7 @@ + @@ -26,6 +27,9 @@ PreserveNewest + + PreserveNewest + @@ -38,8 +42,4 @@ - - - - diff --git a/src/Plugins/BotSharp.Plugin.SqlDriver/Functions/LookupDictionaryFn.cs b/src/Plugins/BotSharp.Plugin.SqlDriver/Functions/LookupDictionaryFn.cs new file mode 100644 index 00000000..94607662 --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.SqlDriver/Functions/LookupDictionaryFn.cs @@ -0,0 +1,79 @@ +using Amazon.Runtime.Internal.Transform; +using BotSharp.Abstraction.Agents.Enums; +using BotSharp.Abstraction.MLTasks; +using BotSharp.Core.Infrastructures; +using BotSharp.Plugin.SqlDriver.Models; +using MySqlConnector; +using static Dapper.SqlMapper; + +namespace BotSharp.Plugin.SqlDriver.Functions; + +public class LookupDictionaryFn : IFunctionCallback +{ + public string Name => "lookup_dictionary"; + private readonly IServiceProvider _services; + + public LookupDictionaryFn(IServiceProvider services) + { + _services = services; + } + + public async Task Execute(RoleDialogModel message) + { + var args = JsonSerializer.Deserialize(message.FunctionArgs); + + var settings = _services.GetRequiredService(); + using var connection = new MySqlConnection(settings.MySqlConnectionString); + var dictionary = new Dictionary(); + var results = connection.Query($"SELECT * FROM {args.Table} LIMIT 10"); + var items = new List(); + foreach(var item in results) + { + items.Add(JsonSerializer.Serialize(item)); + } + + var agentService = _services.GetRequiredService(); + var agent = await agentService.LoadAgent(message.CurrentAgentId); + var prompt = GetPrompt(agent, items, args.Keyword); + + // Ask LLM which one is the best + var llmProviderService = _services.GetRequiredService(); + var model = llmProviderService.GetProviderModel("azure-openai", "gpt-35-turbo"); + + // chat completion + var completion = CompletionProvider.GetChatCompletion(_services, + provider: "azure-openai", + model: model.Name); + + var conversations = new List + { + new RoleDialogModel(AgentRole.User, prompt) + { + CurrentAgentId = message.CurrentAgentId, + MessageId = message.MessageId, + } + }; + + var response = await completion.GetChatCompletions(new Agent + { + Id = message.CurrentAgentId, + Instruction = "" + }, conversations); + + message.Content = response.Content; + + return true; + } + + private string GetPrompt(Agent agent, List task, string keyword) + { + var template = agent.Templates.First(x => x.Name == "lookup_dictionary").Content; + + var render = _services.GetRequiredService(); + return render.Render(template, new Dictionary + { + { "items", task }, + { "keyword", keyword } + }); + } +} diff --git a/src/Plugins/BotSharp.Plugin.SqlDriver/Functions/SqlInsertFn.cs b/src/Plugins/BotSharp.Plugin.SqlDriver/Functions/SqlInsertFn.cs index 42bcd074..e3499b1a 100644 --- a/src/Plugins/BotSharp.Plugin.SqlDriver/Functions/SqlInsertFn.cs +++ b/src/Plugins/BotSharp.Plugin.SqlDriver/Functions/SqlInsertFn.cs @@ -16,6 +16,23 @@ public class SqlInsertFn : IFunctionCallback { var args = JsonSerializer.Deserialize(message.FunctionArgs); var sqlDriver = _services.GetRequiredService(); + + // Check duplication + if (sqlDriver.Statements.Exists(x => x.Statement == args.Statement)) + { + var list = sqlDriver.Statements.Where(x => x.Statement == args.Statement).ToList(); + foreach (var statement in list) + { + var p1 = string.Join(", ", statement.Parameters.OrderBy(x => x.Name).Select(x => x.Value)); + var p2 = string.Join(", ", args.Parameters.OrderBy(x => x.Name).Select(x => x.Value)); + if (p1 == p2) + { + message.Content = "Skip duplicated INSERT statement"; + return false; + } + } + } + sqlDriver.Enqueue(args); message.Content = $"Inserted new record successfully."; if (args.Return != null) diff --git a/src/Plugins/BotSharp.Plugin.SqlDriver/Models/LookupDictionary.cs b/src/Plugins/BotSharp.Plugin.SqlDriver/Models/LookupDictionary.cs new file mode 100644 index 00000000..cf7d6345 --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.SqlDriver/Models/LookupDictionary.cs @@ -0,0 +1,15 @@ +using System.Text.Json.Serialization; + +namespace BotSharp.Plugin.SqlDriver.Models; + +public class LookupDictionary +{ + [JsonPropertyName("table")] + public string Table { get; set; } + + [JsonPropertyName("keyword")] + public string Keyword { get; set; } + + [JsonPropertyName("columns")] + public string[] Columns { get; set; } +} diff --git a/src/Plugins/BotSharp.Plugin.SqlDriver/data/agents/beda4c12-e1ec-4b4b-b328-3df4a6687c4f/functions.json b/src/Plugins/BotSharp.Plugin.SqlDriver/data/agents/beda4c12-e1ec-4b4b-b328-3df4a6687c4f/functions.json index bcc527ee..85703028 100644 --- a/src/Plugins/BotSharp.Plugin.SqlDriver/data/agents/beda4c12-e1ec-4b4b-b328-3df4a6687c4f/functions.json +++ b/src/Plugins/BotSharp.Plugin.SqlDriver/data/agents/beda4c12-e1ec-4b4b-b328-3df4a6687c4f/functions.json @@ -124,5 +124,31 @@ }, "required": [ "sql_statement", "reason", "table", "parameters", "return_field" ] } + }, + { + "name": "lookup_dictionary", + "description": "Get id from dictionary table by keyword if tool or solution mentioned this approach", + "parameters": { + "type": "object", + "properties": { + "table": { + "type": "string", + "description": "table name" + }, + "keyword": { + "type": "string", + "description": "table name" + }, + "columns": { + "type": "array", + "description": "columns", + "items": { + "type": "string", + "description": "column" + } + } + }, + "required": [ "table", "columns", "keyword" ] + } } ] \ No newline at end of file diff --git a/src/Plugins/BotSharp.Plugin.SqlDriver/data/agents/beda4c12-e1ec-4b4b-b328-3df4a6687c4f/instruction.liquid b/src/Plugins/BotSharp.Plugin.SqlDriver/data/agents/beda4c12-e1ec-4b4b-b328-3df4a6687c4f/instruction.liquid index a2aac548..cb0dae8d 100644 --- a/src/Plugins/BotSharp.Plugin.SqlDriver/data/agents/beda4c12-e1ec-4b4b-b328-3df4a6687c4f/instruction.liquid +++ b/src/Plugins/BotSharp.Plugin.SqlDriver/data/agents/beda4c12-e1ec-4b4b-b328-3df4a6687c4f/instruction.liquid @@ -4,8 +4,6 @@ Output the next step smartly. Your response must meet below requirements: * Walk through the provided information, don't run query if there is already related information; -* DO NOT generate duplicated sql statements; -* The return field alias should be meaningful, it can be similar name of reference table column; -* Make sure the SELECT and WHERE fields are in corresponding table schema definition; +* Make sure you have the corresponding table columns information before generating SQL; +* The return field alias should be meaningful, you can use the combination of column and value as the alias name; * Use "Unique Index" to help check record existence; -* For INSERT statement with mutliple records, should return in different meaningful alias; diff --git a/src/Plugins/BotSharp.Plugin.SqlDriver/data/agents/beda4c12-e1ec-4b4b-b328-3df4a6687c4f/templates/lookup_dictionary.liquid b/src/Plugins/BotSharp.Plugin.SqlDriver/data/agents/beda4c12-e1ec-4b4b-b328-3df4a6687c4f/templates/lookup_dictionary.liquid new file mode 100644 index 00000000..cf5b190a --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.SqlDriver/data/agents/beda4c12-e1ec-4b4b-b328-3df4a6687c4f/templates/lookup_dictionary.liquid @@ -0,0 +1,9 @@ +DICTIONARY: + +{% for item in items %} +* {{ item }} +{% endfor %} + +===== +Which item is the best matching with "{{ keyword }}"? +You must return Id and Name field. \ No newline at end of file