diff --git a/src/interface.jl b/src/interface.jl index 881edb0..304a00f 100755 --- a/src/interface.jl +++ b/src/interface.jl @@ -76,7 +76,7 @@ function agentReact( """, :sommelier => """ - You are a sommelier at an online wine reseller who always ask user for wine relevant info before you could help them choosing wine. + You are a helpful sommelier at an online wine reseller who always ask user for wine relevant info before you could help them choosing wine. You provide a personalized recommendation of up to two wines based on the user's preference, and you describe the benefits of each wine in detail. You don't know other people personal info previously. @@ -163,6 +163,9 @@ function agentReact( return newAgent end +""" + Based on Reflexion paper +""" @kwdef mutable struct agentReflex <: agent availableRole::AbstractVector = ["system", "user", "assistant"] agentName::String = "assistant" @@ -196,7 +199,6 @@ end :longterm=>"" ) end - function agentReflex( agentName::String, mqttClientSpec::NamedTuple; @@ -221,9 +223,9 @@ function agentReflex( - wine we have in stock """, ), - thinkingMode::Dict=Dict( + thinkingFormat::Dict=Dict( :no_thinking=> "", - :thinking=> + :react=> """Use the following format: Question: the input question your user is asking and you must answer Plan: first you should always think about the question and the info you have thoroughly then extract and devise a complete plan to find the answer (pay attention to variables and their corresponding numerals). @@ -235,6 +237,20 @@ function agentReflex( Thought: I think I know the answer Answer: Answer of the original question + Begin!""", + :plan=> + """Use the following format: + Question: the input question your user is asking and you must answer + Plan: first you should always think about the question and the info you have thoroughly then extract and devise a complete plan to find the answer (pay attention to variables and their corresponding numerals). + + Begin!""", + :qta=> + """Use the following format: + Question: the input question your user is asking and you must answer + Thought: ask yourself do you have all the info you need? And what to do according to the plan (pay attention to correct numeral calculation and commonsense). + Act: the tool that match your thought, should be one of {toolnames} + ActInput: the input to the action (pay attention to the tool's input) + Begin!""", ), tools::Dict=Dict( @@ -435,9 +451,6 @@ function generatePrompt_mistral_openorca(a::T, usermsg::String, toolnames *= "$toolname," end prompt = replace(prompt, "{toolnames}" => toolnames) - if thinkingMode ∈ [:react] - prompt = replace(prompt, "{tools}" => "You have access to the following tools:\n$toollines") - end prompt = replace(prompt, "{context}" => a.context) @@ -447,6 +460,80 @@ function generatePrompt_mistral_openorca(a::T, usermsg::String, return prompt end + +function genPrompt_mistral_openorca_planning(a::agentReflex, usermsg::String) #WORKING + """ + general prompt format: + + " + <|im_start|>system + {role} + {tools} + {thinkingFormat} + <|im_end|> + {context} + <|im_start|>user + {usermsg} + <|im_end|> + <|im_start|>assistant + + " + + Note: + {context} = {earlierConversation} + {current status} + + {shortterm memory} + {longterm memory} + """ + + prompt = + """ + <|im_start|>system + {role} + {tools} + {thinkingFormat} + <|im_end|> + {context} + <|im_start|>user + {usermsg} + <|im_end|> + <|im_start|>assistant + + """ + prompt = replace(prompt, "{role}" => a.roles[a.role]) + prompt = replace(prompt, "{thinkingFormat}" => a.thinkingFormat[:plan]) + toolnames = "" + toollines = "" + for (toolname, v) in a.tools + toolline = "$toolname: $(v[:description]) $(v[:input]) $(v[:output])\n" + toollines *= toolline + toolnames *= "$toolname," + end + prompt = replace(prompt, "{toolnames}" => toolnames) + prompt = replace(prompt, "{tools}" => "You have access to the following tools:\n$toollines") + + context = + + + + prompt = replace(prompt, "{context}" => a.context) + + prompt *= "<|im_start|>user\nQuestion: " * usermsg * "\n<|im_end|>\n" + prompt *= "<|im_start|>assistant\n" + + return prompt +end + +function composeContext(a::agentReflex) + context = + """ + {earlierConversation} + {current status} + {shortterm memory} + {longterm memory} + """ + + +end + """ Chat with llm. @@ -644,7 +731,7 @@ function conversation(a::agentReflex, usermsg::String; thinkingroundlimit::Int=3 if a.thinkingMode == :no_thinking a.earlierConversation = conversationSummary(a) #TODO should be long conversation before use summary because it leaves out details _ = addNewMessage(a, "user", usermsg) - prompt = generatePrompt_mistral_openorca(a, usermsg, thinkingmode) + prompt = generatePrompt_mistral_openorca(a, usermsg) @show prompt respond = sendReceivePrompt(a, prompt) respond = split(respond, "<|im_end|>")[1] @@ -655,8 +742,6 @@ function conversation(a::agentReflex, usermsg::String; thinkingroundlimit::Int=3 respond = work(a, usermsg) end - respond = work(a, usermsg) - return respond end @@ -678,7 +763,7 @@ function work(a::agentReflex, usermsg::String) @show a.thinkinground toolname = nothing toolinput = nothing - plan = planning(a, prompt) + plan = generatePrompt_planning(a, prompt) @show plan # for # # execute @@ -688,24 +773,6 @@ function work(a::agentReflex, usermsg::String) end end -#WORKING -function planning() - prompt = - """ - <|im_start|>system - You are a helpful assistant. - Your job is to make a concise summary of user's text. - <|im_end|> - - <|im_start|>user - {input} - <|im_end|> - <|im_start|>assistant - - """ -end - - """ make a conversation summary. ```jldoctest