This commit is contained in:
narawat lamaiin
2024-08-27 13:22:53 +07:00
parent a80b8e0260
commit ca0f9cd53f
5 changed files with 70 additions and 141 deletions

View File

@@ -1,6 +1,6 @@
module interface
export addNewMessage, conversation, decisionMaker, evaluator, reflector
export addNewMessage, conversation, decisionMaker, evaluator, reflector, generatechat
# isterminal,
using JSON3, DataStructures, Dates, UUIDs, HTTP, Random, MQTTClient, PrettyPrinting, Serialization
@@ -234,7 +234,8 @@ function decisionMaker(a::T; recent::Integer=5)::Dict{Symbol, Any} where {T<:age
"""
Your name is $(a.name). You are a helpful assistant acting as a polite, website-based sommelier for Yiem's online wine store.
Your goal includes:
1) Help the user select the best wines from your inventory that align with the user's preferences.
1) Get to know the user preferences about wine
2) Help them select the best wines from your inventory that align with their preferences
Your responsibility includes:
1) Make an informed decision about what you need to do to achieve the goal
@@ -251,18 +252,12 @@ function decisionMaker(a::T; recent::Integer=5)::Dict{Symbol, Any} where {T<:age
You must follow the following guidelines:
- Generally speaking, your inventory has some wines from France, the United States, Australia, Spain, and Italy, but you won't know which wines your store carries until you check your inventory.
- All wines in your inventory are always in stock.
- Use the "understand-then-check" inventory strategy to understand the user, as there are many wines in the inventory.
- If the user interrupts, prioritize the user
- Do not ask the user about wine's flavor e.g. floral, citrusy, nutty or some thing similar as these terms cannot be used to search the database.
- Once you present the wines to the user, ask the user if they need any further assistance. Do not offer any additional services. If the user doesn't need any further assistance, say goodbye and invite them to come back next time.
- Once the user has selected their wine, ask the user if they need any further assistance. Do not offer any additional services. If the user doesn't need any further assistance, say goodbye and invite them to come back next time.
You should follow the following guidelines as you see fit:
- If the user interrupts, prioritize the user.
- If you don't already know, find out the user's budget.
- If you don't already know, find out the type of wine the user is looking for, such as red, white, sparkling, rose, dessert, fortified.
- If you don't already know, find out the occasion for which the user is buying wine.
- If you don't already know, find out the characteristics of wine the user is looking for, such as tannin, sweetness, intensity, acidity.
- If you don't already know, find out what food will be served with wine.
- If you haven't already, introduce the wines you found in the database to the user first.
You should follow the following guidelines:
- Identifying at least four preferences before checking inventory significantly improves search results
You should then respond to the user with interleaving Understanding, Reasoning, Plan, Action:
1) Understanding:
@@ -271,13 +266,13 @@ function decisionMaker(a::T; recent::Integer=5)::Dict{Symbol, Any} where {T<:age
- State your step by step reasoning about the current situation.
2) Plan: Based on the current situation, state a complete plan to complete the task. Be specific.
3) Action_name (Must be aligned with your plan): The name of the action which can be one of the following functions:
- CHATBOX which you can use to generate conversation in order to communicate with the user. The input is your intentions for the dialogue. Be specific.
- CHATBOX which you can use to talk with the user. The input is your intentions for the dialogue. Be specific.
- CHECKINVENTORY which you can use to check info about wine in your inventory. The input is a search term in verbal English.
Good query example: black car, a stereo, 200 mile range, electric motor.
- PRESENTBOX which you can use to introduce / suggest / recommend wines you just found in the database to the user. It is better than the CHATBOX function for presenting wines.
- PRESENTBOX which you can use to introduce / suggest / recommend wines you just found in the database to the user.
The input is instructions on how you want the presentation to be conducted.
Here are some input example,
"First, provide detailed introductions of the wines.
Here are some input examples,
"First, provide detailed introductions of Zena Crown, Schrader Cabernet Sauvignon.
Second, if there are multiple wines, offer a thorough comparison of each option, highlighting their differences.
Third, explain the potential impact each option could bring to the user."
- ENDCONVERSATION which you can use when you want to finish the conversation with the user. The input is "NA".
@@ -875,7 +870,7 @@ julia> response = ChatAgent.conversation(newAgent, "Hi! how are you?")
# Signature
"""
function conversation(a::T, userinput::Dict) where {T<:agent}
println("--> conver 1 ", @__FILE__, " ", @__LINE__)
# place holder
actionname = nothing
result = nothing
@@ -887,7 +882,7 @@ function conversation(a::T, userinput::Dict) where {T<:agent}
else
# add usermsg to a.chathistory
addNewMessage(a, "user", userinput[:text])
println("--> conver 2 ", @__FILE__, " ", @__LINE__)
# add user activity to events memory
push!(a.memory[:events],
eventdict(;
@@ -897,7 +892,7 @@ function conversation(a::T, userinput::Dict) where {T<:agent}
action_or_dialogue= userinput[:text],
)
)
println("--> conver 3 ", @__FILE__, " ", @__LINE__)
# use dummy memory to check generatechat() for halucination (checking inventory)
for i in 1:3
actionname, result = think(a)
@@ -905,7 +900,7 @@ function conversation(a::T, userinput::Dict) where {T<:agent}
break
end
end
println("--> conver 4 ", @__FILE__, " ", @__LINE__)
# thought will be added to chat model via context
chatresponse = generatechat(a.memory, a.chathistory, a.text2textInstructLLM)
@@ -952,7 +947,8 @@ function think(a::T)::NamedTuple{(:actionname, :result), Tuple{String, String}}
# map action and input() to llm function
response =
if actionname == "CHATBOX"
(result=actioninput, errormsg=nothing, success=true)
input = "My plan: $(thoughtDict[:plan])"
(result=input, errormsg=nothing, success=true)
elseif actionname == "CHECKINVENTORY"
checkinventory(a, actioninput)
elseif actionname == "PRESENTBOX"
@@ -1023,14 +1019,16 @@ function generatechat(memory::Dict, chathistory::Vector, text2textInstructLLM::F
systemmsg =
"""
Your name is "Jenie". You are a helpful assistant acting as a polite, website-based sommelier for an online wine store.
Your goal is: Recommend the best wines from your inventory that align with the user's preferences.
You are currently talking with the user.
Your goal includes:
1) Help the user select the best wines from your inventory that align with the user's preferences.
Your responsibility includes:
1) Given the situation, convey your thoughts to the user.
Your responsibility does not include:
1) Processing sales orders or engaging in any other sales-related activities.
2) Ordering the wines.
Your responsibility do not include:
1) Asking or guiding the user to make a purchase
2) Processing sales orders or engaging in any other sales-related activities
At each round of conversation, you will be given the current situation:
Your ongoing conversation with the user: ...
@@ -1042,6 +1040,7 @@ function generatechat(memory::Dict, chathistory::Vector, text2textInstructLLM::F
You should follow the following guidelines:
- Focus on the latest conversation.
- If the user interrupts, prioritize the user
You should then respond to the user with:
1) Mentioning_wine: Are you going to mentioning specific wine name to the user? Can be "Yes" or "No"
@@ -1216,16 +1215,20 @@ function generatequestion(a, text2textInstructLLM::Function; recent=nothing)::St
- Once the user has selected their wine, ask the user if they need any further assistance. Do not offer any additional services. If the user doesn't need any further assistance, say goodbye and invite them to come back next time.
You should follow the following guidelines:
- Focus on the latest conversation.
- Do not ask the user about wine's flavor e.g. floral, citrusy, nutty or some thing similar as these terms cannot be used to search the database.
- All wines in your inventory are always in stock.
- Focus on the latest conversation
- If the user interrupts, prioritize the user
- If you don't already know, find out the user's budget
- If you don't already know, find out the type of wine the user is looking for, such as red, white, sparkling, rose, dessert, fortified
- If you don't already know, find out the occasion for which the user is buying wine
- If you don't already know, find out the characteristics of wine the user is looking for, such as tannin, sweetness, intensity, acidity
- If you don't already know, find out what food will be served with wine
- If you haven't already, introduce the wines you found in the database to the user first
You should then respond to the user with:
1) Understanding:
- State your understanding about the current situation
2) Q: Given the situation, "ask yourself" at least five, but no more than ten, questions
3) A: Given the situation, "answer to yourself" the best you can
4) Note: Additional info you want to say
You must only respond in format as described below:
Understanding: ...
@@ -1236,7 +1239,6 @@ function generatequestion(a, text2textInstructLLM::Function; recent=nothing)::St
Q3: ...
A3: ...
...
Note: ...
Here are some examples:
Q: The user is asking for a cappuccino. Do I have it at my cafe?
@@ -1309,7 +1311,7 @@ function generatequestion(a, text2textInstructLLM::Function; recent=nothing)::St
end
# response = string(split(response, "Please")[1]) # LLM usually add comments which is no need.
responsedict = GeneralUtils.textToDict(response,
["Understanding", "Q1", "Note"],
["Understanding", "Q1"],
rightmarker=":", symbolkey=true, lowercasekey=true)
response = "Q1: " * responsedict[:q1]
println("--> generatequestion ", @__FILE__, " ", @__LINE__)

View File

@@ -406,6 +406,16 @@ function extractWineAttributes_1(a::T1, input::T2)::String where {T1<:agent, T2<
try
response = a.text2textInstructLLM(prompt)
# check wheter all attributes are in the response
for word in attributes
if !occursin(word, response)
noise = String(rand('a':'z', 5))
errornote = "Background noise $noise"
error("$word attribute is missing")
end
end
responsedict = GeneralUtils.textToDict(response, attributes, rightmarker=":", symbolkey=true)
for i attributes

View File

@@ -50,6 +50,9 @@ julia> YiemAgent.clearhistory(a)
"""
function clearhistory(a::T) where {T<:agent}
empty!(a.chathistory)
empty!(a.memory[:shortmem])
empty!(a.memory[:events])
a.memory[:chatbox] = ""
end