Merge pull request #662 from hchen2020/master

sql output formatting.
This commit is contained in:
Haiping 2024-10-01 16:44:30 -05:00 committed by GitHub
commit 8e0eb63605
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 46 additions and 4 deletions

View file

@ -36,4 +36,6 @@ public class BuiltInAgentId
/// Plan feasible implementation steps for complex problems
/// </summary>
public const string Planner = "282a7128-69a1-44b0-878c-a9159b88f3b9";
public const string SqlDriver = "beda4c12-e1ec-4b4b-b328-3df4a6687c4f";
}

View file

@ -74,6 +74,12 @@ public partial class ConversationService
// Routing with reasoning
var settings = _services.GetRequiredService<RoutingSettings>();
// 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);

View file

@ -11,11 +11,8 @@
</PropertyGroup>
<ItemGroup>
<Compile Remove="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\templates\**" />
<Compile Remove="packages\**" />
<EmbeddedResource Remove="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\templates\**" />
<EmbeddedResource Remove="packages\**" />
<None Remove="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\templates\**" />
<None Remove="packages\**" />
</ItemGroup>
@ -32,6 +29,7 @@
<None Remove="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\functions\sql_insert.json" />
<None Remove="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\functions\sql_select.json" />
<None Remove="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\instructions\instruction.liquid" />
<None Remove="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\templates\query_result_formatting.liquid" />
</ItemGroup>
<ItemGroup>
@ -71,6 +69,9 @@
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\sql_executor.fn.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\templates\query_result_formatting.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>

View file

@ -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<IConversationService>();
var sqlAgent = await _services.GetRequiredService<IAgentService>().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<RoleDialogModel>
{
new RoleDialogModel(AgentRole.User, message.Content)
});
message.Content = result.Content;
}
return true;
}

View file

@ -6,4 +6,9 @@ public class ExecuteQueryArgs
{
[JsonPropertyName("sql_statements")]
public string[] SqlStatements { get; set; } = [];
/// <summary>
/// Beautifying query result
/// </summary>
public bool FormattingResult { get; set; }
}

View file

@ -48,7 +48,7 @@ public class TwilioVoiceController : TwilioController
[ValidateRequest]
[HttpPost("twilio/voice/{conversationId}/receive/{seqNum}")]
public async Task<TwiMLResult> ReceiveCallerMessage([FromRoute] string conversationId, [FromRoute] int seqNum, [FromQuery] string states, [FromQuery] int attempts, VoiceRequest request)
public async Task<TwiMLResult> ReceiveCallerMessage([FromRoute] string conversationId, [FromRoute] int seqNum, [FromQuery] string states, VoiceRequest request, [FromQuery] int attempts = 1)
{
var twilio = _services.GetRequiredService<TwilioService>();
var messageQueue = _services.GetRequiredService<TwilioMessageQueue>();

View file

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