This commit is contained in:
narawat lamaiin
2025-04-04 15:05:16 +07:00
parent fd5ac82662
commit 00b0ab01a4
2 changed files with 36 additions and 32 deletions

View File

@@ -198,7 +198,17 @@ function decisionMaker(state::T1, context, text2textInstructLLM::Function,
header = ["Comprehension:", "Plan:", "Action_name:", "Action_input:"] header = ["Comprehension:", "Plan:", "Action_name:", "Action_input:"]
dictkey = ["comprehension", "plan", "action_name", "action_input"] dictkey = ["comprehension", "plan", "action_name", "action_input"]
llmkwargs=Dict(
:num_ctx => 32768,
:temperature => 0.1,
)
for attempt in 1:10 for attempt in 1:10
if attempt > 1
println("\nERROR SQLLLM decisionMaker() attempt $attempt/10 ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
llmkwargs[:temperature] = 0.1 * attempt
end
QandA = generatequestion(state, context, text2textInstructLLM; similarSQL=similarSQL_) QandA = generatequestion(state, context, text2textInstructLLM; similarSQL=similarSQL_)
usermsg = usermsg =
@@ -220,8 +230,8 @@ function decisionMaker(state::T1, context, text2textInstructLLM::Function,
] ]
# put in model format # put in model format
prompt = GeneralUtils.formatLLMtext(_prompt; formatname="llama3instruct") prompt = GeneralUtils.formatLLMtext(_prompt; formatname="qwen")
response = text2textInstructLLM(prompt, modelsize="medium") response = text2textInstructLLM(prompt; llmkwargs=llmkwargs)
# LLM tends to generate observation given that it is in the input # LLM tends to generate observation given that it is in the input
response = response =
@@ -249,8 +259,8 @@ function decisionMaker(state::T1, context, text2textInstructLLM::Function,
end end
if occursin("NULL", response) if occursin("NULL", response)
errornote = "\nSQL decisionMaker() NULL response is not allowed" errornote = "\nYour previous attempt was NULL. This is not allowed"
println("Attempt $attempt $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())") println("\nERROR SQLLLM decisionMaker() $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
continue continue
end end
@@ -272,10 +282,12 @@ function decisionMaker(state::T1, context, text2textInstructLLM::Function,
# check whether response has all header # check whether response has all header
detected_kw = GeneralUtils.detect_keyword(header, response) detected_kw = GeneralUtils.detect_keyword(header, response)
if 0 values(detected_kw) if 0 values(detected_kw)
errornote = "\nSQLLLM decisionMaker() response does not have all header" errornote = "\nYour previous attempt did not have all points according to the required response format"
println("\nERROR SQLLLM decisionMaker() $errornote \n$response", @__FILE__, ":", @__LINE__, " $(Dates.now())")
continue continue
elseif sum(values(detected_kw)) > length(header) elseif sum(values(detected_kw)) > length(header)
errornote = "\nSQLLLM decisionMaker() response has duplicated header" errornote = "\nYour previous attempt has duplicated points according to the required response format"
println("\nERROR SQLLLM decisionMaker() $errornote \n$response", @__FILE__, ":", @__LINE__, " $(Dates.now())")
continue continue
end end
@@ -298,43 +310,33 @@ function decisionMaker(state::T1, context, text2textInstructLLM::Function,
toollist = ["TABLEINFO", "RUNSQL"] toollist = ["TABLEINFO", "RUNSQL"]
if responsedict[:action_name] toollist if responsedict[:action_name] toollist
errornote = "\nYou must only use the given functions" errornote = "\nYour previous attempt has action_name that is not in the tool list"
println("Attempt $attempt $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())") println("\nERROR SQLLLM decisionMaker() $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
continue continue
end end
for i in toollist for i in toollist
if occursin(i, responsedict[:action_input]) if occursin(i, responsedict[:action_input])
errornote = "\n action_name is in action_input which is not allowed." errornote = "\nYour previous attempt has action_name in action_input which is not allowed"
println("Attempt $attempt $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())") println("\nERROR SQLLLM decisionMaker() $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
continue continue
end end
end end
for i Symbol.(dictkey) for i Symbol.(dictkey)
if length(JSON3.write(responsedict[i])) == 0 if length(JSON3.write(responsedict[i])) == 0
errornote = "\n $i is empty" errornote = "\nYour previous attempt has empty value for $i"
println("Attempt $attempt $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())") println("\nERROR SQLLLM decisionMaker() $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
continue continue
end end
end end
# check whether response has all header
detected_kw = GeneralUtils.detect_keyword(header, response)
if 0 values(detected_kw)
errornote = "\nSQL decisionMaker() response does not have all header"
continue
elseif sum(values(detected_kw)) > length(header)
errornote = "\nSQL decisionMaker() response has duplicated header"
continue
end
state[:decisionMaker] = responsedict state[:decisionMaker] = responsedict
return responsedict return responsedict
end end
error("DecisionMaker failed to generate a thought \n", response) error("SQLLLM DecisionMaker() failed to generate a thought \n", response)
end end
@@ -446,7 +448,7 @@ function evaluator(state::T1, text2textInstructLLM::Function
] ]
# put in model format # put in model format
prompt = GeneralUtils.formatLLMtext(_prompt; formatname="llama3instruct") prompt = GeneralUtils.formatLLMtext(_prompt; formatname="qwen")
header = ["Trajectory_evaluation:", "Answer_evaluation:", "Accepted_as_answer:", "Score:", "Suggestion:"] header = ["Trajectory_evaluation:", "Answer_evaluation:", "Accepted_as_answer:", "Score:", "Suggestion:"]
dictkey = ["trajectory_evaluation", "answer_evaluation", "accepted_as_answer", "score", "suggestion"] dictkey = ["trajectory_evaluation", "answer_evaluation", "accepted_as_answer", "score", "suggestion"]
@@ -601,7 +603,7 @@ function reflector(config::T1, state::T2)::String where {T1<:AbstractDict, T2<:A
] ]
# put in model format # put in model format
prompt = GeneralUtils.formatLLMtext(_prompt; formatname="llama3instruct") prompt = GeneralUtils.formatLLMtext(_prompt; formatname="qwen")
externalService = config[:externalservice][:text2textinstruct] externalService = config[:externalservice][:text2textinstruct]
# apply LLM specific instruct format # apply LLM specific instruct format
@@ -1011,7 +1013,7 @@ function query(query::T, executeSQL::Function, text2textInstructLLM::Function;
# JSON3.pretty(io, highValueState) # JSON3.pretty(io, highValueState)
# end # end
selected = compareState(query, highValueState, text2textInstructLLM) selected = compareState(query, highValueState, text2textInstructLLM)
resultState = highValueState[selected] resultState = highValueState[selected] #BUG compareState() select 0
end end
latestKey, latestInd = GeneralUtils.findHighestIndexKey(resultState[:thoughtHistory], "observation") latestKey, latestInd = GeneralUtils.findHighestIndexKey(resultState[:thoughtHistory], "observation")
action_input = Symbol("action_input_$latestInd") # latest sql action_input = Symbol("action_input_$latestInd") # latest sql
@@ -1029,7 +1031,9 @@ function query(query::T, executeSQL::Function, text2textInstructLLM::Function;
println("query() return nothing") println("query() return nothing")
end end
return (text=extracted, rawresponse=resultState[:rawresponse]) result = (text=extracted, rawresponse=resultState[:rawresponse])
return result
end end
@@ -1172,7 +1176,7 @@ function generatequestion(state::T1, context, text2textInstructLLM::Function;
] ]
# put in model format # put in model format
prompt = GeneralUtils.formatLLMtext(_prompt; formatname="llama3instruct") prompt = GeneralUtils.formatLLMtext(_prompt; formatname="qwen")
try try
response = text2textInstructLLM(prompt, modelsize="medium") response = text2textInstructLLM(prompt, modelsize="medium")
@@ -1190,7 +1194,7 @@ function generatequestion(state::T1, context, text2textInstructLLM::Function;
responsedict = GeneralUtils.textToDict(response, header; responsedict = GeneralUtils.textToDict(response, header;
dictKey=dictkey, symbolkey=true) dictKey=dictkey, symbolkey=true)
response = "Q1: " * responsedict[:q1] response = "Q1: " * responsedict[:q1]
# println("\nSQLLLM generatequestion() ", @__FILE__, ":", @__LINE__, " $(Dates.now())") # println("\nERROR SQLLLM generatequestion() ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
pprintln(Dict(responsedict)) pprintln(Dict(responsedict))
return response return response
catch e catch e
@@ -1198,7 +1202,7 @@ function generatequestion(state::T1, context, text2textInstructLLM::Function;
showerror(io, e) showerror(io, e)
errorMsg = String(take!(io)) errorMsg = String(take!(io))
st = sprint((io, v) -> show(io, "text/plain", v), stacktrace(catch_backtrace())) st = sprint((io, v) -> show(io, "text/plain", v), stacktrace(catch_backtrace()))
println("\nSQLLLM generatequestion() Attempt $attempt. Error occurred: $errorMsg\n$st ", @__FILE__, ":", @__LINE__, " $(Dates.now())") println("\nERROR SQLLLM generatequestion() Attempt $attempt. Error occurred: $errorMsg\n$st ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
end end
end end
error("generatequestion failed to generate a thought ", response) error("generatequestion failed to generate a thought ", response)

View File

@@ -22,9 +22,9 @@ Default system message template:
<Your responsibility does NOT includes> <Your responsibility does NOT includes>
- -
</Your responsibility does NOT includes> </Your responsibility does NOT includes>
<At each round of conversation, you will be given the following> <At each round of conversation, you will be given the following information>
</At each round of conversation, you will be given the following> </At each round of conversation, you will be given the following information>
<You must follow the following guidelines> <You must follow the following guidelines>
- -
</You must follow the following guidelines> </You must follow the following guidelines>