From 107624359b8ea3a241d12fe1f601cbbf1a1750b4 Mon Sep 17 00:00:00 2001 From: tonaerospace Date: Wed, 13 Dec 2023 06:30:25 +0000 Subject: [PATCH] update --- src/interface.jl | 2 +- src/type.jl | 2 +- src/utils.jl | 267 ++++++++++++++++++++++++++++++++++++----------- 3 files changed, 208 insertions(+), 63 deletions(-) diff --git a/src/interface.jl b/src/interface.jl index 33a2749..22881ee 100755 --- a/src/interface.jl +++ b/src/interface.jl @@ -441,7 +441,7 @@ function conversation(a::agentReflex, usermsg::String; attemptlimit::Int=3) # a.earlierConversation = conversationSummary(a) _ = addNewMessage(a, "user", usermsg) - isusetools = isUseTools(a, usermsg) + isusetools, whattodo = isUseTools(a) # newinfo = extractinfo(a, usermsg) # a.env = newinfo !== nothing ? updateEnvState(a, newinfo) : a.env @show isusetools diff --git a/src/type.jl b/src/type.jl index 9b70d34..676e901 100644 --- a/src/type.jl +++ b/src/type.jl @@ -120,7 +120,7 @@ function agentReflex( roles::Dict=Dict( :assistant => """ - You are a helpful assistant who respond to a stimulus as best you can. + You are a helpful assistant who respond to user as best you can. """, :sommelier => """ diff --git a/src/utils.jl b/src/utils.jl index 5b640e0..e425262 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -363,12 +363,147 @@ end # return thinkingmode # end +#WORKING + +function isUseTools(a::agentReflex) + toollines = "" + for (toolname, v) in a.tools + if toolname ∉ ["chatbox"] + toolline = "$toolname: $(v[:description]) $(v[:input]) $(v[:output])\n" + toollines *= toolline + end + end + + conversation = messagesToString_nomark(a.messages) + + prompt = + """ + <|im_start|>system + $(a.roles[a.role]) + You have access to the following tools: + $toollines + + Your conversation with the user: + $conversation + + From the user's message, ask yourself what do you need 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 + + plan = "N/A" + if isusetool + toollines = "" + for (toolname, v) in a.tools + if toolname ∉ [""] + toolline = "$toolname: $(v[:description]) $(v[:input]) $(v[:output])\n" + toollines *= toolline + end + end + assistant_objective_prompt = + """ + <|im_start|>system + $(a.roles[a.role]) + The info you need from the user to be able to help them selecting their best wine: + - type of food + - occasion + - user's personal taste of wine + - wine price range + - ambient temperature at the serving location + - wines we have in stock + You 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. + + You have access to the following tools: + $toollines + + Your conversation with the user: + $conversation + + Use the following format: + 1. Objective: From your conversation with the user, ask yourself what do you need to do now? + 2. Plan: first you should always think about the stimulus, the info you need and the info you have thoroughly then extract and devise a step by step plan (pay attention to correct numeral calculation and commonsense). + <|im_end|> + + """ + assistant_objective_result = sendReceivePrompt(a, assistant_objective_prompt, temperature=0.2) + @show assistant_objective_result + end + + return isusetool, plan +end + + + + +# function isUseTools(a::agentReflex) +# toollines = "" +# for (toolname, v) in a.tools +# if toolname ∉ ["chatbox"] +# toolline = "$toolname: $(v[:description]) $(v[:input]) $(v[:output])\n" +# toollines *= toolline +# end +# end + +# conversation = messagesToString_nomark(a.messages) + +# prompt = +# """ +# <|im_start|>system +# $(a.roles[a.role]) +# You have access to the following tools: +# $toollines + +# Your conversation with the user: +# $conversation + +# From your conversation, ask yourself what do you need to do now? +# <|im_end|> + +# """ + +# # 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 +# @show prompt +# whattodo = result +# @show whattodo + +# return isusetool, whattodo +# end + # function isUseTools(a::agentReflex, usermsg::String) -# prompt = +# toollines = "" +# for (toolname, v) in a.tools +# if toolname ∉ ["chatbox"] +# toolline = "$toolname: $(v[:description]) $(v[:input]) $(v[:output])\n" +# toollines *= toolline +# end +# end + +# prompt = # """ # <|im_start|>system -# {systemMsg} +# $(a.roles[a.role]) +# You have access to the following tools: +# $toollines # Your earlier conversation with the user: # None @@ -376,73 +511,28 @@ end # 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? +# From the user's message, ask yourself what do you need to do? # <|im_end|> # <|im_start|>assistant -# Answer: + # """ -# toollines = "" + +# # 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 toolname ∉ [:chatbox] -# toolline = "$toolname: $(v[:description]) $(v[:input]) $(v[:output])\n" -# toollines *= toolline +# if occursin(toolname, result) +# isusetool = true +# break # 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 +# whattodo = result +# @show whattodo + +# return isusetool, whattodo # end -function isUseTools(a::agentReflex, usermsg::String) - toollines = "" - for (toolname, v) in a.tools - if toolname ∉ ["chatbox"] - toolline = "$toolname: $(v[:description]) $(v[:input]) $(v[:output])\n" - toollines *= toolline - end - end - - 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 - @@ -561,6 +651,61 @@ function messagesToString(messages::AbstractVector{T}; addressAIas="assistant") return conversation end +#WORKING +function messagesToString_nomark(messages::AbstractVector{T}; addressAIas="assistant") where {T<:AbstractDict} + conversation = "" + if length(messages)!= 0 + for msg in messages + role = msg[:role] + content = msg[:content] + + content = removeTrailingCharacters(content) + + if role == "user" + conversation *= "$role: $content\n" + elseif role == "assistant" + conversation *= "$addressAIas: $content\n" + else + error("undefied condition role = $role $(@__LINE__)") + end + end + else + conversation = "N/A" + end + + return conversation +end + +""" Remove trailing characters from text. + +Arguments: + text, text you want to remove trailing characters + charTobeRemoved, a list of characters to be removed + +Return: + text with specified trailing characters removed + +# Example +```jldoctest +julia> text = "Hello! How can I assist you today?\n\n " +julia> removelist = ['\n', ' ',] +julia> removeTrailingCharacters(text, charTobeRemoved=removelist) +"Hello! How can I assist you today?" +``` +""" +function removeTrailingCharacters(text; charTobeRemoved::AbstractVector{T}=['\n', ' ',]) where {T<:Char} + nouse = 0 + for i in reverse(text) + if i ∈ charTobeRemoved + nouse += 1 + else + break + end + end + + return text[1:end-nouse] +end + #TODO function checkReasonableness(userMsg::String, context::String, tools) @@ -817,7 +962,7 @@ OrderedDict( "Plan 2:" => "I'm meeting my wife this afternoon", ) ``` -""" #WORKING +""" function removeHeaders(shortMemory::OrderedDict, step::Int, skipHeaders::Union{Array{String}, Array{Symbol}, Nothing}=nothing)