From 4c8f021d40f57b3852487be2f0efa5b6e8deb088 Mon Sep 17 00:00:00 2001 From: tonaerospace Date: Sun, 17 Dec 2023 15:31:51 +0000 Subject: [PATCH] update --- src/interface.jl | 104 +++++++++++++++++++++++++++-------------------- 1 file changed, 61 insertions(+), 43 deletions(-) diff --git a/src/interface.jl b/src/interface.jl index f2df26b..080e41e 100755 --- a/src/interface.jl +++ b/src/interface.jl @@ -198,17 +198,18 @@ function planner_mistral_openorca(a::agentReflex) Plan: first you should always think about the objective, the info you have, the info you don't have and thoroughly then extract and devise a complete, step by step plan (pay attention to correct numeral calculation and commonsense). p.s.1 each step of the plan should be a single action. p.s.2 ask the user all you need to know and then search your inventory. + <|im_end|> $conversation <|im_start|>assistant Objective: """ - # p.s.1 each step of the plan should be a single action. - # p.s.2 the last step should be about responding. + #WORKING remove () in steps as LLM sometimes use for (addtional info) but interfere with updatePlan + + result = sendReceivePrompt(a, assistant_plan_prompt, max_tokens=512, temperature=0.1) - result = sendReceivePrompt(a, assistant_plan_prompt, max_tokens=1024, temperature=0.1) - @show raw_plan = result x = split(result, "<|im_end|>")[1] + @show x x = split(x, "Step")[1] x = split(x, "Plan:") objective = x[1] @@ -218,8 +219,8 @@ function planner_mistral_openorca(a::agentReflex) end """ Update the current plan. -""" #WORKING -function updatePlan() +""" +function updatePlan(a::agentReflex) conversation = messagesToString_nomark(a.messages) toollines = "" @@ -233,7 +234,7 @@ function updatePlan() work = dictToString(a.memory[:shortterm]) prompt = - """ + """ <|im_start|>system $(a.roles[a.role]) The required info you need for wine recommendation: @@ -254,12 +255,14 @@ function updatePlan() Your work: $work - You job is to use info from your conversation with the user and your work to up date the plan. + Your job is to update the plan if the info is available from your conversation with the user or your work. + P.S. do not update if no info available. <|im_end|> New plan: - """ - + """ + result = sendReceivePrompt(a, prompt, max_tokens=512, temperature=0.1) + a.memory[:shortterm]["Plan 0:"] = result end function actor_mistral_openorca(a::agentReflex) @@ -321,7 +324,7 @@ function actor_mistral_openorca(a::agentReflex) Thought $(a.step): """ prompt = replace(prompt, "{toolnames}" => toolnames) - + prompt = replace(prompt, "{step}" => a.step) @@ -404,20 +407,27 @@ function work(a::agentReflex) response = nothing while true # Work loop - - a.memory[:shortterm] = OrderedDict{String, Any}() - a.memory[:log] = OrderedDict{String, Any}() + objective = nothing # plan if a.attempt <= a.attemptlimit toolname = nothing toolinput = nothing + + if length(a.memory[:shortterm]) != 0 + updatePlan(a) + @show updatedPlan = a.memory[:shortterm]["Plan 0:"] + else + objective, plan = planner_mistral_openorca(a) + a.memory[:shortterm]["Plan $(a.attempt):"] = plan + a.memory[:log]["Plan $(a.attempt):"] = plan + + println("") + @show objective + @show plan + end + - a.step = 0 - objective, plan = planner_mistral_openorca(a) - println("") - @show objective - @show plan # sometimes LLM add not-need word I don't want # plan = splittext(response, ["Step 1", "<|im_end|>", "Response", "Execution", @@ -426,8 +436,7 @@ function work(a::agentReflex) # println("") # @show plan - a.memory[:shortterm]["Plan $(a.attempt):"] = plan - a.memory[:log]["Plan $(a.attempt):"] = plan + println("") @show a.attempt @@ -477,16 +486,15 @@ function work(a::agentReflex) println("") @show lessonwithcontext - newdict = OrderedDict() - - a.memory[:shortterm] = keepOnlyKeys(a.memory[:shortterm], ["user:"]) - headerToDetect = ["Lesson:", "Context:", ] headers = detectCharacters(lessonwithcontext, headerToDetect) chunkedtext = chunktext(lessonwithcontext, headers) a.memory[:longterm][chunkedtext["Context:"]] = chunkedtext["Lesson:"] a.attempt += 1 + a.step = 0 + a.memory[:shortterm] = OrderedDict{String, Any}() + a.memory[:log] = OrderedDict{String, Any}() println("") println("RETRY $(a.attempt +1)") println("") @@ -521,8 +529,14 @@ end actorState = "chatbox" msgToUser = "message from assistant to user" -""" +""" #WORKING add Obs from Chatbox function actor(a::agentReflex) + if haskey(a.memory[:shortterm], "Act $(a.step):") + if a.memory[:shortterm]["Act $(a.step):"] == "chatbox" + a.memory[:shortterm]["Obs $(a.step):"] = a.messages[end][:content] + end + end + actorState = nothing msgToUser = nothing @@ -531,24 +545,20 @@ function actor(a::agentReflex) while true # Actor loop # decide whether to repeat step or do the next step - if a.step == 0 - a.step = 1 + decision, reason = goNogo(a) + println("") + @show decision + @show reason + # a.memory[:shortterm]["Check $(a.step):"] = reason + if decision == "Yes" # in case there is a cancel, go straight to evaluation + a.step += 1 + elseif decision == "No" + # repeat the latest step + a.memory[:shortterm] = removeHeaders(a.memory[:shortterm], a.step, ["Plan"]) + a.memory[:log] = removeHeaders(a.memory[:log], a.step, ["Plan"]) + println("repeating step $(a.step)") else - decision, reason = goNogo(a) - println("") - @show decision - @show reason - # a.memory[:shortterm]["Check $(a.step):"] = reason - if decision == "Yes" # in case there is a cancel, go straight to evaluation - a.step += 1 - elseif decision == "No" - # repeat the latest step - a.memory[:shortterm] = removeHeaders(a.memory[:shortterm], a.step, ["Plan"]) - a.memory[:log] = removeHeaders(a.memory[:log], a.step, ["Plan"]) - println("repeating step $(a.step)") - else - error("undefined condition decision = $decision $(@__LINE__)") - end + error("undefined condition decision = $decision $(@__LINE__)") end @@ -557,6 +567,7 @@ function actor(a::agentReflex) # check whether LLM already complete the current step iscomplete = checkStepCompletion(a) + @show iscomplete if iscomplete == false prompt_actor = actor_mistral_openorca(a) @@ -1097,6 +1108,13 @@ function checkStepCompletion(a::agentReflex) response = sendReceivePrompt(a, prompt) response = split(response, "<|im_end|>")[1] + + # if occursin("N/A", response) + # response = replace(response, "(N/A)"=>"") + # elseif occursin(response, "(not specified, assume casual)") + # response = replace(response, "(not specified, assume casual)"=>"") + # else + # end # mistral 7B already know info example: 2. Determine the occasion (wedding party). if occursin("(", response) && occursin(")", response)