This commit is contained in:
narawat lamaiin
2025-04-25 21:12:27 +07:00
parent 48a3704f6d
commit 44804041a3
3 changed files with 81 additions and 58 deletions

View File

@@ -284,12 +284,13 @@ function decisionMaker(a::T; recent::Integer=10
# end # end
# change qwen format put in model format # change qwen format put in model format
prompt = GeneralUtils.formatLLMtext(unformatPrompt; formatname="qwen") prompt = GeneralUtils.formatLLMtext(unformatPrompt, "granite3")
response = a.func[:text2textInstructLLM](prompt) response = a.func[:text2textInstructLLM](prompt)
response = GeneralUtils.remove_french_accents(response) response = GeneralUtils.remove_french_accents(response)
response = replace(response, "**"=>"") response = replace(response, "**"=>"")
response = replace(response, "***"=>"") response = replace(response, "***"=>"")
response = replace(response, "<|eot_id|>"=>"") response = replace(response, "<|eot_id|>"=>"")
response = GeneralUtils.deFormatLLMtext(response, "granite3")
# check if response contain more than one functions from ["CHATBOX", "CHECKINVENTORY", "ENDCONVERSATION"] # check if response contain more than one functions from ["CHATBOX", "CHECKINVENTORY", "ENDCONVERSATION"]
count = 0 count = 0
@@ -300,7 +301,7 @@ function decisionMaker(a::T; recent::Integer=10
end end
if count > 1 if count > 1
errornote = "You must use only one function" errornote = "You must use only one function"
println("\nYiemAgent decisionMaker() $errornote ", @__FILE__, ":", @__LINE__, " $(Dates.now())") println("\nYiemAgent decisionMaker() $errornote\n$response ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
continue continue
end end
@@ -601,7 +602,7 @@ end
# # end # # end
# # change qwen format put in model format # # change qwen format put in model format
# prompt = GeneralUtils.formatLLMtext(unformatPrompt; formatname="qwen") # prompt = GeneralUtils.formatLLMtext(unformatPrompt, "granite3")
# response = a.func[:text2textInstructLLM](prompt) # response = a.func[:text2textInstructLLM](prompt)
# response = GeneralUtils.remove_french_accents(response) # response = GeneralUtils.remove_french_accents(response)
@@ -832,7 +833,7 @@ function evaluator(state::T1, text2textInstructLLM::Function
] ]
# put in model format # put in model format
prompt = GeneralUtils.formatLLMtext(_prompt; formatname="qwen") prompt = GeneralUtils.formatLLMtext(_prompt, "granite3")
header = ["Trajectory_evaluation:", "Answer_evaluation:", "Accepted_as_answer:", "Score:", "Suggestion:"] header = ["Trajectory_evaluation:", "Answer_evaluation:", "Accepted_as_answer:", "Score:", "Suggestion:"]
dictkey = ["trajectory_evaluation", "answer_evaluation", "accepted_as_answer", "score", "suggestion"] dictkey = ["trajectory_evaluation", "answer_evaluation", "accepted_as_answer", "score", "suggestion"]
@@ -842,6 +843,7 @@ function evaluator(state::T1, text2textInstructLLM::Function
# sometime LLM output something like **Comprehension**: which is not expected # sometime LLM output something like **Comprehension**: which is not expected
response = replace(response, "**"=>"") response = replace(response, "**"=>"")
response = replace(response, "***"=>"") response = replace(response, "***"=>"")
response = GeneralUtils.deFormatLLMtext(response, "granite3")
# check whether response has all header # check whether response has all header
detected_kw = GeneralUtils.detect_keyword(header, response) detected_kw = GeneralUtils.detect_keyword(header, response)
@@ -1084,8 +1086,7 @@ function think(a::T)::NamedTuple{(:actionname, :result),Tuple{String,String}} wh
# ) # )
# ) # )
# result = chatresponse # result = chatresponse
if actionname ["CHATBOX", "ENDCONVERSATION"] if actionname ["CHATBOX"]
# chatresponse = generatechat(a, thoughtDict)
push!(a.memory[:events], push!(a.memory[:events],
eventdict(; eventdict(;
event_description="the assistant talks to the user.", event_description="the assistant talks to the user.",
@@ -1097,6 +1098,19 @@ function think(a::T)::NamedTuple{(:actionname, :result),Tuple{String,String}} wh
) )
) )
result = actioninput 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 ["PRESENTBOX"] elseif actionname ["PRESENTBOX"]
chatresponse = presentbox(a, thoughtDict) chatresponse = presentbox(a, thoughtDict)
push!(a.memory[:events], push!(a.memory[:events],
@@ -1132,8 +1146,8 @@ function think(a::T)::NamedTuple{(:actionname, :result),Tuple{String,String}} wh
subject= "assistant", subject= "assistant",
thought=thoughtDict, thought=thoughtDict,
actionname=actionname, actionname=actionname,
actioninput= "I searched the database with this query: $actioninput", actioninput= "I found something in the database using this SQL: $actioninput",
outcome= "This is what I've found in the database, $result" outcome= "This is what I found:, $result"
) )
) )
else else
@@ -1193,7 +1207,7 @@ function presentbox(a::sommelier, thoughtDict)
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 presentbox() attempt $attempt/10 ", @__FILE__, ":", @__LINE__, " $(Dates.now())") println("\nYiemAgent presentbox() attempt $attempt/10 ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
# yourthought1 = paraphrase(a.func[:text2textInstructLLM], yourthought) # yourthought1 = paraphrase(a.func[:text2textInstructLLM], yourthought)
# llmkwargs[:temperature] = 0.1 * attempt # llmkwargs[:temperature] += 0.1
else else
# yourthought1 = yourthought # yourthought1 = yourthought
end end
@@ -1213,7 +1227,7 @@ function presentbox(a::sommelier, thoughtDict)
] ]
# put in model format # put in model format
prompt = GeneralUtils.formatLLMtext(_prompt; formatname="qwen") prompt = GeneralUtils.formatLLMtext(_prompt, "granite3")
response = a.func[:text2textInstructLLM](prompt) response = a.func[:text2textInstructLLM](prompt)
@@ -1234,6 +1248,7 @@ function presentbox(a::sommelier, thoughtDict)
response = replace(response, '`' => "") response = replace(response, '`' => "")
response = replace(response, "<|eot_id|>"=>"") response = replace(response, "<|eot_id|>"=>"")
response = GeneralUtils.remove_french_accents(response) response = GeneralUtils.remove_french_accents(response)
response = GeneralUtils.deFormatLLMtext(response, "granite3")
# check whether response has all header # check whether response has all header
detected_kw = GeneralUtils.detect_keyword(header, response) detected_kw = GeneralUtils.detect_keyword(header, response)
@@ -1371,14 +1386,14 @@ function generatechat(a::sommelier, thoughtDict)
llmkwargs=Dict( llmkwargs=Dict(
:num_ctx => 32768, :num_ctx => 32768,
:temperature => 0.1, :temperature => 0.2,
) )
for attempt in 1:10 for attempt in 1:10
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.func[:text2textInstructLLM], yourthought) yourthought1 = paraphrase(a.func[:text2textInstructLLM], yourthought)
llmkwargs[:temperature] = 0.1 * attempt llmkwargs[:temperature] += 0.1
else else
yourthought1 = yourthought yourthought1 = yourthought
end end
@@ -1398,8 +1413,9 @@ function generatechat(a::sommelier, thoughtDict)
] ]
# put in model format # put in model format
prompt = GeneralUtils.formatLLMtext(_prompt; formatname="qwen") prompt = GeneralUtils.formatLLMtext(_prompt, "granite3")
response = a.func[:text2textInstructLLM](prompt; llmkwargs=llmkwargs) response = a.func[:text2textInstructLLM](prompt; llmkwargs=llmkwargs)
response = GeneralUtils.deFormatLLMtext(response, "granite3")
# sometime the model response like this "here's how I would respond: ..." # sometime the model response like this "here's how I would respond: ..."
if occursin("respond:", response) if occursin("respond:", response)
@@ -1476,7 +1492,7 @@ function generatechat(a::sommelier, thoughtDict)
error("generatechat failed to generate a response") error("generatechat failed to generate a response")
end end
#[WORKING] modify it to work with customer object # modify it to work with customer object
function generatechat(a::companion; converPartnerName::Union{String, Nothing}=nothing) function generatechat(a::companion; converPartnerName::Union{String, Nothing}=nothing)
# header = ["Dialogue:"] # header = ["Dialogue:"]
@@ -1509,12 +1525,13 @@ function generatechat(a::companion; converPartnerName::Union{String, Nothing}=no
end end
# put in model format # put in model format
_prompt = GeneralUtils.formatLLMtext(_prompt; formatname="qwen") _prompt = GeneralUtils.formatLLMtext(_prompt, "granite3")
prompt = replace(_prompt, "|>user"=>"|>$(converPartnerName)") prompt = replace(_prompt, "|>user"=>"|>$(converPartnerName)")
prompt = replace(prompt, "|>assistant"=>"|>$(a.name)") prompt = replace(prompt, "|>assistant"=>"|>$(a.name)")
response = a.func[:text2textInstructLLM](prompt; llmkwargs=llmkwargs) response = a.func[:text2textInstructLLM](prompt; llmkwargs=llmkwargs)
response = replace(response, "<|im_start|>"=> "") response = replace(response, "<|im_start|>"=> "")
response = GeneralUtils.deFormatLLMtext(response, "granite3")
# 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
@@ -1529,6 +1546,8 @@ function generatechat(a::companion; converPartnerName::Union{String, Nothing}=no
#[WORKING] some time it copy exactly the same text as previous conversation partner msg. #[WORKING] some time it copy exactly the same text as previous conversation partner msg.
# # check whether response has all header # # check whether response has all header
# detected_kw = GeneralUtils.detect_keyword(header, response) # detected_kw = GeneralUtils.detect_keyword(header, response)
# kwvalue = [i for i in values(detected_kw)] # kwvalue = [i for i in values(detected_kw)]
@@ -1648,8 +1667,6 @@ function generatequestion(a, text2textInstructLLM::Function;
A: ... A: ...
Q: Do I have what the user is looking for in our stock? Q: Do I have what the user is looking for in our stock?
A: ... A: ...
Q: Did I introduce what I found in our inventory to the user already?
A: According to my conversation with the user, not yet.
Q: Am I certain about the information I'm going to share with the user, or should I verify the information first? Q: Am I certain about the information I'm going to share with the user, or should I verify the information first?
A: ... A: ...
Q: What should I do? Q: What should I do?
@@ -1705,7 +1722,7 @@ function generatequestion(a, text2textInstructLLM::Function;
for attempt in 1:10 for attempt in 1:10
if attempt > 1 if attempt > 1
println("\nYiemAgent generatequestion() attempt $attempt/10 ", @__FILE__, ":", @__LINE__, " $(Dates.now())") println("\nYiemAgent generatequestion() attempt $attempt/10 ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
llmkwargs[:temperature] = 0.1 * attempt llmkwargs[:temperature] += 0.1
end end
usermsg = usermsg =
@@ -1723,9 +1740,10 @@ function generatequestion(a, text2textInstructLLM::Function;
] ]
# put in model format # put in model format
prompt = GeneralUtils.formatLLMtext(_prompt; formatname="qwen") prompt = GeneralUtils.formatLLMtext(_prompt, "granite3")
response = text2textInstructLLM(prompt, modelsize="medium", llmkwargs=llmkwargs) response = text2textInstructLLM(prompt, modelsize="medium", llmkwargs=llmkwargs)
response = GeneralUtils.deFormatLLMtext(response, "granite3")
# make sure generatequestion() don't have wine name that is not from retailer inventory # make sure generatequestion() don't have wine name that is not from retailer inventory
# check whether an agent recommend wines before checking inventory or recommend wines # check whether an agent recommend wines before checking inventory or recommend wines
# outside its inventory # outside its inventory
@@ -1852,9 +1870,10 @@ function generateSituationReport(a, text2textInstructLLM::Function; skiprecent::
] ]
# put in model format # put in model format
prompt = GeneralUtils.formatLLMtext(_prompt; formatname="qwen") prompt = GeneralUtils.formatLLMtext(_prompt, "granite3")
response = text2textInstructLLM(prompt) response = text2textInstructLLM(prompt)
response = GeneralUtils.deFormatLLMtext(response, "granite3")
# check whether response has all header # check whether response has all header
detected_kw = GeneralUtils.detect_keyword(header, response) detected_kw = GeneralUtils.detect_keyword(header, response)
@@ -1923,10 +1942,11 @@ function detectWineryName(a, text)
] ]
# put in model format # put in model format
prompt = GeneralUtils.formatLLMtext(_prompt; formatname="qwen") prompt = GeneralUtils.formatLLMtext(_prompt, "granite3")
try try
response = a.func[:text2textInstructLLM](prompt) response = a.func[:text2textInstructLLM](prompt)
response = GeneralUtils.deFormatLLMtext(response, "granite3")
println("\ndetectWineryName() ", @__FILE__, ":", @__LINE__, " $(Dates.now())") println("\ndetectWineryName() ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
pprintln(response) pprintln(response)

View File

@@ -406,9 +406,10 @@ function extractWineAttributes_1(a::T1, input::T2)::String where {T1<:agent, T2<
] ]
# put in model format # put in model format
prompt = GeneralUtils.formatLLMtext(_prompt; formatname="qwen") prompt = GeneralUtils.formatLLMtext(_prompt, "granite3")
response = a.func[:text2textInstructLLM](prompt) response = a.func[:text2textInstructLLM](prompt)
response = GeneralUtils.remove_french_accents(response) response = GeneralUtils.remove_french_accents(response)
response = GeneralUtils.deFormatLLMtext(response, "granite3")
# check wheter all attributes are in the response # check wheter all attributes are in the response
checkFlag = false checkFlag = false
@@ -640,9 +641,10 @@ function extractWineAttributes_2(a::T1, input::T2)::String where {T1<:agent, T2<
] ]
# put in model format # put in model format
prompt = GeneralUtils.formatLLMtext(_prompt; formatname="qwen") prompt = GeneralUtils.formatLLMtext(_prompt, "granite3")
response = a.func[:text2textInstructLLM](prompt) response = a.func[:text2textInstructLLM](prompt)
response = GeneralUtils.deFormatLLMtext(response, "granite3")
# check whether response has all header # check whether response has all header
detected_kw = GeneralUtils.detect_keyword(header, response) detected_kw = GeneralUtils.detect_keyword(header, response)
@@ -765,10 +767,11 @@ function paraphrase(text2textInstructLLM::Function, text::String)
] ]
# put in model format # put in model format
prompt = GeneralUtils.formatLLMtext(_prompt; formatname="qwen") prompt = GeneralUtils.formatLLMtext(_prompt, "granite3")
try try
response = text2textInstructLLM(prompt) response = text2textInstructLLM(prompt)
response = GeneralUtils.deFormatLLMtext(response, "granite3")
# sometime the model response like this "here's how I would respond: ..." # sometime the model response like this "here's how I would respond: ..."
if occursin("respond:", response) if occursin("respond:", response)
errornote = "You don't need to intro your response" errornote = "You don't need to intro your response"
@@ -984,7 +987,7 @@ end
# ] # ]
# # put in model format # # put in model format
# prompt = GeneralUtils.formatLLMtext(_prompt; formatname="qwen") # prompt = GeneralUtils.formatLLMtext(_prompt, "granite3")
# prompt *= # prompt *=
# """ # """
# <|start_header_id|>assistant<|end_header_id|> # <|start_header_id|>assistant<|end_header_id|>

