This commit is contained in:
2024-01-18 18:58:52 +00:00
parent 44006c3112
commit b419a5629c
4 changed files with 435 additions and 371 deletions

View File

@@ -99,7 +99,7 @@ function chat_mistral_openorca(a::agentReflex)
general prompt format:
"
<|im_start|>system
<|system|>
{role}
{tools}
{thinkingFormat}
@@ -136,9 +136,6 @@ function chat_mistral_openorca(a::agentReflex)
<About yourself>
$aboutYourself
</About yourself>
<Your earlier talk with the user>
$(a.earlierConversation)
</Your earlier talk with the user>
</s>
$conversation
<|assistant|>
@@ -155,7 +152,7 @@ function planner_mistral_openorca(a::agentReflex)
general prompt format:
"
<|im_start|>system
<|system|>
{role}
{tools}
{thinkingFormat}
@@ -371,6 +368,7 @@ function selfAwareness(a::agentReflex)
<Your job>
Use the following format strictly:
What do I know: based on observed results, repeat all the information you are gathering.
Info match: explicitly state what information matches what variable in my plan.
What am I missing: based on observed results, describe in detail what you are still missing compared to your plan.
P.S. do not mention any toolnames
</Your job>
@@ -408,7 +406,7 @@ function actor_mistral_openorca(a::agentReflex, selfaware=nothing)
general prompt format:
"
<|im_start|>system
<|system|>
{role}
{tools}
{thinkingFormat}
@@ -450,7 +448,7 @@ function actor_mistral_openorca(a::agentReflex, selfaware=nothing)
# aware = "Self-awareness: Based on action's input and observed results, check your progress against the plan. Then, repeat all the details of what you have been gathered. Finally, describe in detail what you are missing."
thought =
"Self-awareness: $selfaware
Thought: based on self-awareness, You should always plan your next steps and focus on what you missed first. (P.S. 1) let's think a single step. 2) pay attention to correct numeral calculation and commonsense.)
Thought: based on self-awareness, think about what to do next and focus on what you missed first. (P.S. 1) let's think a single step. 2) pay attention to correct numeral calculation and commonsense.)
"
end
@@ -484,11 +482,10 @@ function actor_mistral_openorca(a::agentReflex, selfaware=nothing)
Act: askbox
Actinput: {\"askbox\": \"Hello! Welcome to our wine store.\"}
</Example>
</s>
<|assistant|>
$work
"Thought:"
"Thought: "
"""
prompt = replace(prompt, "{toolnames}" => toolnames)
@@ -507,14 +504,16 @@ function actor_mistral_openorca(a::agentReflex, selfaware=nothing)
response = sendReceivePrompt(a, prompt, max_tokens=1024, temperature=0.4, timeout=300,
stopword=["Thought:", "Obs:", "<|system|>", "</s>", "<|end|>"],
seed=seed)
response = splittext(response, ["/n/n", "END", "End", "Obs", "<|im_end|>"])
response = splittext(response, ["/n/n", "END", "End","obs", "Obs", "<|im_end|>"])
latestTask = shortMemLatestTask(a.memory[:shortterm]) +1
response = "Thought:" * response
if occursin("Thought", response) == false
response = "Thought:" * response
end
headerToDetect = ["Question:", "Plan:", "Self-awareness:", "Thought:",
"Act:", "Actinput:", "Obs:", "...",
headerToDetect = ["Plan:", "Self-awareness:", "Thought:",
"Act:", "Actinput:", "Obs:",
"Answer:", "Conclusion:", "Summary:"]
# replace headers with headers with correct attempt and task number
@@ -522,10 +521,10 @@ function actor_mistral_openorca(a::agentReflex, selfaware=nothing)
response = split(response, "<|")[1]
response = split(response, "</")[1]
# headers = detectCharacters(response, headerToDetect)
# sometime LLM use wrong keyword. use regex to detect "actinput5:" and replace it with "Actinput"
regexmatch = match(r"actinput\d+:", response)
respone = regexmatch !== nothing ? response = replace(response, match=>"Actinput:") : response
response = replace(response, "actinput:"=>"Actinput:")
headerToDetect = ["Plan $(a.attempt):",
"Self-awareness $latestTask:",
@@ -542,18 +541,6 @@ function actor_mistral_openorca(a::agentReflex, selfaware=nothing)
check_2 = haskey(chunkedtext, "Act $latestTask:")
check_3 = haskey(chunkedtext, "Actinput $latestTask:")
#check whether the act has valid json
check_7 = true
if occursin('{', response)
try
act = GeneralUtils.getStringBetweenCharacters(response, '{', '}', endCharLocation="end")
act = JSON3.read(act)
check_7 = true
catch
check_7 = false
end
end
# check for a valid toolname
check_4 = false
for i in toolslist
@@ -566,15 +553,36 @@ function actor_mistral_openorca(a::agentReflex, selfaware=nothing)
# check for empty Thought
check_5 = length(chunkedtext["Thought $latestTask:"]) > 5
# check for empty Actinput
check_6 = length(chunkedtext["Actinput $latestTask:"]) > 5
check_6 = nothing
try
check_6 = length(chunkedtext["Actinput $latestTask:"]) > 5
catch
println("")
@show response
println("")
@show chunkedtext
a.memory[:chunkedtext] = chunkedtext
end
#check whether the act has valid json
check_7 = true
if occursin('{', response)
try
act = GeneralUtils.getStringBetweenCharacters(response, '{', '}', endCharLocation="end")
act = JSON3.read(act)
check_7 = true
catch
check_7 = false
end
end
# print all check_1 to check_6
println("check_1: $check_1, check_2: $check_2, check_3: $check_3, check_4: $check_4,
check_5: $check_5, check_6: $check_6, check_7: $check_7")
if check_1 && check_2 && check_3 && check_4 && check_5 && check_6 && check_7
#WORKING paraphrase selfaware
#TODO paraphrase selfaware
break
end
@show response
@@ -597,10 +605,8 @@ function actor_mistral_openorca(a::agentReflex, selfaware=nothing)
"Check $latestTask:",]
headers = detectCharacters(response, headerToDetect)
chunkedtext = chunktext(response, headers)
chunkedtext = delete!(chunkedtext, "Self-awareness $latestTask")
println("")
@show chunkedtext
toolinput = chunkedtext["Actinput $latestTask:"]
@@ -615,237 +621,9 @@ function actor_mistral_openorca(a::agentReflex, selfaware=nothing)
chunkedtext["Act $latestTask:"] = toolname
return toolname, toolinput, chunkedtext
return (toolname=toolname, toolinput=toolinput, chunkedtext=chunkedtext, selfaware=selfaware)
end
# function actor_mistral_openorca(a::agentReflex, selfaware=nothing)
# getonlykeys = ["Actinput", "Obs"]
# worknoplan = similar(a.memory[:shortterm])
# for (k, v) in a.memory[:shortterm]
# count = 0
# for i in getonlykeys
# if occursin(i, k)
# count += 1
# end
# end
# if count != 0
# worknoplan[k] = v
# end
# end
# work = dictToString(worknoplan)
# """
# general prompt format:
# "
# <|im_start|>system
# {role}
# {tools}
# {thinkingFormat}
# <|im_end|>
# {context}
# <|im_start|>user
# {usermsg}
# <|im_end|>
# <|im_start|>assistant
# "
# Note:
# {context} =
# "
# {earlierConversation}
# {env state}
# {shortterm memory}
# {longterm memory}
# "
# """
# toolslist = []
# toolnames = ""
# toollines = ""
# for (toolname, v) in a.tools
# toolline = "$toolname: $(v[:description]) $(v[:input]) $(v[:output])\n"
# toollines *= toolline
# toolnames *= "$toolname, "
# push!(toolslist, toolname)
# end
# thought = "Thought: you should always think about what to do according to the plan (pay attention to correct numeral calculation and commonsense and do one thing at a time.)"
# if selfaware !== nothing
# # aware = "Self-awareness: based on the recap, assess your progress against the plan then identify areas where you need to address."
# # aware = "Self-awareness: based on the recap and the plan, state your current understanding of the matter in details then identify areas where you need to address."
# # aware = "Self-awareness: Based on Obs, review your progress against the plan. Then, describe in detail the results you have achieved so far. Finally, describe in detail what you are missing. (focus on your actions and their results)"
# # aware = "Self-awareness: Based on action's input and observed results, check your progress against the plan. Then, repeat all the details of what you have been gathered. Finally, describe in detail what you are missing."
# thought =
# "Self-awareness: $selfaware
# Thought: you should always think about what to do based on what are you missing. (P.S. 1) let's think a single step. 2) pay attention to correct numeral calculation and commonsense.)
# "
# end
# aboutYourself =
# """
# Your name is $(a.agentName)
# $(a.roles[a.role])
# """
# prompt =
# """
# <|system|>
# <About yourself>
# $aboutYourself
# </About yourself>
# <You have access to the following tools>
# $toollines
# </You have access to the following tools>
# <Your plan>
# $(a.memory[:shortterm]["Plan 1:"])
# </Your plan>
# <Your job>
# Use the following format:
# $thought
# Act: based on your thought what action to choose?, must be one of [{toolnames}].
# Actinput: your input to the action (pay attention to the tool's input)
# Obs: observed result of the action
# </Your job>
# <Example>
# Thought: Greet user and begin the conversation.
# Act: askbox
# Actinput: {\"askbox\": \"Hello! Welcome to our wine store.\"}
# </Example>
# </s>
# <|assistant|>
# $work
# "Thought:"
# """
# prompt = replace(prompt, "{toolnames}" => toolnames)
# println("")
# @show actor_prompt = prompt
# response = nothing
# chunkedtext = nothing
# latestTask = nothing
# tempcounter = 0.2
# seed = nothing
# while true # while Thought or Act is empty, run actor again
# # tempcounter += 0.2
# @show tempcounter
# response = sendReceivePrompt(a, prompt, max_tokens=1024, temperature=0.4, timeout=180,
# stopword=["Thought:", "Obs:", "<|system|>", "</s>", "<|end|>"],
# seed=seed)
# response = splittext(response, ["/n/n", "END", "End", "Obs", "<|im_end|>"])
# latestTask = shortMemLatestTask(a.memory[:shortterm]) +1
# response = "Thought:" * response
# headerToDetect = ["Question:", "Plan:", "Self-awareness:", "Thought:",
# "Act:", "Actinput:", "Obs:", "...",
# "Answer:", "Conclusion:", "Summary:"]
# # replace headers with headers with correct attempt and task number
# response = replaceHeaders(response, headerToDetect, latestTask)
# response = split(response, "<|")[1]
# response = split(response, "</")[1]
# # headers = detectCharacters(response, headerToDetect)
# println("")
# headerToDetect = ["Plan $(a.attempt):",
# "Self-awareness $latestTask:",
# "Thought $latestTask:",
# "Act $latestTask:",
# "Actinput $latestTask:",
# "Obs $latestTask:",
# "Check $latestTask:",]
# headers = detectCharacters(response, headerToDetect)
# chunkedtext = chunktext(response, headers)
# # assuming length more than 10 character means LLM has valid thinking
# check_1 = haskey(chunkedtext, "Thought $latestTask:")
# check_2 = haskey(chunkedtext, "Act $latestTask:")
# check_3 = haskey(chunkedtext, "Actinput $latestTask:")
# # check whether the act has valid json
# # check_4 = false
# # try
# # act = GeneralUtils.getStringBetweenCharacters(response, '{', '}', endCharLocation="end")
# # act = JSON3.read(act)
# # check_4 = true
# # catch
# # end
# # check for a valid toolname
# check_4 = false
# for i in toolslist
# if occursin(i, chunkedtext["Act $latestTask:"])
# check_4 = true
# break
# end
# end
# # check for empty Thought
# check_5 = length(chunkedtext["Thought $latestTask:"]) > 5
# # check for empty Actinput
# check_6 = length(chunkedtext["Actinput $latestTask:"]) > 5
# # print all check_1 to check_6
# println("check_1: $check_1, check_2: $check_2, check_3: $check_3, check_4: $check_4, check_5: $check_5, check_6: $check_6")
# if check_1 && check_2 && check_3 && check_4 && check_5 && check_6
# #WORKING paraphrase selfaware
# break
# end
# end
# toolname = toolNameBeingCalled(chunkedtext["Act $latestTask:"], a.tools)
# # change trailing number to continue a.memory[:shortterm]
# headerToDetect = ["Question:", "Plan:", "Self-awareness:", "Thought:",
# "Act:", "Actinput:", "Obs:", "...",
# "Answer:", "Conclusion:", "Summary:"]
# response = replaceHeaders(response, headerToDetect, latestTask)
# println("")
# @show actor_response_1 = response
# headerToDetect = ["Plan $(a.attempt):",
# "Thought $latestTask:",
# "Act $latestTask:",
# "Actinput $latestTask:",
# "Obs $latestTask:",
# "Check $latestTask:",]
# headers = detectCharacters(response, headerToDetect)
# chunkedtext = chunktext(response, headers)
# chunkedtext = delete!(chunkedtext, "Self-awareness $latestTask")
# println("")
# @show chunkedtext
# toolinput = chunkedtext["Actinput $latestTask:"]
# # because tools has JSON input but sometime LLM output is not JSON, we need to check.
# if occursin("{", toolinput)
# act = GeneralUtils.getStringBetweenCharacters(response, '{', '}', endCharLocation="end")
# act = copy(JSON3.read(act))
# chunkedtext["Actinput $latestTask:"] = JSON3.write(act[Symbol(toolname)])
# toolinput = act[Symbol(toolname)]
# end
# chunkedtext["Act $latestTask:"] = toolname
# return toolname, toolinput, chunkedtext
# end
"""
Chat with llm.
@@ -882,13 +660,12 @@ end
)
julia> response = ChatAgent.conversation(newAgent, "Hi! how are you?")
```
"""
# """
function conversation(a::agentReflex, usermsg::String; attemptlimit::Int=3)
a.attemptlimit = attemptlimit
workstate = nothing
response = nothing
# a.earlierConversation = conversationSummary(a)
_ = addNewMessage(a, "user", usermsg)
isuseplan = isUsePlans(a)
# newinfo = extractinfo(a, usermsg)
@@ -930,7 +707,7 @@ function work(a::agentReflex)
latestTask = shortMemLatestTask(a.memory[:shortterm])
if haskey(a.memory[:shortterm], "Act $latestTask:")
if occursin("askbox", a.memory[:shortterm]["Act $latestTask:"])
a.memory[:shortterm]["Obs $latestTask:"] = "user response: " * a.messages[end][:content]
a.memory[:shortterm]["Obs $latestTask:"] = "(user response) " * a.messages[end][:content]
end
end
end
@@ -1069,8 +846,10 @@ function actor(a::agentReflex)
println("")
@show selfaware
end
toolname, toolinput, chunkedtext = actor_mistral_openorca(a, selfaware)
actorResult = actor_mistral_openorca(a, selfaware)
println("")
toolname, toolinput, chunkedtext, selfaware = actorResult
@show toolname
@show toolinput
println(typeof(toolinput))
@@ -1088,8 +867,8 @@ function actor(a::agentReflex)
actorState = "formulateFinalResponse"
break
else # function call
f = a.tools[toolname][:func]
toolresult = f(a, toolinput)
f = a.tools[toolname][:func]
toolresult = f(a, actorResult)
@show toolresult
if toolname == "temp"
a.winestockResult = toolresult
@@ -1134,7 +913,7 @@ end
function writeEvaluationGuideline(a::agentReflex)
prompt =
"""
<|im_start|>system
<|system|>
$(a.roles[a.role])
<You have access to the following tools>
askbox: Useful for when you need to ask a customer for more context. Input should be a conversation to customer.
@@ -1186,37 +965,44 @@ end
function grading(a, guideline::T, text::T) where {T<:AbstractString}
prompt =
"""
<|im_start|>system
You have access to the following tools:
askbox: 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.
<|system|>
<You have access to the following tools>
askbox: 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.
</You have access to the following tools>
<Guideline>
$guideline
</Guideline>
<Your response>
$text
</Your response>
<Guideline>
$guideline
</Guideline>
<Your response>
$text
</Your response>
<Your job>
Evaluate your response using the evaluation guideline then give yourself a score out of 9 for your response.
</Your job>
<Example>
{"Evaluate": "Evaluate your response using the evaluation guideline.", "Score": 6}
</Example>
<Your job>
Evaluate your response using the evaluation guideline then give yourself a score out of 9 for your response.
</Your job>
<Example>
{"Evaluate": "Evaluate your response using the evaluation guideline.", "Score": 6}
</Example>
</s>
<assistant>
<|assistant|>
{
"""
println("")
prompt_grading = prompt
@show prompt_grading
println("")
response = sendReceivePrompt(a, prompt, timeout=180a)
response = "{" * split(response, "}")[1] * "}"
@show response
@show jsonresponse = JSON3.read(response)
score = jsonresponse["Score"]
score = nothing
while true
response = sendReceivePrompt(a, prompt, timeout=180a)
try
response = "{" * split(response, "}")[1] * "}"
@show response
@show jsonresponse = JSON3.read(response)
score = jsonresponse["Score"]
catch
println("retry grading")
end
end
return score
end
@@ -1252,21 +1038,23 @@ function analyze(a)
shorttermMemory = dictToString(a.memory[:shortterm])
prompt =
"""
<|im_start|>system
You have access to the following tools:
askbox: 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.
Your work:
$shorttermMemory
You job is to do each of the following in detail to analize your work.
1. What happened?
2. List all relationships, each with cause and effect.
3. Look at each relationship, figure out why it behaved that way.
4. What could you do to improve the response?
<|im_end|>
<|im_start|>assistant
<|system|>
<you have access to the following tools>
askbox: 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.
</you have access to the following tools>
<your earlier work>
$shorttermMemory
</your earlier work>
<your job>
You job is to do each of the following in detail to analize your work.
1. What happened?
2. List all relationships, each with cause and effect.
3. Look at each relationship, figure out why it behaved that way.
4. What could you do to improve the response?
</your job>
</s>
<|assistant|>
"""
@@ -1311,18 +1099,21 @@ end
function selfReflext(a, analysis::T) where {T<:AbstractString}
prompt =
"""
<|im_start|>system
You have access to the following tools:
askbox: 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.
Your report:
$analysis
<|system|>
<You have access to the following tools>
askbox: 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.
</You have access to the following tools>
<your report>
$analysis
</your report>
<your job are>
1. Lesson: what lesson could you learn from your report?.
2. Context: what is the context this lesson could apply to?
</your job are>
</s>
<|assistant|>
Your job are:
1. Lesson: what lesson could you learn from your report?.
2. Context: what is the context this lesson could apply to?
<|im_end|>
"""
response = sendReceivePrompt(a, prompt, max_tokens=1024)
@@ -1441,25 +1232,29 @@ function extractinfo(a, text::T) where {T<:AbstractString}
# determine whether there are any important info in an input text
prompt =
"""
<|im_start|>system
User's message:
$text
Your job is determine whether there are important info in the user's message. Answer: {Yes/No/Not sure}
<|im_end|>
<|system|>
<user's message>
$text
</user's message>
<your job>
Determine whether there are important info in the user's message. Answer: {Yes/No/Not sure}
</your job>
</s>
Answer:
"""
response = sendReceivePrompt(a, prompt, temperature=0.0)
if occursin("Yes", response)
prompt =
"""
<|im_start|>system
User's message:
$text
Your job is to extract important info from the user's message into keys and values using this format: key=value,.
p.s.1 you can extract many key-value pairs.
<|im_end|>
<|system|>
<user's message>
$text
</user's message>
<your job>
Extract important info from the user's message into keys and values using this format: key=value,.
p.s.1 you can extract many key-value pairs.
</your job>
</s>
"""