From e0dc7d29b2529e6a29b0fb52bdbfaa4ee3290670 Mon Sep 17 00:00:00 2001 From: narawat lamaiin Date: Sun, 15 Jun 2025 08:02:59 +0700 Subject: [PATCH] update --- src/interface.jl | 83 ++++++++++-------------------------------------- 1 file changed, 17 insertions(+), 66 deletions(-) diff --git a/src/interface.jl b/src/interface.jl index 449f100..713c65c 100644 --- a/src/interface.jl +++ b/src/interface.jl @@ -1024,7 +1024,7 @@ julia> response = ChatAgent.conversation(newAgent, "Hi! how are you?") """ function conversation(a::sommelier; userinput::Union{Dict, Nothing}=nothing, maximumMsg=50) - #[WORKING] add the ability to start the conversation by sommelier + # place holder actionname = nothing result = nothing @@ -1890,64 +1890,12 @@ function generatechat(a::companion; recentevents::Integer=10, end error("generatechat failed to generate a response") end -# function generatechat(a::companion; converPartnerName::Union{String, Nothing}=nothing, maxattempt=10) -# response = nothing # placeholder for show when error msg show up -# errornote = "N/A" -# llmkwargs=Dict( -# :num_ctx => 32768, -# :temperature => 0.5, -# ) -# for attempt in 1:maxattempt -# if attempt > 1 -# println("\nYiemAgent generatechat() attempt $attempt/$maxattempt ", @__FILE__, ":", @__LINE__, " $(Dates.now())") -# end - -# systemmsg = a.systemmsg * "\nP.S. $errornote\n" -# _prompt = -# [ -# Dict(:name => "system", :text => systemmsg), -# ] -# for i in a.chathistory -# tempdict = Dict{Symbol, String}() -# for j in keys(i) -# if j ∉ [:timestamp] -# tempdict[j] = i[j] -# end -# end -# _prompt = vcat(_prompt, tempdict) -# end - -# # put in model format -# _prompt = GeneralUtils.formatLLMtext(_prompt, a.llmFormatName) - -# # replace user and assistant with partner name -# prompt = replace(_prompt, "|>user"=>"|>$(converPartnerName)") -# prompt = replace(prompt, "|>assistant"=>"|>$(a.name)") - -# response = a.func[:text2textInstructLLM](prompt; llmkwargs=llmkwargs, senderId=a.id) -# response = replace(response, "<|im_start|>"=> "") -# response = GeneralUtils.deFormatLLMtext(response, a.llmFormatName) -# think, response = GeneralUtils.extractthink(response) - -# # check whether LLM just repeat the previous dialogue -# for msg in a.chathistory -# 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 -# end -# end - -# return response -# end -# error("generatechat failed to generate a response") -# end - # modify it to work with customer object function generatechat(a::virtualcustomer; converPartnerName::Union{String, Nothing}=nothing, maxattempt=10, recentEventNum=10 - ) + ) + recent_ind = GeneralUtils.recentElementsIndex(length(a.memory[:events]), recentEventNum; includelatest=true) recentEventsDict = createEventsLog(a.memory[:events]; index=recent_ind) response = nothing # placeholder for show when error msg show up @@ -1979,21 +1927,12 @@ function generatechat(a::virtualcustomer; prompt = GeneralUtils.formatLLMtext(unformatPrompt, a.llmFormatName) # add info prompt = prompt * context - + response = a.func[:text2textInstructLLM](prompt; llmkwargs=llmkwargs, senderId=a.id) response = replace(response, "<|im_start|>"=> "") response = GeneralUtils.deFormatLLMtext(response, a.llmFormatName) think, response = GeneralUtils.extractthink(response) - # check whether LLM just repeat the previous dialogue - for msg in a.chathistory - 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 - end - end - # check whether response has all header detected_kw = GeneralUtils.detect_keyword(header, response) kwvalue = [i for i in values(detected_kw)] @@ -2024,7 +1963,19 @@ function generatechat(a::virtualcustomer; continue end - + # check whether LLM just repeat the previous dialogue + dublicate = false + for msg in a.chathistory + 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 + break + end + end + if dublicate + continue + end # println("\n$prompt", @__FILE__, ":", @__LINE__, " $(Dates.now())") # println("\n $response")