This commit is contained in:
2023-12-16 03:58:46 +00:00
parent bd9ae8ad52
commit d202690286
3 changed files with 145 additions and 240 deletions

View File

@@ -4,7 +4,7 @@ export makeSummary, sendReceivePrompt, chunktext, extractStepFromPlan, checkTota
detectCharacters, findDetectedCharacter, extract_number, toolNameBeingCalled,
isUseTools, conversationSummary, checkReasonableness, replaceHeaders,
addShortMem!, splittext, dictToString, removeHeaders, keepOnlyKeys, experience,
messagesToString, removeTrailingCharacters
messagesToString, messagesToString_nomark, removeTrailingCharacters
using UUIDs, Dates, DataStructures
using CommUtils, GeneralUtils
@@ -624,6 +624,25 @@ function messagesToString(messages::AbstractVector{T}; addressAIas="assistant")
return conversation
end
""" Convert a vector of dict into 1-continous string.
Arguments:
vecofdict, a vector of dict
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?"),]
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 = ""
if length(messages)!= 0
@@ -648,6 +667,39 @@ function messagesToString_nomark(messages::AbstractVector{T}; addressAIas="assis
return conversation
end
function dictToString(shortMemory::T;
skiplist::Union{Array{String}, Array{Symbol}}=[""]) where {T<:AbstractDict}
s = ""
for (k, v) in shortMemory
if k skiplist
new_v = removeTrailingCharacters(v)
s1 = "$k $new_v\n"
s *= s1
end
end
return s
end
# function dictToString(dict::T;
# skiplist::Union{Array{String}, Array{Symbol}}=[]) where {T<:AbstractDict}
# s = ""
# for (k, v) in dict
# if k ∉ skiplist
# s1 = "$k $v"
# s *= s1
# # ensure a newline seperate each sentences
# if s[end] != "\n"
# s *= "\n"
# end
# end
# end
# return s
# end
""" Remove trailing characters from text.
Arguments:
@@ -840,65 +892,6 @@ function replaceHeaders(text::T, headers, step::Int) where {T<:AbstractString}
end
""" Convert short term memory into 1 continous string.
Arguments:
shortMemory = a short term memory of a ChatAgent's agent
skiplist = a list of keys in memory you want to skip
Return:
a short term memory in 1 countinuous string
# Example
```jldoctest
julia> shortMemory = OrderedDict(
"user:" => "Umm",
"Thought 1:" => "I like it.",
"Act 1:" => "chatbox",
"Actinput 1:" => "I get this one.",
)
julia> headers = ["user:"]
julia> dictToString(shortMemory, headers)
"Thought 1: I like it.\nAct 1: chatbox\nActinput 1: I get this one.\n"
```
"""
function shortMemoryToString(shortMemory::OrderedDict,
skiplist::Union{Array{String}, Array{Symbol}})
s = ""
for (k, v) in shortMemory
if k skiplist
s1 = "$k $v"
s *= s1
# ensure a newline seperate each sentences
if s[end] != "\n"
s *= "\n"
end
end
end
return s
end
function dictToString(dict::T,
skiplist::Union{Array{String}, Array{Symbol}}) where {T<:AbstractDict}
s = ""
for (k, v) in dict
if k skiplist
s1 = "$k $v"
s *= s1
# ensure a newline seperate each sentences
if s[end] != "\n"
s *= "\n"
end
end
end
return s
end
""" Remove headers of specific step from memory.
Arguments: