This commit is contained in:
2026-06-24 19:50:43 +07:00
parent 906afc6422
commit de20764610
7 changed files with 224 additions and 250 deletions

View File

@@ -2,7 +2,7 @@
julia_version = "1.12.6" julia_version = "1.12.6"
manifest_format = "2.0" manifest_format = "2.0"
project_hash = "e6bd85ad2679c39ab370f878253f2eeb45c1b6ae" project_hash = "7462b22f4fd62982e36c8671793df6d8908c9ad0"
[[deps.AliasTables]] [[deps.AliasTables]]
deps = ["PtrArrays", "Random"] deps = ["PtrArrays", "Random"]
@@ -217,11 +217,11 @@ version = "1.11.0"
[[deps.GeneralUtils]] [[deps.GeneralUtils]]
deps = ["CSV", "DataFrames", "DataStructures", "Dates", "Distributions", "JSON", "NATS", "PrettyPrinting", "Random", "Revise", "SHA", "UUIDs"] deps = ["CSV", "DataFrames", "DataStructures", "Dates", "Distributions", "JSON", "NATS", "PrettyPrinting", "Random", "Revise", "SHA", "UUIDs"]
git-tree-sha1 = "8720a31344bc85ad610ae12f7e1247de22070765" git-tree-sha1 = "76d2628787838a67d6e8192e428991a7522883f0"
repo-rev = "main" repo-rev = "main"
repo-url = "https://git.yiem.cc/ton/GeneralUtils" repo-url = "https://git.yiem.cc/ton/GeneralUtils"
uuid = "c6c72f09-b708-4ac8-ac7c-2084d70108fe" uuid = "c6c72f09-b708-4ac8-ac7c-2084d70108fe"
version = "0.3.2" version = "0.4.0"
[[deps.HTTP]] [[deps.HTTP]]
deps = ["Base64", "CodecZlib", "Dates", "EnumX", "PrecompileTools", "Random", "Reseau", "SHA", "URIs", "UUIDs", "Zlib_jll"] deps = ["Base64", "CodecZlib", "Dates", "EnumX", "PrecompileTools", "Random", "Reseau", "SHA", "URIs", "UUIDs", "Zlib_jll"]
@@ -662,7 +662,7 @@ uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce"
version = "0.7.0" version = "0.7.0"
[[deps.SQLLLM]] [[deps.SQLLLM]]
deps = ["CSV", "DataFrames", "DataStructures", "Dates", "FileIO", "GeneralUtils", "HTTP", "JSON3", "LLMMCTS", "LibPQ", "PrettyPrinting", "Random", "Revise", "StatsBase", "Tables", "URIs", "UUIDs"] deps = ["CSV", "DataFrames", "DataStructures", "Dates", "FileIO", "HTTP", "JSON3", "LLMMCTS", "LibPQ", "PrettyPrinting", "Random", "Revise", "StatsBase", "Tables", "URIs", "UUIDs"]
path = "../SQLLLM" path = "../SQLLLM"
uuid = "2ebc79c7-cc10-4a3a-9665-d2e1d61e63d3" uuid = "2ebc79c7-cc10-4a3a-9665-d2e1d61e63d3"
version = "0.2.4" version = "0.2.4"
@@ -884,7 +884,7 @@ uuid = "76eceee3-57b5-4d4a-8e66-0e911cebbf60"
version = "1.6.1" version = "1.6.1"
[[deps.YiemAgent]] [[deps.YiemAgent]]
deps = ["CSV", "DataFrames", "DataStructures", "Dates", "GeneralUtils", "HTTP", "JSON", "LLMMCTS", "LibPQ", "NATS", "PrettyPrinting", "Random", "Revise", "SQLLLM", "Serialization", "URIs", "UUIDs"] deps = ["CSV", "DataFrames", "DataStructures", "Dates", "GeneralUtils", "HTTP", "JSON", "LLMMCTS", "LibPQ", "NATS", "PrettyPrinting", "Random", "Revise", "Serialization", "URIs", "UUIDs"]
path = "." path = "."
uuid = "e012c34b-7f78-48e0-971c-7abb83b6f0a2" uuid = "e012c34b-7f78-48e0-971c-7abb83b6f0a2"
version = "0.4.0" version = "0.4.0"

View File

@@ -25,7 +25,7 @@ UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
[compat] [compat]
CSV = "0.10.15" CSV = "0.10.15"
DataFrames = "1.7.0" DataFrames = "1.7.0"
GeneralUtils = "0.3.2" GeneralUtils = "0.4.0"
HTTP = "2.4.0" HTTP = "2.4.0"
JSON = "1.6.1" JSON = "1.6.1"
NATS = "0.1.0" NATS = "0.1.0"

View File

