From 8506a696c60be2fad73aa369b41ac3e4eb9d113b Mon Sep 17 00:00:00 2001 From: tonaerospace Date: Tue, 12 Dec 2023 10:42:51 +0000 Subject: [PATCH] update --- src/interface.jl | 19 +++++++---------- src/utils.jl | 53 ++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 58 insertions(+), 14 deletions(-) diff --git a/src/interface.jl b/src/interface.jl index 5aa26bd..e3f999e 100755 --- a/src/interface.jl +++ b/src/interface.jl @@ -4,7 +4,7 @@ module interface export agentReact, agentReflex, addNewMessage, clearMessage, removeLatestMsg, conversation, writeEvaluationGuideline, grading, analyze, selfReflext, actor_mistral_openorca2, formulateUserresponse, - extractinfo, updateEnvState + extractinfo, updateEnvState, chat_mistral_openorca using JSON3, DataStructures, Dates, UUIDs, HTTP using CommUtils, GeneralUtils @@ -188,8 +188,7 @@ function generatePrompt_mistral_openorca(a::T, usermsg::String, return prompt end -#WORKING -function chat_mistral_openorca(a::agentReflex, usermsg::String) +function chat_mistral_openorca(a::agentReflex) """ general prompt format: @@ -223,18 +222,15 @@ function chat_mistral_openorca(a::agentReflex, usermsg::String) $(a.roles[a.role]) Your earlier talk with the user: $(a.earlierConversation) - What you know about current situations: - $(a.env) - Begin! - <|im_end|> - <|im_start|>user - $usermsg <|im_end|> + $(messagesToString(a.messages)) <|im_start|>assistant """ + response = sendReceivePrompt(a, prompt) response = split(response, "<|im_end|>")[1] + return response end @@ -443,7 +439,7 @@ function conversation(a::agentReflex, usermsg::String; attemptlimit::Int=3) a.attemptlimit = attemptlimit response = nothing - a.earlierConversation = conversationSummary(a) + # a.earlierConversation = conversationSummary(a) _ = addNewMessage(a, "user", usermsg) isusetools = isUseTools(a, usermsg) newinfo = extractinfo(a, usermsg) @@ -454,8 +450,7 @@ function conversation(a::agentReflex, usermsg::String; attemptlimit::Int=3) # response = work(a, usermsg) # end - #WORKING - response = chat_mistral_openorca(a, usermsg) + response = chat_mistral_openorca(a) _ = addNewMessage(a, "assistant", response) return response diff --git a/src/utils.jl b/src/utils.jl index 738e0e6..5b640e0 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -3,7 +3,8 @@ module utils export makeSummary, sendReceivePrompt, chunktext, extractStepFromPlan, checkTotalStepInPlan, detectCharacters, findDetectedCharacter, extract_number, toolNameBeingCalled, isUseTools, conversationSummary, checkReasonableness, replaceHeaders, - addShortMem!, splittext, dictToString, removeHeaders, keepOnlyKeys, experience + addShortMem!, splittext, dictToString, removeHeaders, keepOnlyKeys, experience, + messagesToString using UUIDs, Dates, DataStructures using CommUtils, GeneralUtils @@ -501,7 +502,7 @@ function conversationSummary(a::T) where {T<:agent} prompt = replace(prompt, "{conversation}" => conversation) result = sendReceivePrompt(a, prompt) - summary = result === nothing ? "" : result + summary = result === nothing ? "N/A" : result summary = split(summary, "<|im_end|>")[1] if summary[1:1] == "\n" summary = summary[2:end] @@ -511,7 +512,55 @@ function conversationSummary(a::T) where {T<:agent} return summary end +""" Convert a vector of dict into 1-continous string. +Arguments: + vecofdict, a vector of dict + +Return: + 1-continous string + +# Example +```jldoctest +julia> using ChatAgent +julia> agent = ChatAgent.agentReflex("Jene") +julia> agent.messages = [Dict(:role=> "user", :content=> "Hi there."), + Dict(:role=> "assistant", :content=> "Hello! How can I assist you today?"),] + +julia> messagesToString(agent.messages) +"<|im_start|>user: Hi there.\n<|im_end|><|im_start|>assistant: Hello! How can I assist you today?\n<|im_end|>" +``` +""" #WORKING +function messagesToString(messages::AbstractVector{T}; addressAIas="assistant") where {T<:AbstractDict} + conversation = "" + if length(messages)!= 0 + for msg in messages + role = msg[:role] + content = msg[:content] + + nouse = 0 + for i in reverse(content) + if i == '\n' || i == ' ' + nouse += 1 + else + break + end + end + + if role == "user" + conversation *= "<|im_start|>$role: $(content[1:end-nouse])\n<|im_end|>" + elseif role == "assistant" + conversation *= "<|im_start|>$addressAIas: $(content[1:end-nouse])\n<|im_end|>" + else + error("undefied condition role = $role $(@__LINE__)") + end + end + else + conversation = "N/A" + end + + return conversation +end #TODO function checkReasonableness(userMsg::String, context::String, tools)