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 } }); } }