This commit is contained in:
narawat lamaiin
2024-05-06 13:50:08 +07:00
parent a7fdbcede9
commit cfca2b1839
5 changed files with 188 additions and 113 deletions

View File

@@ -58,12 +58,12 @@ end
# Example
```jldoctest
julia> output_thoughtDict = Dict(
:Thought_1 => "The customer wants to buy a bottle of wine. This is a good start!",
:Action_1 => Dict{Symbol, Any}(
:thought_1 => "The customer wants to buy a bottle of wine. This is a good start!",
:action_1 => Dict{Symbol, Any}(
:action=>"Chatbox",
:input=>"What occasion are you buying the wine for?"
),
:Observation_1 => ""
:observation_1 => ""
)
```
@@ -98,16 +98,6 @@ function decisionMaker(a::T1, state::T2)::Dict{Symbol, Any} where {T1<:agent, T2
# (trajectories)
# """
responseformat =
"""
You should only respond in JSON format as describe below:
{
"Thought": "your reasoning",
"Action": {"name": "action to take", "input": "Action input"},
"Observation": "result of the action"
}
"""
_prompt =
"""
You are a helpful sommelier working for a wine store.
@@ -127,31 +117,32 @@ function decisionMaker(a::T1, state::T2)::Dict{Symbol, Any} where {T1<:agent, T2
Thought can reason about the current situation, and Action can be three types:
1) winestock[query], which you can use to find wine in your inventory. The more input data the better.
2) chatbox[text], which you can use to interact with the user.
3) reccommendbox[answer], which returns your wine reccommendation to the user.
3) recommendbox[answer], which returns your wine reccommendation to the user.
$responseformat
You should only respond in JSON format as describe below:
{
"thought": "your reasoning",
"action": {"name": "action to take", "input": "Action input"},
"observation": "result of the action"
}
Here are some examples:
{
"Question": "I would like to buy a sedan with 8 seats.",
"Thought_1": "Our showroom carries various vehicle model. But I'm not sure whether we have a models that fits the user demand, I need to check our inventory.",
"Action_1": {"name": "inventory", "input": "sedan with 8 seats."},
"Observation_1": "Several model has 8 seats. Available color are black, red green"
"question": "I would like to buy a sedan with 8 seats.",
"thought_1": "Our showroom carries various vehicle model. But I'm not sure whether we have a models that fits the user demand, I need to check our inventory.",
"action_1": {"name": "inventory", "input": "sedan with 8 seats."},
"observation_1": "Several model has 8 seats. Available color are black, red green"
}
{
"Thought_2": "I have to ask the user what color he likes.",
"Action_2": {"name": "chatbox", "input": "Which color do you like?"}
"Observation_2": "I'll take black."
"thought": "I have a few color for the user to choose from. I will ask him what color he likes.",
"action": {"name": "chatbox", "input": "Which color do you like?"}
"observation": "I'll take black."
}
{
"Thought_3": "There is only one model that fits the user preference. It's Yiem model A",
"Action_3": {"name": "recommendation", "input": "I recommend a Yiem model A"}
}
Let's begin!
$(JSON3.write(state[:thoughtHistory]))
{Thought
{thought
"""
# apply LLM specific instruct format
@@ -190,9 +181,9 @@ function decisionMaker(a::T1, state::T2)::Dict{Symbol, Any} where {T1<:agent, T2
"""
Here is an expected JSON format:
{
"Thought": "...",
"Action": {"name": "...", "input": "..."},
"Observation": "..."
"thought": "...",
"action": {"name": "...", "input": "..."},
"observation": "..."
}
"""
thoughtJsonStr = jsoncorrection(a, _thoughtJsonStr, expectedJsonExample)
@@ -224,13 +215,6 @@ julia>
# Signature
"""
function progressValueEstimator(a::T1, state::T2)::Tuple{String, Integer} where {T1<:agent, T2<:AbstractDict}
responseformat =
"""
You should only respond in JSON format as describe below:
{
"Evaluation": {"evaluation": "your evaluation", "score": "your evaluation score"}
}
"""
_prompt =
"""
@@ -239,7 +223,7 @@ function progressValueEstimator(a::T1, state::T2)::Tuple{String, Integer} where
the current situation and actions that can be three types:
1) winestock[query], which you can use to find wine in your inventory.
2) chatbox[text], which you can use to interact with the user.
3) reccommendbox[answer], which returns your wine reccommendation to the user.
3) recommendbox[answer], which returns your wine reccommendation to the user.
Given a question and a trajectory, evaluate its correctness and provide your reasoning and
analysis in detail. Focus on the latest thought, action, and observation. Incomplete trajectories
@@ -247,26 +231,25 @@ function progressValueEstimator(a::T1, state::T2)::Tuple{String, Integer} where
yet. Do not generate additional thoughts or actions. Then ending with the correctness score s
where s is an integer from 1 to 10.
$responseformat
You should only respond in JSON format as describe below:
{"evaluation": "your evaluation", "score": "your evaluation score"}
Here are some examples:
{
"Question": "I'm looking for a sedan with an automatic driving feature.",
"Thought_1": "I have many types of sedans in my inventory, each with diverse features.",
"Thought_2": "But there is only 1 model that has the feature customer wanted.",
"Thought_3": "I should check our inventory first to see if we have it.",
"Action_1": {"name": "inventory", "input": "Yiem model A"},
"Observation_1": "Yiem model A is in stock."
"question": "I'm looking for a sedan with an automatic driving feature.",
"thought_1": "I have many types of sedans in my inventory, each with diverse features.",
"thought_2": "But there is only 1 model that has the feature customer wanted.",
"thought_3": "I should check our inventory first to see if we have it.",
"action_1": {"name": "inventory", "input": "Yiem model A"},
"observation_1": "Yiem model A is in stock."
}
{
"Evaluation": {"evaluation": "This trajectory is correct as it is reasonable to check an inventory for info provided in the question.
{"evaluation": "This trajectory is correct as it is reasonable to check an inventory for info provided in the question.
It is also better to have simple searches corresponding to a single entity, making this the best action.",
"score": 10}
}
Let's begin!:
$(JSON3.write(state[:thoughtHistory]))
{Evaluation
{evaluation
"""
# apply LLM specific instruct format
@@ -304,15 +287,12 @@ function progressValueEstimator(a::T1, state::T2)::Tuple{String, Integer} where
expectedJsonExample =
"""
Here is an expected JSON format:
{
"Evaluation": {"evaluation": "...", "score": "..."}
}
{"evaluation": "...", "score": "..."}
"""
thoughtJsonStr = jsoncorrection(a, _thoughtJsonStr, expectedJsonExample)
thoughtDict = copy(JSON3.read(thoughtJsonStr))
evaluation = thoughtDict[:Evaluation]
resultJsonStr = jsoncorrection(a, _thoughtJsonStr, expectedJsonExample)
resultDict = copy(JSON3.read(resultJsonStr))
return evaluation[:evaluation], evaluation[:score]
return resultDict[:evaluation], resultDict[:score]
end
@@ -355,7 +335,7 @@ julia>
# Signature
"""
function isterminal(state::T)::Tuple{Bool, <:Number} where {T<:AbstractDict}
latestObservationKey, _ = GeneralUtils.findHighestIndexKey(state[:thoughtHistory], "Observation")
latestObservationKey, _ = GeneralUtils.findHighestIndexKey(state[:thoughtHistory], "observation")
latestObservation = state[:thoughtHistory][latestObservationKey]
if latestObservation !== nothing
@@ -455,7 +435,7 @@ function conversation(a::T, userinput::Dict) where {T<:agent}
:customerinfo=> deepcopy(a.keywordinfo[:customerinfo]),
:storeinfo=> deepcopy(a.keywordinfo[:storeinfo]),
:thoughtHistory=> OrderedDict{Symbol, Any}( # contain question, thought_1, action_1, observation_1, thought_2, ...
:Question=> userinput[:text],
:question=> userinput[:text],
)
)
bestplan = runMCTS(a, initialState, decisionMaker, progressValueEstimator, reflector,