From 0a1032c54598a3287b269bc8847a98ec4fdc82ba Mon Sep 17 00:00:00 2001 From: narawat lamaiin Date: Fri, 18 Jul 2025 07:54:59 +0700 Subject: [PATCH] update --- src/interface.jl | 120 ++++++++++++++++++++--------------------------- src/type.jl | 2 +- src/util.jl | 44 +++++++++++++++-- 3 files changed, 91 insertions(+), 75 deletions(-) diff --git a/src/interface.jl b/src/interface.jl index e1420b7..afefc2a 100644 --- a/src/interface.jl +++ b/src/interface.jl @@ -300,21 +300,27 @@ function decisionMaker(a::T; recentevents::Integer=20, maxattempt=10 end # 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 decisionMaker() $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 decisionMaker() $errornote --(not qualify response)--> $response ", @__FILE__, ":", @__LINE__, " $(Dates.now())") + ispass, errormsg = checkAgentResponse_JSON(responsedict, requiredKeys) + if !ispass + errornote = errormsg + println("\nERROR YiemAgent decisionMaker() $errornote --(not qualify response)> $responsedict", @__FILE__, ":", @__LINE__, " $(Dates.now())\n") continue end + # _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 decisionMaker() $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 decisionMaker() $errornote --(not qualify response)--> $response ", @__FILE__, ":", @__LINE__, " $(Dates.now())") + # continue + # end if responsedict[:actionname] ∉ ["CHATBOX", "CHECKWINE", "PRESENTBOX", "ENDCONVERSATION"] errornote = "Your previous attempt didn't use the given functions" @@ -538,19 +544,10 @@ function evaluator(a::T1, timeline, decisiondict, evaluateecontext end # 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())") + ispass, errormsg = checkAgentResponse_JSON(responsedict, requiredKeys) + if !ispass + errornote = errormsg + println("\nERROR YiemAgent evaluator() $errornote --(not qualify response)> $responsedict", @__FILE__, ":", @__LINE__, " $(Dates.now())\n") continue end @@ -977,19 +974,10 @@ function presentbox(a::sommelier, thoughtDict; maxtattempt::Integer=10, recentev end # 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 presentbox() $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 presentbox() $errornote --(not qualify response)--> $response ", @__FILE__, ":", @__LINE__, " $(Dates.now())") + ispass, errormsg = checkAgentResponse_JSON(responsedict, requiredKeys) + if !ispass + errornote = errormsg + println("\nERROR YiemAgent presentbox() $errornote --(not qualify response)> $responsedict", @__FILE__, ":", @__LINE__, " $(Dates.now())\n") continue end @@ -1161,19 +1149,10 @@ function generatechat(a::sommelier, thoughtDict; maxattempt::Integer=10) end # 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())") + ispass, errormsg = checkAgentResponse_JSON(responsedict, requiredKeys) + if !ispass + errornote = errormsg + println("\nERROR YiemAgent generatechat() $errornote --(not qualify response)> $responsedict", @__FILE__, ":", @__LINE__, " $(Dates.now())\n") continue end @@ -1327,16 +1306,15 @@ function generatechat(a::virtualcustomer; # check whether response has all header detected_kw = GeneralUtils.detectKeywordVariation(header, response) - kwvalue = [i for i in values(detected_kw)] - zeroind = findall(x -> x == 0, kwvalue) - missingkeys = [header[i] for i in zeroind] - if 0 ∈ values(detected_kw) + missingkeys = [k for (k, v) in detected_kw if v === nothing] + + if !isempty(missingkeys) errornote = "$missingkeys are missing from your previous response" - println("\nYiemAgent generatechat() $errornote:\n$response ", @__FILE__, ":", @__LINE__, " $(Dates.now())") + println("\nERROR YiemAgent rolegenerator() $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())") continue - elseif sum(values(detected_kw)) > length(header) - errornote = "Your previous attempt has duplicated points" - println("\nYiemAgent generatechat() $errornote:\n$response ", @__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 YiemAgent rolegenerator() $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())") continue end @@ -1754,15 +1732,19 @@ function detectWineryName(a, text) println("\ndetectWineryName() ", @__FILE__, ":", @__LINE__, " $(Dates.now())") try pprintln(response) catch e println(response) end - # check whether response has all header - detected_kw = GeneralUtils.detectKeywordVariation(header, response) - if 0 ∈ values(detected_kw) - errornote = "\nYiemAgent detectWineryName() response does not have all header" - continue - elseif sum(values(detected_kw)) > length(header) - errornote = "\nYiemAgent detectWineryName() response has duplicated header" - continue - end + # check whether response has all header + 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 YiemAgent rolegenerator() $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())") + continue + 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 YiemAgent rolegenerator() $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())") + continue + end responsedict = GeneralUtils.textToDict(response, header; dictKey=dictkey, symbolkey=true) diff --git a/src/type.jl b/src/type.jl index 6dbcf0d..7071571 100644 --- a/src/type.jl +++ b/src/type.jl @@ -7,7 +7,7 @@ using GeneralUtils # ---------------------------------------------- 100 --------------------------------------------- # -#[WORKING] + mutable struct appcontext const connection::NATS.Connection const text2textInstructLLMServiceSubject::String diff --git a/src/util.jl b/src/util.jl index ba4c7ae..018adde 100644 --- a/src/util.jl +++ b/src/util.jl @@ -1,7 +1,8 @@ module util export clearhistory, addNewMessage, chatHistoryToText, eventdict, noises, createTimeline, - availableWineToText, createEventsLog, createChatLog + availableWineToText, createEventsLog, createChatLog, checkAgentResponse_JSON, + checkAgentResponse_text using UUIDs, Dates, DataStructures, HTTP, JSON3 using GeneralUtils @@ -411,12 +412,45 @@ function createChatLog(chatdict::T1; index::Union{UnitRange, Nothing}=nothing end +function checkAgentResponse_text(response::String, requiredHeader::T + )::Tuple where {T<:Array{String}} + detected_kw = GeneralUtils.detectKeywordVariation(requiredHeader, response) + missingkeys = [k for (k, v) in detected_kw if v === nothing] + ispass = false + errormsg = nothing + if !isempty(missingkeys) + errormsg = "$missingkeys are missing from your previous response" + ispass = false + elseif sum([length(i) for i in values(detected_kw)]) > length(requiredHeader) + errormsg = "Your previous attempt has duplicated points according to the required response format" + ispass = false + else + ispass = true + end + return (ispass, errormsg) +end - - - - +function checkAgentResponse_JSON(responsedict::Dict, requiredKeys::T + )::Tuple where {T<:Array{Symbol}} + _responsedictKey = keys(responsedict) + responsedictKey = [i for i in _responsedictKey] # convert into a list + is_requiredKeys_in_responsedictKey = [i ∈ responsedictKey for i in requiredKeys] + ispass = false + errormsg = nothing + if length(is_requiredKeys_in_responsedictKey) > length(requiredKeys) + errormsg = "Your previous attempt has duplicated points according to the required response format" + ispass = false + elseif !all(is_requiredKeys_in_responsedictKey) + zeroind = findall(x -> x == 0, is_requiredKeys_in_responsedictKey) + missingkeys = [requiredKeys[i] for i in zeroind] + errormsg = "$missingkeys are missing from your previous response" + ispass = false + else + ispass = true + end + return (ispass, errormsg) +end