From e73374f563ab288f8f2f2c6fa988184bac47167a Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Tue, 1 Oct 2024 01:18:11 -0500 Subject: [PATCH] refine code --- .../Functions/SummaryPlanFn.cs | 11 ++---- .../Functions/LookupDictionaryFn.cs | 34 +++++++++---------- 2 files changed, 19 insertions(+), 26 deletions(-) diff --git a/src/Plugins/BotSharp.Plugin.Planner/Functions/SummaryPlanFn.cs b/src/Plugins/BotSharp.Plugin.Planner/Functions/SummaryPlanFn.cs index ecd3c78ed..12724c24f 100644 --- a/src/Plugins/BotSharp.Plugin.Planner/Functions/SummaryPlanFn.cs +++ b/src/Plugins/BotSharp.Plugin.Planner/Functions/SummaryPlanFn.cs @@ -36,11 +36,6 @@ public async Task Execute(RoleDialogModel message) var ddlStatements = string.Empty; var relevantKnowledge = states.GetState("planning_result"); var dictionaryItems = states.GetState("dictionary_items"); - var items = new List(); - if (!string.IsNullOrWhiteSpace(dictionaryItems)) - { - items = JsonSerializer.Deserialize>(dictionaryItems); - } foreach (var step in steps) { @@ -60,7 +55,7 @@ public async Task Execute(RoleDialogModel message) } // Summarize and generate query - var summaryPlanPrompt = await GetSummaryPlanPrompt(taskRequirement, relevantKnowledge, items, ddlStatements); + var summaryPlanPrompt = await GetSummaryPlanPrompt(taskRequirement, relevantKnowledge, dictionaryItems, ddlStatements); _logger.LogInformation($"Summary plan prompt:\r\n{summaryPlanPrompt}"); var plannerAgent = new Agent @@ -80,7 +75,7 @@ await HookEmitter.Emit(_services, x => return true; } - private async Task GetSummaryPlanPrompt(string taskDescription, string relevantKnowledge, IEnumerable dictionaryItems, string ddlStatement) + private async Task GetSummaryPlanPrompt(string taskDescription, string relevantKnowledge, string dictionaryItems, string ddlStatement) { var agentService = _services.GetRequiredService(); var render = _services.GetRequiredService(); @@ -100,7 +95,7 @@ await HookEmitter.Emit(_services, async x => { "task_description", taskDescription }, { "summary_requirements", string.Join("\r\n", additionalRequirements) }, { "relevant_knowledges", relevantKnowledge }, - { "dictionary_items", string.Join("\r\n\r\n", dictionaryItems) }, + { "dictionary_items", dictionaryItems }, { "table_structure", ddlStatement }, }); } diff --git a/src/Plugins/BotSharp.Plugin.SqlDriver/Functions/LookupDictionaryFn.cs b/src/Plugins/BotSharp.Plugin.SqlDriver/Functions/LookupDictionaryFn.cs index a27c95def..2c985c9e1 100644 --- a/src/Plugins/BotSharp.Plugin.SqlDriver/Functions/LookupDictionaryFn.cs +++ b/src/Plugins/BotSharp.Plugin.SqlDriver/Functions/LookupDictionaryFn.cs @@ -57,18 +57,10 @@ public async Task Execute(RoleDialogModel message) } var states = _services.GetRequiredService(); - var dictionaryItems = states.GetState("dictionary_items"); + var dictionaryItems = states.GetState("dictionary_items", ""); var newItem = BuildDictionaryItem(args.Table, args.Reason, message.Content); - - var items = new List(); - if (!string.IsNullOrWhiteSpace(dictionaryItems)) - { - items = JsonSerializer.Deserialize>(dictionaryItems); - } - - items.Add(newItem); - //dictionaryItems += "\r\n\r\n" + args.Table + ":\r\n" + args.Reason + ":\r\n" + message.Content + "\r\n"; - states.SetState("dictionary_items", JsonSerializer.Serialize(items)); + dictionaryItems += !string.IsNullOrWhiteSpace(newItem) ? $"\r\n{newItem}\r\n" : string.Empty; + states.SetState("dictionary_items", dictionaryItems); return true; } @@ -105,24 +97,30 @@ private async Task GetAiResponse(Agent agent) private string BuildDictionaryItem(string? table, string? reason, string? result) { - var res = new List(); + var res = string.Empty; if (!string.IsNullOrWhiteSpace(table)) { - res.Add($"Table: {table}"); + res += $"Table: {table}"; } if (!string.IsNullOrWhiteSpace(reason)) { - res.Add($"Reason: {reason}"); + if (!string.IsNullOrWhiteSpace(res)) + { + res += "\r\n"; + } + res += $"Reason: {reason}"; } if (!string.IsNullOrWhiteSpace(result)) { - res.Add($"Result: {result}"); + if (!string.IsNullOrWhiteSpace(res)) + { + res += "\r\n"; + } + res += $"Result: {result}"; } - if (res.IsNullOrEmpty()) return string.Empty; - - return string.Join("\r\n", res); + return res; } }