This commit is contained in:
Your Name
2024-02-08 15:10:20 +07:00
parent 021c66c3c5
commit cb24340777
2 changed files with 75 additions and 90 deletions

View File

@@ -9,7 +9,7 @@ export agentReact, agentReflex,
using JSON3, DataStructures, Dates, UUIDs, HTTP, Random
using CommUtils, GeneralUtils
using ..type, ..utils
using ..type, ..utils, ..llmfunction
# ---------------------------------------------------------------------------- #
# pythoncall setting #
@@ -494,12 +494,16 @@ function selfAwareness(a::agentReflex)
chunkedtext = chunktext(response, headers[1:3])
println("")
_infomatch = chunkedtext["Info matching:"]
_infomatch = GeneralUtils.getStringBetweenCharacters(_infomatch, '{', '}', endCharLocation="next")
infomatch = copy(JSON3.read(_infomatch))
_infomatch = GeneralUtils.getStringBetweenCharacters(_infomatch, '{', '}', endCharLocation="end")
infomatch = GeneralUtils.JSON3read_stringKey(_infomatch)
# infomatch = copy(JSON3.read(_infomatch))
println("")
@show chunkedtext
println("")
@show infomatch
keywordMemoryUpdate!(a.memory[:keyword], infomatch)
response = "What I know about user:" * JSON3.write(a.memory[:keyword]) # * response
@@ -817,41 +821,6 @@ function actor_mistral_openorca(a::agentReflex, selfaware=nothing)
end
end
# 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
@@ -894,9 +863,9 @@ function actor_mistral_openorca(a::agentReflex, selfaware=nothing)
- Luxury level is high
</What I know about the user>
<|assistant|>
Thought: I still don't know what color the user like. I should ask the user.
Thought: Based on what I know about the user, I still don't know what color the user like. I should ask the user.
Act: askbox
Actinput: {\"askbox\": \"What color do you like?\"}
Actinput:
</|assistant|>
</Example 1>
</s>
@@ -1007,7 +976,6 @@ function actor_mistral_openorca(a::agentReflex, selfaware=nothing)
end
if check_1 && check_2 && check_3 && check_4 && check_5 && check_6 && check_7
#TODO paraphrase selfaware
break
end
# print all check_1 to check_6
@@ -1034,21 +1002,27 @@ function actor_mistral_openorca(a::agentReflex, selfaware=nothing)
"Check $latestTask:",]
headers = detectCharacters(response, headerToDetect)
chunkedtext = chunktext(response, headers)
chunkedtext["Act $latestTask:"] = 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)])
a.memory[:c] = chunkedtext
toolinput = act[Symbol(toolname)]
end
# # 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))
# println("")
# @show act
# chunkedtext["Actinput $latestTask:"] = JSON3.write(act[Symbol(toolname)])
# a.memory[:c] = chunkedtext
# toolinput = act[Symbol(toolname)]
# end
chunkedtext["Act $latestTask:"] = toolname
return (toolname=toolname, toolinput=toolinput, chunkedtext=chunkedtext, selfaware=selfaware)
end
@@ -1275,25 +1249,29 @@ function actor(a::agentReflex)
end
actorResult = actor_mistral_openorca(a, selfaware)
println("")
toolname, toolinput, chunkedtext, selfaware = actorResult
println("")
@show toolname
@show toolinput
println(typeof(toolinput))
println("")
addShortMem!(a.memory[:shortterm], chunkedtext)
println("")
if toolname == "askbox" # chat with user
msgToUser = toolinput
msgToUser = askbox(toolinput)
actorState = toolname
#WORKING add only a single Q1 to memory because LLM need to ask the user only 1 question at a time
latestTask = shortMemLatestTask(a.memory[:shortterm]) +1
chunkedtext["Actinput $latestTask:"] = msgToUser
addShortMem!(a.memory[:shortterm], chunkedtext)
break
elseif toolname == "finalanswer"
addShortMem!(a.memory[:shortterm], chunkedtext)
println(">>> already done")
actorState = "formulateFinalResponse"
break
else # function call
addShortMem!(a.memory[:shortterm], chunkedtext)
f = a.tools[toolname][:func]
toolresult = f(a, actorResult)
@show toolresult
@@ -1922,12 +1900,12 @@ end
""" Convert keyword memory into a string.
Arguments:
a, one of ChatAgent's agent.
keywordmemory, a dictionary of keyword memory.
Arguments\n
a : one of ChatAgent's agent.
keywordmemory : a dictionary of keyword memory.
Return:
a string of LLM readout from keyword memory
Return\n
result : a string of LLM readout from keyword memory
Example:
```jldoctest