From 479d17589eab1d5284bbf470680b9cc5bd55423e Mon Sep 17 00:00:00 2001 From: tonaerospace Date: Mon, 4 Dec 2023 10:49:58 +0000 Subject: [PATCH] update --- src/ChatAgent.jl | 6 +++--- src/interface.jl | 44 ++++++++++++++++++-------------------------- src/utils.jl | 39 +++++++++++++++++++++++++++++++++------ 3 files changed, 54 insertions(+), 35 deletions(-) diff --git a/src/ChatAgent.jl b/src/ChatAgent.jl index 6c4cea2..46843b5 100755 --- a/src/ChatAgent.jl +++ b/src/ChatAgent.jl @@ -22,11 +22,11 @@ module ChatAgent #------------------------------------------------------------------------------------------------100 - """ version 0.0.1 + """ version 0.0.5 Todo: - [] add chat mechanism + [WORKING] add formulateUserRespond to AI tools - Change from version: 0.0.0 + Change from version: 0.0.4 - """ diff --git a/src/interface.jl b/src/interface.jl index dd8009c..40b7612 100755 --- a/src/interface.jl +++ b/src/interface.jl @@ -3,7 +3,7 @@ module interface export agentReact, agentReflex, addNewMessage, clearMessage, removeLatestMsg, conversation, writeEvaluationGuideline, - grading, analyze, selfReflext, actor_mistral_openorca2 + grading, analyze, selfReflext, actor_mistral_openorca2, formulateUserRespond using JSON3, DataStructures, Dates, UUIDs, HTTP using CommUtils, GeneralUtils @@ -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: @@ -299,7 +299,7 @@ function planner_mistral_openorca(a::agentReflex) - occasion - user's personal taste of wine - wine price range - - temperature at the serving location + - ambient temperature at the serving location - wines we have in stock You job is to provide a personalized recommendation of up to two wines based on the user's info above, and you describe the benefits of each wine in detail. """ @@ -380,17 +380,7 @@ function actor_mistral_openorca(a::agentReflex) prompt = replace(prompt, "{thinkingFormat}" => a.thinkingFormat[:actor]) prompt = replace(prompt, "{step}" => a.step) - - s = "" - for (k, v) in a.memory[:shortterm] - if k ∉ ["user:", "Plan 1:"] - s1 = "$k $v" - s *= s1 - if s[end] != "\n" - s *= "\n" - end - end - end + s = shortMemoryToString(a.memory[:shortterm], ["user:", "Plan 1:"]) prompt = replace(prompt, "{shorttermMemory}" => s) toolnames = "" @@ -419,6 +409,8 @@ function actor_mistral_openorca(a::agentReflex) return prompt end + + """ Chat with llm. @@ -685,16 +677,16 @@ function work(a::agentReflex, usermsg::String) a.newplan = false end + # enter actor loop actorstate, msgToUser = actor(a) if actorstate == "chatbox" respond = msgToUser break - elseif actorstate == "all steps done" #WORKING add canceled during plan + elseif actorstate == "all steps done" || actorstate == "formulateUserRespond" println("all steps done") respond = formulateUserRespond(a) - error("10") a.memory[:shortterm]["Respond $mark_plan:"] = respond a.memory[:log]["Respond $mark:"] = respond @@ -710,7 +702,7 @@ function work(a::agentReflex, usermsg::String) a.memory[:log] = OrderedDict{String, Any}() break else # self evaluate and reflect then try again - analysis = analyze(a, a.memory[:shortterm]) + analysis = analyze(a) @show analysis error(12) lessonwithcontext = selfReflext(a, analysis) @@ -830,6 +822,9 @@ function actor(a::agentReflex) msgToUser = respond actorState = toolname break + elseif toolname == "formulateUserRespond" + actorState = "formulateUserRespond" + break else # function call f = a.tools[Symbol(toolname)][:func] toolresult = f(a, toolinput) @@ -948,7 +943,10 @@ function grading(a, guideline::T, text::T) where {T<:AbstractString} {Evaluation} Score {}/10. <|im_end|> """ - println("prompt 11 ", prompt) + println("") + prompt_grading = prompt + @show prompt_grading + respond = sendReceivePrompt(a, prompt) println("grading respond 11 = $respond") _score = split(respond[end-5:end], "/")[1] @@ -985,8 +983,7 @@ julia> report = analyze(agent, shorttermMemory) ``` """ function analyze(a) - - + shorttermMemory = shortMemoryToString(a.memory[:shortterm], ["user:"]) prompt = """ <|im_start|>system @@ -1092,12 +1089,7 @@ julia> report = formulateRespond(agent, shorttermMemory) function formulateUserRespond(a) stimulus = a.memory[:shortterm]["user:"] - work = "" - for (k, v) in a.memory[:shortterm] - if k ∉ ["user:",] - work *= "$k, $v\n" - end - end + work = shortMemoryToString(a.memory[:shortterm], ["user:"]) prompt = """ diff --git a/src/utils.jl b/src/utils.jl index a399315..16fac90 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -3,7 +3,7 @@ module utils export makeSummary, sendReceivePrompt, chunktext, extractStepFromPlan, checkTotalStepInPlan, detectCharacters, findDetectedCharacter, extract_number, toolNameBeingCalled, chooseThinkingMode, conversationSummary, checkReasonableness, replaceHeaders, - addShortMem!, splittext + addShortMem!, splittext, shortMemoryToString using UUIDs, Dates, DataStructures using CommUtils, GeneralUtils @@ -619,16 +619,43 @@ function replaceHeaders(text::T, headers, step::Int) where {T<:AbstractString} end +""" Convert short term memory into 1 continous string. +Args: + shortMemory = a short memory of a ChatAgent's agent + skiplist = a list of keys in memory you want to skip +Return: + a short term memory in 1 countinuous string +# Example +```jldoctest +julia> shortMemory = OrderedDict( + "Thought" => "I like it.", + "Act" => "chatbox", + "ActInput" => "I get this one.", + ) +julia> headers = ["user:"] +julia> shortMemoryToString(shortMemory, headers) +``` +""" +function shortMemoryToString(shortMemory::OrderedDict, + skiplist::Union{Array{String}, Array{Symbol}}) + s = "" + for (k, v) in shortMemory + if k ∉ skiplist + s1 = "$k $v" + s *= s1 - - - - - + # ensure a newline seperate each sentences + if s[end] != "\n" + s *= "\n" + end + end + end + return s +end