diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/Enums/BuiltInAgentId.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/Enums/BuiltInAgentId.cs index 2c95818f..55436ae2 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/Enums/BuiltInAgentId.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/Enums/BuiltInAgentId.cs @@ -36,4 +36,6 @@ public class BuiltInAgentId /// Plan feasible implementation steps for complex problems /// public const string Planner = "282a7128-69a1-44b0-878c-a9159b88f3b9"; + + public const string SqlDriver = "beda4c12-e1ec-4b4b-b328-3df4a6687c4f"; } diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs index 13e8edf1..610c1ab4 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs @@ -74,6 +74,12 @@ public partial class ConversationService // Routing with reasoning var settings = _services.GetRequiredService(); + // reload agent in case it has been changed by hook + if (message.CurrentAgentId != agent.Id) + { + agent = await agentService.LoadAgent(message.CurrentAgentId); + } + if (agent.Type == AgentType.Routing) { response = await routing.InstructLoop(message, dialogs); diff --git a/src/Plugins/BotSharp.Plugin.SqlDriver/BotSharp.Plugin.SqlDriver.csproj b/src/Plugins/BotSharp.Plugin.SqlDriver/BotSharp.Plugin.SqlDriver.csproj index 02bca766..4f93597a 100644 --- a/src/Plugins/BotSharp.Plugin.SqlDriver/BotSharp.Plugin.SqlDriver.csproj +++ b/src/Plugins/BotSharp.Plugin.SqlDriver/BotSharp.Plugin.SqlDriver.csproj @@ -11,11 +11,8 @@ - - - @@ -32,6 +29,7 @@ + @@ -71,6 +69,9 @@ PreserveNewest + + PreserveNewest + diff --git a/src/Plugins/BotSharp.Plugin.SqlDriver/Functions/ExecuteQueryFn.cs b/src/Plugins/BotSharp.Plugin.SqlDriver/Functions/ExecuteQueryFn.cs index d8ae5097..165b2211 100644 --- a/src/Plugins/BotSharp.Plugin.SqlDriver/Functions/ExecuteQueryFn.cs +++ b/src/Plugins/BotSharp.Plugin.SqlDriver/Functions/ExecuteQueryFn.cs @@ -1,3 +1,5 @@ +using BotSharp.Abstraction.Agents.Enums; +using BotSharp.Core.Infrastructures; using BotSharp.Plugin.SqlDriver.Models; using Dapper; using Microsoft.Data.SqlClient; @@ -30,6 +32,29 @@ public class ExecuteQueryFn : IFunctionCallback }; message.Content = JsonSerializer.Serialize(results); + + if (args.FormattingResult) + { + var conv = _services.GetRequiredService(); + var sqlAgent = await _services.GetRequiredService().LoadAgent(BuiltInAgentId.SqlDriver); + var prompt = sqlAgent.Templates.FirstOrDefault(x => x.Name == "query_result_formatting"); + + var completion = CompletionProvider.GetChatCompletion(_services, + provider: sqlAgent.LlmConfig.Provider, + model: sqlAgent.LlmConfig.Model); + + var result = await completion.GetChatCompletions(new Agent + { + Id = sqlAgent.Id, + Instruction = prompt.Content, + }, new List + { + new RoleDialogModel(AgentRole.User, message.Content) + }); + + message.Content = result.Content; + } + return true; } diff --git a/src/Plugins/BotSharp.Plugin.SqlDriver/Models/ExecuteQueryArgs.cs b/src/Plugins/BotSharp.Plugin.SqlDriver/Models/ExecuteQueryArgs.cs index b1531e64..ac2279ea 100644 --- a/src/Plugins/BotSharp.Plugin.SqlDriver/Models/ExecuteQueryArgs.cs +++ b/src/Plugins/BotSharp.Plugin.SqlDriver/Models/ExecuteQueryArgs.cs @@ -6,4 +6,9 @@ public class ExecuteQueryArgs { [JsonPropertyName("sql_statements")] public string[] SqlStatements { get; set; } = []; + + /// + /// Beautifying query result + /// + public bool FormattingResult { get; set; } } diff --git a/src/Plugins/BotSharp.Plugin.SqlDriver/data/agents/beda4c12-e1ec-4b4b-b328-3df4a6687c4f/templates/query_result_formatting.liquid b/src/Plugins/BotSharp.Plugin.SqlDriver/data/agents/beda4c12-e1ec-4b4b-b328-3df4a6687c4f/templates/query_result_formatting.liquid new file mode 100644 index 00000000..0ef43495 --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.SqlDriver/data/agents/beda4c12-e1ec-4b4b-b328-3df4a6687c4f/templates/query_result_formatting.liquid @@ -0,0 +1 @@ +Output in human readable format. \ No newline at end of file diff --git a/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioVoiceController.cs b/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioVoiceController.cs index d84ea5a1..d9ff3402 100644 --- a/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioVoiceController.cs +++ b/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioVoiceController.cs @@ -48,7 +48,7 @@ public class TwilioVoiceController : TwilioController [ValidateRequest] [HttpPost("twilio/voice/{conversationId}/receive/{seqNum}")] - public async Task ReceiveCallerMessage([FromRoute] string conversationId, [FromRoute] int seqNum, [FromQuery] string states, [FromQuery] int attempts, VoiceRequest request) + public async Task ReceiveCallerMessage([FromRoute] string conversationId, [FromRoute] int seqNum, [FromQuery] string states, VoiceRequest request, [FromQuery] int attempts = 1) { var twilio = _services.GetRequiredService(); var messageQueue = _services.GetRequiredService(); diff --git a/src/Plugins/BotSharp.Plugin.Twilio/Services/TwilioMessageQueueService.cs b/src/Plugins/BotSharp.Plugin.Twilio/Services/TwilioMessageQueueService.cs index 7c5a8831..e3476ea2 100644 --- a/src/Plugins/BotSharp.Plugin.Twilio/Services/TwilioMessageQueueService.cs +++ b/src/Plugins/BotSharp.Plugin.Twilio/Services/TwilioMessageQueueService.cs @@ -120,6 +120,8 @@ namespace BotSharp.Plugin.Twilio.Services break; } } + // add frequency short words + hints.AddRange(["yes", "no", "correct", "right"]); reply.Hints = string.Join(", ", hints.Select(x => x.ToLower()).Distinct().Reverse()); reply.Content = null; await sessionManager.SetAssistantReplyAsync(message.ConversationId, message.SeqNumber, reply);