From 381f5152ebe6b5b1aff584f2d47f60d8450f2fd3 Mon Sep 17 00:00:00 2001 From: tonaerospace Date: Tue, 28 Nov 2023 04:23:44 +0000 Subject: [PATCH] update --- src/interface.jl | 65 ++++++++++++++++++++++-------------------------- src/type.jl | 4 +-- 2 files changed, 32 insertions(+), 37 deletions(-) diff --git a/src/interface.jl b/src/interface.jl index cf1cdca..0eaada8 100755 --- a/src/interface.jl +++ b/src/interface.jl @@ -283,11 +283,7 @@ function planner_mistral_openorca(a::agentReflex, usermsg::String) {thinkingFormat} <|im_end|> {context} - <|im_start|>user - {usermsg} - <|im_end|> - <|im_start|>assistant - + {shorttermMemory} """ prompt = replace(prompt, "{role}" => a.roles[a.role]) prompt = replace(prompt, "{thinkingFormat}" => a.thinkingFormat[:planner]) @@ -303,24 +299,35 @@ function planner_mistral_openorca(a::agentReflex, usermsg::String) prompt = replace(prompt, "{tools}" => "You have access to the following tools:\n$toollines") context = - """ - {earlierConversation} - {env state} - {longterm memory} - """ + """ + {earlierConversation} + {env state} + {longterm memory} + """ context = replace(context, "{earlierConversation}" => "My earlier talk with the user:\n$(a.earlierConversation)") context = replace(context, "{env state}" => "") context = replace(context, "{longterm memory}" => "") prompt = replace(prompt, "{context}" => context) - prompt = replace(prompt, "{usermsg}" => "Stimulus: $usermsg") + # initialize short term memory + txt = + """ + <|im_start|>user + {usermsg} + <|im_end|> + <|im_start|>assistant + + """ + txt = replace(txt, "{usermsg}" => "Stimulus: $usermsg") + a.memory[:shortterm] = txt + prompt = replace(prompt, "{shorttermMemory}" => a.memory[:shortterm]) return prompt end #WORKING try use Thought/Act/ActInput/Obs loop because some time step 2 depend on step 1 -function actor_mistral_openorca(a::agentReflex, usermsg, plan, step) +function actor_mistral_openorca(a::agentReflex) """ general prompt format: @@ -356,17 +363,13 @@ function actor_mistral_openorca(a::agentReflex, usermsg, plan, step) {thinkingFormat} <|im_end|> {context} - <|im_start|>user - {usermsg} - <|im_end|> - <|im_start|>assistant - {plan} + {shorttermMemory} """ prompt = replace(prompt, "{role}" => a.roles[a.role]) prompt = replace(prompt, "{thinkingFormat}" => a.thinkingFormat[:actor]) - prompt = replace(prompt, "{step}" => step) - prompt = replace(prompt, "{plan}" => plan) + prompt = replace(prompt, "{step}" => a.step) + prompt = replace(prompt, "{shorttermMemory}" => a.memory[:shortterm]) toolnames = "" toollines = "" for (toolname, v) in a.tools @@ -385,11 +388,8 @@ function actor_mistral_openorca(a::agentReflex, usermsg, plan, step) # context = replace(context, "{earlierConversation}" => "My earlier talk with the user:\n$(a.earlierConversation)") context = replace(context, "{env state}" => "") context = replace(context, "{longterm memory}" => "") - prompt = replace(prompt, "{context}" => context) - prompt = replace(prompt, "{usermsg}" => "Stimulus: $usermsg") - return prompt end @@ -627,8 +627,6 @@ function work(a::agentReflex, usermsg::String) @show a.attempt @show usermsg if a.attempt <= a.attemptlimit - logmsg = "user: $usermsg\n" - a.memory[:shortterm] *= logmsg toolname = nothing toolinput = nothing @@ -638,11 +636,9 @@ function work(a::agentReflex, usermsg::String) plan = split(respond, "<|im_end|>")[1] plan = split(plan, "Response:")[1] plan = replace(plan, "Plan:"=>"Plan $(a.attempt):") - logmsg = "assistant: $plan\n" - a.memory[:shortterm] *= logmsg - a.thoughtlog *= logmsg - actorstate, msgToUser = actor(a, usermsg, plan) - + a.memory[:shortterm] *= plan + actorstate, msgToUser = actor(a, plan) + error("333") if actorstate == "chatbox" respond = msgToUser break @@ -680,7 +676,7 @@ function work(a::agentReflex, usermsg::String) @show chunkedtext push!(a.memory[:longterm], Dict(:context=>chunkedtext["Context:"], :lesson=>chunkedtext["Lesson:"])) - error(">>>>>>>>>") + error("22222222") end else error("undefied condition, actorstate $actorstate $(@__LINE__)") @@ -723,11 +719,10 @@ end msgToUser = "message from assistant to user" """ -function actor(a::agentReflex, usermsg, plan) +function actor(a::agentReflex, plan) actorState = nothing msgToUser = nothing - @show plan totalsteps = checkTotalStepInPlan(a, plan) a.step = 0 @@ -736,11 +731,11 @@ function actor(a::agentReflex, usermsg, plan) @show a.step if a.step <= totalsteps # WORKING in step 2, I need to use a.memory[:shortterm] as input to actor() - prompt = actor_mistral_openorca(a, usermsg, plan, a.step) + prompt = actor_mistral_openorca(a) @show prompt respond = sendReceivePrompt(a, prompt) @show respond - + # some time LLM not generate a number after headers but I want it if occursin("Act:", respond) headerToDetect = ["Question:", "Plan:", "Thought:", @@ -754,7 +749,7 @@ function actor(a::agentReflex, usermsg, plan) # add to memory a.memory[:shortterm] *= respond - a.thoughtlog *= respond + error("111") headerToDetect = ["Question $(a.step):", "Plan $(a.step):", "Thought $(a.step):", "Act $(a.step):", "ActInput $(a.step):", "Obs $(a.step):", "...", "Answer $(a.step):", "Conclusion $(a.step):", "Summary $(a.step):"] diff --git a/src/type.jl b/src/type.jl index 0f17593..936fb74 100644 --- a/src/type.jl +++ b/src/type.jl @@ -99,8 +99,8 @@ function agentReflex( """, :actor=> """Use the following format: - Plan: first you should always think about the stimulus and the info you have thoroughly then extract and devise a step by step plan to respond (pay attention to correct numeral calculation and commonsense). - Thought: your should always think what to do to carry out the plan's step {step} (pay attention to correct numeral calculation and commonsense). + Plan: first you should always think about the question and the info you have thoroughly then extract and devise a complete plan to find the answer (pay attention to variables and their corresponding numerals). + Thought: your should always think about how to do step {step} (pay attention to correct numeral calculation and commonsense). Act: the action to take that match your thought, should be one of [{toolnames}] ActInput: the input to the action (pay attention to the tool's input) Obs: the result of the action