From e487f6aadc6ef174457080ae9307f1a0e8afe876 Mon Sep 17 00:00:00 2001 From: tonaerospace Date: Wed, 29 Nov 2023 08:52:20 +0000 Subject: [PATCH] update --- src/interface.jl | 227 +++++++++++++++++++++------------------------ src/llmfunction.jl | 9 +- src/utils.jl | 43 ++++----- 3 files changed, 132 insertions(+), 147 deletions(-) diff --git a/src/interface.jl b/src/interface.jl index 942802a..4d39da4 100755 --- a/src/interface.jl +++ b/src/interface.jl @@ -315,7 +315,7 @@ function planner_mistral_openorca(a::agentReflex) prompt = replace(prompt, "{context}" => context) # initialize short term memory - prompt = replace(prompt, "{usermsg}" => "Stimulus: $(a.memory[:shortterm]["user"])") + prompt = replace(prompt, "{usermsg}" => "Stimulus: $(a.memory[:shortterm]["user:"])") return prompt end @@ -366,7 +366,7 @@ function actor_mistral_openorca(a::agentReflex) s = "" for (k, v) in a.memory[:shortterm] - if k ∉ ["user", "Plan 1:"] + if k ∉ ["user:", "Plan 1:"] s1 = "$k $v" s *= s1 end @@ -617,7 +617,7 @@ function work(a::agentReflex, usermsg::String) if a.thinkingmode == :new_thinking a.earlierConversation = conversationSummary(a) _ = addNewMessage(a, "user", usermsg) - a.memory[:shortterm]["user"] = usermsg + a.memory[:shortterm]["user:"] = usermsg @show a.memory[:shortterm] elseif a.thinkingmode == :continue_thinking #TODO error("continue_thinking $(@__LINE__)") @@ -657,10 +657,11 @@ function work(a::agentReflex, usermsg::String) println("all steps done") respond = formulateRespond(a) + error("10") a.memory[:shortterm]["Respond:"] = respond # evaluate. if score < 8/10 try again. - guideline = writeEvaluationGuideline(a, a.memory[:shortterm]["user"]) + guideline = writeEvaluationGuideline(a, a.memory[:shortterm]["user:"]) @show guideline score = grading(a, guideline, respond) @show score @@ -779,10 +780,15 @@ function actor(a::agentReflex) break else # function call f = a.tools[Symbol(toolname)][:func] - result = f(a, toolinput) - a.memory[:shortterm]["Obs $(a.step):"] = result + toolresult = f(a, toolinput) + @show toolresult + a.memory[:shortterm]["Obs $(a.step):"] = toolresult + + #WORKING goNogo + go = goNogo(a) + @show go + error(11) end - elseif #WORKING plan canceled else #TODO finish all steps actorState = "all steps done" msgToUser = nothing @@ -852,18 +858,15 @@ Return: ```jldoctest julia> using ChatAgent, CommUtils julia> agent = ChatAgent.agentReflex("Jene") -julia> shorttermMemory = - " - user: What's AMD latest product? - assistant: Plan 1: To provide the user with information about AMD's latest product, I will search for the most recent product release from AMD. - 1. Search for \"AMD latest product\" using wikisearch tool. - 2. Identify the most recent product release mentioned in the search results. - 3. Provide the user with the name of the latest product. - - Thought 1: The user wants to know about the latest AMD products, so I should use the wikisearch tool to find information on this topic. - Act 1: wikisearch - ActInput 1: \"AMD latest product\" - Obs 1: No info available." +julia> shorttermMemory = OrderedDict{String, Any}( + "user" => "What's the latest AMD GPU?", + "Plan 1:" => " To answer this question, I will need to search for the latest AMD GPU using the wikisearch tool.\n", + "Act 1:" => " wikisearch\n", + "ActInput 1:" => " amd gpu latest\n", + "Obs 1:" => "No info available for your search query.", + "Act 2:" => " wikisearch\n", + "ActInput 2:" => " amd graphics card latest\n", + "Obs 2:" => "No info available for your search query.") julia> guideline = "\nEvaluation Guideline:\n1. Check if the user's question has been understood correctly.\n2. Evaluate the steps taken to provide the information requested by the user.\n3. Assess whether the correct tools were used for the task.\n4. Determine if the user's request was successfully fulfilled.\n5. Identify any potential improvements or alternative approaches that could be used in the future.\n\nThe respond should include:\n1. A clear understanding of the user's question.\n2. The steps taken to provide the information requested by the user.\n3. An evaluation of whether the correct tools were used for the task.\n4. A confirmation or explanation if the user's request was successfully fulfilled.\n5. Any potential improvements or alternative approaches that could be used in the future." julia> score = grading(agent, guideline, shorttermMemory) 2 @@ -913,22 +916,21 @@ Return: ```jldoctest julia> using ChatAgent, CommUtils julia> agent = ChatAgent.agentReflex("Jene") -julia> shorttermMemory = - " - user: What's AMD latest product? - assistant: Plan 1: To provide the user with information about AMD's latest product, I will search for the most recent product release from AMD. - 1. Search for \"AMD latest product\" using wikisearch tool. - 2. Identify the most recent product release mentioned in the search results. - 3. Provide the user with the name of the latest product. - - Thought 1: The user wants to know about the latest AMD products, so I should use the wikisearch tool to find information on this topic. - Act 1: wikisearch - ActInput 1: \"AMD latest product\" - Obs 1: No info available." +julia> shorttermMemory = OrderedDict{String, Any}( + "user:" => "What's the latest AMD GPU?", + "Plan 1:" => " To answer this question, I will need to search for the latest AMD GPU using the wikisearch tool.\n", + "Act 1:" => " wikisearch\n", + "ActInput 1:" => " amd gpu latest\n", + "Obs 1:" => "No info available for your search query.", + "Act 2:" => " wikisearch\n", + "ActInput 2:" => " amd graphics card latest\n", + "Obs 2:" => "No info available for your search query.") julia> report = analyze(agent, shorttermMemory) ``` """ -function analyze(a, shorttermMemory::T) where {T<:AbstractString} +function analyze(a) + + prompt = """ <|im_start|>system @@ -1018,54 +1020,25 @@ Return: ```jldoctest julia> using ChatAgent, CommUtils julia> agent = ChatAgent.agentReflex("Jene") -julia> shorttermMemory = - " - user: What's AMD latest product? - assistant: Plan 1: To provide the user with information about AMD's latest product, I will search for the most recent product release from AMD. - 1. Search for \"AMD latest product\" using wikisearch tool. - 2. Identify the most recent product release mentioned in the search results. - 3. Provide the user with the name of the latest product. - - Thought 1: The user wants to know about the latest AMD products, so I should use the wikisearch tool to find information on this topic. - Act 1: wikisearch - ActInput 1: \"AMD latest product\" - Obs 1: No info available." +julia> shorttermMemory = OrderedDict{String, Any}( + "user:" => "What's the latest AMD GPU?", + "Plan 1:" => " To answer this question, I will need to search for the latest AMD GPU using the wikisearch tool.\n", + "Act 1:" => " wikisearch\n", + "ActInput 1:" => " amd gpu latest\n", + "Obs 1:" => "No info available for your search query.", + "Act 2:" => " wikisearch\n", + "ActInput 2:" => " amd graphics card latest\n", + "Obs 2:" => "No info available for your search query.") + julia> report = formulateRespond(agent, shorttermMemory) ``` """ -function formulateRespond(a, shorttermMemory::T) where {T<:AbstractString} - prompt = - """ - <|im_start|>system - You have access to the following tools: - chatbox: Useful for when you need to ask a customer for more context. Input should be a conversation to customer. - wikisearch: Useful for when you need to search an encyclopedia Input is keywords and not a question. - - Symbol: - Stimulus: the input user gives to you and you must respond - Plan: a plan - Thought: your thought - Act: the action you took - ActInput: the input to the action - Obs: the result of the action - - Your work: - $shorttermMemory - - From your work, formulate a respond for user's stimulus. - <|im_end|> - """ - - respond = sendReceivePrompt(a, prompt) - return respond -end - -function formulateUserRespond(a) where {T<:AbstractDict} - stimulus = a.memory[:shortterm]["user"] +function formulateUserRespond(a) + stimulus = a.memory[:shortterm]["user:"] work = "" for (k, v) in a.memory[:shortterm] - if k ∉ ["user",] + if k ∉ ["user:",] work *= "$k, $v\n" end end @@ -1093,44 +1066,83 @@ function formulateUserRespond(a) where {T<:AbstractDict} From your work, formulate a respond for user's stimulus. <|im_end|> + Respond: """ - - - respond = sendReceivePrompt(a, prompt) return respond end -""" Determine whether there is a respond available. +""" Determine whether LLM should go to next step. + Args: a, one of ChatAgent's agent. Return: - A respond for user's stimulus. + "Yes" or "no" decision to go next step. # Example ```jldoctest julia> using ChatAgent, CommUtils julia> agent = ChatAgent.agentReflex("Jene") -julia> shorttermMemory = - " - user: What's AMD latest product? - assistant: Plan 1: To provide the user with information about AMD's latest product, I will search for the most recent product release from AMD. - 1. Search for \"AMD latest product\" using wikisearch tool. - 2. Identify the most recent product release mentioned in the search results. - 3. Provide the user with the name of the latest product. - - Thought 1: The user wants to know about the latest AMD products, so I should use the wikisearch tool to find information on this topic. - Act 1: wikisearch - ActInput 1: \"AMD latest product\" - Obs 1: No info available." -julia> report = formulateRespond(agent, shorttermMemory) +julia> shorttermMemory = OrderedDict{String, Any}( + "user:" => "What's the latest AMD GPU?", + "Plan 1:" => " To answer this question, I will need to search for the latest AMD GPU using the wikisearch tool.\n", + "Act 1:" => " wikisearch\n", + "ActInput 1:" => " amd gpu latest\n", + "Obs 1:" => "No info available for your search query.", + "Act 2:" => " wikisearch\n", + "ActInput 2:" => " amd graphics card latest\n", + "Obs 2:" => "No info available for your search query.") + +julia> decision = goNogo(agent) +"Yes" ``` -""" -function isRespond() +""" #WORKING +function goNogo(a) + stimulus = a.memory[:shortterm]["user:"] + work = "" + for (k, v) in a.memory[:shortterm] + if k ∉ ["user:"] + work *= "$k $v" + end + end + + + prompt = + """ + <|im_start|>system + You have access to the following tools: + chatbox: Useful for when you need to ask a customer for more context. Input should be a conversation to customer. + wikisearch: Useful for when you need to search an encyclopedia Input is keywords and not a question. + + Symbol: + Stimulus: the input user gives to you and you must respond + Plan: a plan + Thought: your thought + Act: the action you took + ActInput: the input to the action + Obs: the result of the action + + Stimulus: + $stimulus + + Your work: + $work + + From your work, you job is to decide whether you're ready to do the next step in the plan by choosing one of the following choices: + If you are ready say, "{Yes}". And what is the rationale behind the decision? + If you are not ready say, "{No}". And what is the rationale behind the decision? + <|im_end|> + """ + + respond = sendReceivePrompt(a, prompt) + goraw = respond + @show goraw + decision = GeneralUtils.getStringBetweenCharacters(respond, "{", "}") + return decision end @@ -1152,33 +1164,6 @@ end - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/llmfunction.jl b/src/llmfunction.jl index d594c5d..aef36eb 100644 --- a/src/llmfunction.jl +++ b/src/llmfunction.jl @@ -30,6 +30,7 @@ function wikisearch(a::agentReflex, phrase::T) where {T<:AbstractString} phrase = replace(phrase, "\n"=>"") url = "https://en.wikipedia.org/w/api.php?action=query&format=json&prop=extracts&titles=$(replace(phrase, " " => "%20"))&exintro=1&explaintext=1" + @show url response = HTTP.get(url) json_data = JSON3.read(String(response.body)) page_id = first(keys(json_data["query"]["pages"])) @@ -40,16 +41,18 @@ function wikisearch(a::agentReflex, phrase::T) where {T<:AbstractString} result = nothing try result = json_data["query"]["pages"][page_id]["extract"] + wiki = result + @show wiki catch result = "No info available for your search query." end + if result == "" result = "No info available for your search query." - end - - if result != "No info available for your search query." #TODO for use with wikisearch(). Not good for other tools + else result = makeSummary(a, result) end + return result end diff --git a/src/utils.jl b/src/utils.jl index 9c11eca..a95a93f 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -16,47 +16,44 @@ function makeSummary(a::T1, input::T2) where {T1<:agent, T2<:AbstractString} prompt = """ <|im_start|>system - Your need to determine now whether you can make a summary of user's text. + Input text: + $input - You have the following choices: - If you cannot make a summary say, "{no}". - If you can make a summary say, "{yes}". + Your job is to determine now whether you can make a summary of the input text by choosing one of following choices: + If you cannot make a summary say, "{No}". + If you can make a summary say, "{Yes}". <|im_end|> - - <|im_start|>user - {input} - <|im_end|> - <|im_start|>assistant - """ prompt = replace(prompt, "{input}" => input) result = sendReceivePrompt(a, prompt) result = GeneralUtils.getStringBetweenCharacters(result, "{", "}") - if result == "yes" + println("///// ", result) + if result == "Yes" prompt = """ <|im_start|>system - You are a helpful assistant. - Your job is to make a concise summary of user's text. + Input text: + $input + Your job is to make a concise summary of the input text. <|im_end|> - - <|im_start|>user - {input} - <|im_end|> - <|im_start|>assistant - """ - prompt = replace(prompt, "{input}" => input) result = sendReceivePrompt(a, prompt) - summary = replace(result, "<|im_end|>" => "") - if summary[1:1] == "\n" - summary = summary[2:end] + if result[1:1] == "\n" + println("77777") + summary = result[2:end] + else + println("8888") + summary = result end end return summary end + + + + """ Send a msg to registered mqtt topic within mqttClient.