|
|
|
|
@@ -102,8 +102,8 @@ Dict(
|
|
|
|
|
|
|
|
|
|
# Signature
|
|
|
|
|
"""
|
|
|
|
|
function decisionMaker(state::T1, context, text2textInstructLLM::Function,
|
|
|
|
|
; querySQLVectorDBF::Union{T2, Nothing}=nothing
|
|
|
|
|
function decisionMaker(state::T1, additionalinfo, text2textInstructLLM::Function, llmFormatName::String
|
|
|
|
|
; querySQLVectorDBF::Union{T2, Nothing}=nothing, maxattempt=10
|
|
|
|
|
)::Dict{Symbol, Any} where {T1<:AbstractDict, T2<:Function}
|
|
|
|
|
|
|
|
|
|
# lessonDict =
|
|
|
|
|
@@ -135,19 +135,14 @@ function decisionMaker(state::T1, context, text2textInstructLLM::Function,
|
|
|
|
|
|
|
|
|
|
systemmsg =
|
|
|
|
|
"""
|
|
|
|
|
You are a helpful assistant that find the data from a database to satisfy the user's query.
|
|
|
|
|
You are also eager to improve your helpfulness.
|
|
|
|
|
You are a helpful assistant that find the data from a database to satisfy the user's question.
|
|
|
|
|
You are working under your mentor supervision and you are also eager to improve your helpfulness.
|
|
|
|
|
|
|
|
|
|
For your information:
|
|
|
|
|
- Observation: Result of the immediately preceding action
|
|
|
|
|
|
|
|
|
|
At each round of conversation, the user will give you the following:
|
|
|
|
|
User Query: ...
|
|
|
|
|
Example: ...
|
|
|
|
|
Your Q&A: ...
|
|
|
|
|
Your work progress: ...
|
|
|
|
|
Evaluation: Evaluation of the immediately preceding action and observation
|
|
|
|
|
Suggestion: Suggestion for the immediately preceding action and observation
|
|
|
|
|
At each round of conversation, you will be given the following information:
|
|
|
|
|
context: additional information about the current situation
|
|
|
|
|
|
|
|
|
|
You must follow the following guidelines:
|
|
|
|
|
- Keep SQL queries focused only on the provided information.
|
|
|
|
|
@@ -156,28 +151,28 @@ function decisionMaker(state::T1, context, text2textInstructLLM::Function,
|
|
|
|
|
- Do not create any table in the database
|
|
|
|
|
- A junction table can be used to link tables together. Another use case is for filtering data.
|
|
|
|
|
- If you can't find a single table that can be used to answer the user's query, try joining multiple tables to see if you can obtain the answer.
|
|
|
|
|
- If you are unable to find the requested information, kindly inform the user, "The current data in our database does not provide the specific answer to your query".
|
|
|
|
|
- Text information in the database usually stored in lower case. If your search returns empty, try using lower case to search.
|
|
|
|
|
- If there is no search result from the database, remove the restrictive criteria until a search result is available, and proceed from there.
|
|
|
|
|
|
|
|
|
|
You should then respond to the user with interleaving Comprehension, Plan, Action_name, Action_input:
|
|
|
|
|
Comprehension: state your comprehension about the current situation.
|
|
|
|
|
Plan: Given the current circumstances, outline a detailed, step-by-step plan to accomplish the task. Be specific.
|
|
|
|
|
Action_name: (Typically corresponds to the execution of the first step in your plan)
|
|
|
|
|
1) plan: Given the current circumstances, outline a detailed, step-by-step plan to accomplish the task. Be specific.
|
|
|
|
|
2) action_name: (Typically corresponds to the execution of the first step in your plan)
|
|
|
|
|
Can be one of the following function names:
|
|
|
|
|
- RUNSQL, which you can use to execute SQL against the database. Action_input for this function must be a single SQL query to be executed against the database.
|
|
|
|
|
For more effective text search, it's necessary to use case-insensitivity and the ILIKE operator.
|
|
|
|
|
Do not wrap the SQL as it will be executed against the database directly and SQL must be ended with ';'.
|
|
|
|
|
4) Action_input: Input to the action
|
|
|
|
|
3) action_input: Input to the action
|
|
|
|
|
|
|
|
|
|
You should only respond in format as described below:
|
|
|
|
|
Comprehension: ...
|
|
|
|
|
Plan: ...
|
|
|
|
|
Action_name: ...
|
|
|
|
|
Action_input: ...
|
|
|
|
|
You should only respond in JSON format as described below:
|
|
|
|
|
{
|
|
|
|
|
"plan": "...",
|
|
|
|
|
"action_name": "...",
|
|
|
|
|
"action_input": "..."
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Let's begin!
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
requiredKeys = [:plan, :action_name, :action_input]
|
|
|
|
|
workprogress = ""
|
|
|
|
|
for (k, v) in state[:thoughtHistory]
|
|
|
|
|
if k ∉ [:question]
|
|
|
|
|
@@ -195,105 +190,77 @@ function decisionMaker(state::T1, context, text2textInstructLLM::Function,
|
|
|
|
|
similarSQL_ = sql !== nothing ? sql : "None"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
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:maxattempt
|
|
|
|
|
|
|
|
|
|
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, llmFormatName; similarSQL=similarSQL_)
|
|
|
|
|
|
|
|
|
|
QandA = generatequestion(state, context, text2textInstructLLM; similarSQL=similarSQL_)
|
|
|
|
|
|
|
|
|
|
usermsg =
|
|
|
|
|
context =
|
|
|
|
|
"""
|
|
|
|
|
$(context[:tablelist])
|
|
|
|
|
User query: $(state[:thoughtHistory][:question])
|
|
|
|
|
Example: $similarSQL_
|
|
|
|
|
Your Q&A: $QandA
|
|
|
|
|
Your work progress: $workprogress
|
|
|
|
|
Evaluation: $(state[:evaluation])
|
|
|
|
|
Suggestion: $(state[:suggestion])
|
|
|
|
|
<context>
|
|
|
|
|
<table_schema> This is schema of tables in the database:
|
|
|
|
|
$(additionalinfo[:tablelist])
|
|
|
|
|
</table_schema>
|
|
|
|
|
<most_relevant_SQL> The closest known SQL for this question is:
|
|
|
|
|
$similarSQL_
|
|
|
|
|
</most_relevant_SQL>
|
|
|
|
|
<query_result_of_most_relevant_SQL> This is the query result when executing the most_relevant_SQL against a database. You can use this to see how the data are stored.
|
|
|
|
|
winery: Chateau Montelena, wine_name: The Montelena Estate Cabernet Sauvignon, wine_id: 97264f71-007c-4cce-a3fe-2cc88fba4d05, vintage: 2017, region: Napa Valley, country: United States, wine_type: red, grape: Cabernet Sauvignon, serving_temperature: 15 to 18 Celsius, sweetness: 1, intensity: 5, tannin: 4, acidity: 4, tasting_notes: oak, vanilla, tobacco, blackberry, plum, black cherry, leather, earthy, smoke, price: 19.95, currency: USD
|
|
|
|
|
</query_result_of_most_relevant_SQL>
|
|
|
|
|
<progress> your work progress so far:
|
|
|
|
|
$workprogress
|
|
|
|
|
</progress>
|
|
|
|
|
<suggestion> This is your mentor's suggestion for the immediately preceding action and observation
|
|
|
|
|
$(state[:suggestion])
|
|
|
|
|
</suggestion>
|
|
|
|
|
P.S. $errornote
|
|
|
|
|
</context>
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
_prompt =
|
|
|
|
|
unformatPrompt =
|
|
|
|
|
[
|
|
|
|
|
Dict(:name => "system", :text => systemmsg),
|
|
|
|
|
Dict(:name=> "user", :text=> usermsg)
|
|
|
|
|
Dict(:name => "user", :text => state[:thoughtHistory][:question])
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
# put in model format
|
|
|
|
|
prompt = GeneralUtils.formatLLMtext(_prompt, "granite3")
|
|
|
|
|
response = text2textInstructLLM(prompt; llmkwargs=llmkwargs)
|
|
|
|
|
response = GeneralUtils.deFormatLLMtext(response, "granite3")
|
|
|
|
|
prompt = GeneralUtils.formatLLMtext(unformatPrompt, llmFormatName)
|
|
|
|
|
# add info
|
|
|
|
|
prompt = prompt * context
|
|
|
|
|
response = text2textInstructLLM(prompt)
|
|
|
|
|
response = GeneralUtils.deFormatLLMtext(response, llmFormatName)
|
|
|
|
|
think, response = GeneralUtils.extractthink(response)
|
|
|
|
|
|
|
|
|
|
# LLM tends to generate observation given that it is in the input
|
|
|
|
|
response =
|
|
|
|
|
if occursin("observation:", response)
|
|
|
|
|
string(split(response, "observation:")[1])
|
|
|
|
|
elseif occursin("Observation:", response)
|
|
|
|
|
string(split(response, "Observation:")[1])
|
|
|
|
|
elseif occursin("observation_", response)
|
|
|
|
|
string(split(response, "observation_")[1])
|
|
|
|
|
elseif occursin("Observation_", response)
|
|
|
|
|
string(split(response, "Observation_")[1])
|
|
|
|
|
else
|
|
|
|
|
response
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# sometime LLM output something like **Comprehension**: which is not expected
|
|
|
|
|
response = replace(response, "**"=>"")
|
|
|
|
|
response = replace(response, "***"=>"")
|
|
|
|
|
|
|
|
|
|
# some time LLM output Plan_1: so we need to detect and replace topic numbering
|
|
|
|
|
regex = r"_[0-1000]+:"
|
|
|
|
|
matches = collect(eachmatch(regex, response))
|
|
|
|
|
for m in matches
|
|
|
|
|
response = replace(response, string(m.match)=>":")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if occursin("NULL", response)
|
|
|
|
|
errornote = "\nYour previous attempt was NULL. This is not allowed"
|
|
|
|
|
println("\nERROR SQLLLM decisionMaker() $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
|
|
|
|
continue
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# # detect if there are more than 1 key per categories
|
|
|
|
|
# wordcount = GeneralUtils.countGivenWords(response, header)
|
|
|
|
|
# duplicateKeywordFlag = false
|
|
|
|
|
# for (i, v) in enumerate(wordcount)
|
|
|
|
|
# keyword = header[i]
|
|
|
|
|
# keywordNumber = v
|
|
|
|
|
# if keywordNumber > 1
|
|
|
|
|
# errornote = "\nSQL query has duplicated keyword, $keyword"
|
|
|
|
|
# println("Attempt $attempt $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
|
|
|
|
# duplicateKeywordFlag = true
|
|
|
|
|
# break
|
|
|
|
|
# if occursin("NULL", response)
|
|
|
|
|
# errornote = "\nYour previous attempt contain NULL. It is not allowed in your response"
|
|
|
|
|
# println("\nERROR SQLLLM decisionMaker(). Attempt $attempt/$maxattempt. $errornote --(not qualify response)--> \n$response ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
|
|
|
|
# continue
|
|
|
|
|
# end
|
|
|
|
|
# end
|
|
|
|
|
# duplicateKeywordFlag == true ? continue : nothing
|
|
|
|
|
|
|
|
|
|
# check whether response has all header
|
|
|
|
|
detected_kw = GeneralUtils.detect_keyword(header, response)
|
|
|
|
|
if 0 ∈ values(detected_kw)
|
|
|
|
|
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 = "\nYour previous attempt has duplicated points according to the required response format"
|
|
|
|
|
println("\nERROR SQLLLM decisionMaker() $errornote \n$response", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
|
|
|
|
responsedict = nothing
|
|
|
|
|
try
|
|
|
|
|
responsedict = copy(JSON3.read(response))
|
|
|
|
|
catch
|
|
|
|
|
println("\nERROR YiemAgent generatechat() failed to parse response: $response", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
|
|
|
|
continue
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
responsedict = GeneralUtils.textToDict(response, header;
|
|
|
|
|
dictKey=dictkey, symbolkey=true)
|
|
|
|
|
# check whether all answer's key points are in responsedict
|
|
|
|
|
_responsedictKey = keys(responsedict)
|
|
|
|
|
responsedictKey = [i for i in _responsedictKey] # convert into a list
|
|
|
|
|
is_requiredKeys_in_responsedictKey = [i ∈ responsedictKey for i in requiredKeys]
|
|
|
|
|
|
|
|
|
|
if length(is_requiredKeys_in_responsedictKey) > length(requiredKeys)
|
|
|
|
|
errornote = "Your previous attempt has more key points than answer's required key points."
|
|
|
|
|
println("\nERROR YiemAgent generatechat() $errornote --(not qualify response)--> $response ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
|
|
|
|
continue
|
|
|
|
|
elseif !all(is_requiredKeys_in_responsedictKey)
|
|
|
|
|
zeroind = findall(x -> x == 0, is_requiredKeys_in_responsedictKey)
|
|
|
|
|
missingkeys = [requiredKeys[i] for i in zeroind]
|
|
|
|
|
errornote = "$missingkeys are missing from your previous response"
|
|
|
|
|
println("\nERROR YiemAgent generatechat() $errornote --(not qualify response)--> $response ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
|
|
|
|
continue
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
delete!(responsedict, :observation)
|
|
|
|
|
|
|
|
|
|
@@ -311,35 +278,296 @@ function decisionMaker(state::T1, context, text2textInstructLLM::Function,
|
|
|
|
|
|
|
|
|
|
toollist = ["TABLEINFO", "RUNSQL"]
|
|
|
|
|
if responsedict[:action_name] ∉ toollist
|
|
|
|
|
errornote = "\nYour previous attempt has action_name that is not in the tool list"
|
|
|
|
|
println("\nERROR SQLLLM decisionMaker() $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
|
|
|
|
errornote = "Your previous attempt has action_name that is not in the tool list"
|
|
|
|
|
println("\nERROR SQLLLM decisionMaker(). Attempt $attempt/$maxattempt. $errornote --(not qualify response)--> $(responsedict[:action_name]) ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
|
|
|
|
continue
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
for i in toollist
|
|
|
|
|
if occursin(i, responsedict[:action_input])
|
|
|
|
|
errornote = "\nYour previous attempt has action_name in action_input which is not allowed"
|
|
|
|
|
println("\nERROR SQLLLM decisionMaker() $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
|
|
|
|
errornote = "Your previous attempt has action_name in action_input which is not allowed"
|
|
|
|
|
println("\nERROR SQLLLM decisionMaker(). Attempt $attempt/$maxattempt. $errornote --(not qualify response)--> $(responsedict[:action_input]) ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
|
|
|
|
continue
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
for i ∈ Symbol.(dictkey)
|
|
|
|
|
if length(JSON3.write(responsedict[i])) == 0
|
|
|
|
|
errornote = "\nYour previous attempt has empty value for $i"
|
|
|
|
|
println("\nERROR SQLLLM decisionMaker() $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
|
|
|
|
continue
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
# for i ∈ Symbol.(dictkey)
|
|
|
|
|
# if length(JSON3.write(responsedict[i])) == 0
|
|
|
|
|
# errornote = "Your previous attempt has empty value for $i"
|
|
|
|
|
# println("\nERROR SQLLLM decisionMaker(). Attempt $attempt/$maxattempt. $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
|
|
|
|
# continue
|
|
|
|
|
# end
|
|
|
|
|
# end
|
|
|
|
|
|
|
|
|
|
state[:decisionMaker] = responsedict
|
|
|
|
|
println("\nSQLLLM decisionMaker() ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
|
|
|
|
pprintln(Dict(responsedict))
|
|
|
|
|
|
|
|
|
|
# store for later training
|
|
|
|
|
responsedict[:thoughthistory] = state[:thoughtHistory]
|
|
|
|
|
responsedict[:system] = systemmsg
|
|
|
|
|
responsedict[:prompt] = prompt
|
|
|
|
|
responsedict[:context] = context
|
|
|
|
|
responsedict[:think] = think
|
|
|
|
|
|
|
|
|
|
# # read sessionId
|
|
|
|
|
# sessionid = JSON3.read("/appfolder/app/sessionid.json")
|
|
|
|
|
# # save to filename ./log/decisionlog.txt
|
|
|
|
|
# println("saving SQLLLM decisionMaker() to disk")
|
|
|
|
|
# filename = "agent_decision_log_$(sessionid[:id]).json"
|
|
|
|
|
# filepath = "/appfolder/app/log/$filename"
|
|
|
|
|
# # check whether there is a file path exists before writing to it
|
|
|
|
|
# if !isfile(filepath)
|
|
|
|
|
# decisionlist = [responsedict]
|
|
|
|
|
# println("Creating file $filepath")
|
|
|
|
|
# open(filepath, "w") do io
|
|
|
|
|
# JSON3.pretty(io, decisionlist)
|
|
|
|
|
# end
|
|
|
|
|
# else
|
|
|
|
|
# # read the file and append new data
|
|
|
|
|
# decisionlist = copy(JSON3.read(filepath))
|
|
|
|
|
# push!(decisionlist, responsedict)
|
|
|
|
|
# println("Appending new data to file $filepath")
|
|
|
|
|
# open(filepath, "w") do io
|
|
|
|
|
# JSON3.pretty(io, decisionlist)
|
|
|
|
|
# end
|
|
|
|
|
# end
|
|
|
|
|
|
|
|
|
|
return responsedict
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
error("SQLLLM DecisionMaker() failed to generate a thought \n", response)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# function decisionMaker(state::T1, context, text2textInstructLLM::Function, llmFormatName::String
|
|
|
|
|
# ; querySQLVectorDBF::Union{T2, Nothing}=nothing, maxattempt=10
|
|
|
|
|
# )::Dict{Symbol, Any} where {T1<:AbstractDict, T2<:Function}
|
|
|
|
|
|
|
|
|
|
# # lessonDict =
|
|
|
|
|
# # if isfile("lesson.json")
|
|
|
|
|
# # lessonDict = copy(JSON3.read("lesson.json"))
|
|
|
|
|
# # else
|
|
|
|
|
# # lessonDict = nothing
|
|
|
|
|
# # end
|
|
|
|
|
|
|
|
|
|
# # lessonDict = nothing
|
|
|
|
|
|
|
|
|
|
# # lesson =
|
|
|
|
|
# # if lessonDict === nothing
|
|
|
|
|
# # ""
|
|
|
|
|
# # else
|
|
|
|
|
# # """
|
|
|
|
|
# # You have attempted to help the user before and failed, either because your reasoning for the
|
|
|
|
|
# # recommendation was incorrect or your response did not exactly match the user expectation.
|
|
|
|
|
# # The following lesson(s) give a plan to avoid failing to help the user in the same way you
|
|
|
|
|
# # did previously. Use them to improve your strategy to help the user.
|
|
|
|
|
|
|
|
|
|
# # Here are some lessons in JSON format:
|
|
|
|
|
# # $(JSON3.write(lessonDict))
|
|
|
|
|
|
|
|
|
|
# # When providing the thought and action for the current trial, that into account these failed
|
|
|
|
|
# # trajectories and make sure not to repeat the same mistakes and incorrect answers.
|
|
|
|
|
# # """
|
|
|
|
|
# # end
|
|
|
|
|
|
|
|
|
|
# systemmsg =
|
|
|
|
|
# """
|
|
|
|
|
# You are a helpful assistant that find the data from a database to satisfy the user's query.
|
|
|
|
|
# You are also eager to improve your helpfulness.
|
|
|
|
|
|
|
|
|
|
# For your information:
|
|
|
|
|
# - Observation: Result of the immediately preceding action
|
|
|
|
|
|
|
|
|
|
# At each round of conversation, the user will give you the following:
|
|
|
|
|
# User Query: ...
|
|
|
|
|
# Example: ...
|
|
|
|
|
# Your Q&A: ...
|
|
|
|
|
# Your work progress: ...
|
|
|
|
|
# Evaluation: Evaluation of the immediately preceding action and observation
|
|
|
|
|
# Suggestion: Suggestion for the immediately preceding action and observation
|
|
|
|
|
|
|
|
|
|
# You must follow the following guidelines:
|
|
|
|
|
# - Keep SQL queries focused only on the provided information.
|
|
|
|
|
|
|
|
|
|
# You should follow the following guidelines:
|
|
|
|
|
# - Do not create any table in the database
|
|
|
|
|
# - A junction table can be used to link tables together. Another use case is for filtering data.
|
|
|
|
|
# - If you can't find a single table that can be used to answer the user's query, try joining multiple tables to see if you can obtain the answer.
|
|
|
|
|
# - If you are unable to find the requested information, kindly inform the user, "The current data in our database does not provide the specific answer to your query".
|
|
|
|
|
# - Text information in the database usually stored in lower case. If your search returns empty, try using lower case to search.
|
|
|
|
|
|
|
|
|
|
# You should then respond to the user with interleaving Comprehension, Plan, Action_name, Action_input:
|
|
|
|
|
# Plan: Given the current circumstances, outline a detailed, step-by-step plan to accomplish the task. Be specific.
|
|
|
|
|
# Action_name: (Typically corresponds to the execution of the first step in your plan)
|
|
|
|
|
# Can be one of the following function names:
|
|
|
|
|
# - RUNSQL, which you can use to execute SQL against the database. Action_input for this function must be a single SQL query to be executed against the database.
|
|
|
|
|
# For more effective text search, it's necessary to use case-insensitivity and the ILIKE operator.
|
|
|
|
|
# Do not wrap the SQL as it will be executed against the database directly and SQL must be ended with ';'.
|
|
|
|
|
# 4) Action_input: Input to the action
|
|
|
|
|
|
|
|
|
|
# You should only respond in format as described below:
|
|
|
|
|
# Plan: ...
|
|
|
|
|
# Action_name: ...
|
|
|
|
|
# Action_input: ...
|
|
|
|
|
|
|
|
|
|
# Let's begin!
|
|
|
|
|
# """
|
|
|
|
|
|
|
|
|
|
# workprogress = ""
|
|
|
|
|
# for (k, v) in state[:thoughtHistory]
|
|
|
|
|
# if k ∉ [:question]
|
|
|
|
|
# workprogress *= "$k: $v\n"
|
|
|
|
|
# end
|
|
|
|
|
# end
|
|
|
|
|
|
|
|
|
|
# response = nothing # store for show when error msg show up
|
|
|
|
|
# errornote = "N/A"
|
|
|
|
|
|
|
|
|
|
# # provide similar sql only for the first attempt
|
|
|
|
|
# similarSQL_ = "None"
|
|
|
|
|
# if length(state[:thoughtHistory]) == 1
|
|
|
|
|
# sql, distance = querySQLVectorDBF(state[:thoughtHistory][:question])
|
|
|
|
|
# similarSQL_ = sql !== nothing ? sql : "None"
|
|
|
|
|
# end
|
|
|
|
|
|
|
|
|
|
# header = ["Plan:", "Action_name:", "Action_input:"]
|
|
|
|
|
# dictkey = ["plan", "action_name", "action_input"]
|
|
|
|
|
|
|
|
|
|
# llmkwargs=Dict(
|
|
|
|
|
# :num_ctx => 32768,
|
|
|
|
|
# :temperature => 0.5,
|
|
|
|
|
# )
|
|
|
|
|
|
|
|
|
|
# for attempt in 1:maxattempt
|
|
|
|
|
|
|
|
|
|
# QandA = generatequestion(state, context, text2textInstructLLM, llmFormatName; similarSQL=similarSQL_)
|
|
|
|
|
|
|
|
|
|
# usermsg =
|
|
|
|
|
# """
|
|
|
|
|
# $(context[:tablelist])
|
|
|
|
|
# User query: $(state[:thoughtHistory][:question])
|
|
|
|
|
# Example: $similarSQL_
|
|
|
|
|
# Your Q&A: $QandA
|
|
|
|
|
# Your work progress: $workprogress
|
|
|
|
|
# Evaluation: $(state[:evaluation])
|
|
|
|
|
# Suggestion: $(state[:suggestion])
|
|
|
|
|
# P.S. $errornote
|
|
|
|
|
# """
|
|
|
|
|
|
|
|
|
|
# _prompt =
|
|
|
|
|
# [
|
|
|
|
|
# Dict(:name=> "system", :text=> systemmsg),
|
|
|
|
|
# Dict(:name=> "user", :text=> usermsg)
|
|
|
|
|
# ]
|
|
|
|
|
|
|
|
|
|
# # put in model format
|
|
|
|
|
# prompt = GeneralUtils.formatLLMtext(_prompt, llmFormatName)
|
|
|
|
|
# response = text2textInstructLLM(prompt; llmkwargs=llmkwargs)
|
|
|
|
|
# response = GeneralUtils.deFormatLLMtext(response, llmFormatName)
|
|
|
|
|
# think, response = GeneralUtils.extractthink(response)
|
|
|
|
|
|
|
|
|
|
# # LLM tends to generate observation given that it is in the input
|
|
|
|
|
# response =
|
|
|
|
|
# if occursin("observation:", response)
|
|
|
|
|
# string(split(response, "observation:")[1])
|
|
|
|
|
# elseif occursin("Observation:", response)
|
|
|
|
|
# string(split(response, "Observation:")[1])
|
|
|
|
|
# elseif occursin("observation_", response)
|
|
|
|
|
# string(split(response, "observation_")[1])
|
|
|
|
|
# elseif occursin("Observation_", response)
|
|
|
|
|
# string(split(response, "Observation_")[1])
|
|
|
|
|
# else
|
|
|
|
|
# response
|
|
|
|
|
# end
|
|
|
|
|
|
|
|
|
|
# # sometime LLM output something like **Comprehension**: which is not expected
|
|
|
|
|
# response = replace(response, "**"=>"")
|
|
|
|
|
# response = replace(response, "***"=>"")
|
|
|
|
|
|
|
|
|
|
# # some time LLM output Plan_1: so we need to detect and replace topic numbering
|
|
|
|
|
# regex = r"_[0-1000]+:"
|
|
|
|
|
# matches = collect(eachmatch(regex, response))
|
|
|
|
|
# for m in matches
|
|
|
|
|
# response = replace(response, string(m.match)=>":")
|
|
|
|
|
# end
|
|
|
|
|
|
|
|
|
|
# if occursin("NULL", response)
|
|
|
|
|
# errornote = "\nYour previous attempt was NULL. This is not allowed"
|
|
|
|
|
# println("\nERROR SQLLLM decisionMaker(). Attempt $attempt/$maxattempt. $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
|
|
|
|
# continue
|
|
|
|
|
# end
|
|
|
|
|
|
|
|
|
|
# # # detect if there are more than 1 key per categories
|
|
|
|
|
# # wordcount = GeneralUtils.countGivenWords(response, header)
|
|
|
|
|
# # duplicateKeywordFlag = false
|
|
|
|
|
# # for (i, v) in enumerate(wordcount)
|
|
|
|
|
# # keyword = header[i]
|
|
|
|
|
# # keywordNumber = v
|
|
|
|
|
# # if keywordNumber > 1
|
|
|
|
|
# # errornote = "\nSQL query has duplicated keyword, $keyword"
|
|
|
|
|
# # println("Attempt $attempt $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
|
|
|
|
# # duplicateKeywordFlag = true
|
|
|
|
|
# # break
|
|
|
|
|
# # end
|
|
|
|
|
# # end
|
|
|
|
|
# # duplicateKeywordFlag == true ? continue : nothing
|
|
|
|
|
|
|
|
|
|
# # check whether response has all header
|
|
|
|
|
# detected_kw = GeneralUtils.detect_keyword(header, response)
|
|
|
|
|
# if 0 ∈ values(detected_kw)
|
|
|
|
|
# errornote = "\nYour previous attempt did not have all points according to the required response format"
|
|
|
|
|
# println("\nERROR SQLLLM decisionMaker(). Attempt $attempt/$maxattempt. $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
|
|
|
|
# continue
|
|
|
|
|
# elseif sum(values(detected_kw)) > length(header)
|
|
|
|
|
# errornote = "\nYour previous attempt has duplicated points according to the required response format"
|
|
|
|
|
# println("\nERROR SQLLLM decisionMaker(). Attempt $attempt/$maxattempt. $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
|
|
|
|
# continue
|
|
|
|
|
# end
|
|
|
|
|
|
|
|
|
|
# responsedict = GeneralUtils.textToDict(response, header;
|
|
|
|
|
# dictKey=dictkey, symbolkey=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", "RUNSQL"]
|
|
|
|
|
# if responsedict[:action_name] ∉ toollist
|
|
|
|
|
# errornote = "Your previous attempt has action_name that is not in the tool list"
|
|
|
|
|
# println("\nERROR SQLLLM decisionMaker(). Attempt $attempt/$maxattempt. $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
|
|
|
|
# continue
|
|
|
|
|
# end
|
|
|
|
|
|
|
|
|
|
# for i in toollist
|
|
|
|
|
# if occursin(i, responsedict[:action_input])
|
|
|
|
|
# errornote = "Your previous attempt has action_name in action_input which is not allowed"
|
|
|
|
|
# println("\nERROR SQLLLM decisionMaker(). Attempt $attempt/$maxattempt. $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
|
|
|
|
# continue
|
|
|
|
|
# end
|
|
|
|
|
# end
|
|
|
|
|
|
|
|
|
|
# for i ∈ Symbol.(dictkey)
|
|
|
|
|
# if length(JSON3.write(responsedict[i])) == 0
|
|
|
|
|
# errornote = "Your previous attempt has empty value for $i"
|
|
|
|
|
# println("\nERROR SQLLLM decisionMaker(). Attempt $attempt/$maxattempt. $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
|
|
|
|
# continue
|
|
|
|
|
# end
|
|
|
|
|
# end
|
|
|
|
|
|
|
|
|
|
# state[:decisionMaker] = responsedict
|
|
|
|
|
# return responsedict
|
|
|
|
|
# end
|
|
|
|
|
# error("SQLLLM DecisionMaker() failed to generate a thought \n", response)
|
|
|
|
|
# end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
""" Assigns a scalar value to each new child node to be used for selec-
|
|
|
|
|
tion and backpropagation. This value effectively quantifies the agent's progress in task completion,
|
|
|
|
|
@@ -361,7 +589,8 @@ julia>
|
|
|
|
|
|
|
|
|
|
# Signature
|
|
|
|
|
"""
|
|
|
|
|
function evaluator(state::T1, text2textInstructLLM::Function; maxattempt=10
|
|
|
|
|
function evaluator(state::T1, thoughtDict, text2textInstructLLM::Function, llmFormatName::String;
|
|
|
|
|
maxattempt=10
|
|
|
|
|
) where {T1<:AbstractDict}
|
|
|
|
|
|
|
|
|
|
systemmsg =
|
|
|
|
|
@@ -370,8 +599,6 @@ function evaluator(state::T1, text2textInstructLLM::Function; maxattempt=10
|
|
|
|
|
|
|
|
|
|
Definitions:
|
|
|
|
|
"question" is the user's question
|
|
|
|
|
"understanding" is agent's understanding about the current situation
|
|
|
|
|
"reasoning" is agent's step-by-step reasoning about the current situation
|
|
|
|
|
"plan" is agent's plan to complete the task from the current situation
|
|
|
|
|
"action_name" is the name of the action taken, which can be one of the following functions:
|
|
|
|
|
- RUNSQL, which you can use to execute SQL against the database. Action_input for this function must be a single SQL query to be executed against the database.
|
|
|
|
|
@@ -380,16 +607,14 @@ function evaluator(state::T1, text2textInstructLLM::Function; maxattempt=10
|
|
|
|
|
"action_input" is the input to the action
|
|
|
|
|
"observation" is result of the preceding immediate action
|
|
|
|
|
|
|
|
|
|
<At each round of conversation, the user will give you>
|
|
|
|
|
Trajectory: ...
|
|
|
|
|
Error_note: error note from your previous attempt
|
|
|
|
|
</At each round of conversation, the user will give you>
|
|
|
|
|
At each round of conversation, you will be given the following information:
|
|
|
|
|
trajectory: A history of how you worked on the question chronologically
|
|
|
|
|
evaluatee_context: The context that evaluatee use to make a decision
|
|
|
|
|
|
|
|
|
|
<You must follow the following guidelines>
|
|
|
|
|
You must follow the following guidelines:
|
|
|
|
|
- When the search returns no result, validate whether the SQL query makes sense before accepting it as a valid answer.
|
|
|
|
|
</You must follow the following guidelines>
|
|
|
|
|
|
|
|
|
|
<You should then respond to the user with>
|
|
|
|
|
You should then respond to the user with:
|
|
|
|
|
1) Trajectory_evaluation: Analyze the trajectory of a solution to answer the user's original question.
|
|
|
|
|
- Evaluate the correctness of each section and the overall trajectory based on the given question.
|
|
|
|
|
- Provide detailed reasoning and analysis, focusing on the latest thought, action, and observation.
|
|
|
|
|
@@ -397,7 +622,7 @@ function evaluator(state::T1, text2textInstructLLM::Function; maxattempt=10
|
|
|
|
|
- Do not generate additional thoughts or actions.
|
|
|
|
|
2) Answer_evaluation:
|
|
|
|
|
- Focus only on the matter mentioned in the question and comprehensively analyze how the latest observation's details addresses the question
|
|
|
|
|
3) Accepted_as_answer: Decide whether the latest observation's content answers the question. Can be "Yes" or "No"
|
|
|
|
|
3) Accepted_as_answer: Decide whether the latest observation's content answers the question. Can be "yes" or "no"
|
|
|
|
|
Bad example (The observation didn't answers the question):
|
|
|
|
|
question: Find cars with 4 wheels.
|
|
|
|
|
observation: There are an apple in the table.
|
|
|
|
|
@@ -407,66 +632,77 @@ function evaluator(state::T1, text2textInstructLLM::Function; maxattempt=10
|
|
|
|
|
4) Score: Correctness score s where s is a single integer between 0 to 9.
|
|
|
|
|
For example:
|
|
|
|
|
- 0 indicates that both the trajectory is incorrect, failed or errors and the observation is incorrect or failed
|
|
|
|
|
- 4 indicates that the trajectory are correct but the observation is incorrect or failed
|
|
|
|
|
- 5 indicates that the trajectory are correct, but no results are returned.
|
|
|
|
|
- 4 indicates that the trajectory are correct, but no results are returned.
|
|
|
|
|
- 5 indicates that the trajectory are correct but the observation is incorrect or failed
|
|
|
|
|
- 6 indicates that the trajectory are correct, but the observation's content doesn't directly answer the question
|
|
|
|
|
- 8 indicates that both the trajectory are correct, and the observation's content directly answers the question.
|
|
|
|
|
- 9 indicates a perfect perfomance. Both the trajectory are correct, and the observation's content directly answers the question, surpassing your expectations.
|
|
|
|
|
5) Suggestion: if accepted_as_answer is "No", provide suggestion.
|
|
|
|
|
</You should then respond to the user with>
|
|
|
|
|
5) Suggestion: what are the possible reason of this outcome, what can one learn from it and what suggestion can made?
|
|
|
|
|
|
|
|
|
|
<You should only respond in format as described below>
|
|
|
|
|
You should only respond in format as described below:
|
|
|
|
|
Trajectory_evaluation: ...
|
|
|
|
|
Answer_evaluation: ...
|
|
|
|
|
Accepted_as_answer: ...
|
|
|
|
|
Score: ...
|
|
|
|
|
Suggestion: ...
|
|
|
|
|
</You should only respond in format as described below>
|
|
|
|
|
|
|
|
|
|
Let's begin!
|
|
|
|
|
"""
|
|
|
|
|
#[WORKING] add what I should think --> this will be the think for decisionMaker()
|
|
|
|
|
header = ["Trajectory_evaluation:", "Answer_evaluation:", "Accepted_as_answer:", "Score:", "Suggestion:"]
|
|
|
|
|
dictkey = ["trajectory_evaluation", "answer_evaluation", "accepted_as_answer", "score", "suggestion"]
|
|
|
|
|
|
|
|
|
|
thoughthistory = ""
|
|
|
|
|
for (k, v) in state[:thoughtHistory]
|
|
|
|
|
thoughthistory *= "$k: $v\n"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
errornote = ""
|
|
|
|
|
errornote = "N/A"
|
|
|
|
|
for attempt in 1:maxattempt
|
|
|
|
|
usermsg =
|
|
|
|
|
"""
|
|
|
|
|
Trajectory: $thoughthistory
|
|
|
|
|
<trajectory>
|
|
|
|
|
$thoughthistory
|
|
|
|
|
</trajectory>
|
|
|
|
|
"""
|
|
|
|
|
context =
|
|
|
|
|
"""
|
|
|
|
|
<context>
|
|
|
|
|
<evaluatee_context>
|
|
|
|
|
thoughtDict[:context]
|
|
|
|
|
</evaluatee_context>
|
|
|
|
|
P.S. $errornote
|
|
|
|
|
</context>
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
_prompt =
|
|
|
|
|
unformatPrompt =
|
|
|
|
|
[
|
|
|
|
|
Dict(:name => "system", :text => systemmsg),
|
|
|
|
|
Dict(:name => "user", :text => usermsg)
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
# put in model format
|
|
|
|
|
prompt = GeneralUtils.formatLLMtext(_prompt, "granite3")
|
|
|
|
|
|
|
|
|
|
header = ["Trajectory_evaluation:", "Answer_evaluation:", "Accepted_as_answer:", "Score:", "Suggestion:"]
|
|
|
|
|
dictkey = ["trajectory_evaluation", "answer_evaluation", "accepted_as_answer", "score", "suggestion"]
|
|
|
|
|
prompt = GeneralUtils.formatLLMtext(unformatPrompt, llmFormatName)
|
|
|
|
|
# add info
|
|
|
|
|
prompt = prompt * context
|
|
|
|
|
|
|
|
|
|
response = text2textInstructLLM(prompt, modelsize="medium")
|
|
|
|
|
response = GeneralUtils.deFormatLLMtext(response, "granite3")
|
|
|
|
|
response = GeneralUtils.deFormatLLMtext(response, llmFormatName)
|
|
|
|
|
think, response = GeneralUtils.extractthink(response)
|
|
|
|
|
|
|
|
|
|
# sometime LLM output something like **Comprehension**: which is not expected
|
|
|
|
|
response = replace(response, "**"=>"")
|
|
|
|
|
response = replace(response, "***"=>"")
|
|
|
|
|
|
|
|
|
|
# check whether response has all header
|
|
|
|
|
detected_kw = GeneralUtils.detect_keyword(header, response)
|
|
|
|
|
if 0 ∈ values(detected_kw)
|
|
|
|
|
errornote = "Your previous attempt does not have all answer points"
|
|
|
|
|
println("\nERROR SQLLLM evaluator() Attempt $attempt/$maxattempt. $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
|
|
|
|
detected_kw = GeneralUtils.detectKeywordVariation(header, response)
|
|
|
|
|
missingkeys = [k for (k, v) in detected_kw if v === nothing]
|
|
|
|
|
if !isempty(missingkeys)
|
|
|
|
|
errornote = "$missingkeys are missing from your previous response"
|
|
|
|
|
println("\nERROR SQLLLM extractContent_dataframe() $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
|
|
|
|
continue
|
|
|
|
|
elseif sum(values(detected_kw)) > length(header)
|
|
|
|
|
errornote = "Your previous attempt has duplicated answer point"
|
|
|
|
|
println("\nERROR SQLLLM evaluator() Attempt $attempt/$maxattempt. $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
|
|
|
|
elseif sum([length(i) for i in values(detected_kw)]) > length(header)
|
|
|
|
|
errornote = "\nYour previous attempt has duplicated points according to the required response format"
|
|
|
|
|
println("\nERROR SQLLLM extractContent_dataframe() $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
|
|
|
|
continue
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
@@ -484,9 +720,9 @@ function evaluator(state::T1, text2textInstructLLM::Function; maxattempt=10
|
|
|
|
|
|
|
|
|
|
accepted_as_answer::AbstractString = responsedict[:accepted_as_answer]
|
|
|
|
|
|
|
|
|
|
if accepted_as_answer ∉ ["Yes", "No"] # [PENDING] add errornote into the prompt
|
|
|
|
|
if accepted_as_answer ∉ ["yes", "no"]
|
|
|
|
|
errornote = "Your previous attempt's accepted_as_answer has wrong format"
|
|
|
|
|
println("\nERROR SQLLLM evaluator() Attempt $attempt/$maxattempt. $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
|
|
|
|
println("\nERROR SQLLLM evaluator() Attempt $attempt/$maxattempt. $errornote --(not qualify response)--> $(responsedict[:accepted_as_answer]) ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
|
|
|
|
continue
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
@@ -497,7 +733,7 @@ function evaluator(state::T1, text2textInstructLLM::Function; maxattempt=10
|
|
|
|
|
state[:suggestion] = responsedict[:suggestion]
|
|
|
|
|
|
|
|
|
|
# mark as terminal state when the answer is achieved
|
|
|
|
|
if accepted_as_answer == "Yes"
|
|
|
|
|
if accepted_as_answer ∈ ["Yes", "yes"]
|
|
|
|
|
|
|
|
|
|
# mark the state as terminal state because the evaluation say so.
|
|
|
|
|
state[:isterminal] = true
|
|
|
|
|
@@ -505,9 +741,41 @@ function evaluator(state::T1, text2textInstructLLM::Function; maxattempt=10
|
|
|
|
|
# evaluation score as reward because different answers hold different value for the user.
|
|
|
|
|
state[:reward] = responsedict[:score]
|
|
|
|
|
end
|
|
|
|
|
println("\nEvaluator() ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
|
|
|
|
|
|
|
|
|
println("\nSQLLLM evaluator() ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
|
|
|
|
pprintln(Dict(responsedict))
|
|
|
|
|
|
|
|
|
|
# # store for later training
|
|
|
|
|
# responsedict[:thoughthistory] = state[:thoughtHistory]
|
|
|
|
|
# responsedict[:system] = systemmsg
|
|
|
|
|
# responsedict[:usermsg] = usermsg
|
|
|
|
|
# responsedict[:prompt] = prompt
|
|
|
|
|
# responsedict[:context] = context
|
|
|
|
|
# responsedict[:think] = think
|
|
|
|
|
|
|
|
|
|
# # read sessionId
|
|
|
|
|
# sessionid = JSON3.read("/appfolder/app/sessionid.json")
|
|
|
|
|
# # save to filename ./log/decisionlog.txt
|
|
|
|
|
# println("saving SQLLLM evaluator() to disk")
|
|
|
|
|
# filename = "agent_evaluator_log_$(sessionid[:id]).json"
|
|
|
|
|
# filepath = "/appfolder/app/log/$filename"
|
|
|
|
|
# # check whether there is a file path exists before writing to it
|
|
|
|
|
# if !isfile(filepath)
|
|
|
|
|
# decisionlist = [responsedict]
|
|
|
|
|
# println("Creating file $filepath")
|
|
|
|
|
# open(filepath, "w") do io
|
|
|
|
|
# JSON3.pretty(io, decisionlist)
|
|
|
|
|
# end
|
|
|
|
|
# else
|
|
|
|
|
# # read the file and append new data
|
|
|
|
|
# decisionlist = copy(JSON3.read(filepath))
|
|
|
|
|
# push!(decisionlist, responsedict)
|
|
|
|
|
# println("Appending new data to file $filepath")
|
|
|
|
|
# open(filepath, "w") do io
|
|
|
|
|
# JSON3.pretty(io, decisionlist)
|
|
|
|
|
# end
|
|
|
|
|
# end
|
|
|
|
|
|
|
|
|
|
return responsedict[:score]
|
|
|
|
|
end
|
|
|
|
|
error("Evaluator failed to generate an evaluation, Response: \n$response\n<|End of error|>")
|
|
|
|
|
@@ -705,15 +973,17 @@ function transition(state::T, args::NamedTuple
|
|
|
|
|
|
|
|
|
|
decisionMakerF::Function = args[:decisionMaker]
|
|
|
|
|
evaluatorF::Function = args[:evaluator]
|
|
|
|
|
reflector::Function = args[:reflector]
|
|
|
|
|
# reflector::Function = args[:reflector]
|
|
|
|
|
context = args[:context]
|
|
|
|
|
executeSQL::Function = args[:executeSQL]
|
|
|
|
|
text2textInstructLLM::Function = args[:text2textInstructLLM]
|
|
|
|
|
insertSQLVectorDB::Function = args[:insertSQLVectorDB]
|
|
|
|
|
# insertSQLVectorDB::Function = args[:insertSQLVectorDB]
|
|
|
|
|
querySQLVectorDBF::Function = args[:querySQLVectorDB]
|
|
|
|
|
llmFormatName::String = args[:llmFormatName]
|
|
|
|
|
|
|
|
|
|
# getting SQL from vectorDB
|
|
|
|
|
thoughtDict = decisionMakerF(state, context, text2textInstructLLM; querySQLVectorDBF)
|
|
|
|
|
thoughtDict = decisionMakerF(state, context, text2textInstructLLM, llmFormatName;
|
|
|
|
|
querySQLVectorDBF)
|
|
|
|
|
|
|
|
|
|
# map action and input() to llm function
|
|
|
|
|
response =
|
|
|
|
|
@@ -727,7 +997,8 @@ function transition(state::T, args::NamedTuple
|
|
|
|
|
elseif thoughtDict[:action_name] == "RUNSQL"
|
|
|
|
|
response = SQLexecution(executeSQL, thoughtDict[:action_input])
|
|
|
|
|
if response[:success]
|
|
|
|
|
extracted = extractContent_dataframe(response[:result], text2textInstructLLM, thoughtDict[:action_input])
|
|
|
|
|
extracted = extractContent_dataframe(response[:result], text2textInstructLLM,
|
|
|
|
|
thoughtDict[:action_input], llmFormatName)
|
|
|
|
|
(rawresponse=response[:result], result=extracted, errormsg=nothing, success=true)
|
|
|
|
|
else
|
|
|
|
|
(result=nothing, errormsg=response[:errormsg], success=false)
|
|
|
|
|
@@ -743,7 +1014,7 @@ function transition(state::T, args::NamedTuple
|
|
|
|
|
reward::Integer = haskey(response, :reward) ? response[:reward] : 0
|
|
|
|
|
isterminal::Bool = haskey(response, :isterminal) ? response[:isterminal] : false
|
|
|
|
|
newNodeKey, newstate = makeNewState(state, thoughtDict, rawresponse, JSON3.write(result), select, reward, isterminal)
|
|
|
|
|
progressvalue::Integer = evaluatorF(newstate, text2textInstructLLM)
|
|
|
|
|
progressvalue::Integer = evaluatorF(newstate, thoughtDict, text2textInstructLLM, llmFormatName)
|
|
|
|
|
|
|
|
|
|
return (newNodeKey=newNodeKey, newstate=newstate, progressvalue=progressvalue)
|
|
|
|
|
end
|
|
|
|
|
@@ -835,6 +1106,7 @@ julia> println(result)
|
|
|
|
|
function query(query::T, executeSQL::Function, text2textInstructLLM::Function;
|
|
|
|
|
insertSQLVectorDB::Union{Function, Nothing}=nothing,
|
|
|
|
|
similarSQLVectorDB::Union{Function, Nothing}=nothing,
|
|
|
|
|
llmFormatName="qwen3"
|
|
|
|
|
)::NamedTuple{(:text, :rawresponse), Tuple{Any, Any}} where {T<:AbstractString}
|
|
|
|
|
|
|
|
|
|
# use similarSQLVectorDB to find similar SQL for the query
|
|
|
|
|
@@ -844,7 +1116,8 @@ function query(query::T, executeSQL::Function, text2textInstructLLM::Function;
|
|
|
|
|
response = SQLexecution(executeSQL, sql)
|
|
|
|
|
if response[:success]
|
|
|
|
|
# intention = Dict(:intention=> "$(thoughtDict[:plan])")
|
|
|
|
|
extracted = extractContent_dataframe(response[:result], text2textInstructLLM, sql)
|
|
|
|
|
extracted = extractContent_dataframe(response[:result], text2textInstructLLM, sql,
|
|
|
|
|
llmFormatName)
|
|
|
|
|
return (text=extracted, rawresponse=response[:result])
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
@@ -873,7 +1146,6 @@ function query(query::T, executeSQL::Function, text2textInstructLLM::Function;
|
|
|
|
|
context = Dict(
|
|
|
|
|
:tablelist =>
|
|
|
|
|
"""
|
|
|
|
|
Here are SQL that used to create tables in the database:
|
|
|
|
|
create table customer (
|
|
|
|
|
customer_id uuid primary key default gen_random_uuid (),
|
|
|
|
|
customer_firstname varchar(128),
|
|
|
|
|
@@ -936,7 +1208,7 @@ function query(query::T, executeSQL::Function, text2textInstructLLM::Function;
|
|
|
|
|
wine_name varchar(128) not null,
|
|
|
|
|
winery varchar(128) not null,
|
|
|
|
|
vintage integer not null,
|
|
|
|
|
region varchar(128) not null,
|
|
|
|
|
region varchar(128) not null, -- A field used to store the name of a wine-producing region, such as Napa Valley (California), Bordeaux, Champagne, Tuscany, etc.
|
|
|
|
|
country varchar(128) not null,
|
|
|
|
|
wine_type varchar(128) not null,
|
|
|
|
|
grape varchar(128) not null,
|
|
|
|
|
@@ -946,7 +1218,7 @@ function query(query::T, executeSQL::Function, text2textInstructLLM::Function;
|
|
|
|
|
tannin integer,
|
|
|
|
|
acidity integer,
|
|
|
|
|
fizziness integer,
|
|
|
|
|
tasting_notes text,
|
|
|
|
|
tasting_notes text, -- A field used to record the distinctive flavors of wine such as floral, citrus, apple, earthy, daisy, etc.
|
|
|
|
|
note text,
|
|
|
|
|
other_attributes jsonb,
|
|
|
|
|
|
|
|
|
|
@@ -997,15 +1269,16 @@ function query(query::T, executeSQL::Function, text2textInstructLLM::Function;
|
|
|
|
|
text2textInstructLLM=text2textInstructLLM,
|
|
|
|
|
querySQLVectorDB=similarSQLVectorDB,
|
|
|
|
|
insertSQLVectorDB=insertSQLVectorDB,
|
|
|
|
|
llmFormatName=llmFormatName
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
earlystop(state) = state[:reward] >= 8 ? true : false
|
|
|
|
|
|
|
|
|
|
root, _, resultState, highValueState =
|
|
|
|
|
LLMMCTS.runMCTS(initialstate, transition, transitionargs;
|
|
|
|
|
horizontalSampleExpansionPhase=3,
|
|
|
|
|
horizontalSampleSimulationPhase=3,
|
|
|
|
|
maxSimulationDepth=5,
|
|
|
|
|
horizontalSampleExpansionPhase=1,
|
|
|
|
|
horizontalSampleSimulationPhase=1,
|
|
|
|
|
maxSimulationDepth=1,
|
|
|
|
|
maxiterations=1,
|
|
|
|
|
explorationweight=1.0,
|
|
|
|
|
earlystop=earlystop,
|
|
|
|
|
@@ -1013,17 +1286,17 @@ function query(query::T, executeSQL::Function, text2textInstructLLM::Function;
|
|
|
|
|
multithread=false)
|
|
|
|
|
|
|
|
|
|
# compare all high value state answer then select the best one
|
|
|
|
|
if length(highValueState) > 0
|
|
|
|
|
if length(highValueState) > 1
|
|
|
|
|
# open("/appfolder/app/highValueState.json", "w") do io
|
|
|
|
|
# JSON3.pretty(io, highValueState)
|
|
|
|
|
# end
|
|
|
|
|
selected = compareState(query, highValueState, text2textInstructLLM)
|
|
|
|
|
resultState = highValueState[selected] #BUG compareState() select 0
|
|
|
|
|
selected = compareState(query, highValueState, text2textInstructLLM, llmFormatName)
|
|
|
|
|
resultState = highValueState[selected]
|
|
|
|
|
end
|
|
|
|
|
latestKey, latestInd = GeneralUtils.findHighestIndexKey(resultState[:thoughtHistory], "observation")
|
|
|
|
|
action_input = Symbol("action_input_$latestInd") # latest sql
|
|
|
|
|
sql = resultState[:thoughtHistory][action_input]
|
|
|
|
|
extracted = resultState[:thoughtHistory][latestKey]
|
|
|
|
|
extractedTableContent = resultState[:thoughtHistory][latestKey]
|
|
|
|
|
|
|
|
|
|
# add to vectorDB only if the answer is achieved and the state is terminal
|
|
|
|
|
if insertSQLVectorDB !== nothing && resultState[:isterminal] == true &&
|
|
|
|
|
@@ -1032,11 +1305,11 @@ function query(query::T, executeSQL::Function, text2textInstructLLM::Function;
|
|
|
|
|
insertSQLVectorDB(resultState[:thoughtHistory][:question], sql)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if extracted === nothing
|
|
|
|
|
println("query() return nothing")
|
|
|
|
|
if extractedTableContent === nothing
|
|
|
|
|
println("\nSQLLLM query() return nothing ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
result = (text=extracted, rawresponse=resultState[:rawresponse])
|
|
|
|
|
result = (text=extractedTableContent, rawresponse=resultState[:rawresponse])
|
|
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
end
|
|
|
|
|
@@ -1059,7 +1332,7 @@ function makeNewState(currentstate::T1, thoughtDict::T4, rawresponse, response::
|
|
|
|
|
reward::T3, isterminal::Bool
|
|
|
|
|
)::NamedTuple{(:newNodeKey, :newstate), Tuple{String, Dict{Symbol, <:Any}}} where {T1<:AbstractDict, T2<:AbstractString, T3<:Number, T4<:AbstractDict}
|
|
|
|
|
|
|
|
|
|
keys = [:comprehension, :action_name, :action_input, :observation]
|
|
|
|
|
keys = [:plan, :action_name, :action_input, :observation]
|
|
|
|
|
# latestKeys = []
|
|
|
|
|
|
|
|
|
|
currentstate_latestKey, currentstate_latestIndice =
|
|
|
|
|
@@ -1090,8 +1363,9 @@ function makeNewState(currentstate::T1, thoughtDict::T4, rawresponse, response::
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function generatequestion(state::T1, context, text2textInstructLLM::Function;
|
|
|
|
|
similarSQL::Union{T2, Nothing}=nothing, maxattempt=10
|
|
|
|
|
function generatequestion(state::T1, context, text2textInstructLLM::Function,
|
|
|
|
|
llmFormatName::String;
|
|
|
|
|
similarSQL::Union{T2, Nothing}=nothing, maxattempt=10,
|
|
|
|
|
)::String where {T1<:AbstractDict, T2<:AbstractString}
|
|
|
|
|
|
|
|
|
|
similarSQL =
|
|
|
|
|
@@ -1122,37 +1396,37 @@ function generatequestion(state::T1, context, text2textInstructLLM::Function;
|
|
|
|
|
4) Do not generate any question or comments at the end.
|
|
|
|
|
|
|
|
|
|
You should follow the following guidelines:
|
|
|
|
|
- When querying data in the database, start with broad search terms and refine your query later for more precise results.
|
|
|
|
|
- If there is no search result from the database, remove the restrictive criteria until a search result is available, and proceed from there.
|
|
|
|
|
|
|
|
|
|
You should then respond to the user with:
|
|
|
|
|
1) Understanding:
|
|
|
|
|
- State your understanding about the current situation.
|
|
|
|
|
2) Q: Given the situation, "ask yourself" about the situation at least five, but no more than ten, questions.
|
|
|
|
|
3) A: Given the situation, "answer to yourself" the best you can.
|
|
|
|
|
1) Q: Given the situation, "ask yourself" about the situation at least three, but no more than five, questions.
|
|
|
|
|
2) A: Given the situation, "answer to yourself" the best you can.
|
|
|
|
|
- Do not generate any text after the last answer.
|
|
|
|
|
|
|
|
|
|
You must only respond in format as described below:
|
|
|
|
|
Understanding: ...
|
|
|
|
|
Q1: ...
|
|
|
|
|
A1: ...
|
|
|
|
|
Q2: ...
|
|
|
|
|
A2: ...
|
|
|
|
|
Q3: ...
|
|
|
|
|
A3: ...
|
|
|
|
|
...
|
|
|
|
|
|
|
|
|
|
Here are some examples:
|
|
|
|
|
Q: What information in the hints is not necessary based on the query?
|
|
|
|
|
A: Country is not specified in the query thus it should not be included in an SQL
|
|
|
|
|
|
|
|
|
|
Q: How can I modify a SQL example to fit my specific query needs?
|
|
|
|
|
A: ...
|
|
|
|
|
Q: Why the query failed?
|
|
|
|
|
A: ...
|
|
|
|
|
Q: What criteria become more restrictive as the search scope broadens and can be remove?
|
|
|
|
|
A: In the "2019 Toyota Camry hybrid" search query, "2019" represents the most restrictive criteria because it narrows the data scope to a specific year, whereas "Toyota" and "Camry" are broader categories that allow for more general results.
|
|
|
|
|
Q: What works and what not previously?
|
|
|
|
|
A: ...
|
|
|
|
|
|
|
|
|
|
Let's begin!
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
header = ["Understanding:", "Q1:"]
|
|
|
|
|
dictkey = ["understanding", "q1"]
|
|
|
|
|
header = ["Q1:"]
|
|
|
|
|
dictkey = ["q1"]
|
|
|
|
|
|
|
|
|
|
workprogress = ""
|
|
|
|
|
for (k, v) in state[:thoughtHistory]
|
|
|
|
|
@@ -1162,7 +1436,7 @@ function generatequestion(state::T1, context, text2textInstructLLM::Function;
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
response = nothing # store for show when error msg show up
|
|
|
|
|
errornote = ""
|
|
|
|
|
errornote = "N/A"
|
|
|
|
|
|
|
|
|
|
for attempt in 1:maxattempt
|
|
|
|
|
usermsg =
|
|
|
|
|
@@ -1172,6 +1446,7 @@ function generatequestion(state::T1, context, text2textInstructLLM::Function;
|
|
|
|
|
Example: $similarSQL
|
|
|
|
|
Your work progress: $workprogress
|
|
|
|
|
P.S. $errornote
|
|
|
|
|
/no_think
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
_prompt =
|
|
|
|
|
@@ -1181,10 +1456,11 @@ function generatequestion(state::T1, context, text2textInstructLLM::Function;
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
# put in model format
|
|
|
|
|
prompt = GeneralUtils.formatLLMtext(_prompt, "granite3")
|
|
|
|
|
prompt = GeneralUtils.formatLLMtext(_prompt, llmFormatName)
|
|
|
|
|
|
|
|
|
|
response = text2textInstructLLM(prompt, modelsize="medium")
|
|
|
|
|
response = GeneralUtils.deFormatLLMtext(response, "granite3")
|
|
|
|
|
response = GeneralUtils.deFormatLLMtext(response, llmFormatName)
|
|
|
|
|
think, response = GeneralUtils.extractthink(response)
|
|
|
|
|
|
|
|
|
|
# check if response is valid
|
|
|
|
|
q_number = count("Q", response)
|
|
|
|
|
|