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:
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 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:
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.
- 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.
- 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:
- vintage 0 means non-vintage.
@@ -692,7 +693,7 @@ julia> response = ChatAgent.conversation(newAgent, "Hi! how are you?")
# Signature
"""
function conversation(a::sommelier, userinput::Dict)
function conversation(a::sommelier, userinput::Dict; maximumMsg=50)
# place holder
actionname = nothing
@@ -705,7 +706,7 @@ function conversation(a::sommelier, userinput::Dict)
return "Okay. What shall we talk about?"
else
# add usermsg to a.chathistory
addNewMessage(a, "user", userinput[:text])
addNewMessage(a, "user", userinput[:text]; maximumMsg=maximumMsg)
# add user activity to events memory
push!(a.memory[:events],
@@ -727,13 +728,13 @@ function conversation(a::sommelier, userinput::Dict)
end
end
addNewMessage(a, "assistant", chatresponse)
addNewMessage(a, "assistant", chatresponse; maximumMsg=maximumMsg)
return chatresponse
end
end
function conversation(a::companion, userinput::Dict; maximumMsg=30)
function conversation(a::companion, userinput::Dict; maximumMsg=50)
chatresponse = nothing
if userinput[:text] == "newtopic"
@@ -741,7 +742,7 @@ function conversation(a::companion, userinput::Dict; maximumMsg=30)
return "Okay. What shall we talk about?"
else
# add usermsg to a.chathistory
addNewMessage(a, "user", userinput[:text])
addNewMessage(a, "user", userinput[:text]; maximumMsg=maximumMsg)
# add user activity to events memory
push!(a.memory[:events],
@@ -754,7 +755,7 @@ function conversation(a::companion, userinput::Dict; maximumMsg=30)
)
chatresponse = generatechat(a)
addNewMessage(a, "assistant", chatresponse; maximumMsg=30)
addNewMessage(a, "assistant", chatresponse; maximumMsg=maximumMsg)
push!(a.memory[:events],
eventdict(;
@@ -786,7 +787,7 @@ julia>
"""
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)
actionname = thoughtDict[:action_name]
@@ -928,7 +929,6 @@ function generatechat(a::sommelier, thoughtDict)
- 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?
@@ -1119,9 +1119,9 @@ function generatequestion(a, text2textInstructLLM::Function; recent=nothing)::St
Your responsibility does not include:
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:
Your status: your current status
Recap: recap of what has happened so far
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.
- 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.
- 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:
1) Understanding:
@@ -1222,11 +1223,22 @@ function generatequestion(a, text2textInstructLLM::Function; recent=nothing)::St
errornote = ""
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
usermsg =
"""
Your status: $(GeneralUtils.dict_to_string(a.memory[:state]))
Recap: $(a.memory[:recap])
Recap: $recap)
Your recent events: $timeline
$errornote
"""
@@ -1283,9 +1295,86 @@ function generatequestion(a, text2textInstructLLM::Function; recent=nothing)::St
end
function generateSituationReport(a, text2textInstructLLM::Function; skiprecent::Integer=0
)::Dict
# 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
)::Dict
systemmsg =
"""
You are an assistant being in the given events.
@@ -1312,19 +1401,21 @@ function generateSituationReport(a, text2textInstructLLM::Function; skiprecent::
"""
if length(a.memory[:events]) <= skiprecent
return Dict(:recap => "None")
return nothing
end
events = deepcopy(a.memory[:events][1:end-skiprecent])
# 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
# 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
timeline = createTimeline(a.memory[:events]; skiprecent=skiprecent)
errornote = ""
response = nothing # store for show when error msg show up
@@ -1349,19 +1440,18 @@ function generateSituationReport(a, text2textInstructLLM::Function; skiprecent::
"""
response = text2textInstructLLM(prompt)
# responsedict = GeneralUtils.textToDict(response,
# ["summary", "presented", "selected"],
# rightmarker=":", symbolkey=true)
eventheader = ["Event_$i" for i in eachindex(a.memory[:events])]
responsedict = GeneralUtils.textToDict(response, eventheader,
rightmarker=":", symbolkey=true)
println("\n~~~ generateSituationReport() ", Dates.now(), " ", @__FILE__, " ", @__LINE__)
pprintln(response)
return Dict(:recap => response)
return responsedict
end
error("generateSituationReport failed to generate a response ", response)
end
function detectWineryName(a, text)
systemmsg =