From 380528ba3b743bb3e93e7c72ba1db6260bfd1e93 Mon Sep 17 00:00:00 2001 From: tonaerospace Date: Mon, 5 Feb 2024 10:10:52 +0000 Subject: [PATCH] update --- src/interface.jl | 434 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 335 insertions(+), 99 deletions(-) diff --git a/src/interface.jl b/src/interface.jl index 1db7457..aebe693 100755 --- a/src/interface.jl +++ b/src/interface.jl @@ -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|> - # - # $aboutYourself - # $(a.roleSpecificInstruction[a.role]) - # - - # - # $(a.memory[:shortterm]["Plan 1:"]) - # - # - # 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 - # - # - # 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 - # - # - # <|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", "<|", " +# {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 = +# """ +# +# $(a.memory[:winestocksearchresult]) +# +# """ +# else +# winestocksearchresult = "\n" +# end + +# #WORKING I need a llm json read function + +# prompt = +# """ +# <|system|> +# +# $aboutYourself +# +# +# $toollines +# +# +# $(a.memory[:shortterm]["Plan 1:"]) +# +# +# $(JSON3.write(a.memory[:keyword])) +# +# +# 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 +# +# +# +# $(readKeywordMemory(a)) +# +# 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\"} +# +# +# <|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|>", "", "<|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, ""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|> + # + # $aboutYourself + # + # + # $toollines + # + # + # $(a.memory[:shortterm]["Plan 1:"]) + # + # + # $(JSON3.write(a.memory[:keyword])) + # + # + # 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 + # + # + # + # $(readKeywordMemory(a)) + # + # 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\"} + # + # + # <|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 = """ + <|system|> - $aboutYourself + Your name is $(a.agentName) + $(a.roles[a.role]) - $toollines + $toollines - - $(a.memory[:shortterm]["Plan 1:"]) - - - $(JSON3.write(a.memory[:keyword])) - - 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 - + + + + I'll ask the user for car type, brand, price, color, financing method and luxury level before I'll give them any advice. + - $(readKeywordMemory(a)) + {\"car type\": "SUV",\"brand\":\"Lexus\",\"price\":\"20k dollar\",\"color\": null,\"financing method\": null, \"luxury level\":\"high\"} - 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\"} - + <|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?\"} + + + + $(a.memory[:shortterm]["Plan 1:"]) + + + $(JSON3.write(a.memory[:keyword])) + <|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 = """ - + <|system|> Your name is $(a.agentName)