This commit is contained in:
narawat lamaiin
2024-05-07 06:30:24 +07:00
parent 8cc5606ae8
commit 43e7ba3991
4 changed files with 59 additions and 44 deletions

View File

@@ -318,46 +318,46 @@ function reflector()
end
""" Determine whether the state is a terminal state
# """ Determine whether the state is a terminal state
# Arguments
- `state::T`
a game state
# # Arguments
# - `state::T`
# a game state
# Return
- `(isterminalstate, reward)::Tuple{Bool, <:Number}`
# # Return
# - `(isterminalstate, reward)::Tuple{Bool, <:Number}`
# Example
```jldoctest
julia>
```
# # Example
# ```jldoctest
# julia>
# ```
# TODO
[PENDING] add Reflect()
# # TODO
# [PENDING] add Reflect()
# Signature
"""
function isterminal(state::T)::Tuple{Bool, <:Number} where {T<:AbstractDict}
latestObservationKey, _ = GeneralUtils.findHighestIndexKey(state[:thoughtHistory], "observation")
latestObservation = state[:thoughtHistory][latestObservationKey]
# # Signature
# """
# function isterminal(state::T)::Tuple{Bool, <:Number} where {T<:AbstractDict}
# latestObservationKey, _ = GeneralUtils.findHighestIndexKey(state[:thoughtHistory], "observation")
# latestObservation = state[:thoughtHistory][latestObservationKey]
if latestObservation !== nothing
# if latestObservation !== nothing
# terminal condition is when the user select wine by putting <<winename>> in latest observation
if occursin("<<", latestObservation) && occursin(">>", latestObservation)
isterminalstate = true
reward = 1
else
isterminalstate = false
reward = 0
end
else
isterminalstate = false
reward = 0
end
# # terminal condition is when the user select wine by putting <<winename>> in latest observation
# if occursin("<<", latestObservation) && occursin(">>", latestObservation)
# isterminalstate = true
# reward = 1
# else
# isterminalstate = false
# reward = 0
# end
# else
# isterminalstate = false
# reward = 0
# end
return (isterminalstate, reward)
end
# return (isterminalstate, reward)
# end
""" Chat with llm.
@@ -436,7 +436,10 @@ function conversation(a::T, userinput::Dict) where {T<:agent}
# deepcopy the info to prevent modifying the info unintentionally during MCTS planning
:customerinfo=> deepcopy(a.keywordinfo[:customerinfo]),
:storeinfo=> deepcopy(a.keywordinfo[:storeinfo]),
:storeinfo=> deepcopy(a.keywordinfo[:storeinfo]),
:select=> nothing,
:reward=> 0,
:isterminal=> false,
:thoughtHistory=> OrderedDict{Symbol, Any}( # contain question, thought_1, action_1, observation_1, thought_2, ...
:question=> userinput[:text],
)

View File

@@ -125,6 +125,10 @@ end
julia>
```
# TODO
- [] update docs
- [] add to remove <<< user option select >>> and <<| reward |>>
# Signature
"""
function virtualWineCustomerChatbox(a::T1, input::T2)::String where {T1<:agent, T2<:AbstractString}
@@ -158,9 +162,9 @@ function virtualWineCustomerChatbox(a::T1, input::T2)::String where {T1<:agent,
)
@show outgoingMsg
result = GeneralUtils.sendReceiveMqttMsg(outgoingMsg; timeout=120)
response = result[:response][:text]
response = result[:response]
return response
return (response[:text], response[:select], response[:reward], response[:isterminal])
end

View File

@@ -137,7 +137,7 @@ function expand(a::T1, node::MCTSNode, decisionMaker::Function,
try
thoughtDict = decisionMaker(a, node.state)
newNodeKey, newstate, isterminalstate, reward =
newNodeKey, newstate, reward, isterminalstate =
MCTStransition(a, node.state, thoughtDict, isterminal)
# add progressValueEstimator
@@ -181,11 +181,10 @@ function simulate(a, node::MCTSNode, decisionMaker::Function, progressValueEstim
simTrajectoryReward = 0.0
for depth in 1:maxDepth
if node.isterminal
simTrajectoryReward += node.reward
simTrajectoryReward += node.reward
if node.isterminalrd
break
else
simTrajectoryReward += node.reward
expand(a, node, decisionMaker, progressValueEstimator, isterminal, n=n)
node = selectChildNode(node)
end
@@ -268,7 +267,7 @@ function MCTStransition(a::T1, state::T2, thoughtDict::T3, isterminal::Function
actioninput = thoughtDict[:action][:input]
# map action and input() to llm function
response =
response, select, reward, isterminal =
if actionname == "chatbox"
virtualWineCustomerChatbox(a, actioninput) # virtual customer
elseif actionname == "winestock"
@@ -291,11 +290,13 @@ function MCTStransition(a::T1, state::T2, thoughtDict::T3, isterminal::Function
newstate[:thoughtHistory][latestActionKey] = thoughtDict[:action]
latestObservationKey = Symbol("observation_$(nextIndice)")
newstate[:thoughtHistory][latestObservationKey] = response
newstate[:reward] = reward
newstate[:select] = select
newstate[:isterminal] = isterminal
newNodeKey = GeneralUtils.uuid4snakecase()
isterminalstate, reward = isterminal(newstate)
return (newNodeKey, newstate, isterminalstate, reward)
return (newNodeKey, newstate, reward, isterminal)
end