This commit is contained in:
2026-06-29 20:55:52 +07:00
parent bc81033924
commit c085a445e2
8 changed files with 137 additions and 71 deletions
+13 -40
View File
@@ -2,7 +2,7 @@ module interface
export decisionMaker, evaluator, reflector, transition, query
using LibPQ, DataStructures, JSON3, UUIDs, PrettyPrinting, Dates
using LibPQ, DataStructures, JSON, UUIDs, PrettyPrinting, Dates
using GeneralUtils, LLMMCTS
using ..util, ..llmfunction
@@ -106,33 +106,6 @@ function decisionMaker(state::T1, additionalinfo, text2textInstructLLM::Function
; querySQLVectorDBF::Union{T2, Nothing}=nothing, maxattempt=10
)::Dict{String, 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 question.
@@ -188,7 +161,7 @@ similarSQL_ = "None"
if length(state["thoughtHistory"]) == 1
sql, distance = querySQLVectorDBF(state["thoughtHistory"]["question"])
similarSQL_ = sql !== nothing ? sql : "None"
end
end
for attempt in 1:maxattempt
@@ -211,7 +184,7 @@ similarSQL_ = "None"
$workprogress
</progress>
<suggestion> This is your mentor's suggestion for the immediately preceding action and observation
$(state["suggestion"])
$(state["suggestion"])
</suggestion>
P.S. $errornote
</context>
@@ -239,7 +212,7 @@ $(state["suggestion"])
responsedict = nothing
try
responsedict = copy(JSON3.read(response))
responsedict = copy(JSON.parse(response))
catch
println("\nERROR YiemAgent generatechat() failed to parse response: $response", @__FILE__, ":", @__LINE__, " $(Dates.now())")
continue
@@ -307,10 +280,10 @@ responsedict["thoughthistory"] = state["thoughtHistory"]
responsedict["system"] = systemmsg
responsedict["prompt"] = prompt
responsedict["context"] = context
responsedict["think"] = think
responsedict["think"] = think
# # read sessionId
# sessionid = JSON3.read("/appfolder/app/sessionid.json")
# sessionid = JSON.parse("/appfolder/app/sessionid.json")
# # save to filename ./log/decisionlog.txt
# println("saving SQLLLM decisionMaker() to disk")
# filename = "agent_decision_log_$(sessionid[:id]).json"
@@ -324,7 +297,7 @@ responsedict["thoughthistory"] = state["thoughtHistory"]
# end
# else
# # read the file and append new data
# decisionlist = copy(JSON3.read(filepath))
# decisionlist = copy(JSON.parse(filepath))
# push!(decisionlist, responsedict)
# println("Appending new data to file $filepath")
# open(filepath, "w") do io
@@ -343,7 +316,7 @@ end
# # lessonDict =
# # if isfile("lesson.json")
# # lessonDict = copy(JSON3.read("lesson.json"))
# # lessonDict = copy(JSON.parse("lesson.json"))
# # else
# # lessonDict = nothing
# # end
@@ -754,7 +727,7 @@ function evaluator(state::T1, thoughtDict, text2textInstructLLM::Function, llmFo
# responsedict[:think] = think
# # read sessionId
# sessionid = JSON3.read("/appfolder/app/sessionid.json")
# sessionid = JSON.parse("/appfolder/app/sessionid.json")
# # save to filename ./log/decisionlog.txt
# println("saving SQLLLM evaluator() to disk")
# filename = "agent_evaluator_log_$(sessionid[:id]).json"
@@ -768,7 +741,7 @@ function evaluator(state::T1, thoughtDict, text2textInstructLLM::Function, llmFo
# end
# else
# # read the file and append new data
# decisionlist = copy(JSON3.read(filepath))
# decisionlist = copy(JSON.parse(filepath))
# push!(decisionlist, responsedict)
# println("Appending new data to file $filepath")
# open(filepath, "w") do io
@@ -917,7 +890,7 @@ function reflector(config::T1, state::T2)::String where {T1<:AbstractDict, T2<:A
error("Not valid JSON")
end
reflectionDict = copy(JSON3.read(responseJsonStr))
reflectionDict = copy(JSON.parse(responseJsonStr))
# check if dict has all required value
dummya::AbstractString = reflectionDict[:reflection]
@@ -1295,7 +1268,7 @@ function query(query::T, executeSQL::Function, text2textInstructLLM::Function;
end
latestKey, latestInd = GeneralUtils.findHighestIndexKey(resultState[:thoughtHistory], "observation")
action_input = "action_input_$latestInd" # latest sql
sql = resultState["thoughtHistory"][action_input]
sql = resultState["thoughtHistory"][action_input]
extractedTableContent = resultState["thoughtHistory"][latestKey]
# add to vectorDB only if the answer is achieved and the state is terminal
@@ -1355,7 +1328,7 @@ function makeNewState(currentstate::T1, thoughtDict::T4, rawresponse, response::
newstate["reward"] = reward
newstate["select"] = select
newstate["isterminal"] = isterminal
newstate["rawresponse"] = rawresponse # whatever return from action
newstate["rawresponse"] = rawresponse # whatever return from action
newNodeKey = GeneralUtils.uuid4snakecase()