View File

@@ -284,35 +284,6 @@ timeline = createTimeline(events)
# 2) Assistant> Hi there! with a smile # 2) Assistant> Hi there! with a smile
""" """
# function createTimeline(events::T1; eventindex::Union{UnitRange, Nothing}=nothing
# ) where {T1<:AbstractVector}
# # Initialize empty timeline string
# timeline = ""
# # Determine which indices to use - either provided range or full length
# ind =
# if eventindex !== nothing
# [eventindex...]
# else
# 1:length(events)
# end
# # Iterate through events and format each one
# for (i, event) in zip(ind, events)
# # If no outcome exists, format without outcome
# if event[:outcome] === nothing
# timeline *= "Event_$i $(event[:subject])> $(event[:actioninput])\n"
# # If outcome exists, include it in formatting
# else
# timeline *= "Event_$i $(event[:subject])> $(event[:actioninput]) $(event[:outcome])\n"
# end
# end
# # Return formatted timeline string
# return timeline
# end
function createTimeline(events::T1; eventindex::Union{UnitRange, Nothing}=nothing function createTimeline(events::T1; eventindex::Union{UnitRange, Nothing}=nothing
) where {T1<:AbstractVector} ) where {T1<:AbstractVector}
# Initialize empty timeline string # Initialize empty timeline string
@@ -329,13 +300,11 @@ function createTimeline(events::T1; eventindex::Union{UnitRange, Nothing}=nothin
# Iterate through events and format each one # Iterate through events and format each one
for (i, event) in zip(ind, events) for (i, event) in zip(ind, events)
# If no outcome exists, format without outcome # If no outcome exists, format without outcome
subject = titlecase(event[:subject])
if event[:outcome] === nothing if event[:outcome] === nothing
timeline *= "Event_$i $(event[:subject])> $(event[:actioninput])\n"
timeline *= "Event_$i) Who: $subject Action_name: $(event[:actionname]) Action_input: $(event[:actioninput])\n"
# If outcome exists, include it in formatting # If outcome exists, include it in formatting
else else
timeline *= "Event_$i) Who: $subject Action_name: $(event[:actionname]) Action_input: $(event[:actioninput]) Action output: $(event[:outcome])\n" timeline *= "Event_$i $(event[:subject])> $(event[:actioninput]) $(event[:outcome])\n"
end end
end end
@@ -344,6 +313,37 @@ function createTimeline(events::T1; eventindex::Union{UnitRange, Nothing}=nothin
end end
# function createTimeline(events::T1; eventindex::Union{UnitRange, Nothing}=nothing
# ) where {T1<:AbstractVector}
# # Initialize empty timeline string
# timeline = ""
# # Determine which indices to use - either provided range or full length
# ind =
# if eventindex !== nothing
# [eventindex...]
# else
# 1:length(events)
# end
# # Iterate through events and format each one
# for (i, event) in zip(ind, events)
# # If no outcome exists, format without outcome
# subject = titlecase(event[:subject])
# if event[:outcome] === nothing
# timeline *= "Event_$i) Who: $subject Action_name: $(event[:actionname]) Action_input: $(event[:actioninput])\n"
# # If outcome exists, include it in formatting
# else
# timeline *= "Event_$i) Who: $subject Action_name: $(event[:actionname]) Action_input: $(event[:actioninput]) Action output: $(event[:outcome])\n"
# end
# end
# # Return formatted timeline string
# return timeline
# end