This commit is contained in:
narawat lamaiin
2024-05-09 10:38:51 +07:00
parent d90d430c84
commit ed63c19e66

View File

@@ -176,8 +176,12 @@ function decisionMaker(a::T1, state::T2)::Dict{Symbol, Any} where {T1<:agent, T2
)
)
_response = GeneralUtils.sendReceiveMqttMsg(outgoingMsg)
_thoughtJsonStr = _response[:response][:text]
attempt = 0
while attempt <= 5
attempt += 1
try
response = GeneralUtils.sendReceiveMqttMsg(outgoingMsg)
_responseJsonStr = response[:response][:text]
expectedJsonExample =
"""
Here is an expected JSON format:
@@ -187,10 +191,29 @@ function decisionMaker(a::T1, state::T2)::Dict{Symbol, Any} where {T1<:agent, T2
"observation": "..."
}
"""
thoughtJsonStr = jsoncorrection(a, _thoughtJsonStr, expectedJsonExample)
thoughtDict = copy(JSON3.read(thoughtJsonStr))
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]
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
error("DecisionMaker failed to generate a thought")
end
end
end
@@ -286,17 +309,39 @@ function progressValueEstimator(a::T1, state::T2)::Tuple{String, Integer} where
)
)
_response = GeneralUtils.sendReceiveMqttMsg(outgoingMsg)
_thoughtJsonStr = _response[:response][:text]
attempt = 0
while attempt <= 5
attempt += 1
try
response = GeneralUtils.sendReceiveMqttMsg(outgoingMsg)
_responseJsonStr = response[:response][:text]
expectedJsonExample =
"""
Here is an expected JSON format:
{"evaluation": "...", "score": "..."}
"""
resultJsonStr = jsoncorrection(a, _thoughtJsonStr, expectedJsonExample)
resultDict = copy(JSON3.read(resultJsonStr))
responseJsonStr = jsoncorrection(a, _responseJsonStr, expectedJsonExample)
evaluationDict = copy(JSON3.read(responseJsonStr))
return resultDict[:evaluation], resultDict[:score]
# check if dict has all required value
dummya::AbstractString = evaluationDict[:evaluation]
dummyb::AbstractString = 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
if attempt > 5
error("progressValueEstimator failed to generate a thought")
end
end
end
@@ -313,8 +358,8 @@ julia>
# TODO
- [] update docstring
- [WORKING] implement the function
- [PENDING] add try block. check result that it is expected before returning
- [x] implement the function
- [x] add try block. check result that it is expected before returning
# Signature
"""
@@ -400,21 +445,38 @@ function reflector(a::T1, state::T2)::String where {T1<:agent, T2<:AbstractDict}
)
)
_response = GeneralUtils.sendReceiveMqttMsg(outgoingMsg)
_thoughtJsonStr = _response[:response][:text]
attempt = 0
while attempt <= 5
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))
# expectedJsonExample =
# """
# Here is an expected JSON format:
# {"evaluation": "...", "score": "..."}
# """
# resultJsonStr = jsoncorrection(a, _thoughtJsonStr, expectedJsonExample)
# resultDict = copy(JSON3.read(resultJsonStr))
# check if dict has all required value
dummya::AbstractString = reflectionDict[:reflection]
# return resultDict[:evaluation], resultDict[:score]
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 _thoughtJsonStr
if attempt > 5
error("reflector failed to generate a thought")
end
end
end