Skip to content

Commit a135387

Browse files
author
Haiping Chen
committed
Improve SQL Driver.
1 parent e043476 commit a135387

File tree

10 files changed

+66
-37
lines changed

10 files changed

+66
-37
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ public GetTableColumnsFn(IServiceProvider services)
1717
public async Task<bool> Execute(RoleDialogModel message)
1818
{
1919
var args = JsonSerializer.Deserialize<GetTableColumnsArgs>(message.FunctionArgs);
20-
message.Content = $"Success. Columns of table '{args.Table}':\r\n\r\n";
20+
message.Content = "";
2121

2222
var dbSettings = _services.GetRequiredService<BotSharpDatabaseSettings>();
23-
var dir = Path.Combine(dbSettings.FileRepository, "agents", "ec46f15b-8790-400f-a37f-1e7995b7d6e2", "schemas");
23+
var dir = Path.Combine(dbSettings.FileRepository, "agents", "beda4c12-e1ec-4b4b-b328-3df4a6687c4f", "schemas");
2424

2525
// Search related document by message.Content + args.Description
2626
var files = Directory.GetFiles(dir);

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,6 @@ public async Task<bool> Execute(RoleDialogModel message)
1616
{
1717
var args = JsonSerializer.Deserialize<SqlStatement>(message.FunctionArgs);
1818
var sqlDriver = _services.GetRequiredService<SqlDriverService>();
19-
if (sqlDriver.Statements.Exists(x => x.Statement == args.Statement))
20-
{
21-
var p1 = string.Join(", ", sqlDriver.Statements.Last().Parameters.OrderBy(x => x.Name).Select(x => x.Value));
22-
var p2 = string.Join(", ", args.Parameters.OrderBy(x => x.Name).Select(x => x.Value));
23-
if (p1 == p2)
24-
{
25-
message.Content = "Skipped duplicated statement.";
26-
return false;
27-
}
28-
}
2919
sqlDriver.Enqueue(args);
3020
message.Content = $"Inserted new record successfully.";
3121
if (args.Return != null)

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,24 @@ public async Task<bool> Execute(RoleDialogModel message)
3030
{
3131
dictionary["@" + p.Name] = p.Value;
3232
}
33-
var result = connection.QueryFirst<string>(args.Statement, dictionary);
33+
var result = connection.QueryFirstOrDefault(args.Statement, dictionary);
3434

35+
if (result == null)
36+
{
37+
message.Content = "Record not found";
38+
}
39+
else
40+
{
41+
message.Content = JsonSerializer.Serialize(result);
42+
args.Return.Value = message.Content;
43+
}
44+
3545
sqlDriver.Enqueue(args);
36-
message.Content = $"Retrieved data is: {result}";
3746
}
3847
else
3948
{
4049
sqlDriver.Enqueue(args);
41-
message.Content = $"Success.";
50+
message.Content = $"The {args.Return.Name} is saved to @{args.Return.Alias}";
4251
}
4352

4453
return true;

src/Plugins/BotSharp.Plugin.SqlDriver/Models/SqlParamater.cs renamed to src/Plugins/BotSharp.Plugin.SqlDriver/Models/SqlParameter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace BotSharp.Plugin.SqlDriver.Models;
44

5-
public class SqlParamater
5+
public class SqlParameter
66
{
77
[JsonPropertyName("name")]
88
public string Name { get; set; }

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ public class SqlReturn
1010
[JsonPropertyName("alias")]
1111
public string Alias { get; set; }
1212

13+
public string? Value { get; set; }
14+
1315
public override string ToString()
1416
{
1517
return $"{Alias} - {Name}";

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class SqlStatement
1414
public string Table { get; set; }
1515

1616
[JsonPropertyName("parameters")]
17-
public SqlParamater[] Parameters { get; set; } = new SqlParamater[0];
17+
public SqlParameter[] Parameters { get; set; } = new SqlParameter[0];
1818

1919
[JsonPropertyName("return_field")]
2020
public SqlReturn Return { get; set; }

src/Plugins/BotSharp.Plugin.SqlDriver/Services/SqlDriverService.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,24 @@ public void Enqueue(SqlStatement statement)
2323
{
2424
Console.WriteLine();
2525

26-
Console.Write($"Reason: ");
27-
Console.WriteLine($"{sql.Reason}", Color.Green);
26+
Console.WriteLine($"{sql.Reason}");
2827

29-
Console.Write($"Statement: ");
30-
Console.WriteLine(sql.Statement, Color.Green);
28+
Console.WriteLine(sql.Statement, Color.Yellow);
3129
foreach (var p in sql.Parameters)
3230
{
33-
Console.Write($"@{p.Name}: ");
34-
Console.WriteLine($"{p.Value}", Color.Green);
31+
Console.WriteLine($"@{p.Name} = '{p.Value}'", Color.Green);
3532
}
3633
if (sql.Return != null)
3734
{
3835
Console.Write($"Return: ");
39-
Console.WriteLine($"{sql.Return.Name} as @{sql.Return.Alias}", Color.Green);
36+
if (!string.IsNullOrEmpty(sql.Return.Value))
37+
{
38+
Console.WriteLine($" {sql.Return.Value}", Color.Red);
39+
}
40+
else
41+
{
42+
Console.WriteLine($"{sql.Return.Name} as @{sql.Return.Alias}", Color.Green);
43+
}
4044
}
4145
}
4246
}

src/Plugins/BotSharp.Plugin.SqlDriver/data/agents/beda4c12-e1ec-4b4b-b328-3df4a6687c4f/agent.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
"profiles": [ "tool", "sql" ],
1111
"llmConfig": {
1212
"model": "gpt-4-0125",
13-
"max_recursion_depth": 5
13+
"max_recursion_depth": 10
1414
}
1515
}

src/Plugins/BotSharp.Plugin.SqlDriver/data/agents/beda4c12-e1ec-4b4b-b328-3df4a6687c4f/functions.json

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
11
[
2+
{
3+
"name": "get_table_columns",
4+
"description": "Get related table columns and foreign key informations",
5+
"parameters": {
6+
"type": "object",
7+
"properties": {
8+
"table": {
9+
"type": "string",
10+
"description": "table name"
11+
}
12+
},
13+
"required": [ "table" ]
14+
}
15+
},
216
{
317
"name": "sql_insert",
418
"description": "Insert query is generated if the record doesn't exist.",
@@ -19,10 +33,10 @@
1933
},
2034
"parameters": {
2135
"type": "array",
22-
"description": "parameters for the sql",
36+
"description": "a list of parameters in the statement match with the variables",
2337
"items": {
2438
"type": "object",
25-
"description": "the name and value for the parameter",
39+
"description": "{name:'', value:''}",
2640
"properties": {
2741
"name": {
2842
"type": "string",
@@ -32,7 +46,8 @@
3246
"type": "string",
3347
"description": "real value inferred by the context"
3448
}
35-
}
49+
},
50+
"required": [ "name", "value" ]
3651
}
3752
},
3853
"return_field": {
@@ -47,7 +62,8 @@
4762
"type": "string",
4863
"description": "meaningful field alias"
4964
}
50-
}
65+
},
66+
"required": [ "name", "alias" ]
5167
}
5268
},
5369
"required": [ "sql_statement", "reason", "table", "parameters", "return_field" ]
@@ -86,7 +102,8 @@
86102
"type": "string",
87103
"description": "real value inferred by the context"
88104
}
89-
}
105+
},
106+
"required": [ "name", "value" ]
90107
}
91108
},
92109
"return_field": {
@@ -101,7 +118,8 @@
101118
"type": "string",
102119
"description": "meaningful field alias"
103120
}
104-
}
121+
},
122+
"required": [ "name", "value" ]
105123
}
106124
},
107125
"required": [ "sql_statement", "reason", "table", "parameters", "return_field" ]
Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
You're a SQL driver who knows how to translate text into SQL query.
2-
Think step by step, analyze the user requirement and provided information, output the next step.
2+
Think step by step, analyze the user requirement, you must get table schema first, breakdown into multiple sql statements if user need to insert mulitple records.
3+
Output the next step smartly.
34

45
Your response must meet below requirements:
56
* Walk through the provided information, don't run query if there is already related information;
67
* DO NOT generate duplicated sql statements;
78
* The return field alias should be meaningful, it can be similar name of reference table column;
89
* Make sure the SELECT and WHERE fields are in corresponding table schema definition;
910
* Use "Unique Index" to help check record existence;
11+
* For INSERT statement with mutliple records, should return in different meaningful alias;
1012

11-
{% if tables_definition -%}
12-
=====
13-
Related tables definition:
14-
{{ tables_definition }}
15-
{%- endif %}
13+
==========
14+
Domain Knowledge:
15+
16+
user: How to add client response window (priority)?
17+
assistant: 1. Get client Id by name;
18+
2. Get ClientServiceCodeId from client_ServiceCode by ClientId and ServiceCodeId
19+
3. Convert priority to standard value by match the list [2H, 4H, 1D , 2D, 7D], 1D = 24H, 2D = 48H.
20+
4. Get the Id as PriorityId from data_Priority by standard value;
21+
5. Insert PriorityId and ClientServiceCodeId into Client_ServiceCodePriority

0 commit comments

Comments
 (0)