From 6c3ef4414baffccdd6a1a8333f289f8fe7ea2e04 Mon Sep 17 00:00:00 2001 From: narawat lamaiin Date: Mon, 29 Apr 2024 21:47:31 +0700 Subject: [PATCH] update --- src/interface.jl | 8 +++--- src/mcts.jl | 64 ++++++++++++++++++++++++++++++------------------ src/util.jl | 64 +++++++++++++++++++++++------------------------- 3 files changed, 74 insertions(+), 62 deletions(-) diff --git a/src/interface.jl b/src/interface.jl index 47173bf..55be066 100644 --- a/src/interface.jl +++ b/src/interface.jl @@ -118,7 +118,7 @@ function decisionMaker(a::T1, state::T2)::String where {T1<:agent, T2<:AbstractD "Thought_2": "reasoning", ... "Thought_n": "reasoning", - "Action_1": {"action": "action to take", "input": "Action input"}, + "Action_1": {"name": "action to take", "input": "Action input"}, "Observation_1": "result of the action" } @@ -128,13 +128,13 @@ function decisionMaker(a::T1, state::T2)::String where {T1<:agent, T2<:AbstractD "Thought_1": "I have many cars in my inventory suitable for several usage scenarios.", "Thought_2": "It would be better if I knew what the user intends to do with his car.", "Thought_3": "I will ask the user what is the intended usecase", - "Action_1": {"action": "chatbox", "input": "What will you use it for?"} + "Action_1": {"name": "chatbox", "input": "What will you use it for?"} } { "Question": "I'm looking for a sedan.", "Thought_1": "I have many types of sedans in my inventory, each with diverse features.", "Thought_2": "It would be easier to make a recommendation if I knew what feature the user is looking for. I should ask the user.", - "Action_1": {"action": "chatbox", "input": "Do you have any specific feature in mind?"} + "Action_1": {"name": "chatbox", "input": "Do you have any specific feature in mind?"} } $reflect @@ -311,7 +311,7 @@ function conversation(a::T, userinput::Dict) where {T<:agent} :storeinfo=> deepcopy(a.keywordinfo[:storeinfo]), :thoughtHistory=> Dict{Symbol, Any}( # contain question, thought_1, action_1, observation_1, thought_2, ... - :question=> userinput[:text], + :Question=> userinput[:text], ) ) bestplan = runMCTS(a, initialState, decisionMaker, stateValueEstimator, reflector, diff --git a/src/mcts.jl b/src/mcts.jl index a4a82e1..ae8ff2c 100644 --- a/src/mcts.jl +++ b/src/mcts.jl @@ -131,15 +131,11 @@ function expand(a::T1, node::MCTSNode, state::T2, decisionMaker::Function, state ) :Observation_1 => "" """ - latestActionKey = GeneralUtils.findHighestIndexKey(thoughtDict, "Action") - _action = thoughtDict[latestActionKey] - action = _action[:action] - actioninput = _action[:input] - newState = MCTStransition(a, node.state, action, actioninput) #[] Implement your transition function + newstate = MCTStransition(a, node.state, thoughtDict) #[] Implement your transition function - if newState ∉ keys(node.children) - node.children[newState] = MCTSNode(newState, 0, 0.0, Dict{T, MCTSNode}()) + if newstate ∉ keys(node.children) + node.children[newstate] = MCTSNode(newstate, 0, 0.0, Dict{T, MCTSNode}()) end end error("--> expand") @@ -212,41 +208,61 @@ end one of YiemAgent's agent - `state::T2` current game state - - `action::String` - name of LLM's function to be used - - `actioninput::String` - input to LLM function - + - `thoughtDict::T2` + contain Thought, Action, Observation + # Return + - `newstate::AbstractDict` + next game state # Example ```jldoctest -julia> +julia> thoughtDict = Dict( + :Question=> "I want to buy a bottle of wine." + :Thought_1=> "The customer wants to buy a bottle of wine. This is a good start!", + :Action_1=> Dict{Symbol, Any}( + :name=>"Chatbox", + :input=>"What occasion are you buying the wine for?" + ), + :Observation_1 => "" +) ``` # TODO - - [] update docstring - - [WORKING] implement the function + - [x] update docstring + - [TESTING] implement the function # Signature """ -function MCTStransition(a::T1, state::T2, action::T3, - actioninput::T3) where {T1<:agent, T2<:AbstractDict, T3<:AbstractString} - +function MCTStransition(a::T1, state::T2, thoughtDict::T2)::AbstractDict where {T1<:agent, T2<:AbstractDict} + latestThoughtKey, latestindice = GeneralUtils.findHighestIndexKey(thoughtDict, "Thought") + latestActionKey = GeneralUtils.findHighestIndexKey(thoughtDict, "Action") + _action = thoughtDict[latestActionKey] + actionname = _action[:name] + actioninput = _action[:input] # map action and input() to llm function - result = - if action == "chatbox" - virtualWineCustomerChatbox(a, input) # user virtu - elseif action == "winestock" + response = + if actionname == "chatbox" + virtualWineCustomerChatbox(a, actioninput) # user virtu + elseif actionname == "winestock" - elseif action == "finish" + elseif actionname == "finish" else end + + # add Thought, action, observation to thoughtHistory + newstate = deepcopy(state) + newstate[:thoughtHistory][latestThoughtKey] = thoughtDict[latestThoughtKey] + newstate[:thoughtHistory][latestActionKey] = thoughtDict[latestActionKey] + latestObservationKey = Symbol("Observation_$(latestindice)") + newstate[:thoughtHistory][latestObservationKey] = response + + error("--> transition") - + return newstate end """ diff --git a/src/util.jl b/src/util.jl index a707efa..2fa99d2 100644 --- a/src/util.jl +++ b/src/util.jl @@ -11,44 +11,40 @@ using ..type """ Clear agent chat history. - Arguments\n - ----- - a::agent - an agent +# Arguments + - `a::agent` + an agent - Return\n - ----- - nothing +# Return + - nothing - Example\n - ----- - ```jldoctest - julia> using YiemAgent, MQTTClient, GeneralUtils - julia> client, connection = MakeConnection("test.mosquitto.org", 1883) - julia> connect(client, connection) - julia> msgMeta = GeneralUtils.generate_msgMeta("testtopic") - julia> agentConfig = Dict( - :receiveprompt=>Dict( - :mqtttopic=> "testtopic/receive", - ), - :receiveinternal=>Dict( - :mqtttopic=> "testtopic/internal", - ), - :text2text=>Dict( - :mqtttopic=> "testtopic/text2text", - ), +# Example +```jldoctest +julia> using YiemAgent, MQTTClient, GeneralUtils +julia> client, connection = MakeConnection("test.mosquitto.org", 1883) +julia> connect(client, connection) +julia> msgMeta = GeneralUtils.generate_msgMeta("testtopic") +julia> agentConfig = Dict( + :receiveprompt=>Dict( + :mqtttopic=> "testtopic/receive", + ), + :receiveinternal=>Dict( + :mqtttopic=> "testtopic/internal", + ), + :text2text=>Dict( + :mqtttopic=> "testtopic/text2text", + ), + ) +julia> a = YiemAgent.sommelier( + client, + msgMeta, + agentConfig, ) - julia> a = YiemAgent.sommelier( - client, - msgMeta, - agentConfig, - ) - julia> YiemAgent.addNewMessage(a, "user", "hello") - julia> YiemAgent.clearhistory(a) - ``` +julia> YiemAgent.addNewMessage(a, "user", "hello") +julia> YiemAgent.clearhistory(a) +``` - Signature\n - ----- +# Signature """ function clearhistory(a::T) where {T<:agent} empty!(a.chathistory)