This commit is contained in:
2024-01-15 08:00:04 +00:00
parent 745e9e85fc
commit 67b226e4e8
6 changed files with 151 additions and 96 deletions

View File

@@ -7,7 +7,7 @@ export agentReact, agentReflex,
formulateUserresponse, extractinfo, updateEnvState, chat_mistral_openorca,
recap
using JSON3, DataStructures, Dates, UUIDs, HTTP
using JSON3, DataStructures, Dates, UUIDs, HTTP, Random
using CommUtils, GeneralUtils
using ..type, ..utils
@@ -386,18 +386,6 @@ function actor_mistral_openorca(a::agentReflex, selfaware=nothing)
push!(toolslist, toolname)
end
# shorttermMemory = dictToString(a.memory[:shortterm], skiplist=["user:"])
# conversation = conversationSummary(a)
# println("")
# @show conversationSum = conversation
# context =
# """
# Your talk with the user:
# $conversation
# """
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
@@ -407,7 +395,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 incomplete task 3. pay attention to correct numeral calculation and commonsense.)
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.)
"
end
@@ -432,14 +420,14 @@ function actor_mistral_openorca(a::agentReflex, selfaware=nothing)
<Your job>
Use the following format:
$thought
Act: an action you intend to do based on your thought, must be one of [{toolnames}].
Act: an action should aligned with your thought, 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 2: {\"askbox\": \"Hello! Welcome to our wine store. I'd be happy to help you find a perfect bottle for your occasion. Could you please tell me about the special event or occasion for which you are buying this wine?\"}
Actinput: {\"askbox\": \"Hello! Welcome to our wine store.\"}
</Example>
</s>
@@ -457,20 +445,16 @@ function actor_mistral_openorca(a::agentReflex, selfaware=nothing)
latestTask = nothing
tempcounter = 0.2
seed = nothing
while true # while Thought or Act is empty, run actor again
tempcounter += 0.2
# tempcounter += 0.2
@show tempcounter
response = sendReceivePrompt(a, prompt, max_tokens=1024, temperature=tempcounter, timeout=180,
stopword=["Obs:", "<|system|>", "</s>"])
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
# if start == "Thought:"
# response = "Thought $latestTask: " * response
# else
# response = "Self-awareness $latestTask: " * response
# end
response = "Thought:" * response
@@ -499,34 +483,40 @@ function actor_mistral_openorca(a::agentReflex, selfaware=nothing)
chunkedtext = chunktext(response, headers)
# assuming length more than 10 character means LLM has valid thinking
@show iskey_Thought = haskey(chunkedtext, "Thought $latestTask:")
@show iskey_Act = haskey(chunkedtext, "Act $latestTask:")
@show iskey_Actinput = haskey(chunkedtext, "Actinput $latestTask:")
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
isJsonReadable = false
try
act = GeneralUtils.getStringBetweenCharacters(response, '{', '}', endCharLocation="end")
act = JSON3.read(act)
isJsonReadable = true
catch
end
if iskey_Thought && iskey_Act && iskey_Actinput
istoolnameValid = false
for i in toolslist
if occursin(i, chunkedtext["Act $latestTask:"])
istoolnameValid = true
break
end
end
# check_4 = false
# try
# act = GeneralUtils.getStringBetweenCharacters(response, '{', '}', endCharLocation="end")
# act = JSON3.read(act)
# check_4 = true
# catch
# end
if length(chunkedtext["Thought $latestTask:"]) > 5 && istoolnameValid &&
length(chunkedtext["Actinput $latestTask:"]) > 5 && isJsonReadable
# check for a valid toolname
check_4 = false
for i in toolslist
if occursin(i, chunkedtext["Act $latestTask:"])
check_4 = true
break
end
end
println("retry actor")
# 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)
@@ -537,7 +527,7 @@ function actor_mistral_openorca(a::agentReflex, selfaware=nothing)
"Answer:", "Conclusion:", "Summary:"]
response = replaceHeaders(response, headerToDetect, latestTask)
println("")
@show actor_response_1 = response
headerToDetect = ["Plan $(a.attempt):",
"Thought $latestTask:",
"Act $latestTask:",
@@ -547,20 +537,28 @@ function actor_mistral_openorca(a::agentReflex, selfaware=nothing)
headers = detectCharacters(response, headerToDetect)
chunkedtext = chunktext(response, headers)
chunkedtext = delete!(chunkedtext, "Self-awareness $latestTask")
act = GeneralUtils.getStringBetweenCharacters(response, '{', '}', endCharLocation="end")
println("")
@show actor_response_1 = act
act = copy(JSON3.read(act))
chunkedtext["Act $latestTask:"] = toolname
chunkedtext["Actinput $latestTask:"] = act[Symbol(toolname)]
toolinput = act[Symbol(toolname)]
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.
@@ -1089,7 +1087,7 @@ function formulateUserresponse(a)
Your work:
$work
From your talk with the user and your work, formulate a response for the user.
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.
</s>
<|assistant|>
response:
@@ -1098,6 +1096,35 @@ function formulateUserresponse(a)
return response
end
# function formulateUserresponse(a)
# conversation = messagesToString_nomark(a.messages, addressAIas="I")
# work = dictToString(a.memory[:shortterm])
# 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
# Your talk with the user:
# $conversation
# Your work:
# $work
# From your talk with the user and your work, formulate a response for the user .
# </s>
# <|assistant|>
# response:
# """
# response = sendReceivePrompt(a, prompt)
# return response
# end
""" Extract important info from text into key-value pair text.
@@ -1416,12 +1443,6 @@ end