This commit is contained in:
narawat lamaiin
2024-04-29 21:47:31 +07:00
parent 3316ec4ecf
commit 6c3ef4414b
3 changed files with 74 additions and 62 deletions

View File

@@ -118,7 +118,7 @@ function decisionMaker(a::T1, state::T2)::String where {T1<:agent, T2<:AbstractD
"Thought_2": "reasoning", "Thought_2": "reasoning",
... ...
"Thought_n": "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" "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_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_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", "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.", "Question": "I'm looking for a sedan.",
"Thought_1": "I have many types of sedans in my inventory, each with diverse features.", "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.", "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 $reflect
@@ -311,7 +311,7 @@ function conversation(a::T, userinput::Dict) where {T<:agent}
:storeinfo=> deepcopy(a.keywordinfo[:storeinfo]), :storeinfo=> deepcopy(a.keywordinfo[:storeinfo]),
:thoughtHistory=> Dict{Symbol, Any}( # contain question, thought_1, action_1, observation_1, thought_2, ... :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, bestplan = runMCTS(a, initialState, decisionMaker, stateValueEstimator, reflector,

View File

@@ -131,15 +131,11 @@ function expand(a::T1, node::MCTSNode, state::T2, decisionMaker::Function, state
) )
:Observation_1 => "" :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) if newstate keys(node.children)
node.children[newState] = MCTSNode(newState, 0, 0.0, Dict{T, MCTSNode}()) node.children[newstate] = MCTSNode(newstate, 0, 0.0, Dict{T, MCTSNode}())
end end
end end
error("--> expand") error("--> expand")
@@ -212,41 +208,61 @@ end
one of YiemAgent's agent one of YiemAgent's agent
- `state::T2` - `state::T2`
current game state current game state
- `action::String` - `thoughtDict::T2`
name of LLM's function to be used contain Thought, Action, Observation
- `actioninput::String`
input to LLM function
# Return # Return
- `newstate::AbstractDict`
next game state
# Example # Example
```jldoctest ```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 # TODO
- [] update docstring - [x] update docstring
- [WORKING] implement the function - [TESTING] implement the function
# Signature # Signature
""" """
function MCTStransition(a::T1, state::T2, action::T3, function MCTStransition(a::T1, state::T2, thoughtDict::T2)::AbstractDict where {T1<:agent, T2<:AbstractDict}
actioninput::T3) where {T1<:agent, T2<:AbstractDict, T3<:AbstractString} 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 # map action and input() to llm function
result = response =
if action == "chatbox" if actionname == "chatbox"
virtualWineCustomerChatbox(a, input) # user virtu virtualWineCustomerChatbox(a, actioninput) # user virtu
elseif action == "winestock" elseif actionname == "winestock"
elseif action == "finish" elseif actionname == "finish"
else else
end end
error("--> transition")
# 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 end
""" """

View File

@@ -11,44 +11,40 @@ using ..type
""" Clear agent chat history. """ Clear agent chat history.
Arguments\n # Arguments
----- - `a::agent`
a::agent an agent
an agent
Return\n # Return
----- - nothing
nothing
Example\n # Example
----- ```jldoctest
```jldoctest julia> using YiemAgent, MQTTClient, GeneralUtils
julia> using YiemAgent, MQTTClient, GeneralUtils julia> client, connection = MakeConnection("test.mosquitto.org", 1883)
julia> client, connection = MakeConnection("test.mosquitto.org", 1883) julia> connect(client, connection)
julia> connect(client, connection) julia> msgMeta = GeneralUtils.generate_msgMeta("testtopic")
julia> msgMeta = GeneralUtils.generate_msgMeta("testtopic") julia> agentConfig = Dict(
julia> agentConfig = Dict( :receiveprompt=>Dict(
:receiveprompt=>Dict( :mqtttopic=> "testtopic/receive",
:mqtttopic=> "testtopic/receive", ),
), :receiveinternal=>Dict(
:receiveinternal=>Dict( :mqtttopic=> "testtopic/internal",
:mqtttopic=> "testtopic/internal", ),
), :text2text=>Dict(
:text2text=>Dict( :mqtttopic=> "testtopic/text2text",
:mqtttopic=> "testtopic/text2text", ),
), )
julia> a = YiemAgent.sommelier(
client,
msgMeta,
agentConfig,
) )
julia> a = YiemAgent.sommelier( julia> YiemAgent.addNewMessage(a, "user", "hello")
client, julia> YiemAgent.clearhistory(a)
msgMeta, ```
agentConfig,
)
julia> YiemAgent.addNewMessage(a, "user", "hello")
julia> YiemAgent.clearhistory(a)
```
Signature\n # Signature
-----
""" """
function clearhistory(a::T) where {T<:agent} function clearhistory(a::T) where {T<:agent}
empty!(a.chathistory) empty!(a.chathistory)