BotSharp/src/Plugins/BotSharp.Plugin.SqlDriver/Functions/LookupDictionaryFn.cs

98 lines
3.8 KiB
C#
Raw Normal View History

2024-09-24 15:45:57 +00:00
using Azure;
2024-02-22 19:37:14 +00:00
using BotSharp.Abstraction.Agents.Enums;
using BotSharp.Abstraction.MLTasks;
2024-09-30 20:14:37 +00:00
using BotSharp.Abstraction.Routing;
using BotSharp.Core.Agents.Services;
2024-02-22 19:37:14 +00:00
using BotSharp.Core.Infrastructures;
using BotSharp.Plugin.SqlDriver.Models;
using MySqlConnector;
2024-09-30 20:14:37 +00:00
using System.Text.RegularExpressions;
2024-02-22 19:37:14 +00:00
using static Dapper.SqlMapper;
2024-09-30 20:14:37 +00:00
using static System.Net.Mime.MediaTypeNames;
2024-02-22 19:37:14 +00:00
namespace BotSharp.Plugin.SqlDriver.Functions;
public class LookupDictionaryFn : IFunctionCallback
{
2024-09-24 15:45:57 +00:00
public string Name => "sql_dictionary_lookup";
2024-02-22 19:37:14 +00:00
private readonly IServiceProvider _services;
public LookupDictionaryFn(IServiceProvider services)
{
_services = services;
}
public async Task<bool> Execute(RoleDialogModel message)
{
var args = JsonSerializer.Deserialize<LookupDictionary>(message.FunctionArgs);
2024-09-30 20:14:37 +00:00
// get table DDL
var fn = _services.GetRequiredService<IRoutingService>();
var msgCopy = RoleDialogModel.From(message);
await fn.InvokeFunction("sql_table_definition", msgCopy);
// refine SQL
var agentService = _services.GetRequiredService<IAgentService>();
var currentAgent = await agentService.LoadAgent(message.CurrentAgentId);
var dictionarySqlPrompt = await GetDictionarySQLPrompt(args.SqlStatement, msgCopy.Content);
var plannerAgent = new Agent
{
Id = string.Empty,
Name = "sqlDriver_DictionarySearch",
Instruction = dictionarySqlPrompt,
TemplateDict = new Dictionary<string, object>(),
LlmConfig = currentAgent.LlmConfig
};
var response = await GetAiResponse(plannerAgent);
args = JsonSerializer.Deserialize<LookupDictionary>(response.Content);
2024-09-24 15:45:57 +00:00
// check if need to instantely
2024-02-22 19:37:14 +00:00
var settings = _services.GetRequiredService<SqlDriverSetting>();
2024-09-24 15:45:57 +00:00
using var connection = new MySqlConnection(settings.MySqlExecutionConnectionString);
var result = connection.Query(args.SqlStatement);
2024-02-22 19:37:14 +00:00
2024-09-24 15:45:57 +00:00
if (result == null)
2024-02-22 19:37:14 +00:00
{
2024-09-24 15:45:57 +00:00
message.Content = "Record not found";
}
else
2024-02-22 19:37:14 +00:00
{
2024-09-24 15:45:57 +00:00
message.Content = JsonSerializer.Serialize(result);
}
var states = _services.GetRequiredService<IConversationStateService>();
var dictionaryItems = states.GetState("dictionary_items", "");
2024-09-30 20:14:37 +00:00
dictionaryItems += "\r\n\r\n" + args.Table + ":\r\n" + args.Reason + ":\r\n" + message.Content + "\r\n";
2024-09-24 15:45:57 +00:00
states.SetState("dictionary_items", dictionaryItems);
2024-02-22 19:37:14 +00:00
return true;
}
2024-09-30 20:14:37 +00:00
private async Task<string> GetDictionarySQLPrompt(string originalSql, string tableStructure)
{
var agentService = _services.GetRequiredService<IAgentService>();
var render = _services.GetRequiredService<ITemplateRender>();
var knowledgeHooks = _services.GetServices<IKnowledgeHook>();
var agent = await agentService.GetAgent(BuiltInAgentId.Planner);
var template = agent.Templates.FirstOrDefault(x => x.Name == "database.dictionary.sql")?.Content ?? string.Empty;
var responseFormat = JsonSerializer.Serialize(new LookupDictionary{ });
return render.Render(template, new Dictionary<string, object>
{
{ "original_sql", originalSql },
{ "table_structure", tableStructure },
{ "response_format", responseFormat }
});
}
private async Task<RoleDialogModel> GetAiResponse(Agent plannerAgent)
{
var text = "Check and correct the SQL statement.";
var message = new RoleDialogModel(AgentRole.User, text);
var completion = CompletionProvider.GetChatCompletion(_services,
provider: plannerAgent.LlmConfig.Provider,
model: plannerAgent.LlmConfig.Model);
return await completion.GetChatCompletions(plannerAgent, new List<RoleDialogModel> { message });
}
2024-02-22 19:37:14 +00:00
}