From 1d0353d793d25126f93e2af01a30387f7b274758 Mon Sep 17 00:00:00 2001 From: narawat Date: Thu, 9 Jul 2026 20:14:04 +0700 Subject: [PATCH] update --- src/interface.jl | 209 +++++------------------------------------------ 1 file changed, 21 insertions(+), 188 deletions(-) diff --git a/src/interface.jl b/src/interface.jl index 382684a..24f35d7 100644 --- a/src/interface.jl +++ b/src/interface.jl @@ -155,18 +155,23 @@ function decisionMaker(a::T; recentevents::Integer=20, maxattempt=10 # fall back to normal text because LLM default to natural chat when it didn't use action_call else - responsedict = OrderedDict( - "plan"=> "I will talk to the user", - "action_name"=> "CHAT_BOX", - "action_input"=> response[2:end-1] # remove { } at the front and back that added by clean_json_response - ) + try + responsedict = OrderedDict( + "plan"=> "I will talk to the user", + "action_name"=> "CHAT_BOX", + "action_input"=> response[2:end-1] # remove { } at the front and back that added by clean_json_response + ) + catch e + println("\nERROR YiemAgent decisionMaker(). $e --(not qualify response)-> $response", @__FILE__, ":", @__LINE__, " $(Dates.now())\n") + continue + end end # check whether all answer's key points are in responsedict ispass, errormsg = GeneralUtils.checkAgentResponse_JSON(responsedict, requiredKeys) if !ispass errornote = errormsg - println("\nERROR YiemAgent decisionMaker() $errornote --(not qualify response)> $responsedict", @__FILE__, ":", @__LINE__, " $(Dates.now())\n") + println("\nERROR YiemAgent decisionMaker() $errornote --(not qualify response)-> $responsedict", @__FILE__, ":", @__LINE__, " $(Dates.now())\n") continue end @@ -674,11 +679,16 @@ function generatechat(a::T; recentevents::Integer=20, maxattempt=10 # fall back to normal text because LLM default to natural chat when it didn't use action_call else - responsedict = OrderedDict( - "plan"=> "I will talk to the user", - "action_name"=> "CHAT_BOX", - "action_input"=> response[2:end-1] # remove { } at the front and back that added by clean_json_response - ) + try + responsedict = OrderedDict( + "plan"=> "I will talk to the user", + "action_name"=> "CHAT_BOX", + "action_input"=> response[2:end-1] # remove { } at the front and back that added by clean_json_response + ) + catch e + println("\nERROR YiemAgent decisionMaker(). $e --(not qualify response)-> $response", @__FILE__, ":", @__LINE__, " $(Dates.now())\n") + continue + end end # check whether all answer's key points are in responsedict @@ -702,183 +712,6 @@ function generatechat(a::T; recentevents::Integer=20, maxattempt=10 end error("YiemAgent generatechat() failed to generate a thought ", response) end -# function generatechat(a::T; recentevents::Integer=20, maxattempt=10 -# )::String where {T<:agent} - -# # lessonDict = copy(JSON.parsefile("lesson.json")) - -# # lesson = -# # if isempty(lessonDict) -# # "" -# # else -# # lessons = Dict{String, Any}() -# # for (k, v) in lessonDict -# # lessons[k] = lessonDict[k][:lesson] -# # end - -# # """ -# # You have attempted to help the user before and failed, either because your reasoning for the -# # recommendation was incorrect or your response did not exactly match the user expectation. -# # The following lesson(s) give a plan to avoid failing to help the user in the same way you -# # did previously. Use them to improve your strategy to help the user. - -# # Here are some lessons in JSON format: -# # $(JSON.json(lessons)) - -# # When providing the thought and action for the current trial, that into account these failed -# # trajectories and make sure not to repeat the same mistakes and incorrect answers. -# # """ -# # end - -# # recentevents_ind = GeneralUtils.recentElementsIndex( -# # length(a.memory["events"]), recentevents; includelatest=true) - -# 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 immediately 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. -# - Customer may provide images for you to look up. -# - 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. -# -# -# You are continuing the conversation with the user. -# -# -# Your name is $(a.name). You are a helpful sommelier for website-based $(a.retailername)'s wine store. You are working under your mentor supervision. -# -# -# 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 objective -# 2) Keep the conversation with the customer going smoothly -# -# -# 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. -# -# -# Dialogue you want to chat with the user -# -# -# "CHAT_BOX": "..." -# -# """ - -# system_msg = Dict( -# "role" => "system", -# "content" => [ -# Dict("type" => "text", "text" => systemmsg), -# ] -# ) - -# chathistory = deepcopy(a.chathistory[2:end]) -# pushfirst!(chathistory, system_msg) - -# requiredKeys = ["CHAT_BOX"] -# context = -# """ -# -# -# $(GeneralUtils.dict_to_string_html(a.memory["shortmem"])) -# -# -# """ - -# # add context to text of the latest message (in the front). -# # use for loop because in openai format, each msg may contain both text and image. -# for d in chathistory[end]["content"] -# if d["type"] == "text" -# d["text"] = context * d["text"] -# break -# end -# end -# errornote = "N/A" -# response = nothing # placeholder for show when error msg show up - -# for attempt in 1:maxattempt -# if attempt > 1 -# println("\nYiemAgent generatechat() attempt $attempt/$maxattempt ", @__FILE__, ":", @__LINE__, " $(Dates.now())") -# end - -# msg = Dict( -# "model" => "gemma-4-E4B-it-UD-Q4_K_XL", -# "messages" => chathistory, -# "temperature" => 0.7 -# ) -# @info "YiemAgent generatechat() 2-2 attempt $attempt " @__LINE__ -# response = a.context.text2textInstructLLM(a.id, msg) -# @info "YiemAgent generatechat() 2-3 attempt $attempt " @__LINE__ -# response = GeneralUtils.clean_json_response(response) - -# response = GeneralUtils.remove_french_accents(response) -# think, response = GeneralUtils.extractthink(response) -# response = String(split(response, ", observation")[1]) # in case LLM generate observation key which it isn't supposed to -# response = strip(response) -# @show response -# responsedict = nothing -# if occursin("CHAT_BOX", response) -# @info "YiemAgent generatechat() 2-4 attempt $attempt " @__LINE__ -# try -# @info "YiemAgent generatechat() 2-5 attempt $attempt " @__LINE__ -# _responsedict = JSON.parse(response) -# responsedict = GeneralUtils.dictify(_responsedict; keytype=String, sort_order=requiredKeys) -# catch -# @info "YiemAgent generatechat() 2-6 attempt $attempt " @__LINE__ -# println("\nERROR YiemAgent generatechat() failed to parse response: $response ", @__FILE__, ":", @__LINE__, " $(Dates.now())") -# continue -# end -# # fall back to normal text because LLM default to natural chat when it didn't use action_call -# else -# @info "YiemAgent generatechat() 2-7 attempt $attempt " @__LINE__ -# responsedict = OrderedDict( -# "CHAT_BOX"=> response -# ) -# end - -# if length(keys(responsedict)) > length(requiredKeys) -# @info "YiemAgent generatechat() 2-7-1 attempt $attempt " @__LINE__ -# continue -# end - -# @info "YiemAgent generatechat() 2-8 attempt $attempt " @__LINE__ -# # check whether all answer's key points are in responsedict -# ispass, errormsg = GeneralUtils.checkAgentResponse_JSON(responsedict, requiredKeys) -# if !ispass -# @info "YiemAgent generatechat() 2-9 attempt $attempt " @__LINE__ -# errornote = errormsg -# println("\nERROR YiemAgent generatechat() $errornote --(not qualify response)> $responsedict ", @__FILE__, ":", @__LINE__, " $(Dates.now())\n") -# continue -# end - -# @info "YiemAgent generatechat() 2-12 attempt $attempt " @__LINE__ -# # println("\nYiemAgent generatechat() ", @__FILE__, ":", @__LINE__, " $(Dates.now())") -# # pprintln(responsedict) - -# return responsedict["CHAT_BOX"] -# end -# @info "YiemAgent generatechat() 2-13 attempt $attempt " @__LINE__ -# error("YiemAgent generatechat() failed to generate a thought ", response) -# end function generatequestion(a, text2textInstructLLM::Function, timeline)::String