Skip to content

Commit ed03df0

Browse files
authored
Merge pull request #753 from Joannall/master
update sql validator
2 parents a6ffbc3 + 53b43ea commit ed03df0

File tree

10 files changed

+37
-16
lines changed

10 files changed

+37
-16
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ private async Task<RoleDialogModel> GetAiResponse(Agent plannerAgent)
9595
var conv = _services.GetRequiredService<IConversationService>();
9696
var wholeDialogs = conv.GetDialogHistory();
9797

98+
wholeDialogs.Last().Content += "\r\nOutput in JSON format.";
99+
98100
var completion = CompletionProvider.GetChatCompletion(_services,
99101
provider: plannerAgent.LlmConfig.Provider,
100102
model: plannerAgent.LlmConfig.Model);

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,12 @@ public async Task<bool> Execute(RoleDialogModel message)
7272
message.Content = summary.Content;
7373

7474
// Validate the sql result
75-
await fn.InvokeFunction("validate_sql", message);
76-
75+
var args = JsonSerializer.Deserialize<SummaryPlan>(message.FunctionArgs);
76+
if (args.IsSqlTemplate == false)
77+
{
78+
await fn.InvokeFunction("validate_sql", message);
79+
}
80+
7781
await HookEmitter.Emit<IPlanningHook>(_services, async hook =>
7882
await hook.OnPlanningCompleted(nameof(TwoStageTaskPlanner), message)
7983
);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace BotSharp.Plugin.Planner.TwoStaging.Models;
2+
3+
public class SummaryPlan
4+
{
5+
[JsonPropertyName("is_sql_template")]
6+
public bool IsSqlTemplate { get; set; } = false;
7+
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
"parameters": {
55
"type": "object",
66
"properties": {
7+
"is_sql_template": {
8+
"type": "boolean",
9+
"description": "If user request is to generate sql template instead of actual sql statement."
10+
},
711
"related_tables": {
812
"type": "array",
913
"description": "table name in planning steps",
@@ -13,6 +17,6 @@
1317
}
1418
}
1519
},
16-
"required": [ "related_tables" ]
20+
"required": [ "related_tables", "is_sql_template" ]
1721
}
1822
}

src/Plugins/BotSharp.Plugin.Planner/data/agents/282a7128-69a1-44b0-878c-a9159b88f3b9/templates/two_stage.summarize.liquid

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
You are a planning summarizer. You will generate the final output in JSON format based on the task description, knowledge and related table structure and relationship.
1+
You are a planning summarizer. You will generate the final output in JSON format with short explanation based on the task description, knowledge and related table structure and relationship.
22

