This commit is contained in:
2024-01-15 15:00:13 +00:00
parent 4052fe92b3
commit 0f59725ac4
2 changed files with 308 additions and 25 deletions

View File

@@ -278,6 +278,58 @@ function updatePlan(a::agentReflex)
end
# function selfAwareness(a::agentReflex)
# 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)
# aboutYourself =
# """
# Your name is $(a.agentName)
# $(a.roles[a.role])
# """
# prompt =
# """
# <|system|>
# <About yourself>
# $aboutYourself
# </About yourself>
# <Your work>
# $work
# </your work
# <Your plan>
# $(a.memory[:shortterm]["Plan 1:"])
# </Your plan>
# <Your job>
# Use the following format:
# What I know: based on observed results, repeat all the details of what you have been gathered.
# Current progress: focus on checking your progress against the plan.
# What I am missing: describe in detail what you are missing.
# </Your job>
# </s>
# <|assistant|>
# What I know:
# """
# response = sendReceivePrompt(a, prompt, max_tokens=1024, temperature=0.4, timeout=180,
# stopword=["/n/n", "END", "End", "Obs", "<|", "</"])
# @show response
# return response
# end
function selfAwareness(a::agentReflex)
getonlykeys = ["Actinput", "Obs"]
worknoplan = similar(a.memory[:shortterm])
@@ -314,16 +366,15 @@ function selfAwareness(a::agentReflex)
$(a.memory[:shortterm]["Plan 1:"])
</Your plan>
<Your job>
Use the following format:
What I know: based on observed results, repeat all the details of what you have been gathered.
Current progress: check your progress against the plan.
What I am missing: describe in detail what you are missing.
Use the following format strictly:
What do I know: based on observed results, repeat all the details of what you have been gathered.
What am I missing: based on observed results, describe in detail what you are still missing compared to your plan.
</Your job>
</s>
<|assistant|>
What I know:
"""
response = sendReceivePrompt(a, prompt, max_tokens=1024, temperature=0.4, timeout=180,
response = sendReceivePrompt(a, prompt, max_tokens=1024, temperature=0.2, timeout=180,
stopword=["/n/n", "END", "End", "Obs", "<|", "</"])
@show response
@@ -395,7 +446,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: you should always think about what to do according to self-awareness (1. let's think a single step, 2. focus on what is missing, 3. pay attention to correct numeral calculation and commonsense.)
Thought: based on self-awareness, you should always prioritize what you are missing first. Then think about what to do next. (P.S. 1) let's think a single step. 2) pay attention to correct numeral calculation and commonsense.)
"
end
@@ -420,7 +471,7 @@ function actor_mistral_openorca(a::agentReflex, selfaware=nothing)
<Your job>
Use the following format:
$thought
Act: an action should aligned with your thought, must be one of [{toolnames}].
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>
@@ -557,6 +608,233 @@ function actor_mistral_openorca(a::agentReflex, selfaware=nothing)
return toolname, toolinput, chunkedtext
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
"""
@@ -1074,25 +1352,30 @@ function formulateUserresponse(a)
prompt =
"""
<|system|>
Symbol:
Plan: a plan
Thought: your thought
Act: the action you took
Actinput: the input to the action
Obs: the result of the action
<symbol meaning>
Plan: a plan
Thought: your thought
Act: the action you took
Actinput: the input to the action
Obs: the result of the action
</symbol meaning>
Your talk with the user:
$conversation
<your talk with the user>
$conversation
</your talk with the user>
Your work:
$work
<your earlier work>
$work
</your earlier work>
From your talk with the user and your work, formulate a response for the user by comparing and explaining your rational behind each choice in details.
<your job>
Based on your talk with the user and your work, present a response that compares and justifies each option in great detail.
</your job>
</s>
<|assistant|>
Response:
Recommendation:
"""
response = sendReceivePrompt(a, prompt)
response = sendReceivePrompt(a, prompt, max_tokens=1024, timeout=300)
return response
end

View File

@@ -151,16 +151,16 @@ function winestock(a::agentReflex, query::Dict)
acidity = 5, high acidity
</You have the following conversion table>
<Your job>
Consult the conversion table then write a specific SQL command from a JSON-format query.
List of keywords not allowed in SQL: ["BETWEEN", "--"]
Consult the conversion table then write a specific SQL command using only available info from a JSON-format query.
List of keywords not allowed in SQL: ["BETWEEN", "--", "WHEN]
</Your job>
<Example 1>
query: {\"wine type\": \"white\", \"wine characteristics\": \"full-bodied | off-dry | low acidity | medium tannin\", \"price\": {\"max\": \"50\"}}
assistant: SELECT * FROM White WHERE intensity = 5 AND sweetness = 2 AND acidity = 1 AND tannin = 3 AND price <= 50;
</Example 1>
<Example 2>
query: {\"wine characteristics\":\"low-bodied | semi-sweet | low tannin\",\"price\":\"22 USD\",\"occasion\":\"anniversary\",\"wine type\":\"Rose\",\"food\":\"American dishes\"}
assistant: SELECT * FROM Rose WHERE intensity = 1 AND sweetness = 3 AND tannin = 1 AND price <= 22;
query: {\"wine characteristics\":\"low-bodied | semi-sweet | low-medium tannin\",\"price\":\"22 USD\",\"occasion\":\"anniversary\",\"wine type\":\"Rose\",\"food\":\"American dishes\"}
assistant: SELECT * FROM Rose WHERE intensity = 1 AND sweetness = 3 AND (tannin = 2 OR tannin = 3) AND price <= 22;
</Example 2>
</s>
<|query|>
@@ -183,7 +183,7 @@ function winestock(a::agentReflex, query::Dict)
end
end
println("")
@show db_sql = _sql
@show db_sql = replace(_sql, '\n'=>'')
# remove any blank character in front of a string
newsql = nothing