This commit is contained in:
2023-12-02 09:26:43 +00:00
parent 6fe84e0cb1
commit 58c7b80c4e
3 changed files with 63 additions and 43 deletions

View File

@@ -246,7 +246,7 @@ function chat_mistral_openorca(a::agentReflex, usermsg::String)
return prompt
end
#WORKING mark only attemp, step
function planner_mistral_openorca(a::agentReflex)
"""
general prompt format:
@@ -288,7 +288,7 @@ function planner_mistral_openorca(a::agentReflex)
{usermsg}
<|im_end|>
<|im_start|>assistant
Plan $(a.attempt):
Plan:
"""
prompt = replace(prompt, "{role}" => a.roles[a.role])
prompt = replace(prompt, "{thinkingFormat}" => a.thinkingFormat[:planner])
@@ -362,6 +362,8 @@ function actor_mistral_openorca(a::agentReflex)
"
"""
mark = "$(a.step)"
prompt =
"""
<|im_start|>system
@@ -371,6 +373,7 @@ function actor_mistral_openorca(a::agentReflex)
{context}
<|im_end|>
{shorttermMemory}
Thought $mark:
"""
prompt = replace(prompt, "{role}" => a.roles[a.role])
@@ -406,7 +409,8 @@ function actor_mistral_openorca(a::agentReflex)
context = replace(context, "{earlierConversation}" => "My earlier talk with the user:\n$(a.earlierConversation)")
context = replace(context, "{env state}" => "")
context = replace(context, "{longterm memory}" => "")
context = replace(context, "{plan}" => "My plan:\n$(a.memory[:shortterm]["Plan 1:"])")
mark = "$(a.attempt)"
context = replace(context, "{plan}" => "My plan:\n$(a.memory[:shortterm]["Plan $mark:"])")
prompt = replace(prompt, "{context}" => context)
return prompt
@@ -627,6 +631,8 @@ end
function work(a::agentReflex, usermsg::String)
respond = nothing
mark_plan = "$(a.attempt)"
mark_actor = "$(a.step)"
if a.thinkingmode == :new_thinking
a.earlierConversation = conversationSummary(a)
@@ -634,23 +640,22 @@ function work(a::agentReflex, usermsg::String)
a.memory[:shortterm]["user:"] = usermsg
a.memory[:log]["user:"] = usermsg
a.newplan = true
a.attempt = 0
a.attempt = 1
elseif a.thinkingmode == :continue_thinking #TODO
println("continue_thinking!!")
_ = addNewMessage(a, "user", usermsg)
a.memory[:shortterm]["Obs $(a.step)-$(a.substep):"] = usermsg
a.memory[:log]["Obs $(a.step):"] = usermsg
a.memory[:shortterm]["Obs $mark_actor:"] = usermsg
a.memory[:log]["Obs $mark_actor:"] = usermsg
else
error("undefined condition thinkingmode = $thinkingmode $(@__LINE__)")
end
while true # Work loop
# plan
a.attempt += 1
@show a.attempt
@show usermsg
if a.attempt <= a.attemptlimit
toolname = nothing
toolinput = nothing
if a.newplan == true
@@ -658,17 +663,19 @@ function work(a::agentReflex, usermsg::String)
println("")
@show prompt_plan
respond = sendReceivePrompt(a, prompt_plan, max_tokens=1024)
println("")
plan_raw = respond
@show plan_raw
# sometimes LLM add not-need word I don't want
plan = splittext(respond, ["<|im_end|>", "Response", "Execution",
"Result", "Recommendation"])
"Result", "Recommendation", "My response"])
# plan = replace(plan, "Plan:"=>"")
println("")
@show plan
a.memory[:shortterm]["Plan $(a.attempt):"] = plan
a.memory[:log]["Plan $(a.attempt):"] = plan
a.step = 0
a.substep = 0
a.memory[:shortterm]["Plan $mark_plan:"] = plan
a.memory[:log]["Plan $mark_plan:"] = plan
a.step = 1
a.newplan = false
end
@@ -682,8 +689,9 @@ function work(a::agentReflex, usermsg::String)
respond = formulateRespond(a)
error("10")
a.memory[:shortterm]["Respond $(a.attempt):"] = respond
a.memory[:log]["Respond $(a.attempt):"] = respond
a.memory[:shortterm]["Respond $mark_plan:"] = respond
a.memory[:log]["Respond $mark:"] = respond
# evaluate. if score < 8/10 try again.
guideline = writeEvaluationGuideline(a, a.memory[:shortterm]["user:"])
@@ -720,6 +728,7 @@ function work(a::agentReflex, usermsg::String)
error("attempt limit reach")
break
end
a.attempt += 1
end
# good enough answer
@@ -760,8 +769,7 @@ function actor(a::agentReflex)
totalsteps = checkTotalStepInPlan(a)
while true # Actor loop
a.step += 1
a.substep += 1
@show a.step
if a.step <= totalsteps
@@ -783,7 +791,7 @@ function actor(a::agentReflex)
"Act:", "ActInput:", "Obs:", "...",
"Answer:", "Conclusion:", "Summary:"]
headers = detectCharacters(respond, headerToDetect)
respond = replaceHeaders(respond, headers, a.attempt, a.step, a.substep)
respond = replaceHeaders(respond, headers, a.step)
end
respond = split(respond, "<|im_end|>")[1]
@@ -792,18 +800,14 @@ function actor(a::agentReflex)
println("")
@show respond_actor
headerToDetect = ["Question $(a.step):",
"Plan $(a.step):",
"Thought $(a.step)-$(a.substep):",
"Act $(a.step)-$(a.substep):",
"ActInput $(a.step)-$(a.substep):",
"Obs $(a.step)-$(a.substep):",
"Check $(a.step)-$(a.substep):",
"Answer $(a.step):",
"Conclusion $(a.step):",
"Summary $(a.step):"]
mark_plan = "$(a.attempt)"
mark_actor = "$(a.step)"
headerToDetect = ["Plan $mark_plan:",
"Thought $mark_actor:",
"Act $mark_actor:",
"ActInput $mark_actor:",
"Obs $mark_actor:",
"Check $mark_actor:",]
headers = detectCharacters(respond, headerToDetect)
chunkedtext = chunktext(respond, headers)
@show chunkedtext
@@ -811,8 +815,8 @@ function actor(a::agentReflex)
# add to memory
a.memory[:shortterm] = addShortMem!(a.memory[:shortterm], chunkedtext)
toolname = toolNameBeingCalled(chunkedtext["Act $(a.step)-$(a.substep):"], a.tools)
toolinput = chunkedtext["ActInput $(a.step)-$(a.substep):"]
toolname = toolNameBeingCalled(chunkedtext["Act $mark_actor:"], a.tools)
toolinput = chunkedtext["ActInput $mark_actor:"]
@show toolname
@show toolinput
@@ -821,19 +825,21 @@ function actor(a::agentReflex)
respond = toolinput
msgToUser = respond
actorState = toolname
a.step += 1
break
else # function call
f = a.tools[Symbol(toolname)][:func]
toolresult = f(a, toolinput)
@show toolresult
a.memory[:shortterm]["Obs $(a.step)-$(a.substep):"] = toolresult
a.memory[:shortterm]["Obs $mark_actor:"] = toolresult
go, reason = goNogo(a)
a.memory[:shortterm]["Check $(a.step)-$(a.substep):"] = reason
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"

View File

@@ -34,11 +34,10 @@ abstract type agent end
# messages= [Dict(:role=>"system", :content=> "", :timestamp=> Dates.now()),]
messages = Vector{Dict{Symbol, Any}}()
tools::Union{Dict, Nothing} = nothing
newplan::Bool = false
newplan::Bool = false # if true, new plan will be generated
attemptlimit::Int = 5 # thinking round limit
attempt::Int = 0 # attempted number
step::Int = 0 # step number
substep::Int = 0 # step number
attempt::Int = 1 # attempted number
step::Int = 1 # step number
thinkingmode::Symbol = :no_thinking
thinkingFormat::Union{Dict, Nothing} = nothing
memory::Dict = Dict(

View File

@@ -230,8 +230,23 @@ function extractStepFromPlan(a::agent, plan::T, step::Int) where {T<:AbstractStr
end
function checkTotalStepInPlan(a::agent)
p = a.memory[:shortterm]["Plan 1:"]
plan = "Plan 1: $p"
headers = []
for (k, v) in agent.memory[:shortterm]
push!(headers, k)
end
# Plan will have number e.g. Plan 3: so I need a way to detect latest Plan
header = nothing
for i in reverse(headers)
if occursin("Plan", i)
header = i
break
end
end
p = a.memory[:shortterm][header]
plan = "Plan: $p"
prompt =
"""
<|im_start|>system
@@ -586,7 +601,7 @@ julia> headers = ["Thought", "Act"]
```
"""
function replaceHeaders(text::T, headers, attempt::Int, step::Int, substep::Int) where {T<:AbstractString}
function replaceHeaders(text::T, headers, step::Int) where {T<:AbstractString}
newtext = text
for i in headers
header = i[:char][1:end-1]
@@ -594,7 +609,7 @@ function replaceHeaders(text::T, headers, attempt::Int, step::Int, substep::Int)
startind = findfirst(i[:char], newtext)[1]
stopind = findnext(":", newtext, startind+1)[end]
word = newtext[startind: stopind]
newtext = replace(newtext, word=> "$header $attempt-$step-$substep:")
newtext = replace(newtext, word=> "$header $step:")
end
end