This commit is contained in:
2023-11-30 00:08:26 +00:00
parent d9fceef95a
commit 7861604b1f
4 changed files with 56 additions and 27 deletions

View File

@@ -351,12 +351,13 @@ function actor_mistral_openorca(a::agentReflex)
prompt =
"""
<|im_start|>system
{role}
{context}
{tools}
{role}
{thinkingFormat}
<|im_end|>
{context}
{shorttermMemory}
Thought $(a.step):
"""
prompt = replace(prompt, "{role}" => a.roles[a.role])
@@ -392,7 +393,7 @@ 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}" => "Plan:\n$(a.memory[:shortterm]["Plan 1:"])")
context = replace(context, "{plan}" => "My plan:\n$(a.memory[:shortterm]["Plan 1:"])")
prompt = replace(prompt, "{context}" => context)
return prompt
@@ -618,11 +619,12 @@ function work(a::agentReflex, usermsg::String)
a.earlierConversation = conversationSummary(a)
_ = addNewMessage(a, "user", usermsg)
a.memory[:shortterm]["user:"] = usermsg
@show a.memory[:shortterm]
a.memory[:log]["user:"] = usermsg
elseif a.thinkingmode == :continue_thinking #TODO
error("continue_thinking $(@__LINE__)")
_ = addNewMessage(a, "user", usermsg)
a.thought *= "Obs $(a.attempt): $usermsg\n"
a.memory[:shortterm]["Obs $(a.step):"] = usermsg
a.memory[:log]["Obs $(a.step):"] = usermsg
else
error("undefined condition thinkingmode = $thinkingmode $(@__LINE__)")
end
@@ -641,13 +643,16 @@ function work(a::agentReflex, usermsg::String)
respond = sendReceivePrompt(a, prompt)
# sometimes LLM add not-need word I don't want
plan = split(respond, "<|im_end|>")[1]
plan = split(plan, "Response:")[1]
plan = split(plan, "Execution:")[1]
plan = split(plan, "Result:")[1]
# plan = split(respond, "<|im_end|>")[1]
# plan = split(plan, "Response:")[1]
# plan = split(plan, "Execution:")[1]
# plan = split(plan, "Result:")[1]
# plan = split(plan, "Recommendation:")[1]
plan = splitext(plan, ["<|im_end|>", "Response:", "Execution:", "Result:", "Recommendation:"])
plan = replace(plan, "Plan:"=>"")
a.memory[:shortterm]["Plan $(a.attempt):"] = plan
a.memory[:log]["Plan $(a.attempt):"] = plan
actorstate, msgToUser = actor(a)
if actorstate == "chatbox"
@@ -658,7 +663,8 @@ function work(a::agentReflex, usermsg::String)
respond = formulateRespond(a)
error("10")
a.memory[:shortterm]["Respond:"] = respond
a.memory[:shortterm]["Respond $(a.attempt):"] = respond
a.memory[:log]["Respond $(a.attempt):"] = respond
# evaluate. if score < 8/10 try again.
guideline = writeEvaluationGuideline(a, a.memory[:shortterm]["user:"])
@@ -668,11 +674,12 @@ function work(a::agentReflex, usermsg::String)
if score >= 8 # good enough answer
@show a.memory[:shortterm]
a.memory[:shortterm] = OrderedDict{String, Any}()
a.memory[:log] = OrderedDict{String, Any}()
break
else # self evaluate and reflect then try again
analysis = analyze(a, a.memory[:shortterm])
@show analysis
error(12)
lessonwithcontext = selfReflext(a, analysis)
@show lessonwithcontext
a.memory[:shortterm] = OrderedDict{String, Any}()
@@ -784,10 +791,10 @@ function actor(a::agentReflex)
@show toolresult
a.memory[:shortterm]["Obs $(a.step):"] = toolresult
#WORKING goNogo
go = goNogo(a)
@show go
error(11)
if go == "No" # in case there is a cancel, go straight to evaluation
a.step = totalsteps
end
end
else #TODO finish all steps
actorState = "all steps done"
@@ -1099,7 +1106,7 @@ julia> shorttermMemory = OrderedDict{String, Any}(
julia> decision = goNogo(agent)
"Yes"
```
""" #WORKING
"""
function goNogo(a)
stimulus = a.memory[:shortterm]["user:"]
work = ""

View File

@@ -47,11 +47,11 @@ function wikisearch(a::agentReflex, phrase::T) where {T<:AbstractString}
result = "No info available for your search query."
end
if result == ""
result = "No info available for your search query."
else
result = makeSummary(a, result)
end
# if result == ""
# result = "No info available for your search query."
# else
# result = makeSummary(a, result)
# end
return result
end
@@ -106,7 +106,6 @@ end

View File

@@ -34,7 +34,6 @@ abstract type agent end
# messages= [Dict(:role=>"system", :content=> "", :timestamp=> Dates.now()),]
messages = Vector{Dict{Symbol, Any}}()
tools::Union{Dict, Nothing} = nothing
thoughtlog::String = "" # logs unfinished thoughts
attempt::Int = 0 # attempted number
step::Int = 0 # step number
attemptlimit::Int = 5 # thinking round limit
@@ -43,6 +42,7 @@ abstract type agent end
memory::Dict = Dict(
:shortterm=> OrderedDict{String, Any}(),
:longterm=> OrderedDict{String, Any}(),
:log=> OrderedDict{String, Any}(), # span from user stimulus -> multiples attempts -> final respond
)
end
@@ -100,7 +100,7 @@ function agentReflex(
:actor=>
"""
Use the following format:
Thought: your should always think about how to do step {step} of the plan (pay attention to correct numeral calculation and commonsense).
Thought: think about how to do 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

View File

@@ -3,7 +3,7 @@ module utils
export makeSummary, sendReceivePrompt, chunktext, extractStepFromPlan, checkTotalStepInPlan,
detectCharacters, findDetectedCharacter, extract_number, toolNameBeingCalled,
chooseThinkingMode, conversationSummary, checkReasonableness, addStepNumber,
addShortMem!
addShortMem!, splittext
using UUIDs, Dates, DataStructures
using CommUtils, GeneralUtils
@@ -27,8 +27,7 @@ function makeSummary(a::T1, input::T2) where {T1<:agent, T2<:AbstractString}
prompt = replace(prompt, "{input}" => input)
result = sendReceivePrompt(a, prompt)
result = GeneralUtils.getStringBetweenCharacters(result, "{", "}")
println("///// ", result)
if result == "Yes"
if result == "Yes" # seperate summary part
prompt =
"""
<|im_start|>system
@@ -44,6 +43,9 @@ function makeSummary(a::T1, input::T2) where {T1<:agent, T2<:AbstractString}
summary = result
end
end
input_summary = input
@show input_summary
@show summary
return summary
end
@@ -327,7 +329,7 @@ end
function chooseThinkingMode(a::agentReflex, usermsg::String)
thinkingmode = nothing
if length(a.thoughtlog) != 0
if length(a.memory[:log]) != 0
thinkingmode = :continue_thinking
else
prompt =
@@ -531,8 +533,29 @@ end
""" Split text using all keywords in a list. Start spliting from rightmost of the text.
Args:
text = a text you want to split
list = a list of keywords you want to split
Return:
a leftmost text after split
# Example
```jldoctest
julia> text = "Consider the type of food, occasion and temperature at the serving location."
julia> list = ["at", "and"]
"Consider the type of food, occasion "
```
"""
function splittext(text, list)
newtext = text
for i in list
newtext = split(newtext, i)[1]
end
return newtext
end