This commit is contained in:
2024-01-11 03:01:29 +00:00
parent 14db6ec6f1
commit e495a801f8
4 changed files with 332 additions and 306 deletions

View File

@@ -326,12 +326,12 @@ end
""" Determine from a user message whether an assistant need to use tools.
Arguments:
a, one of ChatAgent's agent.
Arguments:
a, one of ChatAgent's agent.
Return:
1. true/false # is LLM going to use tools
2. objective # what LLM going to do
Return:
1. true/false # is LLM going to use tools
2. objective # what LLM going to do
"""
function isUsePlans(a::agentReflex)
toollines = ""
@@ -443,22 +443,22 @@ end
""" Convert a vector of dict into 1-continous string.
Arguments:
vecofdict, a vector of dict
Arguments:
vecofdict, a vector of dict
Return:
1-continous string
Return:
1-continous string
# Example
```jldoctest
julia> using ChatAgent
julia> agent = ChatAgent.agentReflex("Jene")
julia> agent.messages = [Dict(:role=> "user", :content=> "Hi there."),
Dict(:role=> "assistant", :content=> "Hello! How can I assist you today?"),]
Example:
```jldoctest
julia> using ChatAgent
julia> agent = ChatAgent.agentReflex("Jene")
julia> agent.messages = [Dict(:role=> "user", :content=> "Hi there."),
Dict(:role=> "assistant", :content=> "Hello! How can I assist you today?"),]
julia> messagesToString(agent.messages)
"<|im_start|>user: Hi there.\n<|im_end|><|im_start|>assistant: Hello! How can I assist you today?\n<|im_end|>"
```
julia> messagesToString(agent.messages)
"<|im_start|>user: Hi there.\n<|im_end|><|im_start|>assistant: Hello! How can I assist you today?\n<|im_end|>"
```
"""
function messagesToString(messages::AbstractVector{T}; addressAIas="assistant") where {T<:AbstractDict}
conversation = ""
@@ -523,22 +523,22 @@ end
""" Convert a vector of dict into 1-continous string.
Arguments:
vecofdict, a vector of dict
Arguments:
vecofdict, a vector of dict
Return:
1-continous string
Return:
1-continous string
# Example
```jldoctest
julia> using ChatAgent
julia> agent = ChatAgent.agentReflex("Jene")
julia> agent.messages = [Dict(:role=> "user", :content=> "Hi there."),
Dict(:role=> "assistant", :content=> "Hello! How can I assist you today?"),]
Example:
```jldoctest
julia> using ChatAgent
julia> agent = ChatAgent.agentReflex("Jene")
julia> agent.messages = [Dict(:role=> "user", :content=> "Hi there."),
Dict(:role=> "assistant", :content=> "Hello! How can I assist you today?"),]
julia> messagesToString(agent.messages)
"user: Hi there.\nassistant: Hello! How can I assist you today?\n"
```
julia> messagesToString(agent.messages)
"user: Hi there.\nassistant: Hello! How can I assist you today?\n"
```
"""
function messagesToString_nomark(messages::AbstractVector{T}; addressAIas="assistant") where {T<:AbstractDict}
conversation = ""
@@ -599,20 +599,20 @@ end
""" Remove trailing characters from text.
Arguments:
text, text you want to remove trailing characters
charTobeRemoved, a list of characters to be removed
Arguments:
text, text you want to remove trailing characters
charTobeRemoved, a list of characters to be removed
Return:
text with specified trailing characters removed
Return:
text with specified trailing characters removed
# Example
```jldoctest
julia> text = "Hello! How can I assist you today?\n\n "
julia> removelist = ['\n', ' ',]
julia> removeTrailingCharacters(text, charTobeRemoved=removelist)
"Hello! How can I assist you today?"
```
Example:
```jldoctest
julia> text = "Hello! How can I assist you today?\n\n "
julia> removelist = ['\n', ' ',]
julia> removeTrailingCharacters(text, charTobeRemoved=removelist)
"Hello! How can I assist you today?"
```
"""
function removeTrailingCharacters(text; charTobeRemoved::AbstractVector{T}=['\n', ' ',]) where {T<:Char}
nouse = 0
@@ -674,25 +674,25 @@ end
""" Add chunked text to a short term memory of a chat agent
Arguments:
shortMem = short memory of a chat agent,
chunkedtext = a dict contains text
Arguments:
shortMem = short memory of a chat agent,
chunkedtext = a dict contains text
Return: no return
Return: no return
# Example
```jldoctest
julia> chunkedtext = OrderedDict{String, String}(
"Thought 1:" => " I should always think about...",
"Act 1:" => " wikisearch",
"Actinput 1:" => " latest AMD GPU",)
julia> shortMem = OrderedDict{String, Any}()
julia> addShortMem!(shortMem, chunkedtext)
OrderedDict{String, Any} with 3 entries:
"Thought 1:" => " I should always think about..."
"Act 1:" => " wikisearch"
"Actinput 1:" => " latest AMD GPU"
```
Example:
```jldoctest
julia> chunkedtext = OrderedDict{String, String}(
"Thought 1:" => " I should always think about...",
"Act 1:" => " wikisearch",
"Actinput 1:" => " latest AMD GPU",)
julia> shortMem = OrderedDict{String, Any}()
julia> addShortMem!(shortMem, chunkedtext)
OrderedDict{String, Any} with 3 entries:
"Thought 1:" => " I should always think about..."
"Act 1:" => " wikisearch"
"Actinput 1:" => " latest AMD GPU"
```
"""
function addShortMem!(shortMem::OrderedDict{String, Any}, chunkedtext::T) where {T<:AbstractDict}
for (k, v) in chunkedtext
@@ -706,19 +706,19 @@ end
""" Split text using all keywords in a list. Start spliting from rightmost of the text.
Arguments:
text = a text you want to split
list = a list of keywords you want to split
Arguments:
text = a text you want to split
list = a list of keywords you want to split
Return:
a leftmost text after split
Return:
a leftmost text after split
# Example
```jldoctest
julia> text = "Consider the type of food, occasion and temperature at the serving location."
julia> list = ["at", "and"]
"Consider the type of food, occasion "
```
Example:
```jldoctest
julia> text = "Consider the type of food, occasion and temperature at the serving location."
julia> list = ["at", "and"]
"Consider the type of food, occasion "
```
"""
function splittext(text, list)
newtext = text
@@ -757,20 +757,20 @@ end
""" Add step number to header in a text
Arguments:
text = a text you want to split
headers = a list of keywords you want to add step and substep to
step = a number you want to add
Arguments:
text = a text you want to split
headers = a list of keywords you want to add step and substep to
step = a number you want to add
Return:
a leftmost text after split
Return:
a leftmost text after split
# Example
```jldoctest
julia> text = "Consider the type of food, occasion and temperature at the serving location."
julia> headers = ["Thought", "Act"]
Example:
```jldoctest
julia> text = "Consider the type of food, occasion and temperature at the serving location."
julia> headers = ["Thought", "Act"]
```
```
"""
function replaceHeaders(text::T, headers, step::Int) where {T<:AbstractString}
newtext = text
@@ -794,39 +794,39 @@ end
""" Remove headers of specific step from memory.
Arguments:
shortMemory = a short term memory of a ChatAgent's agent
skipHeaders = a list of keys in memory you want to skip
step = a step number you want to remove
Arguments:
shortMemory = a short term memory of a ChatAgent's agent
skipHeaders = a list of keys in memory you want to skip
step = a step number you want to remove
Return:
a short term memory
Return:
a short term memory
# Example
```jldoctest
julia> shortMemory = OrderedDict(
"user:" => "May I try this one?",
"Plan 1:" => "testing a small portion of icecream",
"Thought 1:" => "I like it.",
"Act 1:" => "chatbox",
"Actinput 1:" => "I get this one.",
"Plan 2:" => "I'm meeting my wife this afternoon",
"Thought 2:" => "I also want it for my wife",
"Act 2:" => "chatbox",
"Actinput 2:" => "I would like to get 2 more",
)
julia> skipHeaders = ["Plan"]
julia> step = 2
julia> removeHeaders(shortMemory, step, skipHeaders)
OrderedDict(
"user:" => "May I try this one?",
"Plan 1:" => "testing a small portion of icecream",
"Thought 1:" => "I like it.",
"Act 1:" => "chatbox",
"Actinput 1:" => "I get this one.",
"Plan 2:" => "I'm meeting my wife this afternoon",
)
```
Example:
```jldoctest
julia> shortMemory = OrderedDict(
"user:" => "May I try this one?",
"Plan 1:" => "testing a small portion of icecream",
"Thought 1:" => "I like it.",
"Act 1:" => "chatbox",
"Actinput 1:" => "I get this one.",
"Plan 2:" => "I'm meeting my wife this afternoon",
"Thought 2:" => "I also want it for my wife",
"Act 2:" => "chatbox",
"Actinput 2:" => "I would like to get 2 more",
)
julia> skipHeaders = ["Plan"]
julia> step = 2
julia> removeHeaders(shortMemory, step, skipHeaders)
OrderedDict(
"user:" => "May I try this one?",
"Plan 1:" => "testing a small portion of icecream",
"Thought 1:" => "I like it.",
"Act 1:" => "chatbox",
"Actinput 1:" => "I get this one.",
"Plan 2:" => "I'm meeting my wife this afternoon",
)
```
"""
function removeHeaders(shortMemory::OrderedDict, step::Int,
skipHeaders::Union{Array{String}, Array{Symbol}, Nothing}=nothing)
@@ -859,32 +859,32 @@ end
""" Keep only specified keys in a dictionary. All non-specified keys will be removed.
Arguments:
dict = a dictionary
keys = keys you want to keep in a dict
Arguments:
dict = a dictionary
keys = keys you want to keep in a dict
Return:
a dict with all non-specified keys removed
Return:
a dict with all non-specified keys removed
# Example
```jldoctest
julia> dict = OrderedDict(
"user:" => "May I try this one?",
"Plan 1:" => "testing a small portion of icecream",
"Thought 1:" => "I like it.",
"Act 1:" => "chatbox",
"Actinput 1:" => "I get this one.",
"Plan 2:" => "I'm meeting my wife this afternoon",
"Thought 2:" => "I also want it for my wife",
"Act 2:" => "chatbox",
"Actinput 2:" => "I would like to get 2 more",
)
julia> keys = ["user:"]
julia> keepOnlyKeys(dict, keys)
OrderedDict(
"user:" => "May I try this one?",
)
```
Example:
```jldoctest
julia> dict = OrderedDict(
"user:" => "May I try this one?",
"Plan 1:" => "testing a small portion of icecream",
"Thought 1:" => "I like it.",
"Act 1:" => "chatbox",
"Actinput 1:" => "I get this one.",
"Plan 2:" => "I'm meeting my wife this afternoon",
"Thought 2:" => "I also want it for my wife",
"Act 2:" => "chatbox",
"Actinput 2:" => "I would like to get 2 more",
)
julia> keys = ["user:"]
julia> keepOnlyKeys(dict, keys)
OrderedDict(
"user:" => "May I try this one?",
)
```
"""
function keepOnlyKeys(dict::T1, keys::T2) where {T1<:AbstractDict, T2<:AbstractVector}
newdict = similar(dict)
@@ -899,20 +899,20 @@ end
""" Convert experience dict into 1 string for LLM to use.
Arguments:
dict = a dictionary contain past experience
Arguments:
dict = a dictionary contain past experience
Return:
An experience in 1 string without context keys.
Return:
An experience in 1 string without context keys.
# Example
```jldoctest
julia> dict = OrderedDict{String, Any}(
" This lesson can be applied to various situations => " Gathering accurate and relevant information about the user's preferences, budget, and event details is crucial for providing personalized recommendations.\n"
)
julia> experience(dict)
Example:
```jldoctest
julia> dict = OrderedDict{String, Any}(
" This lesson can be applied to various situations => " Gathering accurate and relevant information about the user's preferences, budget, and event details is crucial for providing personalized recommendations.\n"
)
julia> experience(dict)
```
```
"""
function experience(dict::T) where {T<:AbstractDict}
s = ""
@@ -925,18 +925,17 @@ end
""" Get the latest step number of short term memory
Arguments:
dict = a dictionary contain past experience
Arguments:
dict = a dictionary contain past experience
Return:
latest step number
Return:
latest step number
# Example
```jldoctest
julia> dict = OrderedDict(
"Plan 1:" => "1. Ask about the type of food that will be served at the wedding party.")
julia>shortMemLatestTask(dict)
1
Example:
```jldoctest
julia> dict = OrderedDict(
"Plan 1:" => "1. Ask about the type of food that will be served at the wedding party.")
julia>shortMemLatestTask(dict)
"""
function shortMemLatestTask(dict::T) where {T<:AbstractDict}
_latest_step = keys(dict)