diff --git a/src/interface.jl b/src/interface.jl index 28cff70..45e3daf 100644 --- a/src/interface.jl +++ b/src/interface.jl @@ -185,40 +185,41 @@ function decisionMaker(a::T1, state::T2)::Dict{Symbol, Any} where {T1<:agent, T2 ) attempt = 0 - while attempt <= 5 + while true attempt += 1 - try - response = GeneralUtils.sendReceiveMqttMsg(outgoingMsg) - _responseJsonStr = response[:response][:text] - expectedJsonExample = - """ - Here is an expected JSON format: - { - "thought": "...", - "action": {"name": "...", "input": "..."}, - "observation": "..." - } - """ - responseJsonStr = jsoncorrection(a, _responseJsonStr, expectedJsonExample) - thoughtDict = copy(JSON3.read(responseJsonStr)) + if attempt <= 5 + try + response = GeneralUtils.sendReceiveMqttMsg(outgoingMsg) + _responseJsonStr = response[:response][:text] + expectedJsonExample = + """ + Here is an expected JSON format: + { + "thought": "...", + "action": {"name": "...", "input": "..."}, + "observation": "..." + } + """ + responseJsonStr = jsoncorrection(a, _responseJsonStr, expectedJsonExample) + thoughtDict = copy(JSON3.read(responseJsonStr)) - # check if dict has all required value - dummya::AbstractString = thoughtDict[:thought] - dummyb::AbstractString = thoughtDict[:action][:name] - dummyc::AbstractString = thoughtDict[:action][:input] + # check if dict has all required value + dummya::AbstractString = thoughtDict[:thought] + dummyb::AbstractString = thoughtDict[:action][:name] + dummyc::AbstractString = thoughtDict[:action][:input] - return thoughtDict - catch e - io = IOBuffer() - showerror(io, e) - errorMsg = String(take!(io)) - st = sprint((io, v) -> show(io, "text/plain", v), stacktrace(catch_backtrace())) - println("") - @warn "Error occurred: $errorMsg\n$st" - println("") - end + return thoughtDict + catch e + io = IOBuffer() + showerror(io, e) + errorMsg = String(take!(io)) + st = sprint((io, v) -> show(io, "text/plain", v), stacktrace(catch_backtrace())) + println("") + @warn "Error occurred: $errorMsg\n$st" + println("") + end - if attempt > 5 + else error("DecisionMaker failed to generate a thought") end end @@ -315,35 +316,36 @@ function progressValueEstimator(a::T1, state::T2)::Tuple{String, Integer} where ) attempt = 0 - while attempt <= 5 + while true attempt += 1 - try - response = GeneralUtils.sendReceiveMqttMsg(outgoingMsg) - _responseJsonStr = response[:response][:text] - expectedJsonExample = - """ - Here is an expected JSON format: - {"evaluation": "...", "score": "..."} - """ - responseJsonStr = jsoncorrection(a, _responseJsonStr, expectedJsonExample) - evaluationDict = copy(JSON3.read(responseJsonStr)) + if attempt <= 5 + try + response = GeneralUtils.sendReceiveMqttMsg(outgoingMsg) + _responseJsonStr = response[:response][:text] + expectedJsonExample = + """ + Here is an expected JSON format: + {"evaluation": "...", "score": "..."} + """ + responseJsonStr = jsoncorrection(a, _responseJsonStr, expectedJsonExample) + evaluationDict = copy(JSON3.read(responseJsonStr)) - # check if dict has all required value - dummya::AbstractString = evaluationDict[:evaluation] - dummyb::Integer = evaluationDict[:score] + # check if dict has all required value + dummya::AbstractString = evaluationDict[:evaluation] + dummyb::Integer = evaluationDict[:score] - return (evaluationDict[:evaluation], evaluationDict[:score]) - catch e - io = IOBuffer() - showerror(io, e) - errorMsg = String(take!(io)) - st = sprint((io, v) -> show(io, "text/plain", v), stacktrace(catch_backtrace())) - println("") - @warn "Error occurred: $errorMsg\n$st" - println("") - end + return (evaluationDict[:evaluation], evaluationDict[:score]) + catch e + io = IOBuffer() + showerror(io, e) + errorMsg = String(take!(io)) + st = sprint((io, v) -> show(io, "text/plain", v), stacktrace(catch_backtrace())) + println("") + @warn "Error occurred: $errorMsg\n$st" + println("") + end - if attempt > 5 + else error("progressValueEstimator failed to generate an evaluation") end end @@ -451,34 +453,35 @@ function reflector(a::T1, state::T2)::String where {T1<:agent, T2<:AbstractDict} ) attempt = 0 - while attempt <= 5 + while true attempt += 1 - try - response = GeneralUtils.sendReceiveMqttMsg(outgoingMsg) - _responseJsonStr = response[:response][:text] - expectedJsonExample = - """ - Here is an expected JSON format: - {"reflection": "..."} - """ - responseJsonStr = jsoncorrection(a, _responseJsonStr, expectedJsonExample) - reflectionDict = copy(JSON3.read(responseJsonStr)) + if attempt <= 5 + try + response = GeneralUtils.sendReceiveMqttMsg(outgoingMsg) + _responseJsonStr = response[:response][:text] + expectedJsonExample = + """ + Here is an expected JSON format: + {"reflection": "..."} + """ + responseJsonStr = jsoncorrection(a, _responseJsonStr, expectedJsonExample) + reflectionDict = copy(JSON3.read(responseJsonStr)) - # check if dict has all required value - dummya::AbstractString = reflectionDict[:reflection] + # check if dict has all required value + dummya::AbstractString = reflectionDict[:reflection] - return reflectionDict[:reflection] - catch e - io = IOBuffer() - showerror(io, e) - errorMsg = String(take!(io)) - st = sprint((io, v) -> show(io, "text/plain", v), stacktrace(catch_backtrace())) - println("") - @warn "Error occurred: $errorMsg\n$st" - println("") - end + return reflectionDict[:reflection] + catch e + io = IOBuffer() + showerror(io, e) + errorMsg = String(take!(io)) + st = sprint((io, v) -> show(io, "text/plain", v), stacktrace(catch_backtrace())) + println("") + @warn "Error occurred: $errorMsg\n$st" + println("") + end - if attempt > 5 + else error("reflector failed to generate a thought") end end diff --git a/src/llmfunction.jl b/src/llmfunction.jl index 6956562..6cd628c 100644 --- a/src/llmfunction.jl +++ b/src/llmfunction.jl @@ -164,24 +164,25 @@ function virtualWineCustomerChatbox(a::T1, input::T2 ) attempt = 0 - while attempt <= 5 + while true attempt += 1 - try - result = GeneralUtils.sendReceiveMqttMsg(outgoingMsg; timeout=120) - response = result[:response] + if attempt <= 5 + try + result = GeneralUtils.sendReceiveMqttMsg(outgoingMsg; timeout=120) + response = result[:response] - return (response[:text], response[:select], response[:reward], response[:isterminal]) - catch e - io = IOBuffer() - showerror(io, e) - errorMsg = String(take!(io)) - st = sprint((io, v) -> show(io, "text/plain", v), stacktrace(catch_backtrace())) - println("") - @warn "Error occurred: $errorMsg\n$st" - println("") - end + return (response[:text], response[:select], response[:reward], response[:isterminal]) + catch e + io = IOBuffer() + showerror(io, e) + errorMsg = String(take!(io)) + st = sprint((io, v) -> show(io, "text/plain", v), stacktrace(catch_backtrace())) + println("") + @warn "Error occurred: $errorMsg\n$st" + println("") + end - if attempt > 5 + else error("virtualWineCustomerChatbox failed to get a response") end end