Skip to content

Commit 89205bd

Browse files
committed
Sync with upstream
2 parents cc44845 + 8e0eb63 commit 89205bd

File tree

21 files changed

+136
-74
lines changed

21 files changed

+136
-74
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.Planner/Functions/SecondaryStagePlanFn.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,14 @@ public SecondaryStagePlanFn(IServiceProvider services, ILogger<SecondaryStagePla
1717

1818
public async Task<bool> Execute(RoleDialogModel message)
1919
{
20-
var fn = _services.GetRequiredService<IRoutingService>();
2120
var agentService = _services.GetRequiredService<IAgentService>();
2221
var knowledgeService = _services.GetRequiredService<IKnowledgeService>();
2322
var knowledgeSettings = _services.GetRequiredService<KnowledgeBaseSettings>();
2423
var states = _services.GetRequiredService<IConversationStateService>();
2524

2625
var msgSecondary = RoleDialogModel.From(message);
27-
var collectionName = knowledgeSettings.Default.CollectionName ?? KnowledgeCollectionName.BotSharp;
26+
var collectionName = knowledgeSettings.Default.CollectionName;
2827
var planPrimary = states.GetState("planning_result");
29-
var taskPrimary = states.GetState("requirement_detail");
3028

3129
var taskSecondary = JsonSerializer.Deserialize<SecondaryBreakdownTask>(msgSecondary.FunctionArgs);
3230

@@ -35,8 +33,8 @@ public async Task<bool> Execute(RoleDialogModel message)
3533
{
3634
Confidence = 0.6f
3735
});
38-
var knowledgeResults = "";
39-
knowledgeResults = string.Join("\r\n\r\n=====\r\n", knowledges.Select(x => x.ToQuestionAnswer()));
36+
37+
var knowledgeResults = string.Join("\r\n\r\n=====\r\n", knowledges.Select(x => x.ToQuestionAnswer()));
4038

4139
// Get second stage planning prompt
4240
var currentAgent = await agentService.LoadAgent(message.CurrentAgentId);
@@ -45,7 +43,7 @@ public async Task<bool> Execute(RoleDialogModel message)
4543

4644
var plannerAgent = new Agent
4745
{
48-
Id = string.Empty,
46+
Id = BuiltInAgentId.Planner,
4947
Name = "planning_2nd",
5048
Instruction = secondPlanningPrompt,
5149
TemplateDict = new Dictionary<string, object>(),

src/Plugins/BotSharp.Plugin.Planner/Functions/SummaryPlanFn.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public async Task<bool> Execute(RoleDialogModel message)
3333
var states = _services.GetRequiredService<IConversationStateService>();
3434
var steps = states.GetState("planning_result").JsonArrayContent<SecondStagePlan>();
3535
var allTables = new List<string>();
36-
var ddlStatements = "";
36+
var ddlStatements = string.Empty;
3737
var relevantKnowledge = states.GetState("planning_result");
3838
var dictionaryItems = states.GetState("dictionary_items");
3939

@@ -42,6 +42,7 @@ public async Task<bool> Execute(RoleDialogModel message)
4242
allTables.AddRange(step.Tables);
4343
}
4444
var distinctTables = allTables.Distinct().ToList();
45+
4546
foreach (var table in distinctTables)
4647
{
4748
var msgCopy = RoleDialogModel.From(message);
@@ -92,7 +93,7 @@ await HookEmitter.Emit<IPlanningHook>(_services, async x =>
9293
return render.Render(template, new Dictionary<string, object>
9394
{
9495
{ "task_description", taskDescription },
95-
{ "summary_requirements", string.Join("\r\n",additionalRequirements) },
96+
{ "summary_requirements", string.Join("\r\n", additionalRequirements) },
9697
{ "relevant_knowledges", relevantKnowledge },
9798
{ "dictionary_items", dictionaryItems },
9899
{ "table_structure", ddlStatement },

src/Plugins/BotSharp.Plugin.Planner/data/agents/282a7128-69a1-44b0-878c-a9159b88f3b9/agent.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"profiles": [ "planning" ],
1212
"utilities": [ "two-stage-planner" ],
1313
"llmConfig": {
14-
"provider": "azure-openai",
14+
"provider": "openai",
1515
"model": "gpt-4o",
1616
"max_recursion_depth": 10
1717
}

src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelMemoryStoreProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public async Task<IEnumerable<VectorCollectionData>> Search(string collectionNam
7474
{
7575
resultTexts.Add(new VectorCollectionData
7676
{
77-
Data = new Dictionary<string, string> { { "text", record.Metadata.Text } },
77+
Data = new Dictionary<string, object> { { "text", record.Metadata.Text } },
7878
Score = score,
7979
Vector = withVector ? record.Embedding.ToArray() : null
8080
});
@@ -83,7 +83,7 @@ public async Task<IEnumerable<VectorCollectionData>> Search(string collectionNam
8383
return resultTexts;
8484
}
8585

86-
public async Task<bool> Upsert(string collectionName, Guid id, float[] vector, string text, Dictionary<string, string>? payload)
86+
public async Task<bool> Upsert(string collectionName, Guid id, float[] vector, string text, Dictionary<string, object>? payload)
8787
{
8888
#pragma warning disable SKEXP0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
8989
await _memoryStore.UpsertAsync(collectionName, MemoryRecord.LocalRecord(id.ToString(), text, null, vector));

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<None Remove="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\functions\sql_insert.json" />
3030
<None Remove="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\functions\sql_select.json" />
3131
<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" />
3233
</ItemGroup>
3334

3435
<ItemGroup>
@@ -68,6 +69,9 @@
6869
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\sql_executor.fn.liquid">
6970
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
7071
</Content>
72+
<Content Include="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\templates\query_result_formatting.liquid">
73+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
74+
</Content>
7175
</ItemGroup>
7276

7377
<ItemGroup>
@@ -80,8 +84,4 @@
8084
<ProjectReference Include="..\..\Infrastructure\BotSharp.Core\BotSharp.Core.csproj" />
8185
</ItemGroup>
8286

83-
<ItemGroup>
84-
<Folder Include="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\templates\" />
85-
</ItemGroup>
86-
8787
</Project>

src/Plugins/BotSharp.Plugin.SqlDriver/Controllers/SqlDriverController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ public SqlDriverController(IServiceProvider services)
1919
public async Task<bool> ImportDbKnowledge(ImportDbKnowledgeRequest request)
2020
{
2121
var dbKnowledge = _services.GetRequiredService<DbKnowledgeService>();
22-
return await dbKnowledge.Import(request.Provider ?? "openai", request.Model ?? "gpt-4o", request.Schema);
22+
return await dbKnowledge.Import(request);
2323
}
2424
}

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

Lines changed: 27 additions & 5 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;
@@ -28,16 +30,37 @@ public async Task<bool> Execute(RoleDialogModel message)
2830
"SqlServer" => RunQueryInSqlServer(args.SqlStatements),
2931
_ => throw new NotImplementedException($"Database type {settings.DatabaseType} is not supported.")
3032
};
31-
33+
3234
if (results.Count() == 0)
3335
{
3436
message.Content = "No record found";
37+
return true;
3538
}
36-
else
39+
40+
message.Content = JsonSerializer.Serialize(results);
41+
42+
if (args.FormattingResult)
3743
{
38-
message.Content = JsonSerializer.Serialize(results);
44+
var conv = _services.GetRequiredService<IConversationService>();
45+
var sqlAgent = await _services.GetRequiredService<IAgentService>().LoadAgent(BuiltInAgentId.SqlDriver);
46+
var prompt = sqlAgent.Templates.FirstOrDefault(x => x.Name == "query_result_formatting");
47+
48+
var completion = CompletionProvider.GetChatCompletion(_services,
49+
provider: sqlAgent.LlmConfig.Provider,
50+
model: sqlAgent.LlmConfig.Model);
51+
52+
var result = await completion.GetChatCompletions(new Agent
53+
{
54+
Id = sqlAgent.Id,
55+
Instruction = prompt.Content,
56+
}, new List<RoleDialogModel>
57+
{
58+
new RoleDialogModel(AgentRole.User, message.Content)
59+
});
60+
61+
message.Content = result.Content;
3962
}
40-
63+
4164
return true;
4265
}
4366

@@ -52,7 +75,6 @@ private IEnumerable<dynamic> RunQueryInSqlServer(string[] sqlTexts)
5275
{
5376
var settings = _services.GetRequiredService<SqlDriverSetting>();
5477
using var connection = new SqlConnection(settings.SqlServerExecutionConnectionString ?? settings.SqlServerConnectionString);
55-
var dictionary = new Dictionary<string, object>();
5678
return connection.Query(string.Join("\r\n", sqlTexts));
5779
}
5880
}

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

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using BotSharp.Plugin.SqlDriver.Models;
2-
using Fluid.Ast.BinaryExpressions;
32
using Microsoft.Data.SqlClient;
43
using Microsoft.Extensions.Logging;
54
using MySqlConnector;
@@ -37,18 +36,14 @@ public async Task<bool> Execute(RoleDialogModel message)
3736
};
3837

3938
message.Content = string.Join("\r\n\r\n", tableDdls);
40-
41-
//var states = _services.GetRequiredService<IConversationStateService>();
42-
//states.SetState($"table_definition_{args.Table}", message.Content);
43-
4439
return true;
4540
}
4641

4742
private List<string> GetDdlFromMySql(string[] tables)
4843
{
4944
var settings = _services.GetRequiredService<SqlDriverSetting>();
5045
var tableDdls = new List<string>();
51-
using var connection = new MySqlConnection(settings.MySqlExecutionConnectionString);
46+
using var connection = new MySqlConnection(settings.MySqlExecutionConnectionString ?? settings.MySqlConnectionString);
5247
connection.Open();
5348

5449
foreach (var table in tables)
@@ -76,7 +71,6 @@ private List<string> GetDdlFromMySql(string[] tables)
7671
}
7772

7873
connection.Close();
79-
8074
return tableDdls;
8175
}
8276

@@ -92,27 +86,27 @@ private List<string> GetDdlFromSqlServer(string[] tables)
9286
try
9387
{
9488
var sql = @$"DECLARE @TableName NVARCHAR(128) = '{table}';
95-
DECLARE @SQL NVARCHAR(MAX) = 'CREATE TABLE ' + @TableName + ' (';
96-
97-
SELECT @SQL = @SQL + '
98-
' + COLUMN_NAME + ' ' +
99-
DATA_TYPE +
100-
CASE
101-
WHEN CHARACTER_MAXIMUM_LENGTH IS NOT NULL AND DATA_TYPE LIKE '%char%'
102-
THEN '(' + CAST(CHARACTER_MAXIMUM_LENGTH AS VARCHAR(10)) + ')'
103-
WHEN DATA_TYPE IN ('decimal', 'numeric')
104-
THEN '(' + CAST(NUMERIC_PRECISION AS VARCHAR(10)) + ',' + CAST(NUMERIC_SCALE AS VARCHAR(10)) + ')'
105-
ELSE ''
106-
END + ' ' +
107-
CASE WHEN IS_NULLABLE = 'NO' THEN 'NOT NULL' ELSE 'NULL' END + ','
108-
FROM INFORMATION_SCHEMA.COLUMNS
109-
WHERE TABLE_NAME = @TableName
110-
ORDER BY ORDINAL_POSITION;
111-
112-
-- Remove the last comma and add closing parenthesis
113-
SET @SQL = LEFT(@SQL, LEN(@SQL) - 1) + ');';
114-
115-
SELECT @SQL;";
89+
DECLARE @SQL NVARCHAR(MAX) = 'CREATE TABLE ' + @TableName + ' (';
90+
91+
SELECT @SQL = @SQL + '
92+
' + COLUMN_NAME + ' ' +
93+
DATA_TYPE +
94+
CASE
95+
WHEN CHARACTER_MAXIMUM_LENGTH IS NOT NULL AND DATA_TYPE LIKE '%char%'
96+
THEN '(' + CAST(CHARACTER_MAXIMUM_LENGTH AS VARCHAR(10)) + ')'
97+
WHEN DATA_TYPE IN ('decimal', 'numeric')
98+
THEN '(' + CAST(NUMERIC_PRECISION AS VARCHAR(10)) + ',' + CAST(NUMERIC_SCALE AS VARCHAR(10)) + ')'
99+
ELSE ''
100+
END + ' ' +
101+
CASE WHEN IS_NULLABLE = 'NO' THEN 'NOT NULL' ELSE 'NULL' END + ','
102+
FROM INFORMATION_SCHEMA.COLUMNS
103+
WHERE TABLE_NAME = @TableName
104+
ORDER BY ORDINAL_POSITION;
105+
106+
-- Remove the last comma and add closing parenthesis
107+
SET @SQL = LEFT(@SQL, LEN(@SQL) - 1) + ');';
108+
109+
SELECT @SQL;";
116110

117111
using var command = new SqlCommand(sql, connection);
118112
using var reader = command.ExecuteReader();
@@ -129,7 +123,6 @@ FROM INFORMATION_SCHEMA.COLUMNS
129123
}
130124

131125
connection.Close();
132-
133126
return tableDdls;
134127
}
135128
}

0 commit comments

Comments
 (0)