This commit is contained in:
2023-12-11 13:49:29 +00:00
parent 2e55921074
commit 3931b44e0a
3 changed files with 196 additions and 45 deletions

View File

@@ -457,8 +457,8 @@ function conversation(a::agentReflex, usermsg::String; attemptlimit::Int=3)
_ = addNewMessage(a, "user", usermsg)
isusetools = isUseTools(a, usermsg)
newinfo = extractinfo(a, usermsg)
a.env = newinfo !== nothing ? updateEnvState(a, newinfo)
a.env = newinfo !== nothing ? updateEnvState(a, newinfo) : a.env
error(1)
#WORKING
if isusetools # use tools before responseing
response = work(a, usermsg)
@@ -485,21 +485,11 @@ end
function work(a::agentReflex, usermsg::String)
response = nothing
if a.thinkingmode == :new_thinking
_ = addNewMessage(a, "user", usermsg)
a.memory[:shortterm] = OrderedDict{String, Any}()
a.memory[:log] = OrderedDict{String, Any}()
a.memory[:shortterm]["user:"] = usermsg
a.memory[:log]["user:"] = usermsg
a.newplan = true
elseif a.thinkingmode == :continue_thinking
println("continue_thinking!!")
_ = addNewMessage(a, "user", usermsg)
a.memory[:shortterm]["Obs $(a.step):"] = usermsg
a.memory[:log]["Obs $(a.step):"] = usermsg
else
error("undefined condition thinkingmode = $thinkingmode $(@__LINE__)")
end
a.memory[:shortterm] = OrderedDict{String, Any}()
a.memory[:log] = OrderedDict{String, Any}()
a.memory[:shortterm]["user:"] = usermsg
a.memory[:log]["user:"] = usermsg
a.newplan = true
while true # Work loop
# plan
@@ -600,6 +590,124 @@ function work(a::agentReflex, usermsg::String)
return response
end
# function work(a::agentReflex, usermsg::String)
# response = nothing
# if a.thinkingmode == :new_thinking
# _ = addNewMessage(a, "user", usermsg)
# a.memory[:shortterm] = OrderedDict{String, Any}()
# a.memory[:log] = OrderedDict{String, Any}()
# a.memory[:shortterm]["user:"] = usermsg
# a.memory[:log]["user:"] = usermsg
# a.newplan = true
# elseif a.thinkingmode == :continue_thinking
# println("continue_thinking!!")
# _ = addNewMessage(a, "user", usermsg)
# a.memory[:shortterm]["Obs $(a.step):"] = usermsg
# a.memory[:log]["Obs $(a.step):"] = usermsg
# else
# error("undefined condition thinkingmode = $thinkingmode $(@__LINE__)")
# end
# while true # Work loop
# # plan
# if a.attempt <= a.attemptlimit
# toolname = nothing
# toolinput = nothing
# if a.newplan == true
# a.attempt += 1
# a.step = 0
# prompt_plan = planner_mistral_openorca(a)
# println("")
# @show prompt_plan
# response = sendReceivePrompt(a, prompt_plan, max_tokens=1024)
# # 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
# println("")
# @show a.attempt
# # enter actor loop
# actorstate, msgToUser = actor(a)
# if actorstate == "chatbox"
# response = msgToUser
# break
# elseif actorstate == "all steps done" || actorstate == "formulateUserresponse"
# println("all steps done")
# response = formulateUserresponse(a)
# println("")
# formulatedresponse = response
# @show formulatedresponse
# a.memory[:shortterm]["response $(a.attempt):"] = response
# a.memory[:log]["response $(a.attempt):"] = response
# # evaluate. if score < 8/10 try again.
# guideline = writeEvaluationGuideline(a, a.memory[:shortterm]["user:"])
# println("")
# @show guideline
# score = grading(a, guideline, response)
# @show score
# if score >= 6 # good enough answer
# break
# else # self evaluate and reflect then try again
# analysis = analyze(a)
# println("")
# @show analysis
# lessonwithcontext = selfReflext(a, analysis)
# 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.newplan = true
# println("")
# println("RETRY $(a.attempt +1)")
# println("")
# end
# else
# error("undefied condition, actorstate $actorstate $(@__LINE__)")
# break
# end
# else
# error("attempt limit reach")
# break
# end
# end
# # good enough answer
# # communicates with user
# _ = addNewMessage(a, "assistant", response)
# return response
# end
# function evaluate()

