From ef67e9fd19707c84999cb30ef0adbc45f0b98477 Mon Sep 17 00:00:00 2001 From: tonaerospace Date: Mon, 27 Nov 2023 03:01:40 +0000 Subject: [PATCH] update add thoughtlog --- src/interface.jl | 50 ++++++++++++++++++++++++++++++++++-------------- src/type.jl | 2 +- src/utils.jl | 3 ++- 3 files changed, 39 insertions(+), 16 deletions(-) diff --git a/src/interface.jl b/src/interface.jl index 66e4b00..094f57f 100755 --- a/src/interface.jl +++ b/src/interface.jl @@ -609,7 +609,8 @@ function work(a::agentReflex, usermsg::String) if a.thinkingmode == :new_thinking a.earlierConversation = conversationSummary(a) _ = addNewMessage(a, "user", usermsg) - elseif a.thinkingmode == :continue_thinking + a.thoughtlog = "user: $usermsg\n" + elseif a.thinkingmode == :continue_thinking #TODO error("continue_thinking $(@__LINE__)") _ = addNewMessage(a, "user", usermsg) a.thought *= "Obs $(a.attempt): $usermsg\n" @@ -636,6 +637,7 @@ function work(a::agentReflex, usermsg::String) _plan = replace(plan, "Plan:"=>"Plan $(a.attempt):") logmsg = "assistant: $_plan\n" a.memory[:shortterm] *= logmsg + a.thoughtlog *= logmsg actorstate, msgToUser = actor(a, plan) if actorstate == "chatbox" @@ -658,14 +660,22 @@ function work(a::agentReflex, usermsg::String) @show score if score >= 8 # good enough answer @show a.memory[:shortterm] - # a.memory[:shortterm] = "" - # a.thoughtlog = "" + a.memory[:shortterm] = "" + a.thoughtlog = "" break else # self evaluate and reflect then try again report = analyze(a, a.memory[:shortterm]) @show report - lesson = selfReflext(a, report) - @show lesson + lessonwithcontext = selfReflext(a, report) + @show lessonwithcontext + a.memory[:shortterm] = "" + #TODO add lesson and context into longterm memory + headerToDetect = ["Lesson:", "Context:", ] + headers = detectCharacters(lessonwithcontext, headerToDetect) + chunkedtext = chunktext(lessonwithcontext, headers) + @show chunkedtext + push!(a.memory[:longterm], Dict(:context=>chunkedtext["Context:"], + :lesson=>chunkedtext["Lesson:"])) error(">>>>>>>>>>") end else @@ -679,10 +689,19 @@ function work(a::agentReflex, usermsg::String) end # good enough answer + + + # communicates with user + _ = addNewMessage(a, "assistant", respond) return respond end +function evaluate() + +end + + """ Actor function. @@ -725,6 +744,7 @@ function actor(a::agentReflex, plan::T) where {T<:AbstractString} # add to memory _respond = addStepNumber(respond, headers, a.step) a.memory[:shortterm] *= _respond + a.thoughtlog *= _respond chunkedtext = chunktext(respond, headers) toolname = toolNameBeingCalled(chunkedtext["Act:"], a.tools) @@ -732,10 +752,8 @@ function actor(a::agentReflex, plan::T) where {T<:AbstractString} @show toolname @show toolinput - #WORKING if toolname == "chatbox" # chat with user respond = toolinput - _ = addNewMessage(a, "assistant", respond) msgToUser = respond actorState = toolname break @@ -744,6 +762,7 @@ function actor(a::agentReflex, plan::T) where {T<:AbstractString} result = f(a, toolinput) _result = "\nObs $(a.step): $result\n" a.memory[:shortterm] *= _result + a.thoughtlog *= _result msgToUser = result end else #TODO finish all steps @@ -902,15 +921,16 @@ function analyze(a, shorttermMemory::T) where {T<:AbstractString} Your work: $shorttermMemory - Use the following steps to analize your work in detail. + Do each of the following steps in detail to analize your work. 1. What happened? - 2. List all cause and effect relationships. - 3. Analyze each relationship to figure it out why it behaved that way. - 4. What could you do to improve the respond? + 2. List all relationships, each with cause and effect . + 3. Look at each relationship, figure out why it behaved that way. + 4. Do relationships behaved differently than your expectation? If yes, why? + 5. What could you do to improve the respond? <|im_end|> """ - respond = sendReceivePrompt(a, prompt) + respond = sendReceivePrompt(a, prompt, max_tokens=2048) return respond end @@ -959,11 +979,13 @@ function selfReflext(a, report::T) where {T<:AbstractString} Your report: $report - What lesson could be drawn from your report?. + Your job are: + 1. Lesson: what lesson could you learn from your report?. + 2. Context: what is the context this lesson could apply to? <|im_end|> """ - respond = sendReceivePrompt(a, prompt) + respond = sendReceivePrompt(a, prompt, max_tokens=2048) return respond end diff --git a/src/type.jl b/src/type.jl index ec7903f..0d1d458 100644 --- a/src/type.jl +++ b/src/type.jl @@ -42,7 +42,7 @@ abstract type agent end thinkingFormat::Union{Dict, Nothing} = nothing memory::Dict = Dict( :shortterm=> "", - :longterm=> Dict{Symbol, Any}(), + :longterm=> Vector{Dict{Symbol, Any}}(), ) end diff --git a/src/utils.jl b/src/utils.jl index e9bdb64..6f6839b 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -91,11 +91,12 @@ end ) ``` """ -function sendReceivePrompt(a::T, prompt::String; timeout::Int=120) where {T<:agent} +function sendReceivePrompt(a::T, prompt::String; max_tokens=256, timeout::Int=120) where {T<:agent} a.msgMeta[:msgId] = "$(uuid4())" # new msg id for each msg msg = Dict( :msgMeta=> a.msgMeta, :txt=> prompt, + :max_tokens=>max_tokens ) payloadChannel = Channel(1)