This commit is contained in:
narawat lamaiin
2025-01-15 06:13:18 +07:00
parent a29e8049a7
commit 2206831bab
4 changed files with 298 additions and 40 deletions

View File

@@ -162,7 +162,7 @@ function decisionMaker(a::T; recent::Integer=5)::Dict{Symbol,Any} where {T<:agen
Your responsibility excludes: Your responsibility excludes:
1) Asking or guiding the user to make a purchase 1) Asking or guiding the user to make a purchase
2) Processing sales orders or engaging in any other sales-related activities 2) Processing sales orders or engaging in any other sales-related activities
3) Answering questions and offering additional services beyond just recommendations, such as discount, reward program, promotion, delivery, box, gift wrapping or packaging, personalized messages. For these, inform customers that they can reach out to our sales team at the store. 3) Answering questions and offering additional services beyond just recommendations.
At each round of conversation, you will be given the current situation: At each round of conversation, you will be given the current situation:
Your recent events: latest 5 events of the situation Your recent events: latest 5 events of the situation
@@ -180,6 +180,7 @@ function decisionMaker(a::T; recent::Integer=5)::Dict{Symbol,Any} where {T<:agen
- When searching an inventory, search as broadly as possible based on the information you have gathered so far. - When searching an inventory, search as broadly as possible based on the information you have gathered so far.
- Encourage the customer to explore different options and try new things. - Encourage the customer to explore different options and try new things.
- Sometimes, the item a user desires might not be available in your inventory. In such cases, inform the user that the item is unavailable and suggest an alternative instead. - Sometimes, the item a user desires might not be available in your inventory. In such cases, inform the user that the item is unavailable and suggest an alternative instead.
- If a customer requests information about discounts, quantity, rewards programs, promotions, delivery options, boxes, gift wrapping, packaging, or personalized messages, please inform them that they can contact our sales team at the store.
For your information: For your information:
- vintage 0 means non-vintage. - vintage 0 means non-vintage.
@@ -692,7 +693,7 @@ julia> response = ChatAgent.conversation(newAgent, "Hi! how are you?")
# Signature # Signature
""" """
function conversation(a::sommelier, userinput::Dict) function conversation(a::sommelier, userinput::Dict; maximumMsg=50)
# place holder # place holder
actionname = nothing actionname = nothing
@@ -705,7 +706,7 @@ function conversation(a::sommelier, userinput::Dict)
return "Okay. What shall we talk about?" return "Okay. What shall we talk about?"
else else
# add usermsg to a.chathistory # add usermsg to a.chathistory
addNewMessage(a, "user", userinput[:text]) addNewMessage(a, "user", userinput[:text]; maximumMsg=maximumMsg)
# add user activity to events memory # add user activity to events memory
push!(a.memory[:events], push!(a.memory[:events],
@@ -727,13 +728,13 @@ function conversation(a::sommelier, userinput::Dict)
end end
end end
addNewMessage(a, "assistant", chatresponse) addNewMessage(a, "assistant", chatresponse; maximumMsg=maximumMsg)
return chatresponse return chatresponse
end end
end end
function conversation(a::companion, userinput::Dict; maximumMsg=30) function conversation(a::companion, userinput::Dict; maximumMsg=50)
chatresponse = nothing chatresponse = nothing
if userinput[:text] == "newtopic" if userinput[:text] == "newtopic"
@@ -741,7 +742,7 @@ function conversation(a::companion, userinput::Dict; maximumMsg=30)
return "Okay. What shall we talk about?" return "Okay. What shall we talk about?"
else else
# add usermsg to a.chathistory # add usermsg to a.chathistory
addNewMessage(a, "user", userinput[:text]) addNewMessage(a, "user", userinput[:text]; maximumMsg=maximumMsg)
# add user activity to events memory # add user activity to events memory
push!(a.memory[:events], push!(a.memory[:events],
@@ -754,7 +755,7 @@ function conversation(a::companion, userinput::Dict; maximumMsg=30)
) )
chatresponse = generatechat(a) chatresponse = generatechat(a)
addNewMessage(a, "assistant", chatresponse; maximumMsg=30) addNewMessage(a, "assistant", chatresponse; maximumMsg=maximumMsg)
push!(a.memory[:events], push!(a.memory[:events],
eventdict(; eventdict(;
@@ -786,7 +787,7 @@ julia>
""" """
function think(a::T)::NamedTuple{(:actionname, :result),Tuple{String,String}} where {T<:agent} function think(a::T)::NamedTuple{(:actionname, :result),Tuple{String,String}} where {T<:agent}
a.memory[:recap] = generateSituationReport(a, a.func[:text2textInstructLLM]; skiprecent=3) a.memory[:recap] = generateSituationReport(a, a.func[:text2textInstructLLM]; skiprecent=0)
thoughtDict = decisionMaker(a; recent=3) thoughtDict = decisionMaker(a; recent=3)
actionname = thoughtDict[:action_name] actionname = thoughtDict[:action_name]
@@ -928,7 +929,6 @@ function generatechat(a::sommelier, thoughtDict)
- If the user interrupts, prioritize the user - If the user interrupts, prioritize the user
- Medium and full-bodied red wines should not be paired with spicy foods. - Medium and full-bodied red wines should not be paired with spicy foods.
You should then respond to the user with: You should then respond to the user with:
1) Chat: Given the situation, How would you respond to the user to express your thoughts honestly and keep the conversation going smoothly? 1) Chat: Given the situation, How would you respond to the user to express your thoughts honestly and keep the conversation going smoothly?
@@ -1119,9 +1119,9 @@ function generatequestion(a, text2textInstructLLM::Function; recent=nothing)::St
Your responsibility does not include: Your responsibility does not include:
1) Processing sales orders or engaging in any other sales-related activities. 1) Processing sales orders or engaging in any other sales-related activities.
2) Answering questions and offering additional services beyond just recommendations.
At each round of conversation, you will be given the current situation: At each round of conversation, you will be given the current situation:
Your status: your current status
Recap: recap of what has happened so far Recap: recap of what has happened so far
Your recent events: latest 5 events of the situation Your recent events: latest 5 events of the situation
@@ -1144,6 +1144,7 @@ function generatequestion(a, text2textInstructLLM::Function; recent=nothing)::St
- 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. - 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 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. - 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.
- Medium and full-bodied red wines should not be paired with spicy foods. - Medium and full-bodied red wines should not be paired with spicy foods.
- If a customer requests information about discounts, quantity, rewards programs, promotions, delivery options, boxes, gift wrapping, packaging, or personalized messages, please inform them that they can contact our sales team at the store.
You should then respond to the user with: You should then respond to the user with:
1) Understanding: 1) Understanding:
@@ -1222,11 +1223,22 @@ function generatequestion(a, text2textInstructLLM::Function; recent=nothing)::St
errornote = "" errornote = ""
response = nothing # store for show when error msg show up response = nothing # store for show when error msg show up
#[WORKING]
recap =
if a.memory[:recap] === nothing
"None"
else
if length(a.memory[:events]) > recent
GeneralUtils.dictToString(a.memory[:recap][1:end-recent])
else
"None"
end
end
for attempt in 1:10 for attempt in 1:10
usermsg = usermsg =
""" """
Your status: $(GeneralUtils.dict_to_string(a.memory[:state])) Recap: $recap)
Recap: $(a.memory[:recap])
Your recent events: $timeline Your recent events: $timeline
$errornote $errornote
""" """
@@ -1283,6 +1295,83 @@ function generatequestion(a, text2textInstructLLM::Function; recent=nothing)::St
end end
# function generateSituationReport(a, text2textInstructLLM::Function; skiprecent::Integer=0
# )::Dict
# systemmsg =
# """
# You are an assistant being in the given events.
# Your task is to writes a summary for each event in an ongoing, interleaving series.
# At each round of conversation, you will be given the situation:
# Total events: number of events you need to summarize.
# Events timeline: ...
# Context: ...
# You should then respond to the user with:
# event: a detailed summary for each event without exaggerated details.
# You must only respond in format as described below:
# Event_1: ...
# Event_2: ...
# ...
# Here are some examples:
# Event_1: The user ask me about where to buy a toy.
# Event_2: I told the user to go to the store at 2nd floor.
# Let's begin!
# """
# if length(a.memory[:events]) <= skiprecent
# return Dict(:recap => "None")
# end
# events = deepcopy(a.memory[:events][1:end-skiprecent])
# timeline = ""
# for (i, event) in enumerate(events)
# if event[:outcome] === nothing
# timeline *= "$i) $(event[:subject])> $(event[:actioninput])\n"
# else
# timeline *= "$i) $(event[:subject])> $(event[:actioninput]) $(event[:outcome])\n"
# end
# end
# errornote = ""
# response = nothing # store for show when error msg show up
# for attempt in 1:10
# usermsg = """
# Total events: $(length(events))
# Events timeline: $timeline
# $errornote
# """
# _prompt =
# [
# Dict(:name => "system", :text => systemmsg),
# Dict(:name => "user", :text => usermsg)
# ]
# # put in model format
# prompt = GeneralUtils.formatLLMtext(_prompt; formatname="llama3instruct")
# prompt *= """
# <|start_header_id|>assistant<|end_header_id|>
# """
# response = text2textInstructLLM(prompt)
# # responsedict = GeneralUtils.textToDict(response,
# # ["summary", "presented", "selected"],
# # rightmarker=":", symbolkey=true)
# println("\n~~~ generateSituationReport() ", Dates.now(), " ", @__FILE__, " ", @__LINE__)
# pprintln(response)
# return Dict(:recap => response)
# end
# error("generateSituationReport failed to generate a response ", response)
# end
function generateSituationReport(a, text2textInstructLLM::Function; skiprecent::Integer=0 function generateSituationReport(a, text2textInstructLLM::Function; skiprecent::Integer=0
)::Dict )::Dict
@@ -1312,19 +1401,21 @@ function generateSituationReport(a, text2textInstructLLM::Function; skiprecent::
""" """
if length(a.memory[:events]) <= skiprecent if length(a.memory[:events]) <= skiprecent
return Dict(:recap => "None") return nothing
end end
events = deepcopy(a.memory[:events][1:end-skiprecent]) # events = deepcopy(a.memory[:events][1:end-skiprecent])
timeline = "" # timeline = ""
for (i, event) in enumerate(events) # for (i, event) in enumerate(events)
if event[:outcome] === nothing # if event[:outcome] === nothing
timeline *= "$i) $(event[:subject])> $(event[:actioninput])\n" # timeline *= "$i) $(event[:subject])> $(event[:actioninput])\n"
else # else
timeline *= "$i) $(event[:subject])> $(event[:actioninput]) $(event[:outcome])\n" # timeline *= "$i) $(event[:subject])> $(event[:actioninput]) $(event[:outcome])\n"
end # end
end # end
timeline = createTimeline(a.memory[:events]; skiprecent=skiprecent)
errornote = "" errornote = ""
response = nothing # store for show when error msg show up response = nothing # store for show when error msg show up
@@ -1349,19 +1440,18 @@ function generateSituationReport(a, text2textInstructLLM::Function; skiprecent::
""" """
response = text2textInstructLLM(prompt) response = text2textInstructLLM(prompt)
# responsedict = GeneralUtils.textToDict(response, eventheader = ["Event_$i" for i in eachindex(a.memory[:events])]
# ["summary", "presented", "selected"], responsedict = GeneralUtils.textToDict(response, eventheader,
# rightmarker=":", symbolkey=true) rightmarker=":", symbolkey=true)
println("\n~~~ generateSituationReport() ", Dates.now(), " ", @__FILE__, " ", @__LINE__) println("\n~~~ generateSituationReport() ", Dates.now(), " ", @__FILE__, " ", @__LINE__)
pprintln(response) pprintln(response)
return Dict(:recap => response) return responsedict
end end
error("generateSituationReport failed to generate a response ", response) error("generateSituationReport failed to generate a response ", response)
end end
function detectWineryName(a, text) function detectWineryName(a, text)
systemmsg = systemmsg =

View File

@@ -669,6 +669,171 @@ function extractWineAttributes_2(a::T1, input::T2)::String where {T1<:agent, T2<
end end
# function concept(a::sommelier, thoughtDict)
# systemmsg =
# """
# Your name: N/A
# Situation:
# - You are a helpful assistant
# Your vision:
# - This is a good opportunity to help the user
# Your mission:
# - To describe the concept of a conversation
# Mission's objective includes:
# - To
# Your responsibility includes:
# 1) Given the situation, convey your thoughts to the user.
# Your responsibility excludes:
# 1) Asking or guiding the user to make a purchase
# 2) Processing sales orders or engaging in any other sales-related activities
# 3) Answering questions and offering additional services beyond just recommendations, such as delivery, box, gift wrapping, personalized messages. Customers can reach out to our sales at the store.
# Your profile:
# - You are a young professional in a big company.
# - You are avid party goer
# - You like beer.
# - You know nothing about wine.
# - You have a budget of 1500usd.
# Additional information:
# - your boss like spicy food.
# - your boss is a middle-aged man.
# At each round of conversation, you will be given the following information:
# Your ongoing conversation with the user: ...
# Context: ...
# Your thoughts: Your current thoughts in your mind
# You MUST follow the following guidelines:
# - Do not offer additional services you didn't thought.
# You should follow the following guidelines:
# - Focus on the latest conversation.
# - If the user interrupts, prioritize the user
# - Medium and full-bodied red wines should not be paired with spicy foods.
# You should then respond to the user with:
# 1) Chat: Given the situation, How would you respond to the user to express your thoughts honestly and keep the conversation going smoothly?
# You should only respond in format as described below:
# Chat: ...
# Here are some examples of response format:
# Chat: "I see. Let me think about it. I'll get back to you with my recommendation."
# Let's begin!
# """
# # a.memory[:shortmem][:available_wine] is a dataframe.
# context =
# if haskey(a.memory[:shortmem], :available_wine)
# "Available wines $(GeneralUtils.dfToString(a.memory[:shortmem][:available_wine]))"
# else
# "None"
# end
# chathistory = vectorOfDictToText(a.chathistory)
# errornote = ""
# response = nothing # placeholder for show when error msg show up
# for attempt in 1:10
# usermsg = """
# Your ongoing conversation with the user: $chathistory
# Contex: $context
# Your thoughts: $(thoughtDict[:understanding]) $(thoughtDict[:reasoning]) $(thoughtDict[:plan])
# $errornote
# """
# _prompt =
# [
# Dict(:name => "system", :text => systemmsg),
# Dict(:name => "user", :text => usermsg)
# ]
# # put in model format
# prompt = GeneralUtils.formatLLMtext(_prompt; formatname="llama3instruct")
# prompt *= """
# <|start_header_id|>assistant<|end_header_id|>
# """
# try
# response = a.func[:text2textInstructLLM](prompt)
# # sometime the model response like this "here's how I would respond: ..."
# if occursin("respond:", response)
# errornote = "You don't need to intro your response"
# error("generatechat() response contain : ", Dates.now(), " ", @__FILE__, " ", @__LINE__)
# end
# response = GeneralUtils.remove_french_accents(response)
# response = replace(response, '*'=>"")
# response = replace(response, '$' => "USD")
# response = replace(response, '`' => "")
# response = GeneralUtils.remove_french_accents(response)
# responsedict = GeneralUtils.textToDict(response, ["Chat"],
# rightmarker=":", symbolkey=true, lowercasekey=true)
# for i ∈ [:chat]
# if length(JSON3.write(responsedict[i])) == 0
# error("$i is empty ", Dates.now(), " ", @__FILE__, " ", @__LINE__)
# end
# end
# # check if there are more than 1 key per categories
# for i ∈ [:chat]
# matchkeys = GeneralUtils.findMatchingDictKey(responsedict, i)
# if length(matchkeys) > 1
# error("generatechat has more than one key per categories")
# end
# end
# # check if Context: is in chat
# if occursin("Context:", responsedict[:chat])
# error("Context: is in text. This is not allowed")
# end
# println("\n~~~ generatechat() ", Dates.now(), " ", @__FILE__, " ", @__LINE__)
# pprintln(Dict(responsedict))
# # check whether an agent recommend wines before checking inventory or recommend wines
# # outside its inventory
# # ask LLM whether there are any winery mentioned in the response
# mentioned_winery = detectWineryName(a, responsedict[:chat])
# if mentioned_winery != "None"
# mentioned_winery = String.(strip.(split(mentioned_winery, ",")))
# # check whether the wine is in event
# isWineInEvent = false
# for winename in mentioned_winery
# for event in a.memory[:events]
# if event[:outcome] !== nothing && occursin(winename, event[:outcome])
# isWineInEvent = true
# break
# end
# end
# end
# # if wine is mentioned but not in timeline or shortmem,
# # then the agent is not supposed to recommend the wine
# if isWineInEvent == false
# errornote = "Previously: You recommend a wine that is not in your inventory which is not allowed."
# error("Previously: You recommend a wine that is not in your inventory which is not allowed.")
# end
# end
# result = responsedict[:chat]
# return result
# catch e
# io = IOBuffer()
# showerror(io, e)
# errorMsg = String(take!(io))
# st = sprint((io, v) -> show(io, "text/plain", v), stacktrace(catch_backtrace()))
# println("\nAttempt $attempt. Error occurred: $errorMsg\n$st ", Dates.now(), " ", @__FILE__, " ", @__LINE__)
# end
# end
# error("generatechat failed to generate a response")
# end
""" Attemp to correct LLM response's incorrect JSON response. """ Attemp to correct LLM response's incorrect JSON response.
# Arguments # Arguments

View File

@@ -185,6 +185,7 @@ function sommelier(
:state=> Dict{Symbol, Any}( :state=> Dict{Symbol, Any}(
:wine_presented_to_user=> "None", :wine_presented_to_user=> "None",
), ),
:recap=> nothing,
) )
newAgent = sommelier( newAgent = sommelier(

View File

@@ -198,18 +198,20 @@ function eventdict(;
end end
function createTimeline(memory::T1, recent) where {T1<:AbstractVector} function createTimeline(memory::T1; skiprecent::Integer=0) where {T1<:AbstractVector}
totalevents = length(memory) # totalevents = length(memory)
ind = # ind =
if totalevents > recent # if totalevents > skiprecent
start = totalevents - recent # start = totalevents - skiprecent
start:totalevents # start:totalevents
else # else
1:totalevents # 1:totalevents
end # end
events = memory[1:end-skiprecent]
timeline = "" timeline = ""
for (i, event) in enumerate(memory[ind]) for (i, event) in enumerate(events)
if event[:outcome] === nothing if event[:outcome] === nothing
timeline *= "$i) $(event[:subject])> $(event[:actioninput])\n" timeline *= "$i) $(event[:subject])> $(event[:actioninput])\n"
else else