Skip to content

Commit 3328692

Browse files
author
Haiping Chen
committed
Fix issue of duplicate funcation call.
1 parent 5e1f5c0 commit 3328692

File tree

9 files changed

+37
-73
lines changed

9 files changed

+37
-73
lines changed

src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,10 @@ public async Task<bool> GetChatCompletionsStreamingAsync(Agent agent, List<RoleD
231231
{
232232
if (message.Role == ChatRole.Function)
233233
{
234+
chatCompletionsOptions.Messages.Add(new ChatRequestAssistantMessage(string.Empty)
235+
{
236+
FunctionCall = new FunctionCall(message.FunctionName, message.FunctionArgs),
237+
});
234238
chatCompletionsOptions.Messages.Add(new ChatRequestFunctionMessage(message.FunctionName, message.Content));
235239
}
236240
else if (message.Role == ChatRole.User)
@@ -287,7 +291,7 @@ private string GetPrompt(ChatCompletionsOptions chatCompletionsOptions)
287291
if (x.Role == ChatRole.Function)
288292
{
289293
var m = x as ChatRequestFunctionMessage;
290-
return $"{m.Role}: {m.Name} => {m.Content}";
294+
return $"{m.Role}: {m.Content}";
291295
}
292296
else if (x.Role == ChatRole.User)
293297
{
@@ -299,7 +303,9 @@ private string GetPrompt(ChatCompletionsOptions chatCompletionsOptions)
299303
else if (x.Role == ChatRole.Assistant)
300304
{
301305
var m = x as ChatRequestAssistantMessage;
302-
return $"{m.Role}: {m.Content}";
306+
return m.FunctionCall != null ?
307+
$"{m.Role}: Call function {m.FunctionCall.Name}({m.FunctionCall.Arguments})" :
308+
$"{m.Role}: {m.Content}";
303309
}
304310
else
305311
{

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,16 @@ public async Task<bool> Execute(RoleDialogModel message)
1818
var sqlDriver = _services.GetRequiredService<SqlDriverService>();
1919
if (sqlDriver.Statements.Exists(x => x.Statement == args.Statement))
2020
{
21-
message.Content = "Skipped duplicated statement.";
22-
return false;
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+
}
2328
}
2429
sqlDriver.Enqueue(args);
25-
message.Content = $"Inserted new record {JsonSerializer.Serialize(args.Parameters)} successfully";
30+
message.Content = $"Inserted new record successfully.";
2631
if (args.Return != null)
2732
{
2833
/*sqlDriver.Enqueue(new SqlStatement

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

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public SqlSelect(IServiceProvider services)
1717
public async Task<bool> Execute(RoleDialogModel message)
1818
{
1919
var args = JsonSerializer.Deserialize<SqlStatement>(message.FunctionArgs);
20+
var sqlDriver = _services.GetRequiredService<SqlDriverService>();
21+
2022
// check if need to instantely
2123
var execNow = !args.Parameters.Any(x => x.Value.StartsWith("@"));
2224
if (execNow)
@@ -30,20 +32,11 @@ public async Task<bool> Execute(RoleDialogModel message)
3032
}
3133
var result = connection.QueryFirst<string>(args.Statement, dictionary);
3234

33-
if (args.IsCheckExistence)
34-
{
35-
message.Content = result == null ?
36-
$"The record does not exist" :
37-
$"The record already exists";
38-
}
39-
else
40-
{
41-
message.Content = $"Retrieved result is {result} ({args.Reason})";
42-
}
35+
sqlDriver.Enqueue(args);
36+
message.Content = $"Retrieved data is: {result}";
4337
}
4438
else
4539
{
46-
var sqlDriver = _services.GetRequiredService<SqlDriverService>();
4740
sqlDriver.Enqueue(args);
4841
message.Content = $"Success.";
4942
}

src/Plugins/BotSharp.Plugin.SqlDriver/Hooks/SqlDriverContentGeneratingHook.cs

Lines changed: 0 additions & 32 deletions
This file was deleted.

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ public class SqlStatement
1313
[JsonPropertyName("table")]
1414
public string Table { get; set; }
1515

16-
[JsonPropertyName("is_check_existence")]
17-
public bool IsCheckExistence { get; set; }
18-
1916
[JsonPropertyName("parameters")]
2017
public SqlParamater[] Parameters { get; set; } = new SqlParamater[0];
2118

src/Plugins/BotSharp.Plugin.SqlDriver/SqlDriverPlugin.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using BotSharp.Abstraction.Loggers;
2-
31
namespace BotSharp.Plugin.SqlDriver;
42

53
public class SqlDriverPlugin : IBotSharpPlugin
@@ -19,6 +17,5 @@ public void RegisterDI(IServiceCollection services, IConfiguration config)
1917

2018
services.AddScoped<SqlDriverService>();
2119
services.AddScoped<IKnowledgeHook, SqlDriverKnowledgeHook>();
22-
services.AddScoped<IContentGeneratingHook, SqlDriverContentGeneratingHook>();
2320
}
2421
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"isPublic": true,
1010
"profiles": [ "tool", "sql" ],
1111
"llmConfig": {
12-
"max_recursion_depth": 10
12+
"model": "gpt-4-0125",
13+
"max_recursion_depth": 5
1314
}
1415
}

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
"type": "string",
1414
"description": "reason"
1515
},
16+
"table": {
17+
"type": "string",
18+
"description": "related table"
19+
},
1620
"parameters": {
1721
"type": "array",
1822
"description": "parameters for the sql",
@@ -46,7 +50,7 @@
4650
}
4751
}
4852
},
49-
"required": [ "sql_statement", "reason", "parameters", "return_field" ]
53+
"required": [ "sql_statement", "reason", "table", "parameters", "return_field" ]
5054
}
5155
},
5256
{
@@ -63,9 +67,9 @@
6367
"type": "string",
6468
"description": "reason"
6569
},
66-
"is_check_existence": {
67-
"type": "boolean",
68-
"description": "check record existence"
70+
"table": {
71+
"type": "string",
72+
"description": "related table"
6973
},
7074
"parameters": {
7175
"type": "array",
@@ -100,7 +104,7 @@
100104
}
101105
}
102106
},
103-
"required": [ "sql_statement", "reason", "is_check_existence", "parameters", "return_field" ]
107+
"required": [ "sql_statement", "reason", "table", "parameters", "return_field" ]
104108
}
105109
}
106110
]
Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
11
You're a SQL driver who knows how to translate text into SQL query.
2-
Analyze the user requirement, think step by step, breakdown complex task into multiple steps.
2+
Think step by step, analyze the user requirement and provided information, output the next step.
33

44
Your response must meet below requirements:
5+
* Walk through the provided information, don't run query if there is already related information;
56
* DO NOT generate duplicated sql statements;
67
* The return field alias should be meaningful, it can be similar name of reference table column;
7-
* Double check if the fields in the SQL query are correct;
8+
* Make sure the SELECT and WHERE fields are in corresponding table schema definition;
89
* Use "Unique Index" to help check record existence;
910

10-
{% if return_variables and return_variables != empty -%}
11+
{% if tables_definition -%}
1112
=====
12-
Below variables can be used by subsequent SQL:
13-
{% for v in return_variables %}
14-
- @{{ v }}
15-
{% endfor %}
16-
{%- endif %}
17-
18-
{% if table_definition -%}
19-
=====
20-
Related table {{ related_table }} definition:
21-
{{ table_definition }}
13+
Related tables definition:
14+
{{ tables_definition }}
2215
{%- endif %}

0 commit comments

Comments
 (0)