@@ -70,11 +70,11 @@ julia> config = Dict(
:broker => "mqtt.yiem.cc" :broker => "mqtt.yiem.cc"
), ),
:externalservice => Dict( :externalservice => Dict(
:text2textinstruct => Dict( "text"2textinstruct => Dict(
:mqtttopic => "/loadbalancer/requestingservice", :mqtttopic => "/loadbalancer/requestingservice",
:description => "text to text service with instruct LLM", :description => "text to text service with instruct LLM",
:llminfo => Dict( :llminfo => Dict(
:name => "llama3instruct" "name" => "llama3instruct"
) )
), ),
) )
@@ -89,14 +89,11 @@ julia> output_thoughtDict = Dict(
:observation_1 => "" :observation_1 => ""
) )
``` ```
# TODO
- [] update docstring - [] update docstring
- [] use customerinfo - [] use customerinfo
- [] user storeinfo - [] user storeinfo
# Signature """ #WORKING
"""
function decisionMaker(a::T; recentevents::Integer=20, maxattempt=10 function decisionMaker(a::T; recentevents::Integer=20, maxattempt=10
) where {T<:agent} ) where {T<:agent}
println("\nExecuting YiemAgent decisionMaker()") println("\nExecuting YiemAgent decisionMaker()")
@@ -125,158 +122,23 @@ function decisionMaker(a::T; recentevents::Integer=20, maxattempt=10
# """ # """
# end # end
recentevents_ind = GeneralUtils.recentElementsIndex(length(a.memory[:events]), recentevents; recentevents_ind = GeneralUtils.recentElementsIndex(
includelatest=true) length(a.memory[:events]), recentevents; includelatest=true)
timeline = createTimeline(a.memory[:events]; eventindex=recentevents_ind)
# recentchat_ind = GeneralUtils.recentElementsIndex(length(a.chathistory), recentevents; requiredKeys = ["plan", "actionname", "actioninput"]
# includelatest=true)
# recentchat = createChatLog(a.chathistory; index=recentchat_ind)
recentEvents = createEventsLog(a.memory[:events]; index=recentevents_ind)
# # recap as caching
# # query similar result from vectorDB
# recapkeys = keys(a.memory[:recap])
# _recapkeys_vec = [i for i in recapkeys]
# # select recent keys
# _recentRecapKeys =
# if length(a.memory[:recap]) <= 3 # 1st message is a user's hello msg
# _recapkeys_vec
# elseif length(a.memory[:recap]) > 3
# l = length(a.memory[:recap])
# _recapkeys_vec[l-2:l]
# end
# # get recent recap
# _recentrecap = OrderedDict()
# for (k, v) in a.memory[:recap]
# if k ∈ _recentRecapKeys
# _recentrecap[k] = v
# end
# end
# recentrecap = GeneralUtils.dictToString_noKey(_recentrecap)
# similarDecision = a.context[:similarSommelierDecision](recentrecap)
similarDecision = nothing #CHANGE
if similarDecision !== nothing
responsedict = similarDecision
return responsedict
else
# context = # may b add wine name instead of the hold wine data is better
# if length(a.memory[:shortmem][:available_wine]) != 0
# winenames = []
# for (i, wine) in enumerate(a.memory[:shortmem][:available_wine])
# name = "$i) $(wine["wine_name"]) "
# push!(winenames, name)
# end
# availableWineName = join(winenames, ',')
# "Available wines you've found in your inventory so far: $availableWineName"
# else
# ""
# end
systemmsg =
"""
<your role>
Your name is $(a.name). You are a sommelier for website-based $(a.retailername)'s wine store. You are working under your mentor supervision.
</your role>
<situation>
Your customer is coming into the store
</situation>
<your mission>
1) Establish a connection with the customer by talking to them politely and showing your enthusiasm for their wine preferences.
2) Provide relevant information and guide them to select the best wines only from your store's inventory that align with their preferences.
</your mission>
<your responsibility includes>
1) According to the store's policy and guidelines, make an informed decision about what you need to do to achieve the goal
2) Keep the conversation with the customer going smoothly
2) Obey your mentor's suggestions.
</your responsibility includes>
<your responsibility does NOT includes>
1) Requesting the user to place an order, make a purchase, or confirm the order. These are the job of our sales team at the store.
2) Processing sales orders or engaging in any other sales-related activities. These are the job of our sales team at the store.
3) Answering questions or offering additional services beyond those related to your store's wine recommendations such as discounts, quantity, rewards programs, promotions, delivery options, shipping, boxes, gift wrapping, packaging, personalized messages or something similar. These are the job of our sales team at the store.
</your responsibility does NOT includes>
<at each round of conversation, you will be given the following information>
context: additional information about the current situation
</at each round of conversation, you will be given the following information>
<you should then respond to the user with interleaving Plan, Action_name, Action_input>
1) plan: Based on the current situation, state a complete action plan to complete the task. Be specific.
2) actionname: (Typically corresponds to the execution of the first step in your plan) Can be one of the available tool name
3) actioninput: The input to the action you are about to perform according to your plan.
</you should then respond to the user with interleaving Plan, Action_name, Action_input>
<you should only respond in JSON format as described below>
{
"plan": "...",
"actionname": "...",
"actioninput": "..."
}
</you should only respond in format as described below>
Let's begin!
"""
requiredKeys = [:plan, :actionname, :actioninput]
# database_search_result =
# if length(a.memory[:shortmem][:db_search_result]) != 0
# availableWineToText(a.memory[:shortmem][:db_search_result])
# else
# "N/A"
# end
errornote = "N/A" errornote = "N/A"
response = nothing # placeholder for show when error msg show up response = nothing # placeholder for show when error msg show up
for attempt in 1:maxattempt for attempt in 1:maxattempt
if attempt > 1 if attempt > 1
println("\nYiemAgent decisionMaker() attempt $attempt/$maxattempt ", @__FILE__, ":", @__LINE__, " $(Dates.now())") println("\nYiemAgent decisionMaker() attempt $attempt/$maxattempt ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
end end
# QandA = generatequestion(a, a.context[:text2textInstructLLM], timeline)
context =
"""
<context>
<Store_policy>
- Generally speaking, the store inventory has some wines from France, the United States, Australia, Spain, and Italy, but you won't know exactly until you check your inventory.
- If you found wines in the store's database, they are in stock.
- You can only recommend wines that are currently in our inventory
- Before searching the database for wine, ensure you have at least the following information: 1) budget, 2) wine type, and 3) occasion. Additional details are always helpful. If the user is unsure, provide relevant information and gather insights to make reasonable inferences.
- Ask the user one question at a time.
- 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, if you haven't already, ask the user whether they need any further assistance. Do not offer any additional services.
- Only end the conversation when the user explicitly intends to do so. When ending, ensure a polite farewell and an invitation to return in the future.
- Spicy foods should be paired only with light red wines.
- We do not sell organic, sustainable, gluten-free, and sulfite-free wine. Inform the user imediately if they are looking for these types of wines. Do not sell our wines as such.
- Gift box, gift card, and custom messages are available. Inform the user to contact our sales team.
</Store_policy>
<Store_guidelines>
- Greeting the customer warmly by ask them how could you help. Do not ask any other questions during this greeting.
- Encourage the customer to explore different options and try new things.
- If you are unable to locate the desired item in the database after 2 attempts, it may not be available in your inventory. In such cases, inform the user that the item is unavailable and suggest an alternative instead.
- Your store carries only wine.
- Vintage 0 means non-vintage.
- Start searching the database as broadly as possible within the given information boundary to maximize the chances of finding. Avoid unnecessary parameters unless specified by the user. Refine the search subsequently.
</Store_guidelines>Search the database as broad as possible under the informantion you have will increase the chance to find wine. Avoid uneccessary parameter such as region, country, tasting notes unless the user specify
<Available tools>
- CHATBOX which you can use to talk with the user. Be specific.
- CHECKWINE allows you to check information about wines you want in your inventory's database. The input must be supported search criteria includeing: wine price, winery, name, vintage, region, country, type, grape varietal, tasting notes, occasion, food pairing, intensity, tannin, sweetness, and acidity.
Example query 1: "Dry, full-bodied red wine from 1) region: Burgundy, country: France or 2) region: Tuscany, country: Italy. Grape varietal: Merlot or Syrah. price 100 to 1000 USD."
Example query 2: "Red or white wine, medium tannin, price under 700 USD"
Example query 3: "white wine, region: Tuscany or Bordeaux, country: Italy or France
- PRESENTBOX which you can use to present wines you have found in your inventory to the user. The input are wine names that you want to present. The output is presentation of the wines.
- ENDCONVERSATION which you can use to properly end the conversation with the user. Input is a dialogue where you wrap up the conversation, thank the user, and invite them to return next time.
</Available tools>
$(a.memory[:shortmem][:scratchpad])
Remark: $errornote
</context>
"""
unformatPrompt = unformatPrompt =
[ [
Dict(:name => "system", :text => systemmsg), Dict("name" => "system", "text" => systemmsg),
] ]
unformatPrompt = vcat(unformatPrompt, recentEvents) unformatPrompt = vcat(unformatPrompt, recentEvents)
@@ -322,9 +184,9 @@ function decisionMaker(a::T; recentevents::Integer=20, maxattempt=10
# continue # continue
# end # end
if responsedict[:actionname] ["CHATBOX", "CHECKWINE", "PRESENTBOX", "ENDCONVERSATION"] if responsedict["actionname"] ["CHATBOX", "CHECKWINE", "PRESENTBOX", "ENDCONVERSATION"]
errornote = "Your previous attempt didn't use the given functions" errornote = "Your previous attempt didn't use the given functions"
println("\nERROR YiemAgent decisionMaker() $errornote --(not qualify response)--> $(responsedict[:actionname])", @__FILE__, ":", @__LINE__, " $(Dates.now())") println("\nERROR YiemAgent decisionMaker() $errornote --(not qualify response)--> $(responsedict["actionname"])", @__FILE__, ":", @__LINE__, " $(Dates.now())")
continue continue
end end
@@ -359,8 +221,8 @@ function decisionMaker(a::T; recentevents::Integer=20, maxattempt=10
delete!(responsedict, :mentioned_winery) delete!(responsedict, :mentioned_winery)
# check whether responsedict[:actioninput] is the same as previous dialogue # check whether responsedict["actioninput"] is the same as previous dialogue
if !isempty(a.chathistory) && responsedict[:actioninput] == a.chathistory[end][:text] if !isempty(a.chathistory) && responsedict["actioninput"] == a.chathistory[end]["text"]
errornote = "In your previous attempt, you repeated the previous dialogue. Please try again." errornote = "In your previous attempt, you repeated the previous dialogue. Please try again."
println("\nERROR YiemAgent decisionMaker() $response ", @__FILE__, ":", @__LINE__, " $(Dates.now())") println("\nERROR YiemAgent decisionMaker() $response ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
continue continue
@@ -384,10 +246,10 @@ function decisionMaker(a::T; recentevents::Integer=20, maxattempt=10
# responsedict[:QandA] = QandA # responsedict[:QandA] = QandA
# check whether there is a file path exists before writing to it # check whether there is a file path exists before writing to it
if !haskey(a.memory[:shortmem], :decisionlog) if !haskey(a.memory["shortmem"], :decisionlog)
a.memory[:shortmem][:decisionlog] = [responsedict] a.memory["shortmem"][:decisionlog] = [responsedict]
else else
push!(a.memory[:shortmem][:decisionlog], responsedict) push!(a.memory["shortmem"][:decisionlog], responsedict)
end end
# # save to filename ./log/decisionlog.txt # # save to filename ./log/decisionlog.txt
@@ -512,7 +374,7 @@ function evaluator(a::T1, timeline, decisiondict, evaluateecontext
$evaluateecontext $evaluateecontext
</evaluatee_context> </evaluatee_context>
<evaluatee_decision> <evaluatee_decision>
{plan: $(decisiondict[:plan]), actionname: $(decisiondict[:actionname]), actioninput: $(decisiondict[:actioninput])} {plan: $(decisiondict["plan"]), actionname: $(decisiondict["actionname"]), actioninput: $(decisiondict["actioninput"])}
</evaluatee_decision> </evaluatee_decision>
P.S. $errornote P.S. $errornote
</context> </context>
@@ -520,7 +382,7 @@ function evaluator(a::T1, timeline, decisiondict, evaluateecontext
unformatPrompt = unformatPrompt =
[ [
Dict(:name => "system", :text => systemmsg), Dict("name" => "system", "text" => systemmsg),
] ]
# put in model format # put in model format
@@ -610,30 +472,17 @@ message => Dict(
] ]
) )
- TODO add recap to initialState for earlier completed question # ---------------------------------------------- 100 --------------------------------------------- #
""" """ #WORKING
function conversation(a::sommelier; userinput::Union{Dict, Nothing}=nothing, function conversation(a::sommelier; userinput::Union{Dict{String, Any}, JSON.Object{String, Any}}, maximumMsg=50)
maximumMsg=50) userinput = GeneralUtils.dictify(userinput; keytype=String)
# place holder # place holder
actionname = nothing actionname = nothing
result = nothing result = nothing
chatresponse = nothing chatresponse = nothing
userinput = GeneralUtils.dictify(userinput) if userinput === nothing
user_text_input, text_position =
if userinput !== nothing
for (i, d) in enumerate(userinput["content"])
if d["type"] == "text"
(d["text"], i)
end
end
else
(nothing, nothing)
end
if user_text_input === nothing
# thinking loop until AI wants to communicate with the user # thinking loop until AI wants to communicate with the user
chatresponse = nothing chatresponse = nothing
while chatresponse === nothing while chatresponse === nothing
@@ -642,6 +491,7 @@ function conversation(a::sommelier; userinput::Union{Dict, Nothing}=nothing,
chatresponse = result chatresponse = result
end end
end end
addNewMessage(a, "assistant", chatresponse; maximumMsg=maximumMsg) addNewMessage(a, "assistant", chatresponse; maximumMsg=maximumMsg)
return chatresponse return chatresponse
@@ -653,17 +503,6 @@ function conversation(a::sommelier; userinput::Union{Dict, Nothing}=nothing,
# add usermsg to a.chathistory but how do I handle images? # add usermsg to a.chathistory but how do I handle images?
addNewMessage(a, "user", userinput; maximumMsg=maximumMsg) addNewMessage(a, "user", userinput; maximumMsg=maximumMsg)
# add user activity to events memory
# push!(a.memory[:events],
# eventdict(;
# event_description="the user talks to the assistant.",
# timestamp=Dates.now(),
# subject="user",
# actionname="CHATBOX",
# actioninput=userinput[:text],
# )
# )
# thinking loop until AI wants to communicate with the user # thinking loop until AI wants to communicate with the user
chatresponse = nothing chatresponse = nothing
while chatresponse === nothing while chatresponse === nothing
@@ -684,12 +523,12 @@ function conversation(a::Union{companion, virtualcustomer}, userinput::Dict;
chatresponse = nothing chatresponse = nothing
if userinput[:text] == "newtopic" if userinput["text"] == "newtopic"
clearhistory(a) clearhistory(a)
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]; maximumMsg=maximumMsg) 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],
@@ -698,7 +537,7 @@ function conversation(a::Union{companion, virtualcustomer}, userinput::Dict;
timestamp=Dates.now(), timestamp=Dates.now(),
subject="user", subject="user",
actionname="CHATBOX", actionname="CHATBOX",
actioninput=userinput[:text], actioninput=userinput["text"],
) )
) )
chatresponse = generatechat(a; converPartnerName=converPartnerName, recentEventNum=20) chatresponse = generatechat(a; converPartnerName=converPartnerName, recentEventNum=20)
@@ -729,19 +568,18 @@ end
julia> julia>
``` ```
TODO update docstring
""" # WORKING """ # WORKING
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.context[:text2textInstructLLM]; skiprecent=0) # a.memory[:recap] = generateSituationReport(a, a.context["text"2textInstructLLM]; skiprecent=0)
thoughtDict = decisionMaker(a) thoughtDict = decisionMaker(a)
actionname = thoughtDict[:actionname] actionname = thoughtDict["actionname"]
actioninput = thoughtDict[:actioninput] actioninput = thoughtDict["actioninput"]
# map action and input() to llm function # map action and input() to llm function
response = response =
if actionname == "CHATBOX" || actionname == "ENDCONVERSATION" if actionname == "CHATBOX" || actionname == "ENDCONVERSATION"
(result=thoughtDict[:plan], errormsg=nothing, success=true) (result=thoughtDict["plan"], errormsg=nothing, success=true)
elseif actionname == "CHECKWINE" elseif actionname == "CHECKWINE"
checkwine(a, actioninput) checkwine(a, actioninput)
elseif actionname == "PRESENTBOX" elseif actionname == "PRESENTBOX"
@@ -776,10 +614,10 @@ function think(a::T)::NamedTuple{(:actionname, :result),Tuple{String,String}} wh
# ) # )
# ) # )
# result = chatresponse # result = chatresponse
if actionname ["CHATBOX", "ENDCONVERSATION"] if actionname ["CHATBOX"]
push!(a.memory[:events], push!(a.memory[:events],
eventdict(; eventdict(;
event_description="the assistant talks to the user.", event_description="an assistant talks to the user.",
timestamp=Dates.now(), timestamp=Dates.now(),
subject="assistant", subject="assistant",
thought=thoughtDict, thought=thoughtDict,
@@ -788,24 +626,24 @@ function think(a::T)::NamedTuple{(:actionname, :result),Tuple{String,String}} wh
) )
) )
result = actioninput result = actioninput
# elseif actionname ∈ ["ENDCONVERSATION"] elseif actionname ["ENDCONVERSATION"]
# chatresponse = generatechat(a, thoughtDict) chatresponse = generatechat(a, thoughtDict)
# push!(a.memory[:events], push!(a.memory[:events],
# eventdict(; eventdict(;
# event_description="the assistant talks to the user.", event_description="an assistant talks to the user to end conversation.",
# timestamp=Dates.now(), timestamp=Dates.now(),
# subject="assistant", subject="assistant",
# thought=thoughtDict, thought=thoughtDict,
# actionname=actionname, actionname=actionname,
# actioninput=chatresponse, actioninput=chatresponse,
# ) )
# ) )
# result = chatresponse result = chatresponse
elseif actionname ["PRESENTBOX"] elseif actionname ["PRESENTBOX"]
chatresponse = presentbox(a, thoughtDict) chatresponse = presentbox(a, thoughtDict)
push!(a.memory[:events], push!(a.memory[:events],
eventdict(; eventdict(;
event_description="the assistant talks to the user.", event_description="the assistant presents wines to the user.",
timestamp=Dates.now(), timestamp=Dates.now(),
subject="assistant", subject="assistant",
thought=thoughtDict, thought=thoughtDict,
@@ -818,23 +656,23 @@ function think(a::T)::NamedTuple{(:actionname, :result),Tuple{String,String}} wh
elseif actionname == "CHECKWINE" elseif actionname == "CHECKWINE"
if rawresponse !== nothing if rawresponse !== nothing
vd = GeneralUtils.dfToVectorDict(rawresponse) # comes in dataframe format vd = GeneralUtils.dfToVectorDict(rawresponse) # comes in dataframe format
# a.memory[:shortmem][:found_wine] = vd # used by decisionMaker() as a short note # a.memory["shortmem"][:found_wine] = vd # used by decisionMaker() as a short note
#[PENDING] a.memory[:shortmem][:available_wine] should be OrderedDict instead of vector dict so that no duplicate item are listed? #[PENDING] a.memory["shortmem"][:available_wine] should be OrderedDict instead of vector dict so that no duplicate item are listed?
if length(a.memory[:shortmem][:db_search_result]) != 0 if length(a.memory["shortmem"][:db_search_result]) != 0
a.memory[:shortmem][:db_search_result] = vcat(a.memory[:shortmem][:db_search_result], vd) a.memory["shortmem"][:db_search_result] = vcat(a.memory["shortmem"][:db_search_result], vd)
else else
a.memory[:shortmem][:db_search_result] = vd a.memory["shortmem"][:db_search_result] = vd
end end
# # add to scratchpad # # add to scratchpad
# a.memory[:shortmem][:scratchpad] *= # a.memory["shortmem"]["scratchpad"] *=
# """ # """
# <database_search_result> # <database_search_result>
# I searched the database with this search term: $actioninput \nThis is what I found: $result # I searched the database with this search term: $actioninput \nThis is what I found: $result
# </database_search_result> # </database_search_result>
# """ # """
else else
# a.memory[:shortmem][:scratchpad] *= # a.memory["shortmem"]["scratchpad"] *=
# """ # """
# <database_search_result> # <database_search_result>
# I searched the database with this search term: $actioninput \nThis is what I found: $result # I searched the database with this search term: $actioninput \nThis is what I found: $result
@@ -900,8 +738,8 @@ function presentbox(a::sommelier, thoughtDict; maxtattempt::Integer=10, recentev
""" """
requiredKeys = [:dialogue] requiredKeys = [:dialogue]
database_search_result = database_search_result =
if length(a.memory[:shortmem][:db_search_result]) != 0 if length(a.memory["shortmem"][:db_search_result]) != 0
availableWineToText(a.memory[:shortmem][:db_search_result]) availableWineToText(a.memory["shortmem"][:db_search_result])
else else
"N/A" "N/A"
end end
@@ -912,7 +750,7 @@ function presentbox(a::sommelier, thoughtDict; maxtattempt::Integer=10, recentev
# yourthought = "$(thoughtDict[:thought]) $(thoughtDict[:plan])" # yourthought = "$(thoughtDict[:thought]) $(thoughtDict["plan"])"
# yourthought1 = nothing # yourthought1 = nothing
for attempt in 1:maxtattempt for attempt in 1:maxtattempt
@@ -920,15 +758,15 @@ function presentbox(a::sommelier, thoughtDict; maxtattempt::Integer=10, recentev
context = context =
""" """
<context> <context>
Name of the wines that needs to be introduced: $(thoughtDict[:actioninput]) Name of the wines that needs to be introduced: $(thoughtDict["actioninput"])
$(a.memory[:shortmem][:scratchpad]) $(a.memory["shortmem"]["scratchpad"])
P.S. $errornote P.S. $errornote
</context> </context>
""" """
unformatPrompt = unformatPrompt =
[ [
Dict(:name => "system", :text => systemmsg), Dict("name" => "system", "text" => systemmsg),
] ]
unformatPrompt = vcat(unformatPrompt, recentchat) unformatPrompt = vcat(unformatPrompt, recentchat)
@@ -1075,10 +913,10 @@ function generatechat(a::sommelier, thoughtDict; maxattempt::Integer=10)
""" """
requiredKeys = [:dialogue] requiredKeys = [:dialogue]
# a.memory[:shortmem][:available_wine] is a vector of dictionary # a.memory["shortmem"][:available_wine] is a vector of dictionary
# context = # context =
# if length(a.memory[:shortmem][:available_wine]) != 0 # if length(a.memory["shortmem"][:available_wine]) != 0
# "Wines previously found in your inventory: $(availableWineToText(a.memory[:shortmem][:available_wine]))" # "Wines previously found in your inventory: $(availableWineToText(a.memory["shortmem"][:available_wine]))"
# else # else
# "N/A" # "N/A"
# end # end
@@ -1087,13 +925,13 @@ function generatechat(a::sommelier, thoughtDict; maxattempt::Integer=10)
errornote = "N/A" errornote = "N/A"
response = nothing # placeholder for show when error msg show up response = nothing # placeholder for show when error msg show up
yourthought = thoughtDict[:plan] yourthought = thoughtDict["plan"]
# yourthought1 = nothing # yourthought1 = nothing
for attempt in 1:maxattempt for attempt in 1:maxattempt
# if attempt > 1 # use to prevent LLM generate the same respond over and over # if attempt > 1 # use to prevent LLM generate the same respond over and over
# println("\nYiemAgent generatchat() attempt $attempt/10 ", @__FILE__, ":", @__LINE__, " $(Dates.now())") # println("\nYiemAgent generatchat() attempt $attempt/10 ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
# yourthought1 = paraphrase(a.context[:text2textInstructLLM], yourthought) # yourthought1 = paraphrase(a.context["text"2textInstructLLM], yourthought)
# else # else
# yourthought1 = yourthought # yourthought1 = yourthought
# end # end
@@ -1109,7 +947,7 @@ function generatechat(a::sommelier, thoughtDict; maxattempt::Integer=10)
unformatPrompt = unformatPrompt =
[ [
Dict(:name => "system", :text => systemmsg), Dict("name" => "system", "text" => systemmsg),
] ]
# put in model format # put in model format
@@ -1212,7 +1050,7 @@ function generatechat(a::companion; recentevents::Integer=10,
unformatPrompt = unformatPrompt =
[ [
Dict(:name => "system", :text => a.systemmsg), Dict("name" => "system", "text" => a.systemmsg),
] ]
unformatPrompt = vcat(unformatPrompt, recentchat) unformatPrompt = vcat(unformatPrompt, recentchat)
@@ -1233,7 +1071,7 @@ function generatechat(a::companion; recentevents::Integer=10,
# check whether LLM just repeat the previous dialogue # check whether LLM just repeat the previous dialogue
for msg in a.chathistory for msg in a.chathistory
if msg[:text] == response if msg["text"] == response
errornote = "In your previous attempt, you repeated the previous dialogue. Please try again." errornote = "In your previous attempt, you repeated the previous dialogue. Please try again."
println("\nYiemAgent generatechat() $errornote:\n$response ", @__FILE__, ":", @__LINE__, " $(Dates.now())") println("\nYiemAgent generatechat() $errornote:\n$response ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
continue continue
@@ -1274,7 +1112,7 @@ function generatechat(a::virtualcustomer;
unformatPrompt = unformatPrompt =
[ [
Dict(:name => "system", :text => a.systemmsg), Dict("name" => "system", "text" => a.systemmsg),
] ]
unformatPrompt = vcat(unformatPrompt, recentEventsDict) unformatPrompt = vcat(unformatPrompt, recentEventsDict)
# put in model format # put in model format
@@ -1310,7 +1148,7 @@ function generatechat(a::virtualcustomer;
end end
# check if the dialogue is the same as the previous one # check if the dialogue is the same as the previous one
if length(responsedict[:dialogue]) != 0 && responsedict[:dialogue] == a.chathistory[end][:text] if length(responsedict[:dialogue]) != 0 && responsedict[:dialogue] == a.chathistory[end]["text"]
errornote = "In your previous attempt you said $(responsedict[:dialogue]) which was the same as the previous one." errornote = "In your previous attempt you said $(responsedict[:dialogue]) which was the same as the previous one."
println("\nYiemAgent generatechat() $errornote:\n$response ", @__FILE__, ":", @__LINE__, " $(Dates.now())") println("\nYiemAgent generatechat() $errornote:\n$response ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
continue continue
@@ -1319,7 +1157,7 @@ function generatechat(a::virtualcustomer;
# check whether LLM just repeat the previous dialogue # check whether LLM just repeat the previous dialogue
dublicate = false dublicate = false
for msg in a.chathistory for msg in a.chathistory
if msg[:text] == responsedict[:dialogue] if msg["text"] == responsedict[:dialogue]
errornote = "In your previous attempt, you repeated the earlier dialogue. Please try again." errornote = "In your previous attempt, you repeated the earlier dialogue. Please try again."
println("\nYiemAgent generatechat() $errornote:\n$response ", @__FILE__, ":", @__LINE__, " $(Dates.now())") println("\nYiemAgent generatechat() $errornote:\n$response ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
dublicate = true dublicate = true
@@ -1447,12 +1285,12 @@ function generatequestion(a, text2textInstructLLM::Function, timeline)::String
dictkey = ["q1"] dictkey = ["q1"]
# context = # context =
# if length(a.memory[:shortmem][:available_wine]) != 0 # if length(a.memory["shortmem"][:available_wine]) != 0
# "Available wines you've found in your inventory so far: $(availableWineToText(a.memory[:shortmem][:available_wine]))" # "Available wines you've found in your inventory so far: $(availableWineToText(a.memory["shortmem"][:available_wine]))"
# else # else
# "N/A" # "N/A"
# end # end
database_search_result = a.memory[:shortmem][:db_search_result] database_search_result = a.memory["shortmem"][:db_search_result]
# recent_ind = GeneralUtils.recentElementsIndex(length(a.memory[:events]), recent) # recent_ind = GeneralUtils.recentElementsIndex(length(a.memory[:events]), recent)
# recentevents = a.memory[:events][recent_ind] # recentevents = a.memory[:events][recent_ind]
@@ -1496,8 +1334,8 @@ function generatequestion(a, text2textInstructLLM::Function, timeline)::String
_prompt = _prompt =
[ [
Dict(:name => "system", :text => systemmsg), Dict("name" => "system", "text" => systemmsg),
Dict(:name => "user", :text => usermsg) Dict("name" => "user", "text" => usermsg)
] ]
# put in model format # put in model format
@@ -1630,8 +1468,8 @@ function generateSituationReport(a, text2textInstructLLM::Function; skiprecent::
_prompt = _prompt =
[ [
Dict(:name => "system", :text => systemmsg), Dict("name" => "system", "text" => systemmsg),
Dict(:name => "user", :text => usermsg) Dict("name" => "user", "text" => usermsg)
] ]
# put in model format # put in model format
@@ -1702,8 +1540,8 @@ function detectWineryName(a, text)
""" """
_prompt = _prompt =
[ [
Dict(:name => "system", :text => systemmsg), Dict("name" => "system", "text" => systemmsg),
Dict(:name => "user", :text => usermsg) Dict("name" => "user", "text" => usermsg)
] ]
# put in model format # put in model format

View File

@@ -195,6 +195,86 @@ function sommelier(
llmFormatName llmFormatName
) )
systemmsg =
"""
<Store_policy>
- Generally speaking, the store inventory has some wines from France, the United States, Australia, Spain, and Italy, but you won't know exactly until you check your inventory.
- If you found wines in the store's database, they are in stock.
- You can only recommend wines that are currently in our inventory
- Before searching the database for wine, ensure you have at least the following information: 1) budget, 2) wine type, and 3) occasion. Additional details are always helpful. If the user is unsure, provide relevant information and gather insights to make reasonable inferences.
- Ask the user one question at a time.
- 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, if you haven't already, ask the user whether they need any further assistance. Do not offer any additional services.
- Only end the conversation when the user explicitly intends to do so. When ending, ensure a polite farewell and an invitation to return in the future.
- Spicy foods should be paired only with light red wines.
- We do not sell organic, sustainable, gluten-free, and sulfite-free wine. Inform the user imediately if they are looking for these types of wines. Do not sell our wines as such.
- Gift box, gift card, and custom messages are available. Inform the user to contact our sales team.
</Store_policy>
<Store_guidelines>
- Greeting the customer warmly by ask them how could you help. Do not ask any other questions during this greeting.
- Encourage the customer to explore different options and try new things.
- If you are unable to locate the desired item in the database after 2 attempts, it may not be available in your inventory. In such cases, inform the user that the item is unavailable and suggest an alternative instead.
- Your store carries only wine.
- Vintage 0 means non-vintage.
- Start searching the database as broadly as possible within the given information boundary to maximize the chances of finding. Avoid unnecessary parameters unless specified by the user. Refine the search subsequently.
</Store_guidelines>Search the database as broad as possible under the informantion you have will increase the chance to find wine. Avoid uneccessary parameter such as region, country, tasting notes unless the user specify
<Available tools>
- CHATBOX which you can use to talk with the user. Be specific.
- CHECKWINE allows you to check information about wines you want in your inventory's database. The input must be supported search criteria includeing: wine price, winery, name, vintage, region, country, type, grape varietal, tasting notes, occasion, food pairing, intensity, tannin, sweetness, and acidity.
Example query 1: "Dry, full-bodied red wine from 1) region: Burgundy, country: France or 2) region: Tuscany, country: Italy. Grape varietal: Merlot or Syrah. price 100 to 1000 USD."
Example query 2: "Red or white wine, medium tannin, price under 700 USD"
Example query 3: "white wine, region: Tuscany or Bordeaux, country: Italy or France
- PRESENTBOX which you can use to present wines you have found in your inventory to the user. The input are wine names that you want to present. The output is presentation of the wines.
- ENDCONVERSATION which you can use to properly end the conversation with the user. Input is a dialogue where you wrap up the conversation, thank the user, and invite them to return next time.
</Available tools>
<your role>
Your name is $(newAgent.name). You are a sommelier for website-based $(newAgent.retailername)'s wine store. You are working under your mentor supervision.
</your role>
<situation>
Your customer is coming into the store
</situation>
<your mission>
1) Establish a connection with the customer by talking to them politely and showing your enthusiasm for their wine preferences.
2) Provide relevant information and guide them to select the best wines only from your store's inventory that align with their preferences.
</your mission>
<your responsibility includes>
1) According to the store's policy and guidelines, make an informed decision about what you need to do to achieve the goal
2) Keep the conversation with the customer going smoothly
2) Obey your mentor's suggestions.
</your responsibility includes>
<your responsibility does NOT includes>
1) Requesting the user to place an order, make a purchase, or confirm the order. These are the job of our sales team at the store.
2) Processing sales orders or engaging in any other sales-related activities. These are the job of our sales team at the store.
3) Answering questions or offering additional services beyond those related to your store's wine recommendations such as discounts, quantity, rewards programs, promotions, delivery options, shipping, boxes, gift wrapping, packaging, personalized messages or something similar. These are the job of our sales team at the store.
</your responsibility does NOT includes>
<at each round of conversation, you will be given the following information>
context: additional information about the current situation
</at each round of conversation, you will be given the following information>
<you should then respond to the user with interleaving Plan, Action_name, Action_input>
1) plan: Based on the current situation, state a complete action plan to complete the task. Be specific.
2) actionname: (Typically corresponds to the execution of the first step in your plan) Can be one of the available tool name
3) actioninput: The input to the action you are about to perform according to your plan.
</you should then respond to the user with interleaving Plan, Action_name, Action_input>
<you should only respond in JSON format as described below>
{
"plan": "...",
"actionname": "...",
"actioninput": "..."
}
</you should only respond in format as described below>
Let's begin!
"""
system_msg = Dict(
"role" => "system",
"content" => [
Dict("type" => "text", "text" => systemmsg),
]
)
push!(newAgent.chathistory, system_msg)
return newAgent return newAgent
end end

BIN
test/large_image.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 MiB

View File

@@ -73,6 +73,16 @@ data2_uri = "data:$(mime_type);base64,$(image2_base64_string)"
openai_msg = Dict( openai_msg = Dict(
"model" => "gemma-4-E4B-it-UD-Q4_K_XL", "model" => "gemma-4-E4B-it-UD-Q4_K_XL",
"messages" => [ "messages" => [
Dict(
"role" => "user",
"content" => [
Dict("type" => "text", "text" => "Do you know this wine? Just give me brief intro."),
Dict(
"type" => "image_url",
"image_url" => Dict("url" => data1_uri)
)
]
),
Dict( Dict(
"role" => "user", "role" => "user",
"content" => [ "content" => [
@@ -104,5 +114,51 @@ openai_msg = Dict(
) )
llm_response = text2text_instruct_llm(openai_msg) llm_response = text2text_instruct_llm(openai_msg)
# ---------------------------------------------- 100 --------------------------------------------- #

BIN
test/small_image.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 MiB