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

View File

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