1- using Azure ;
21using BotSharp . Abstraction . Agents . Enums ;
3- using BotSharp . Abstraction . MLTasks ;
42using BotSharp . Abstraction . Routing ;
5- using BotSharp . Core . Agents . Services ;
63using BotSharp . Core . Infrastructures ;
74using BotSharp . Plugin . SqlDriver . Models ;
85using MySqlConnector ;
9- using System . Text . RegularExpressions ;
106using static Dapper . SqlMapper ;
11- using static System . Net . Mime . MediaTypeNames ;
127
138namespace BotSharp . Plugin . SqlDriver . Functions ;
149
@@ -35,15 +30,16 @@ public async Task<bool> Execute(RoleDialogModel message)
3530 var agentService = _services . GetRequiredService < IAgentService > ( ) ;
3631 var currentAgent = await agentService . LoadAgent ( message . CurrentAgentId ) ;
3732 var dictionarySqlPrompt = await GetDictionarySQLPrompt ( args . SqlStatement , msgCopy . Content ) ;
38- var plannerAgent = new Agent
33+ var agent = new Agent
3934 {
40- Id = string . Empty ,
35+ Id = message . CurrentAgentId ?? string . Empty ,
4136 Name = "sqlDriver_DictionarySearch" ,
4237 Instruction = dictionarySqlPrompt ,
4338 TemplateDict = new Dictionary < string , object > ( ) ,
4439 LlmConfig = currentAgent . LlmConfig
4540 } ;
46- var response = await GetAiResponse ( plannerAgent ) ;
41+
42+ var response = await GetAiResponse ( agent ) ;
4743 args = JsonSerializer . Deserialize < LookupDictionary > ( response . Content ) ;
4844
4945 // check if need to instantely
@@ -59,13 +55,24 @@ public async Task<bool> Execute(RoleDialogModel message)
5955 {
6056 message . Content = JsonSerializer . Serialize ( result ) ;
6157 }
58+
6259 var states = _services . GetRequiredService < IConversationStateService > ( ) ;
63- var dictionaryItems = states . GetState ( "dictionary_items" , "" ) ;
64- dictionaryItems += "\r \n \r \n " + args . Table + ":\r \n " + args . Reason + ":\r \n " + message . Content + "\r \n " ;
65- states . SetState ( "dictionary_items" , dictionaryItems ) ;
60+ var dictionaryItems = states . GetState ( "dictionary_items" ) ;
61+ var newItem = BuildDictionaryItem ( args . Table , args . Reason , message . Content ) ;
62+
63+ var items = new List < string > ( ) ;
64+ if ( ! string . IsNullOrWhiteSpace ( dictionaryItems ) )
65+ {
66+ items = JsonSerializer . Deserialize < List < string > > ( dictionaryItems ) ;
67+ }
68+
69+ items . Add ( newItem ) ;
70+ //dictionaryItems += "\r\n\r\n" + args.Table + ":\r\n" + args.Reason + ":\r\n" + message.Content + "\r\n";
71+ states . SetState ( "dictionary_items" , JsonSerializer . Serialize ( items ) ) ;
6672
6773 return true ;
6874 }
75+
6976 private async Task < string > GetDictionarySQLPrompt ( string originalSql , string tableStructure )
7077 {
7178 var agentService = _services . GetRequiredService < IAgentService > ( ) ;
@@ -83,15 +90,39 @@ private async Task<string> GetDictionarySQLPrompt(string originalSql, string tab
8390 { "response_format" , responseFormat }
8491 } ) ;
8592 }
86- private async Task < RoleDialogModel > GetAiResponse ( Agent plannerAgent )
93+
94+ private async Task < RoleDialogModel > GetAiResponse ( Agent agent )
8795 {
8896 var text = "Check and correct the SQL statement." ;
8997 var message = new RoleDialogModel ( AgentRole . User , text ) ;
9098
9199 var completion = CompletionProvider . GetChatCompletion ( _services ,
92- provider : plannerAgent . LlmConfig . Provider ,
93- model : plannerAgent . LlmConfig . Model ) ;
100+ provider : agent . LlmConfig . Provider ,
101+ model : agent . LlmConfig . Model ) ;
102+
103+ return await completion . GetChatCompletions ( agent , new List < RoleDialogModel > { message } ) ;
104+ }
105+
106+ private string BuildDictionaryItem ( string ? table , string ? reason , string ? result )
107+ {
108+ var res = new List < string > ( ) ;
109+ if ( ! string . IsNullOrWhiteSpace ( table ) )
110+ {
111+ res . Add ( $ "Table: { table } ") ;
112+ }
113+
114+ if ( ! string . IsNullOrWhiteSpace ( reason ) )
115+ {
116+ res . Add ( $ "Reason: { reason } ") ;
117+ }
118+
119+ if ( ! string . IsNullOrWhiteSpace ( result ) )
120+ {
121+ res . Add ( $ "Result: { result } ") ;
122+ }
123+
124+ if ( res . IsNullOrEmpty ( ) ) return string . Empty ;
94125
95- return await completion . GetChatCompletions ( plannerAgent , new List < RoleDialogModel > { message } ) ;
126+ return string . Join ( " \r \n " , res ) ;
96127 }
97128}
0 commit comments