diff --git a/src/interface.jl b/src/interface.jl index 4888be2..dd8009c 100755 --- a/src/interface.jl +++ b/src/interface.jl @@ -373,7 +373,7 @@ function actor_mistral_openorca(a::agentReflex) {context} <|im_end|> {shorttermMemory} - Thought: + Thought $(a.step): """ prompt = replace(prompt, "{role}" => a.roles[a.role]) @@ -386,6 +386,9 @@ function actor_mistral_openorca(a::agentReflex) if k ∉ ["user:", "Plan 1:"] s1 = "$k $v" s *= s1 + if s[end] != "\n" + s *= "\n" + end end end prompt = replace(prompt, "{shorttermMemory}" => s) @@ -615,11 +618,13 @@ function conversation(a::agentReflex, usermsg::String; attemptlimit::Int=3) a.earlierConversation = conversationSummary(a) #TODO should be long conversation before use summary because it leaves out details _ = addNewMessage(a, "user", usermsg) prompt = chat_mistral_openorca(a, usermsg) + println("") @show prompt respond = sendReceivePrompt(a, prompt) respond = split(respond, "<|im_end|>")[1] respond = replace(respond, "\n" => "") _ = addNewMessage(a, "assistant", respond) + println("") @show respond else respond = work(a, usermsg) @@ -645,6 +650,7 @@ function work(a::agentReflex, usermsg::String) println("continue_thinking!!") _ = addNewMessage(a, "user", usermsg) a.memory[:shortterm]["Obs $mark_actor:"] = usermsg + a.step += 1 a.memory[:log]["Obs $mark_actor:"] = usermsg else error("undefined condition thinkingmode = $thinkingmode $(@__LINE__)") @@ -687,7 +693,7 @@ function work(a::agentReflex, usermsg::String) elseif actorstate == "all steps done" #WORKING add canceled during plan println("all steps done") - respond = formulateRespond(a) + respond = formulateUserRespond(a) error("10") a.memory[:shortterm]["Respond $mark_plan:"] = respond @@ -777,30 +783,27 @@ function actor(a::agentReflex) println("") @show prompt_actor respond = sendReceivePrompt(a, prompt_actor) - respond = split(respond, "Obs")[1] + respond = splittext(respond, ["Obs", "<|im_end|>"]) respond_actor_raw = respond + println("") @show respond_actor_raw - if occursin("Thought", respond) - ind = findfirst(":", respond)[end] - respond = respond[ind+1:end] + if !occursin("Thought", respond) + respond = "Thought: " * respond end - respond = "Thought: " * respond + + headerToDetect = ["Question:", "Plan:", "Thought:", + "Act:", "ActInput:", "Obs:", "...", + "Answer:", "Conclusion:", "Summary:"] - # some time LLM not generate a number after headers but I want it - if occursin("Act:", respond) - headerToDetect = ["Question:", "Plan:", "Thought:", - "Act:", "ActInput:", "Obs:", "...", - "Answer:", "Conclusion:", "Summary:"] - headers = detectCharacters(respond, headerToDetect) - respond = replaceHeaders(respond, headers, a.step) - end + # replace headers with headers with correct attempt and step number + respond = replaceHeaders(respond, headerToDetect, a.step) - respond = split(respond, "<|im_end|>")[1] + headers = detectCharacters(respond, headerToDetect) respond_actor = respond println("") @show respond_actor - + mark_plan = "$(a.attempt)" mark_actor = "$(a.step)" headerToDetect = ["Plan $mark_plan:", @@ -832,14 +835,15 @@ function actor(a::agentReflex) toolresult = f(a, toolinput) @show toolresult a.memory[:shortterm]["Obs $mark_actor:"] = toolresult + a.step += 1 go, reason = goNogo(a) + @show go a.memory[:shortterm]["Check $mark_actor:"] = reason if go == "No" # in case there is a cancel, go straight to evaluation a.step -= 1 error(113) end - a.step += 1 end else #TODO finish all steps actorState = "all steps done" @@ -1190,8 +1194,9 @@ function goNogo(a) """ respond = sendReceivePrompt(a, prompt) + decision = GeneralUtils.getStringBetweenCharacters(respond, "{", "}") - start = findfirst("}", respond) +1 + start = findfirst("}", respond)[end] +1 reason = respond[start:end] return decision, reason diff --git a/src/type.jl b/src/type.jl index 70e5744..ffd1691 100644 --- a/src/type.jl +++ b/src/type.jl @@ -110,10 +110,11 @@ function agentReflex( :actor=> """ Use the following format: - Thought: ask yourself do you have all the info you need and what to do according to step {step} of the plan (pay attention to correct numeral calculation and commonsense). + Thought: ask yourself do you have what you need and what to do according to step {step} of the plan (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 + Check: check whether you are ready for the next step of the plan """, ), tools::Dict=Dict( diff --git a/src/utils.jl b/src/utils.jl index 0f5b4ff..a399315 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -604,12 +604,13 @@ julia> headers = ["Thought", "Act"] function replaceHeaders(text::T, headers, step::Int) where {T<:AbstractString} newtext = text for i in headers - header = i[:char][1:end-1] + header = i[1:end-1] # not include ":" if occursin(header, newtext) - startind = findfirst(i[:char], newtext)[1] + startind = findfirst(header, newtext)[1] stopind = findnext(":", newtext, startind+1)[end] word = newtext[startind: stopind] - newtext = replace(newtext, word=> "$header $step:") + newword = "$header $step:" + newtext = replace(newtext, word=> newword) end end