This commit is contained in:
2026-07-11 21:45:49 +07:00
parent 8bd4986be2
commit 688a8c4df2
8 changed files with 682 additions and 254 deletions
+35 -39
View File
@@ -108,9 +108,6 @@ function decisionMaker(a::T; recentevents::Integer=20, maxattempt=10
context =
"""
<internal_context_for_assistant>
<assistant_action_history>
$(GeneralUtils.dict_to_string_html(a.memory["shortmem"]))
</assistant_action_history>
</internal_context_for_assistant>
"""
@@ -384,7 +381,7 @@ function conversation(a::sommelier; userinput::Union{Dict{String, Any}, JSON.Obj
loopcount += 1
if loopcount > max_think_loop
@info "YiemAgent conversation() 2-1 think count $loopcount " @__LINE__
r = generatechat(a)
r = generatechat!(a)
@info "YiemAgent conversation() 2-2 think count $loopcount " @__LINE__
return r
end
@@ -399,6 +396,22 @@ function conversation(a::sommelier; userinput::Union{Dict{String, Any}, JSON.Obj
)
addNewMessage(a, "assistant", assistant_response; maximumMsg=maximumMsg)
return thoughtdict["action_input"]
else
action_name = thoughtdict["action_name"]
action_input = thoughtdict["action_input"]
action_call = Dict{String, Any}(
"role" => "action_call",
"content" => [Dict("type" => "text", "text" => "{action_name: $action_name, action_input: $action_input}"),]
)
addNewMessage(a, "action_call", action_call; maximumMsg=maximumMsg)
action_result = thoughtdict["action_result"]
actionresult = Dict{String, Any}(
"role" => "action_result",
"content" => [Dict("type" => "text", "text" => "$action_result"),]
)
addNewMessage(a, "actionresult", actionresult; maximumMsg=maximumMsg)
end
end
end
@@ -425,7 +438,7 @@ function think(a::T)::NamedTuple{(:thoughtdict, :result_raw), Tuple{OrderedDict,
result_raw = nothing
if thoughtdict["action_name"] ["CHAT_BOX"]
@info "YiemAgent think() 2 " @__LINE__
thoughtdict, result_raw = chatbox!(a, thoughtdict)
thoughtdict, result_raw = generatechat!(a)
elseif thoughtdict["action_name"] == "END_CONVER_GUIDELINE"
@info "YiemAgent think() 3 " @__LINE__
@@ -438,21 +451,21 @@ function think(a::T)::NamedTuple{(:thoughtdict, :result_raw), Tuple{OrderedDict,
elseif thoughtdict["action_name"] == "CHECK_WINE"
@info "YiemAgent think() 5 " @__LINE__
thoughtdict, result_raw = checkwine!(a, thoughtdict)
thoughtdict, result_raw = checkwine!(a, thoughtdict; useSQLLLM=false)
else
@info "YiemAgent think() 6 " @__LINE__
error("condition is not defined ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
end
max_ind =
if length(a.memory["shortmem"]) == 0
0
else
k = keys(a.memory["shortmem"])
maximum(parse.(Int, k))
end
a.memory["shortmem"]["$(max_ind + 1)"] = thoughtdict
# max_ind =
# if length(a.memory["shortmem"]) == 0
# 0
# else
# k = keys(a.memory["shortmem"])
# maximum(parse.(Int, k))
# end
# a.memory["shortmem"]["$(max_ind + 1)"] = thoughtdict
@info "YiemAgent think() 7 " @__LINE__
pprintln(thoughtdict)
@@ -530,8 +543,8 @@ end
#PENDING
function generatechat(a::T; recentevents::Integer=20, maxattempt=10
)::String where {T<:agent}
function generatechat!(a::T; maxattempt::Integer=10
)::NamedTuple{(:thoughtdict, :result_raw), Tuple{OrderedDict, Any}} where {T<:agent}
# lessonDict = copy(JSON.parsefile("lesson.json"))
@@ -605,9 +618,9 @@ function generatechat(a::T; recentevents::Integer=20, maxattempt=10
- 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.
# 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 and rationale. Be specific.
2) **action_name**, (Typically corresponds to the execution of the first step in your plan). Must be "CHAT_BOX
3) **action_input**, Dialogue you want to chat with the user according to your plan.
1) "plan", Based on the current situation, state a complete action plan to complete the task and rationale. Be specific.
2) "action_name", Must be "CHAT_BOX
3) "action_input", Dialogue you want to chat with the user according to your plan.
After the action is executed you gets "action_result". It is the output from the action you selected.
# you should only respond in JSON format as described below
@@ -623,27 +636,11 @@ function generatechat(a::T; recentevents::Integer=20, maxattempt=10
]
)
chathistory = deepcopy(a.chathistory[2:end])
chathistory = deepcopy(a.chathistory[2:end]) # use deep copy because I want to replace system msg
pushfirst!(chathistory, system_msg)
requiredKeys = ["plan", "action_name", "action_input"]
context =
"""
<internal_context_for_assistant>
<assistant_action_history>
$(GeneralUtils.dict_to_string_html(a.memory["shortmem"]))
</assistant_action_history>
</internal_context_for_assistant>
"""
# 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
@@ -654,7 +651,7 @@ function generatechat(a::T; recentevents::Integer=20, maxattempt=10
msg = Dict(
"model" => "gemma-4-E4B-it-UD-Q4_K_XL",
"messages" => a.chathistory,
"messages" => chathistory,
"temperature" => 0.7
)
@@ -665,7 +662,6 @@ function generatechat(a::T; recentevents::Integer=20, maxattempt=10
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(requiredKeys[2], response)
@@ -708,7 +704,7 @@ function generatechat(a::T; recentevents::Integer=20, maxattempt=10
# println("\nYiem decisionMaker() ", @__FILE__, ":", @__LINE__, " $(Dates.now())")
# pprintln(responsedict)
return responsedict["action_input"]
return (thoughtdict=responsedict, result_raw=responsedict["action_input"])
end
error("YiemAgent generatechat() failed to generate a thought ", response)
end