Skip to content

Commit 8e0eb63

Browse files
authored
Merge pull request #662 from hchen2020/master
sql output formatting.
2 parents 62e1ce5 + 829e440 commit 8e0eb63

File tree

8 files changed

+46
-4
lines changed

8 files changed

+46
-4
lines changed

src/Infrastructure/BotSharp.Abstraction/Agents/Enums/BuiltInAgentId.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,6 @@ public class BuiltInAgentId
3636
/// Plan feasible implementation steps for complex problems
3737
/// </summary>
3838
public const string Planner = "282a7128-69a1-44b0-878c-a9159b88f3b9";
39+
40+
public const string SqlDriver = "beda4c12-e1ec-4b4b-b328-3df4a6687c4f";
3941
}

src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ public async Task<bool> SendMessage(string agentId,
7474
// Routing with reasoning
7575
var settings = _services.GetRequiredService<RoutingSettings>();
7676

77+
// reload agent in case it has been changed by hook
78+
if (message.CurrentAgentId != agent.Id)
79+
{
80+
agent = await agentService.LoadAgent(message.CurrentAgentId);
81+
}
82+
7783
if (agent.Type == AgentType.Routing)
7884
{
7985
response = await routing.InstructLoop(message, dialogs);

src/Plugins/BotSharp.Plugin.SqlDriver/BotSharp.Plugin.SqlDriver.csproj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,8 @@
1111
</PropertyGroup>
1212

1313
<ItemGroup>
14-
<Compile Remove="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\templates\**" />
1514
<Compile Remove="packages\**" />
16-
<EmbeddedResource Remove="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\templates\**" />
1715
<EmbeddedResource Remove="packages\**" />
18-
<None Remove="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\templates\**" />
1916
<None Remove="packages\**" />
2017
</ItemGroup>
2118

@@ -32,6 +29,7 @@
3229
<None Remove="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\functions\sql_insert.json" />
3330
<None Remove="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\functions\sql_select.json" />
3431
<None Remove="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\instructions\instruction.liquid" />
32+
<None Remove="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\templates\query_result_formatting.liquid" />
3533
</ItemGroup>
3634

3735
<ItemGroup>
@@ -71,6 +69,9 @@
7169
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\sql_executor.fn.liquid">
7270
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
7371
</Content>
72+
<Content Include="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\templates\query_result_formatting.liquid">
73+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
74+
</Content>
7475
</ItemGroup>
7576

7677
<ItemGroup>

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using BotSharp.Abstraction.Agents.Enums;
2+
using BotSharp.Core.Infrastructures;
13
using BotSharp.Plugin.SqlDriver.Models;
24
using Dapper;
35
using Microsoft.Data.SqlClient;
@@ -30,6 +32,29 @@ public async Task<bool> Execute(RoleDialogModel message)
3032
};
3133

3234
message.Content = JsonSerializer.Serialize(results);
35+
36+
if (args.FormattingResult)
37+
{
38+
var conv = _services.GetRequiredService<IConversationService>();
39+
var sqlAgent = await _services.GetRequiredService<IAgentService>().LoadAgent(BuiltInAgentId.SqlDriver);
40+
var prompt = sqlAgent.Templates.FirstOrDefault(x => x.Name == "query_result_formatting");
41+
42+
var completion = CompletionProvider.GetChatCompletion(_services,
43+
provider: sqlAgent.LlmConfig.Provider,
44+
model: sqlAgent.LlmConfig.Model);
45+
46+
var result = await completion.GetChatCompletions(new Agent
47+
{
48+
Id = sqlAgent.Id,
49+
Instruction = prompt.Content,
50+
}, new List<RoleDialogModel>
51+
{
52+
new RoleDialogModel(AgentRole.User, message.Content)
53+
});
54+
55+
message.Content = result.Content;
56+
}
57+
3358
return true;
3459
}
3560

src/Plugins/BotSharp.Plugin.SqlDriver/Models/ExecuteQueryArgs.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,9 @@ public class ExecuteQueryArgs
66
{
77
[JsonPropertyName("sql_statements")]
88
public string[] SqlStatements { get; set; } = [];
9+
10+
/// <summary>
11+
/// Beautifying query result
12+
/// </summary>
13+
public bool FormattingResult { get; set; }
914
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Output in human readable format.

src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioVoiceController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public TwiMLResult InitiateConversation(VoiceRequest request, [FromQuery] string
4848

4949
[ValidateRequest]
5050
[HttpPost("twilio/voice/{conversationId}/receive/{seqNum}")]
51-
public async Task<TwiMLResult> ReceiveCallerMessage([FromRoute] string conversationId, [FromRoute] int seqNum, [FromQuery] string states, [FromQuery] int attempts, VoiceRequest request)
51+
public async Task<TwiMLResult> ReceiveCallerMessage([FromRoute] string conversationId, [FromRoute] int seqNum, [FromQuery] string states, VoiceRequest request, [FromQuery] int attempts = 1)
5252
{
5353
var twilio = _services.GetRequiredService<TwilioService>();
5454
var messageQueue = _services.GetRequiredService<TwilioMessageQueue>();

src/Plugins/BotSharp.Plugin.Twilio/Services/TwilioMessageQueueService.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ private async Task ProcessUserMessageAsync(CallerMessage message)
120120
break;
121121
}
122122
}
123+
// add frequency short words
124+
hints.AddRange(["yes", "no", "correct", "right"]);
123125
reply.Hints = string.Join(", ", hints.Select(x => x.ToLower()).Distinct().Reverse());
124126
reply.Content = null;
125127
await sessionManager.SetAssistantReplyAsync(message.ConversationId, message.SeqNumber, reply);

0 commit comments

Comments
 (0)