View File

@@ -165,7 +165,7 @@ function agentReflex(
p.s.1 each step should be a single action.
p.s.2 don't respond to the stimulus yet.
""",
:actor=>
:actor=>
"""
Use the following format:
Thought: you should always think about do you have all the required info and what to do according to step {step} of the plan and the info you have (pay attention to correct numeral calculation and commonsense).

View File

@@ -363,40 +363,83 @@ end
# return thinkingmode
# end
# function isUseTools(a::agentReflex, usermsg::String)
# prompt =
# """
# <|im_start|>system
# {systemMsg}
# Your earlier conversation with the user:
# None
# User's message:
# $usermsg
# From the user's message, Are there any explicit request from the user? Answer: {Yes/No/Not sure}. What do you need to do?
# <|im_end|>
# <|im_start|>assistant
# Answer:
# """
# toollines = ""
# for (toolname, v) in a.tools
# if toolname ∉ [:chatbox]
# toolline = "$toolname: $(v[:description]) $(v[:input]) $(v[:output])\n"
# toollines *= toolline
# end
# end
# prompt = replace(prompt, "{systemMsg}" => a.roles[a.role])
# prompt = replace(prompt, "{tools}" => toollines)
# result = sendReceivePrompt(a, prompt, temperature=0.2)
# if occursin("Yes", result)
# isUseTool = result
# @show isUseTool
# return true
# else
# return false
# end
# end
function isUseTools(a::agentReflex, usermsg::String)
prompt =
"""
<|im_start|>system
{systemMsg}
You have access to the following tools:
{tools}
User's message:
{input}
From the user's message, Do you need to any tools before responseing? Answer: {Yes/No/Not sure}. What will the you do?
<|im_end|>
<|im_start|>assistant
Answer:
"""
toollines = ""
for (toolname, v) in a.tools
if toolname [:chatbox]
if toolname ["chatbox"]
toolline = "$toolname: $(v[:description]) $(v[:input]) $(v[:output])\n"
toollines *= toolline
end
end
prompt = replace(prompt, "{systemMsg}" => a.roles[a.role])
prompt = replace(prompt, "{tools}" => toollines)
prompt = replace(prompt, "{input}" => usermsg)
result = sendReceivePrompt(a, prompt, temperature=0.2)
if occursin("Yes", result)
return true
else
return false
prompt =
"""
<|im_start|>system
$(a.roles[a.role])
You have access to the following tools:
$toollines
Your earlier conversation with the user:
None
User's message:
$usermsg
From the user's message, what to do?
<|im_end|>
<|im_start|>assistant
"""
# if LLM mentions any tools, use Plan/Thought/Act loop
isusetool = false
result = sendReceivePrompt(a, prompt, temperature=0.2)
for (toolname, v) in a.tools
if occursin(toolname, result)
isusetool = true
break
end
end
return isusetool
end
@@ -441,7 +484,7 @@ function conversationSummary(a::T) where {T<:agent}
"""
conversation = ""
summary = "nothing"
summary = ""
if length(a.messages)!= 0
for msg in a.messages[1:end-1]
role = msg[:role]
@@ -458,7 +501,7 @@ function conversationSummary(a::T) where {T<:agent}
prompt = replace(prompt, "{conversation}" => conversation)
result = sendReceivePrompt(a, prompt)
summary = result === nothing ? "nothing" : result
summary = result === nothing ? "" : result
summary = split(summary, "<|im_end|>")[1]
if summary[1:1] == "\n"
summary = summary[2:end]