diff --git a/Manifest.toml b/Manifest.toml index bf971b5..83d63ea 100644 --- a/Manifest.toml +++ b/Manifest.toml @@ -2,7 +2,7 @@ julia_version = "1.12.6" manifest_format = "2.0" -project_hash = "e6bd85ad2679c39ab370f878253f2eeb45c1b6ae" +project_hash = "7462b22f4fd62982e36c8671793df6d8908c9ad0" [[deps.AliasTables]] deps = ["PtrArrays", "Random"] @@ -217,11 +217,11 @@ version = "1.11.0" [[deps.GeneralUtils]] 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-url = "https://git.yiem.cc/ton/GeneralUtils" uuid = "c6c72f09-b708-4ac8-ac7c-2084d70108fe" -version = "0.3.2" +version = "0.4.0" [[deps.HTTP]] 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" [[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" uuid = "2ebc79c7-cc10-4a3a-9665-d2e1d61e63d3" version = "0.2.4" @@ -884,7 +884,7 @@ uuid = "76eceee3-57b5-4d4a-8e66-0e911cebbf60" version = "1.6.1" [[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 = "." uuid = "e012c34b-7f78-48e0-971c-7abb83b6f0a2" version = "0.4.0" diff --git a/Project.toml b/Project.toml index f41bfb4..7f390db 100644 --- a/Project.toml +++ b/Project.toml @@ -25,7 +25,7 @@ UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" [compat] CSV = "0.10.15" DataFrames = "1.7.0" -GeneralUtils = "0.3.2" +GeneralUtils = "0.4.0" HTTP = "2.4.0" JSON = "1.6.1" NATS = "0.1.0" diff --git a/src/interface.jl b/src/interface.jl index 7d1297b..2ddbbca 100644 --- a/src/interface.jl +++ b/src/interface.jl @@ -70,11 +70,11 @@ julia> config = Dict( :broker => "mqtt.yiem.cc" ), :externalservice => Dict( - :text2textinstruct => Dict( + "text"2textinstruct => Dict( :mqtttopic => "/loadbalancer/requestingservice", :description => "text to text service with instruct LLM", :llminfo => Dict( - :name => "llama3instruct" + "name" => "llama3instruct" ) ), ) @@ -89,14 +89,11 @@ julia> output_thoughtDict = Dict( :observation_1 => "" ) ``` - -# TODO - [] update docstring - [] use customerinfo - [] user storeinfo -# Signature -""" +""" #WORKING function decisionMaker(a::T; recentevents::Integer=20, maxattempt=10 ) where {T<:agent} println("\nExecuting YiemAgent decisionMaker()") @@ -125,158 +122,23 @@ function decisionMaker(a::T; recentevents::Integer=20, maxattempt=10 # """ # end - recentevents_ind = GeneralUtils.recentElementsIndex(length(a.memory[:events]), recentevents; - includelatest=true) - timeline = createTimeline(a.memory[:events]; eventindex=recentevents_ind) + recentevents_ind = GeneralUtils.recentElementsIndex( + length(a.memory[:events]), recentevents; includelatest=true) - # recentchat_ind = GeneralUtils.recentElementsIndex(length(a.chathistory), recentevents; - # includelatest=true) - # recentchat = createChatLog(a.chathistory; index=recentchat_ind) - recentEvents = createEventsLog(a.memory[:events]; index=recentevents_ind) + requiredKeys = ["plan", "actionname", "actioninput"] - - # # recap as caching - # # query similar result from vectorDB - # recapkeys = keys(a.memory[:recap]) - # _recapkeys_vec = [i for i in recapkeys] + errornote = "N/A" + response = nothing # placeholder for show when error msg show up - # # 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 name is $(a.name). You are a sommelier for website-based $(a.retailername)'s wine store. You are working under your mentor supervision. - - - Your customer is coming into the store - - - 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. - - - 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. - - - 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. - - - context: additional information about the current situation - - - 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. - - - { - "plan": "...", - "actionname": "...", - "actioninput": "..." - } - - - 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" - response = nothing # placeholder for show when error msg show up for attempt in 1:maxattempt if attempt > 1 println("\nYiemAgent decisionMaker() attempt $attempt/$maxattempt ", @__FILE__, ":", @__LINE__, " $(Dates.now())") end - - # QandA = generatequestion(a, a.context[:text2textInstructLLM], timeline) - - context = - """ - - - - 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. - - - - 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. - 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 - - - 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. - - $(a.memory[:shortmem][:scratchpad]) - Remark: $errornote - - """ unformatPrompt = [ - Dict(:name => "system", :text => systemmsg), + Dict("name" => "system", "text" => systemmsg), ] unformatPrompt = vcat(unformatPrompt, recentEvents) @@ -322,9 +184,9 @@ function decisionMaker(a::T; recentevents::Integer=20, maxattempt=10 # continue # 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" - 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 end @@ -359,8 +221,8 @@ function decisionMaker(a::T; recentevents::Integer=20, maxattempt=10 delete!(responsedict, :mentioned_winery) - # check whether responsedict[:actioninput] is the same as previous dialogue - if !isempty(a.chathistory) && responsedict[:actioninput] == a.chathistory[end][:text] + # check whether responsedict["actioninput"] is the same as previous dialogue + if !isempty(a.chathistory) && responsedict["actioninput"] == a.chathistory[end]["text"] errornote = "In your previous attempt, you repeated the previous dialogue. Please try again." println("\nERROR YiemAgent decisionMaker() $response ", @__FILE__, ":", @__LINE__, " $(Dates.now())") continue @@ -384,10 +246,10 @@ function decisionMaker(a::T; recentevents::Integer=20, maxattempt=10 # responsedict[:QandA] = QandA # check whether there is a file path exists before writing to it - if !haskey(a.memory[:shortmem], :decisionlog) - a.memory[:shortmem][:decisionlog] = [responsedict] + if !haskey(a.memory["shortmem"], :decisionlog) + a.memory["shortmem"][:decisionlog] = [responsedict] else - push!(a.memory[:shortmem][:decisionlog], responsedict) + push!(a.memory["shortmem"][:decisionlog], responsedict) end # # save to filename ./log/decisionlog.txt @@ -512,7 +374,7 @@ function evaluator(a::T1, timeline, decisiondict, evaluateecontext $evaluateecontext - {plan: $(decisiondict[:plan]), actionname: $(decisiondict[:actionname]), actioninput: $(decisiondict[:actioninput])} + {plan: $(decisiondict["plan"]), actionname: $(decisiondict["actionname"]), actioninput: $(decisiondict["actioninput"])} P.S. $errornote @@ -520,7 +382,7 @@ function evaluator(a::T1, timeline, decisiondict, evaluateecontext unformatPrompt = [ - Dict(:name => "system", :text => systemmsg), + Dict("name" => "system", "text" => systemmsg), ] # put in model format @@ -610,30 +472,17 @@ message => Dict( ] ) -- TODO add recap to initialState for earlier completed question -""" -function conversation(a::sommelier; userinput::Union{Dict, Nothing}=nothing, - maximumMsg=50) - +# ---------------------------------------------- 100 --------------------------------------------- # +""" #WORKING +function conversation(a::sommelier; userinput::Union{Dict{String, Any}, JSON.Object{String, Any}}, maximumMsg=50) + userinput = GeneralUtils.dictify(userinput; keytype=String) + # place holder actionname = nothing result = nothing chatresponse = nothing - userinput = GeneralUtils.dictify(userinput) - - 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 + if userinput === nothing # thinking loop until AI wants to communicate with the user chatresponse = nothing while chatresponse === nothing @@ -642,6 +491,7 @@ function conversation(a::sommelier; userinput::Union{Dict, Nothing}=nothing, chatresponse = result end end + addNewMessage(a, "assistant", chatresponse; maximumMsg=maximumMsg) 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? 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 chatresponse = nothing while chatresponse === nothing @@ -684,12 +523,12 @@ function conversation(a::Union{companion, virtualcustomer}, userinput::Dict; chatresponse = nothing - if userinput[:text] == "newtopic" + if userinput["text"] == "newtopic" clearhistory(a) return "Okay. What shall we talk about?" else # 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 push!(a.memory[:events], @@ -698,7 +537,7 @@ function conversation(a::Union{companion, virtualcustomer}, userinput::Dict; timestamp=Dates.now(), subject="user", actionname="CHATBOX", - actioninput=userinput[:text], + actioninput=userinput["text"], ) ) chatresponse = generatechat(a; converPartnerName=converPartnerName, recentEventNum=20) @@ -729,19 +568,18 @@ end julia> ``` -TODO update docstring """ # WORKING -function think(a::T)::NamedTuple{(:actionname, :result),Tuple{String,String}} where {T<:agent} - # a.memory[:recap] = generateSituationReport(a, a.context[:text2textInstructLLM]; skiprecent=0) +function think(a::T)::namedTuple{(:actionname, :result),Tuple{String,String}} where {T<:agent} + # a.memory[:recap] = generateSituationReport(a, a.context["text"2textInstructLLM]; skiprecent=0) thoughtDict = decisionMaker(a) - actionname = thoughtDict[:actionname] - actioninput = thoughtDict[:actioninput] + actionname = thoughtDict["actionname"] + actioninput = thoughtDict["actioninput"] # map action and input() to llm function response = if actionname == "CHATBOX" || actionname == "ENDCONVERSATION" - (result=thoughtDict[:plan], errormsg=nothing, success=true) + (result=thoughtDict["plan"], errormsg=nothing, success=true) elseif actionname == "CHECKWINE" checkwine(a, actioninput) elseif actionname == "PRESENTBOX" @@ -776,10 +614,10 @@ function think(a::T)::NamedTuple{(:actionname, :result),Tuple{String,String}} wh # ) # ) # result = chatresponse - if actionname ∈ ["CHATBOX", "ENDCONVERSATION"] + if actionname ∈ ["CHATBOX"] push!(a.memory[:events], eventdict(; - event_description="the assistant talks to the user.", + event_description="an assistant talks to the user.", timestamp=Dates.now(), subject="assistant", thought=thoughtDict, @@ -788,24 +626,24 @@ function think(a::T)::NamedTuple{(:actionname, :result),Tuple{String,String}} wh ) ) result = actioninput - # elseif actionname ∈ ["ENDCONVERSATION"] - # chatresponse = generatechat(a, thoughtDict) - # push!(a.memory[:events], - # eventdict(; - # event_description="the assistant talks to the user.", - # timestamp=Dates.now(), - # subject="assistant", - # thought=thoughtDict, - # actionname=actionname, - # actioninput=chatresponse, - # ) - # ) - # result = chatresponse + elseif actionname ∈ ["ENDCONVERSATION"] + chatresponse = generatechat(a, thoughtDict) + push!(a.memory[:events], + eventdict(; + event_description="an assistant talks to the user to end conversation.", + timestamp=Dates.now(), + subject="assistant", + thought=thoughtDict, + actionname=actionname, + actioninput=chatresponse, + ) + ) + result = chatresponse elseif actionname ∈ ["PRESENTBOX"] chatresponse = presentbox(a, thoughtDict) push!(a.memory[:events], eventdict(; - event_description="the assistant talks to the user.", + event_description="the assistant presents wines to the user.", timestamp=Dates.now(), subject="assistant", thought=thoughtDict, @@ -818,23 +656,23 @@ function think(a::T)::NamedTuple{(:actionname, :result),Tuple{String,String}} wh elseif actionname == "CHECKWINE" if rawresponse !== nothing vd = GeneralUtils.dfToVectorDict(rawresponse) # comes in dataframe format - # 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? - 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"][: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? + if length(a.memory["shortmem"][:db_search_result]) != 0 + a.memory["shortmem"][:db_search_result] = vcat(a.memory["shortmem"][:db_search_result], vd) else - a.memory[:shortmem][:db_search_result] = vd + a.memory["shortmem"][:db_search_result] = vd end # # add to scratchpad - # a.memory[:shortmem][:scratchpad] *= + # a.memory["shortmem"]["scratchpad"] *= # """ # # I searched the database with this search term: $actioninput \nThis is what I found: $result # # """ else - # a.memory[:shortmem][:scratchpad] *= + # a.memory["shortmem"]["scratchpad"] *= # """ # # 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] database_search_result = - if length(a.memory[:shortmem][:db_search_result]) != 0 - availableWineToText(a.memory[:shortmem][:db_search_result]) + if length(a.memory["shortmem"][:db_search_result]) != 0 + availableWineToText(a.memory["shortmem"][:db_search_result]) else "N/A" 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 for attempt in 1:maxtattempt @@ -920,15 +758,15 @@ function presentbox(a::sommelier, thoughtDict; maxtattempt::Integer=10, recentev context = """ - Name of the wines that needs to be introduced: $(thoughtDict[:actioninput]) - $(a.memory[:shortmem][:scratchpad]) + Name of the wines that needs to be introduced: $(thoughtDict["actioninput"]) + $(a.memory["shortmem"]["scratchpad"]) P.S. $errornote """ unformatPrompt = [ - Dict(:name => "system", :text => systemmsg), + Dict("name" => "system", "text" => systemmsg), ] unformatPrompt = vcat(unformatPrompt, recentchat) @@ -1075,10 +913,10 @@ function generatechat(a::sommelier, thoughtDict; maxattempt::Integer=10) """ requiredKeys = [:dialogue] - # a.memory[:shortmem][:available_wine] is a vector of dictionary + # a.memory["shortmem"][:available_wine] is a vector of dictionary # context = - # if length(a.memory[:shortmem][:available_wine]) != 0 - # "Wines previously found in your inventory: $(availableWineToText(a.memory[:shortmem][:available_wine]))" + # if length(a.memory["shortmem"][:available_wine]) != 0 + # "Wines previously found in your inventory: $(availableWineToText(a.memory["shortmem"][:available_wine]))" # else # "N/A" # end @@ -1087,13 +925,13 @@ function generatechat(a::sommelier, thoughtDict; maxattempt::Integer=10) errornote = "N/A" response = nothing # placeholder for show when error msg show up - yourthought = thoughtDict[:plan] + yourthought = thoughtDict["plan"] # yourthought1 = nothing for attempt in 1:maxattempt # if attempt > 1 # use to prevent LLM generate the same respond over and over # println("\nYiemAgent generatchat() attempt $attempt/10 ", @__FILE__, ":", @__LINE__, " $(Dates.now())") - # yourthought1 = paraphrase(a.context[:text2textInstructLLM], yourthought) + # yourthought1 = paraphrase(a.context["text"2textInstructLLM], yourthought) # else # yourthought1 = yourthought # end @@ -1109,7 +947,7 @@ function generatechat(a::sommelier, thoughtDict; maxattempt::Integer=10) unformatPrompt = [ - Dict(:name => "system", :text => systemmsg), + Dict("name" => "system", "text" => systemmsg), ] # put in model format @@ -1212,7 +1050,7 @@ function generatechat(a::companion; recentevents::Integer=10, unformatPrompt = [ - Dict(:name => "system", :text => a.systemmsg), + Dict("name" => "system", "text" => a.systemmsg), ] unformatPrompt = vcat(unformatPrompt, recentchat) @@ -1233,7 +1071,7 @@ function generatechat(a::companion; recentevents::Integer=10, # check whether LLM just repeat the previous dialogue 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." println("\nYiemAgent generatechat() $errornote:\n$response ", @__FILE__, ":", @__LINE__, " $(Dates.now())") continue @@ -1274,7 +1112,7 @@ function generatechat(a::virtualcustomer; unformatPrompt = [ - Dict(:name => "system", :text => a.systemmsg), + Dict("name" => "system", "text" => a.systemmsg), ] unformatPrompt = vcat(unformatPrompt, recentEventsDict) # put in model format @@ -1310,7 +1148,7 @@ function generatechat(a::virtualcustomer; end # 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." println("\nYiemAgent generatechat() $errornote:\n$response ", @__FILE__, ":", @__LINE__, " $(Dates.now())") continue @@ -1319,7 +1157,7 @@ function generatechat(a::virtualcustomer; # check whether LLM just repeat the previous dialogue dublicate = false 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." println("\nYiemAgent generatechat() $errornote:\n$response ", @__FILE__, ":", @__LINE__, " $(Dates.now())") dublicate = true @@ -1447,12 +1285,12 @@ function generatequestion(a, text2textInstructLLM::Function, timeline)::String dictkey = ["q1"] # context = - # if length(a.memory[:shortmem][:available_wine]) != 0 - # "Available wines you've found in your inventory so far: $(availableWineToText(a.memory[:shortmem][:available_wine]))" + # if length(a.memory["shortmem"][:available_wine]) != 0 + # "Available wines you've found in your inventory so far: $(availableWineToText(a.memory["shortmem"][:available_wine]))" # else # "N/A" # 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) # recentevents = a.memory[:events][recent_ind] @@ -1496,8 +1334,8 @@ function generatequestion(a, text2textInstructLLM::Function, timeline)::String _prompt = [ - Dict(:name => "system", :text => systemmsg), - Dict(:name => "user", :text => usermsg) + Dict("name" => "system", "text" => systemmsg), + Dict("name" => "user", "text" => usermsg) ] # put in model format @@ -1630,8 +1468,8 @@ function generateSituationReport(a, text2textInstructLLM::Function; skiprecent:: _prompt = [ - Dict(:name => "system", :text => systemmsg), - Dict(:name => "user", :text => usermsg) + Dict("name" => "system", "text" => systemmsg), + Dict("name" => "user", "text" => usermsg) ] # put in model format @@ -1702,8 +1540,8 @@ function detectWineryName(a, text) """ _prompt = [ - Dict(:name => "system", :text => systemmsg), - Dict(:name => "user", :text => usermsg) + Dict("name" => "system", "text" => systemmsg), + Dict("name" => "user", "text" => usermsg) ] # put in model format diff --git a/src/type.jl b/src/type.jl index 7c4bfde..001eb43 100644 --- a/src/type.jl +++ b/src/type.jl @@ -194,6 +194,86 @@ function sommelier( context, llmFormatName ) + + systemmsg = + """ + + - 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. + + + - 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. + 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 + + - 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. + + + 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 customer is coming into the store + + + 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. + + + 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. + + + 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. + + + context: additional information about the current situation + + + 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. + + + { + "plan": "...", + "actionname": "...", + "actioninput": "..." + } + + + Let's begin! + """ + + system_msg = Dict( + "role" => "system", + "content" => [ + Dict("type" => "text", "text" => systemmsg), + ] + ) + + push!(newAgent.chathistory, system_msg) return newAgent end diff --git a/test/large_image.png b/test/large_image.png new file mode 100644 index 0000000..2a89fd8 Binary files /dev/null and b/test/large_image.png differ diff --git a/test/runtests.jl b/test/runtests.jl index 8c2658b..3b9cd98 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -73,6 +73,16 @@ data2_uri = "data:$(mime_type);base64,$(image2_base64_string)" openai_msg = Dict( "model" => "gemma-4-E4B-it-UD-Q4_K_XL", "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( "role" => "user", "content" => [ @@ -104,5 +114,51 @@ openai_msg = Dict( ) llm_response = text2text_instruct_llm(openai_msg) + + + + + +# ---------------------------------------------- 100 --------------------------------------------- # + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/small_image.png b/test/small_image.png new file mode 100644 index 0000000..7ac301b Binary files /dev/null and b/test/small_image.png differ