From 00b0ab01a4249a6b449b108a7fdf7b107afc79dd Mon Sep 17 00:00:00 2001 From: narawat lamaiin Date: Fri, 4 Apr 2025 15:05:16 +0700 Subject: [PATCH] update --- src/interface.jl | 64 +++++++++++++++++++++------------------ system_prompt_template.jl | 4 +-- 2 files changed, 36 insertions(+), 32 deletions(-) diff --git a/src/interface.jl b/src/interface.jl index 1591264..d24b72e 100644 --- a/src/interface.jl +++ b/src/interface.jl @@ -198,7 +198,17 @@ function decisionMaker(state::T1, context, text2textInstructLLM::Function, header = ["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 + 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_) usermsg = @@ -220,8 +230,8 @@ function decisionMaker(state::T1, context, text2textInstructLLM::Function, ] # put in model format - prompt = GeneralUtils.formatLLMtext(_prompt; formatname="llama3instruct") - response = text2textInstructLLM(prompt, modelsize="medium") + prompt = GeneralUtils.formatLLMtext(_prompt; formatname="qwen") + response = text2textInstructLLM(prompt; llmkwargs=llmkwargs) # LLM tends to generate observation given that it is in the input response = @@ -249,8 +259,8 @@ function decisionMaker(state::T1, context, text2textInstructLLM::Function, end if occursin("NULL", response) - errornote = "\nSQL decisionMaker() NULL response is not allowed" - println("Attempt $attempt $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())") + errornote = "\nYour previous attempt was NULL. This is not allowed" + println("\nERROR SQLLLM decisionMaker() $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())") continue end @@ -272,10 +282,12 @@ function decisionMaker(state::T1, context, text2textInstructLLM::Function, # check whether response has all header detected_kw = GeneralUtils.detect_keyword(header, response) 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 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 end @@ -298,43 +310,33 @@ function decisionMaker(state::T1, context, text2textInstructLLM::Function, toollist = ["TABLEINFO", "RUNSQL"] if responsedict[:action_name] ∉ toollist - errornote = "\nYou must only use the given functions" - println("Attempt $attempt $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())") + errornote = "\nYour previous attempt has action_name that is not in the tool list" + println("\nERROR SQLLLM decisionMaker() $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())") continue end for i in toollist if occursin(i, responsedict[:action_input]) - errornote = "\n action_name is in action_input which is not allowed." - println("Attempt $attempt $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())") + errornote = "\nYour previous attempt has action_name in action_input which is not allowed" + println("\nERROR SQLLLM decisionMaker() $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())") continue end end for i ∈ Symbol.(dictkey) if length(JSON3.write(responsedict[i])) == 0 - errornote = "\n $i is empty" - println("Attempt $attempt $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())") + errornote = "\nYour previous attempt has empty value for $i" + println("\nERROR SQLLLM decisionMaker() $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())") continue 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 return responsedict end - error("DecisionMaker failed to generate a thought \n", response) + error("SQLLLM DecisionMaker() failed to generate a thought \n", response) end @@ -446,7 +448,7 @@ function evaluator(state::T1, text2textInstructLLM::Function ] # 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:"] 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 - prompt = GeneralUtils.formatLLMtext(_prompt; formatname="llama3instruct") + prompt = GeneralUtils.formatLLMtext(_prompt; formatname="qwen") externalService = config[:externalservice][:text2textinstruct] # apply LLM specific instruct format @@ -1011,7 +1013,7 @@ function query(query::T, executeSQL::Function, text2textInstructLLM::Function; # JSON3.pretty(io, highValueState) # end selected = compareState(query, highValueState, text2textInstructLLM) - resultState = highValueState[selected] + resultState = highValueState[selected] #BUG compareState() select 0 end latestKey, latestInd = GeneralUtils.findHighestIndexKey(resultState[:thoughtHistory], "observation") action_input = Symbol("action_input_$latestInd") # latest sql @@ -1029,7 +1031,9 @@ function query(query::T, executeSQL::Function, text2textInstructLLM::Function; println("query() return nothing") end - return (text=extracted, rawresponse=resultState[:rawresponse]) + result = (text=extracted, rawresponse=resultState[:rawresponse]) + + return result end @@ -1172,7 +1176,7 @@ function generatequestion(state::T1, context, text2textInstructLLM::Function; ] # put in model format - prompt = GeneralUtils.formatLLMtext(_prompt; formatname="llama3instruct") + prompt = GeneralUtils.formatLLMtext(_prompt; formatname="qwen") try response = text2textInstructLLM(prompt, modelsize="medium") @@ -1190,7 +1194,7 @@ function generatequestion(state::T1, context, text2textInstructLLM::Function; responsedict = GeneralUtils.textToDict(response, header; dictKey=dictkey, symbolkey=true) response = "Q1: " * responsedict[:q1] - # println("\nSQLLLM generatequestion() ", @__FILE__, ":", @__LINE__, " $(Dates.now())") + # println("\nERROR SQLLLM generatequestion() ", @__FILE__, ":", @__LINE__, " $(Dates.now())") pprintln(Dict(responsedict)) return response catch e @@ -1198,7 +1202,7 @@ function generatequestion(state::T1, context, text2textInstructLLM::Function; showerror(io, e) errorMsg = String(take!(io)) 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 error("generatequestion failed to generate a thought ", response) diff --git a/system_prompt_template.jl b/system_prompt_template.jl index a3d1d24..158c943 100644 --- a/system_prompt_template.jl +++ b/system_prompt_template.jl @@ -22,9 +22,9 @@ Default system message template: - - + - + -