From cef52a6cf14fff13486d671d5c88a0d8b1bf4bae Mon Sep 17 00:00:00 2001 From: narawat Date: Thu, 16 Nov 2023 14:04:25 +0000 Subject: [PATCH] update --- src/interface.jl | 71 +++++++++++++++++++++++++++++------------------- 1 file changed, 43 insertions(+), 28 deletions(-) diff --git a/src/interface.jl b/src/interface.jl index 6f11cd6..87f75c6 100755 --- a/src/interface.jl +++ b/src/interface.jl @@ -166,7 +166,7 @@ end julia> addNewMessage(agent1, "user", "Where should I go to buy snacks") ```` """ -function addNewMessage(a::T, role::String, content::String) where {T<:agent} +function addNewMessage(a::T1, role::String, content::T2) where {T1<:agent, T2<:AbstractString} if role ∉ a.availableRole # guard against typo error("role is not in agent.availableRole") end @@ -321,7 +321,8 @@ function generatePrompt_mistral_openorca(a::T, usermsg::String) where {T<:agent} return prompt end -function generatePrompt_react_mistral_openorca(a::T, usermsg::String) where {T<:agent} +function generatePrompt_react_mistral_openorca(a::T, usermsg::String, + continuethought::Bool=false) where {T<:agent} prompt = """ <|im_start|>system @@ -347,8 +348,13 @@ function generatePrompt_react_mistral_openorca(a::T, usermsg::String) where {T<: prompt = replace(prompt, "{context}" => a.context) - prompt *= "<|im_start|>user\nQTS: " * usermsg * "\n<|im_end|>\n" - prompt *= "<|im_start|>assistant\n" + if continuethought == false + prompt *= "<|im_start|>user\nQTS: " * usermsg * "\n<|im_end|>\n" + prompt *= "<|im_start|>assistant\n" + else + prompt *= "Obs: $usermsg\n" + end + return prompt end @@ -356,20 +362,31 @@ end #WORKING function conversation(a::T, usermsg::String) where {T<:agent} userintend = identifyUserIntention(a, usermsg) - + @show userintend #BUG nothing sometime why? may be llm need more time to respond respond = nothing # AI thinking mode if userintend == "chat" - summary = conversationSummary(a) + a.context = conversationSummary(a) #BUG should be long conversation before use summary because it leaves out details _ = addNewMessage(a, "user", usermsg) prompt = generatePrompt_mistral_openorca(a, usermsg) + @show prompt respond = sendReceivePrompt(a, prompt) - respond = replace(respond, "\n<|im_end|>" => "") + respond = split(respond, "<|im_end|>")[1] + respond = replace(respond, "\n" => "") _ = addNewMessage(a, "assistant", respond) elseif userintend == "wine" #WORKING + if a.thought == "nothing" # new thought + a.context = conversationSummary(a) + _ = addNewMessage(a, "user", usermsg) + prompt = generatePrompt_react_mistral_openorca(a, usermsg) + @show prompt + + else # continue thought + end + error("wine done") elseif userintend == "thought" @@ -388,17 +405,6 @@ function conversation(a::T, usermsg::String) where {T<:agent} - # if a.thought == "nothing" - # a.context = conversationSummary(a) - # addNewMessage(a, "user", usermsg) - # prompt = generatePrompt_react_mistral_openorca(a, usermsg) - # @show prompt - - # else - - - # end - @@ -446,17 +452,18 @@ function conversationSummary(a::T) where {T<:agent} <|im_start|>system You are a helpful assistant. <|im_end|> - - <|im_start|>user - Please make a detailed bullet summary of the following earlier conversation between you and the user. - <|im_end|> Here are the context for the question: {context} + <|im_start|>user + Please make a detailed bullet summary of the following earlier conversation between you and the user. {conversation} + <|im_end|> + <|im_start|>assistant + """ conversation = "" - summary = "" + summary = "nothing" if length(a.messages)!= 0 for msg in a.messages role = msg[:role] @@ -472,11 +479,16 @@ function conversationSummary(a::T) where {T<:agent} end prompt = replace(prompt, "{conversation}" => conversation) prompt = replace(prompt, "{context}" => a.context) + println("<<<<<") + @show prompt result = sendReceivePrompt(a, prompt) summary = result === nothing ? "nothing" : result + summary = replace(summary, "<|im_end|>" => "") if summary[1:1] == "\n" summary = summary[2:end] end + @show summary + println(">>>>>") end return summary @@ -567,15 +579,17 @@ function identifyUserIntention(a::T, usermsg::String) where {T<:agent} """ <|im_start|>system You are a helpful assistant. Your job is to determine intention of the question. + If the user question is relate to you earlier thought say, {thought}". If the user question is about general conversation say, "{chat}". If the user question is about getting wine say, "{wine}". - If the user question is related to your earlier thought say, "{thought}". + If you can't determine the intention say, "{chat}". + <|im_end|> Your earlier thought: {earlier thought} + Here are the context for the question: {context} - <|im_end|> <|im_start|>user {input} @@ -583,14 +597,15 @@ function identifyUserIntention(a::T, usermsg::String) where {T<:agent} <|im_start|>assistant """ - prompt = replace(prompt, "{input}" => usermsg) prompt = replace(prompt, "{earlier thought}" => a.thought) prompt = replace(prompt, "{context}" => "") + prompt = replace(prompt, "{input}" => usermsg) + - result = sendReceivePrompt(a, prompt) + result = sendReceivePrompt(a, prompt) #BUG answer is not in the choice {} answer = result === nothing ? nothing : GeneralUtils.getStringBetweenCharacters(result, "{", "}") - return answer + return answer end