This commit is contained in:
2023-12-16 01:13:42 +00:00
parent 05ccee2f92
commit bd9ae8ad52

View File

@@ -234,7 +234,7 @@ function chat_mistral_openorca(a::agentReflex)
return response
end
#WORKING
function planner_mistral_openorca(a::agentReflex)
"""
general prompt format:
@@ -511,6 +511,7 @@ end
"""
function conversation(a::agentReflex, usermsg::String; attemptlimit::Int=3)
a.attemptlimit = attemptlimit
workstate = nothing
response = nothing
# a.earlierConversation = conversationSummary(a)
@@ -521,10 +522,16 @@ function conversation(a::agentReflex, usermsg::String; attemptlimit::Int=3)
@show isusetools
if isusetools # use tools before responseing
response = work(a)
workstate, response = work(a)
end
response = chat_mistral_openorca(a) #TODO if chatbox is used, skip this
# if LLM using chatbox, use returning msg form chatbox as conversation response
if workstate == "chatbox" ?
#TODO paraphrase msg so that it is human friendlier word.
else
response = chat_mistral_openorca(a)
end
response = removeTrailingCharacters(response)
_ = addNewMessage(a, "assistant", response)
@@ -536,51 +543,46 @@ end
There are many work() depend on thinking mode.
"""
function work(a::agentReflex)
actorstate = nothing
workstate = nothing
response = nothing
while true # Work loop
#WORKING
a.memory[:shortterm] = OrderedDict{String, Any}()
a.memory[:log] = OrderedDict{String, Any}()
# plan
if a.attempt <= a.attemptlimit
toolname = nothing
toolinput = nothing
if a.newplan == true
a.attempt += 1
a.step = 0
objective, plan = planner_mistral_openorca(a)
println("")
@show objective
@show plan
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",
# "Result", "Recommendation", "My response"])
# plan = replace(plan, "Plan:"=>"")
# println("")
# @show plan
a.newplan = false
a.memory[:shortterm]["Plan $(a.attempt):"] = plan
a.memory[:log]["Plan $(a.attempt):"] = plan
end
# sometimes LLM add not-need word I don't want
# plan = splittext(response, ["Step 1", "<|im_end|>", "Response", "Execution",
# "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
println("")
@show a.attempt
# enter actor loop
actorstate, msgToUser = actor(a)
actorstate, msgToUser = actor(a) #WORKING
if actorstate == "chatbox"
response = msgToUser
workstate = actorstate
break
elseif actorstate == "all steps done" || actorstate == "formulateUserresponse"
elseif actorstate == "all steps done"
println("all steps done")
response = formulateUserresponse(a)
@@ -592,7 +594,7 @@ function work(a::agentReflex)
a.memory[:shortterm]["response $(a.attempt):"] = response
a.memory[:log]["response $(a.attempt):"] = response
# evaluate. if score < 8/10 try again.
# evaluate. if score > 6/10 good enough.
guideline = writeEvaluationGuideline(a, a.memory[:shortterm]["user:"])
println("")
@@ -600,7 +602,7 @@ function work(a::agentReflex)
score = grading(a, guideline, response)
@show score
if score >= 6 # good enough answer
if score > 6 # good enough answer
break
else # self evaluate and reflect then try again
analysis = analyze(a)
@@ -621,11 +623,10 @@ function work(a::agentReflex)
chunkedtext = chunktext(lessonwithcontext, headers)
a.memory[:longterm][chunkedtext["Context:"]] = chunkedtext["Lesson:"]
a.newplan = true
a.attempt += 1
println("")
println("RETRY $(a.attempt +1)")
println("")
end
else
error("undefied condition, actorstate $actorstate $(@__LINE__)")
@@ -638,11 +639,7 @@ function work(a::agentReflex)
end
# good enough answer
# communicates with user
_ = addNewMessage(a, "assistant", response)
return actorstate, response
return workstate, response
end
@@ -902,6 +899,8 @@ function actor(a::agentReflex)
totalsteps = checkTotalStepInPlan(a)
while true # Actor loop
# decide whether to repeat step or do the next step
if a.step == 0
a.step = 1
else
@@ -917,10 +916,8 @@ function actor(a::agentReflex)
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)")
elseif decision == "formulateUserresponse"
actorState = "formulateUserresponse"
msgToUser = nothing
break
#WORKING
else
error("undefined condition decision = $decision $(@__LINE__)")
end
@@ -928,7 +925,7 @@ function actor(a::agentReflex)
@show a.step
if a.step < totalsteps
if a.step < totalsteps -1 # the last step of the plan is responding, let work() do this part
prompt_actor = actor_mistral_openorca(a)
@@ -978,14 +975,10 @@ function actor(a::agentReflex)
msgToUser = toolinput
actorState = toolname
break
elseif toolname == "formulateUserresponse"
msgToUser = toolinput
actorState = toolname
break
elseif toolname == "skipstep"
# skip
else # function call
f = a.tools[Symbol(toolname)][:func]
f = a.tools[toolname][:func]
toolresult = f(a, toolinput)
@show toolresult
a.memory[:shortterm]["Obs $(a.step):"] = toolresult
@@ -1365,8 +1358,6 @@ function goNogo(a)
decision = "Yes"
elseif occursin("No", response)
decision = "No"
elseif occursin("formulateUserresponse", response)
decision = "formulateUserresponse"
else
error("undefied condition, decision $decision $(@__LINE__)")
end