This commit is contained in:
2025-01-04 16:11:20 +07:00
parent 370f3501b9
commit 4fa16c4b76
2 changed files with 76 additions and 127 deletions

View File

@@ -224,75 +224,80 @@ function decisionMaker(state::T1, context, text2textInstructLLM::Function,
"""
<|start_header_id|>assistant<|end_header_id|>
"""
response = text2textInstructLLM(prompt)
println("\nSQL decisionMaker() rawresponse: ", response)
try
response = text2textInstructLLM(prompt)
println("\nSQL decisionMaker() rawresponse: ", response)
header = ["Understanding", "Reasoning", "Plan", "Action_name", "Action_input", "Observation"]
# detect if there are more than 1 key per categories
count = GeneralUtils.countGivenWords(response, header)
if sum(count) > length(header)
error("\nSQL decisionMaker() duplicated keywords", @__FILE__, " ", @__LINE__)
end
# textToDict() search for action_input
responsedict = GeneralUtils.textToDict(response, header,
rightmarker=":", symbolkey=true, lowercasekey=true)
delete!(responsedict, :observation)
# remove backticks Error occurred: MethodError: no method matching occursin(::String, ::Vector{String})
if occursin("```", responsedict[:action_input])
sql = GeneralUtils.extract_triple_backtick_text(responsedict[:action_input])[1]
if sql[1:4] == "sql\n"
sql = sql[5:end]
end
sql = split(sql, ';') # some time there are comments in the sql
sql = sql[1] * ';'
responsedict[:action_input] = sql
end
toollist = ["TABLEINFO", "GETDATA"]
if responsedict[:action_name] toollist
error("SQL decisionMaker() didn't use the given functions ", @__FILE__, " ", @__LINE__)
end
for i in toollist
if occursin(i, responsedict[:action_input])
error("Action_name is in action_input which is not allowed.")
end
end
for i [:understanding, :reasoning, :plan, :action_name, :action_input]
if length(JSON3.write(responsedict[i])) == 0
error("$i is empty ", @__FILE__, " ", @__LINE__)
end
end
# check if there are more than 1 key per categories
for i [:understanding, :reasoning, :plan, :action_name, :action_input]
matchkeys = GeneralUtils.findMatchingDictKey(responsedict, i)
if length(matchkeys) > 1
error("DecisionMaker has more than one key per categories")
end
end
state[:decisionMaker] = responsedict
return responsedict
catch e
io = IOBuffer()
showerror(io, e)
errorMsg = String(take!(io))
st = sprint((io, v) -> show(io, "text/plain", v), stacktrace(catch_backtrace()))
println("")
println("\n~~~ SQLLLM decisionMaker() Attempt $attempt. Error occurred: $errorMsg\n$st ", @__FILE__, " ", @__LINE__)
println("")
if occursin("NULL", response)
errornote = "\nSQL decisionMaker() NULL response is not allowed"
println("Attempt $attempt $errornote ", @__FILE__, " ", @__LINE__)
break
end
header = ["Understanding", "Reasoning", "Plan", "Action_name", "Action_input", "Observation"]
# detect if there are more than 1 key per categories
count = GeneralUtils.countGivenWords(response, header)
if sum(count) > length(header)
errornote = "\nSQL decisionMaker() duplicated keywords"
println("Attempt $attempt $errornote ", @__FILE__, " ", @__LINE__)
break
end
# textToDict() search for action_input
responsedict = GeneralUtils.textToDict(response, header,
rightmarker=":", symbolkey=true, lowercasekey=true)
delete!(responsedict, :observation)
# remove backticks Error occurred: MethodError: no method matching occursin(::String, ::Vector{String})
if occursin("```", responsedict[:action_input])
sql = GeneralUtils.extract_triple_backtick_text(responsedict[:action_input])[1]
if sql[1:4] == "sql\n"
sql = sql[5:end]
end
sql = split(sql, ';') # some time there are comments in the sql
sql = sql[1] * ';'
responsedict[:action_input] = sql
end
toollist = ["TABLEINFO", "GETDATA"]
if responsedict[:action_name] toollist
errornote = "\nSQL decisionMaker() didn't use the given functions"
println("Attempt $attempt $errornote ", @__FILE__, " ", @__LINE__)
break
end
for i in toollist
if occursin(i, responsedict[:action_input])
errornote = "\nSQL decisionMaker() action_name is in action_input which is not allowed."
println("Attempt $attempt $errornote ", @__FILE__, " ", @__LINE__)
break
end
end
for i [:understanding, :reasoning, :plan, :action_name, :action_input]
if length(JSON3.write(responsedict[i])) == 0
errornote = "\nSQL decisionMaker() $i is empty"
println("Attempt $attempt $errornote ", @__FILE__, " ", @__LINE__)
break
end
end
# check if there are more than 1 key per categories
for i [:understanding, :reasoning, :plan, :action_name, :action_input]
matchkeys = GeneralUtils.findMatchingDictKey(responsedict, i)
if length(matchkeys) > 1
errornote = "\nSQL decisionMaker() $i has more than one key"
println("Attempt $attempt $errornote ", @__FILE__, " ", @__LINE__)
break
end
end
state[:decisionMaker] = responsedict
return responsedict
end
error("DecisionMaker failed to generate a thought ", response)
end
@@ -797,7 +802,7 @@ function transition(state::T, args::NamedTuple
# so that other simulation start from this same node is not contaminated with actioninput
listAllTable_json(executeSQL)
elseif thoughtDict[:action_name] == "TABLEINFO"
input = copy(JSON3.read(thoughtDict[:action_input]))
input = copy(JSON3.read(thoughtDict[:action_input])) # BUG thoughtDict[:action_input] = "\"wine\""
tableinfo(executeSQL, input)
elseif thoughtDict[:action_name] == "GETDATA"
response = SQLexecution(executeSQL, thoughtDict[:action_input])