This commit is contained in:
2024-02-05 10:10:52 +00:00
parent 3a7f277372
commit 380528ba3b

View File

@@ -411,7 +411,6 @@ end
# end
function selfAwareness(a::agentReflex)
latestTask = shortMemLatestTask(a.memory[:shortterm])
getonlykeys = ["Actinput", "Obs"]
worknoplan = similar(a.memory[:shortterm])
@@ -483,7 +482,6 @@ function selfAwareness(a::agentReflex)
println("")
@show selfaware_1 = response
#WORKING
headerToDetect = ["Info extraction:", "Info mapping:", "Info matching:", "Actinput"]
headers = detectCharacters(response, headerToDetect)
@@ -497,39 +495,7 @@ function selfAwareness(a::agentReflex)
println("")
@show chunkedtext
keywordMemoryUpdate!(a.memory[:keyword], infomatch)
# prompt =
# """
# <|system|>
# <About yourself>
# $aboutYourself
# $(a.roleSpecificInstruction[a.role])
# </About yourself>
# <Your plan>
# $(a.memory[:shortterm]["Plan 1:"])
# </Your plan>
# <Your job>
# Use the following format strictly:
# What I know: what you know in JSON format
# What am I missing: based on what I know, list all items I am missing
# P.S. do not mention any toolnames
# </Your job>
# <Example>
# What I know: {"car type": "SUV", "engine type": "electric motor", "color": null, "financing": null}
# What am I missing:
# - The user's preferred color
# - The user's financing method
# </Example>
# </s>
# <|assistant|>
# What I know: $(JSON3.write(a.memory[:keyword]))
# """
# response = sendReceivePrompt(a, prompt, max_tokens=1024, temperature=0.4, timeout=180,
# stopword=["/n/n", "END", "End", "Obs", "<|", "</"])
# response = split(response, "<|")[1]
# response = split(response, "</")[1]
response = "What I know about user:" * JSON3.write(a.memory[:keyword]) # * response
println("")
@@ -539,51 +505,269 @@ function selfAwareness(a::agentReflex)
return response
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:
# "
# <|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.)"
# startword = "Thought:"
# if selfaware !== nothing
# "
# Thought: based on what you know, you should focus on what you need to improve first then follow your plan to decide what to do next. (P.S. 1) let's think a single step. 2) pay attention to correct numeral calculation and commonsense.)
# "
# end
# # your should request the missing information first before making a decision
# aboutYourself =
# """
# Your name is $(a.agentName)
# $(a.roles[a.role])
# """
# winestocksearchresult = nothing
# if haskey(a.memory, :winestocksearchresult) && a.memory[:winestockResult] !== nothing
# winestocksearchresult =
# """
# <winestock search result>
# $(a.memory[:winestocksearchresult])
# </winestock search result>
# """
# else
# winestocksearchresult = "\n"
# end
# #WORKING I need a llm json read function
# 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>
# <What I know about the user>
# $(JSON3.write(a.memory[:keyword]))
# </What I know about the user>
# <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>
# <What I know about the user>
# $(readKeywordMemory(a))
# </What I know about the user>
# Thought: based on what you know, I think he also need to know whether there are any charging station near by his house. I should search the internet to get this info.
# Act: internetsearch
# Actinput: {\"internetsearch\": \"EV charging station near Bangkok\"}
# </Example>
# </s>
# <|assistant|>
# $startword
# """
# prompt = replace(prompt, "{toolnames}" => toolnames)
# println("")
# @show actor_prompt = prompt
# response = nothing
# chunkedtext = nothing
# latestTask = nothing
# while true # while Thought or Act is empty, run actor again
# response = sendReceivePrompt(a, prompt, max_tokens=1024, temperature=0.4, timeout=300,
# stopword=["Thought:", "Obs:", "<|system|>", "</s>", "<|end|>"],
# seed=rand(1000000:2000000))
# println("")
# @show actor_raw = response
# response = splittext(response, ["/n/n", "END", "End","obs", "Obs", "<|im_end|>"])
# response = split(response, "<|")[1]
# response = split(response, "</")[1]
# # response = split(response, "Thought:")[end]
# latestTask = shortMemLatestTask(a.memory[:shortterm]) +1
# response = startword * response
# headerToDetect = ["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]
# # 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:")
# println("")
# @show actor_response = response
# 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 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 = 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
# #TODO paraphrase selfaware
# break
# end
# @show retrying_actor = response
# 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)
# 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=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:
"
<|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 = ""
@@ -623,39 +807,91 @@ function actor_mistral_openorca(a::agentReflex, selfaware=nothing)
#WORKING I need a llm json read function
# 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>
# <What I know about the user>
# $(JSON3.write(a.memory[:keyword]))
# </What I know about the user>
# <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>
# <What I know about the user>
# $(readKeywordMemory(a))
# </What I know about the user>
# Thought: based on what you know, I think he also need to know whether there are any charging station near by his house. I should search the internet to get this info.
# Act: internetsearch
# Actinput: {\"internetsearch\": \"EV charging station near Bangkok\"}
# </Example>
# </s>
# <|assistant|>
# $startword
# """
"""
- Car type is SUV
- Brand is Lexus
- Price is 20k dollar
- No info on the car color yet
- No info on the financing method yet
- Luxury level is high
"""
prompt =
"""
<s>
<|system|>
<About yourself>
$aboutYourself
Your name is $(a.agentName)
$(a.roles[a.role])
</About yourself>
<You have access to the following tools>
$toollines
$toollines
</You have access to the following tools>
<Your plan>
$(a.memory[:shortterm]["Plan 1:"])
</Your plan>
<What I know about the user>
$(JSON3.write(a.memory[:keyword]))
</What I know about the user>
<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
Use the following format:
Thought: considering what you know about the user and your plan, you should think about what to do next by prioritize what you don't know first. (P.S. 1) let's think a single step. 2) pay attention to correct numeral calculation and commonsense.)
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>
</|system|>
<Example 1>
<my plan>
I'll ask the user for car type, brand, price, color, financing method and luxury level before I'll give them any advice.
</my plan>
<What I know about the user>
$(readKeywordMemory(a))
{\"car type\": "SUV",\"brand\":\"Lexus\",\"price\":\"20k dollar\",\"color\": null,\"financing method\": null, \"luxury level\":\"high\"}
</What I know about the user>
Thought: based on what you know, I think he also need to know whether there are any charging station near by his house. I should search the internet to get this info.
Act: internetsearch
Actinput: {\"internetsearch\": \"EV charging station near Bangkok\"}
</Example>
<|assistant|>
Thought: I still don't know what color the user like. I should ask the user.
Act: askbox
Actinput: {\"askbox\": \"What color do you like?\"}
</|assistant|>
</Example 1>
</s>
<My plan>
$(a.memory[:shortterm]["Plan 1:"])
</My plan>
<What I know about the user>
$(JSON3.write(a.memory[:keyword]))
</What I know about the user>
<|assistant|>
$startword
Thought:
"""
prompt = replace(prompt, "{toolnames}" => toolnames)
@@ -1733,7 +1969,7 @@ function readKeywordMemory(a; keywordmemory::Union{AbstractDict, Nothing}=nothin
jsonstr = JSON3.write(tempdict)
prompt =
"""
<s>
<s>
<|system|>
<About yourself>
Your name is $(a.agentName)