33
Requirements:
44
{{ summary_requirements }}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,21 @@ public ExecuteQueryFn(IServiceProvider services, SqlDriverSetting setting, ILogg
3030
public async Task<bool> Execute(RoleDialogModel message)
3131
{
3232
var args = JsonSerializer.Deserialize<ExecuteQueryArgs>(message.FunctionArgs);
33-
var refinedArgs = await RefineSqlStatement(message, args);
33+
//var refinedArgs = await RefineSqlStatement(message, args);
3434
var dbHook = _services.GetRequiredService<ISqlDriverHook>();
3535
var dbType = dbHook.GetDatabaseType(message);
3636

3737
try
3838
{
3939
var results = dbType.ToLower() switch
4040
{
41-
"mysql" => RunQueryInMySql(refinedArgs.SqlStatements),
42-
"sqlserver" => RunQueryInSqlServer(refinedArgs.SqlStatements),
43-
"redshift" => RunQueryInRedshift(refinedArgs.SqlStatements),
41+
"mysql" => RunQueryInMySql(args.SqlStatements),
42+
"sqlserver" => RunQueryInSqlServer(args.SqlStatements),
43+
"redshift" => RunQueryInRedshift(args.SqlStatements),
4444
_ => throw new NotImplementedException($"Database type {dbType} is not supported.")
4545
};
4646

47-
if (refinedArgs.SqlStatements.Length == 1 && refinedArgs.SqlStatements[0].StartsWith("DROP TABLE"))
47+
if (args.SqlStatements.Length == 1 && args.SqlStatements[0].StartsWith("DROP TABLE"))
4848
{
4949
message.Content = "Drop table successfully";
5050
return true;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ public async Task<bool> Execute(RoleDialogModel message)
4141
var dbType = dbHook.GetDatabaseType(message);
4242
var validateSql = dbType.ToLower() switch
4343
{
44-
"mysql" => $"explain\r\n{sql}",
44+
"mysql" => $"explain\r\n{sql.Replace("SET ", "-- SET ", StringComparison.InvariantCultureIgnoreCase).Replace(";", "; explain ").TrimEnd("explain ".ToCharArray())}",
4545
"sqlserver" => $"SET PARSEONLY ON;\r\n{sql}\r\nSET PARSEONLY OFF;",
4646
"redshift" => $"explain\r\n{sql}",
4747
_ => throw new NotImplementedException($"Database type {dbType} is not supported.")
4848
};
4949
var msgCopy = RoleDialogModel.From(message);
5050
msgCopy.FunctionArgs = JsonSerializer.Serialize(new ExecuteQueryArgs
5151
{
52-
SqlStatements = new string[] { validateSql }
52+
SqlStatements = [validateSql]
5353
});
5454

5555
var fn = _services.GetRequiredService<IRoutingService>();
@@ -74,7 +74,7 @@ public async Task<bool> Execute(RoleDialogModel message)
7474
Message = "Correct SQL Statement",
7575
Data = new Dictionary<string, object>
7676
{
77-
{ "original_sql", sql },
77+
{ "original_sql", message.Content },
7878
{ "error_message", ex.Message },
7979
{ "table_structure", ddl }
8080
}

src/Plugins/BotSharp.Plugin.SqlDriver/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/functions/verify_dictionary_term.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "verify_dictionary_term",
3-
"description": "Get id from dictionary table by keyword. Call this function only if need_lookup_dictionary is true and is_insert is false. You can only query one table at a time.",
3+
"description": "Get id from dictionary table by keyword. Call this function only if need_lookup_dictionary is true, is_table_from_knowledge is true and is_insert is false. You can only query one table at a time. The table name must come from the global/domain knowledge.",
44
"parameters": {
55
"type": "object",
66
"properties": {
@@ -16,9 +16,13 @@
1616
"type": "boolean",
1717
"description": "if SQL statement is inserting."
1818
},
19+
"is_table_from_knowledge": {
20+
"type": "boolean",
21+
"description": "if table is from the global/domain knowledge."
22+
},
1923
"tables": {
2024
"type": "array",
21-
"description": "all related dictionary tables must be from related knowledge in the context",
25+
"description": "all related dictionary tables must be from global/domain knowledge in the context",
2226
"items": {
2327
"type": "string",
2428
"description": "table name from related knowledge in the context"

src/Plugins/BotSharp.Plugin.SqlDriver/data/agents/beda4c12-e1ec-4b4b-b328-3df4a6687c4f/templates/database.summarize.mysql.liquid

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ If not, generate the query step by step based on the planning.
44

55
The query must exactly based on the provided table structure. And carefully review the foreign keys to make sure you include all the accurate information.
66

7-
Note: Output should be only the sql query with sql comments that can be directly run in mysql database with version 8.0.
7+
Note: Output should be only the sql query with short sql comments and explanation that can be directly run in mysql database with version 8.0.
88

99
Don't use the sql statement that specify target table for update in FROM clause.
1010
For example, you CAN'T write query as below:

src/Plugins/BotSharp.Plugin.SqlDriver/data/agents/beda4c12-e1ec-4b4b-b328-3df4a6687c4f/templates/sql_statement_correctness.liquid

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
You are a sql statement corrector. You will need to refer to the table structure and rewrite the original sql statement so it's using the correct information, e.g. column name.
2-
Output the sql statement only without comments, in JSON format: {{ response_format }}
2+
Correct the sql statement and keep only the original explanation and comments without any information related to error message{% if response_format %} in JSON format: {{ response_format }} {% endif %}.
33
Make sure all the column names are defined in the Table Structure.
44

55
=====

0 commit comments

Comments
 (0)