update
This commit is contained in:
103
src/util.jl
103
src/util.jl
@@ -1,6 +1,6 @@
|
||||
module util
|
||||
|
||||
export clearhistory, addNewMessage
|
||||
export clearhistory, addNewMessage, formatLLMtext, formatLLMtext_llama3instruct
|
||||
|
||||
using UUIDs, Dates, DataStructures, HTTP, MQTTClient, JSON3
|
||||
using GeneralUtils
|
||||
@@ -101,22 +101,114 @@ end
|
||||
Signature\n
|
||||
-----
|
||||
"""
|
||||
function addNewMessage(a::T1, role::String, text::T2;
|
||||
function addNewMessage(a::T1, name::String, text::T2;
|
||||
maximumMsg::Integer=20) where {T1<:agent, T2<:AbstractString}
|
||||
if role ∉ ["system", "user", "assistant"] # guard against typo
|
||||
error("role is not in agent.availableRole $(@__LINE__)")
|
||||
if name ∉ ["system", "user", "assistant"] # guard against typo
|
||||
error("name is not in agent.availableRole $(@__LINE__)")
|
||||
end
|
||||
|
||||
#[] summarize the oldest 10 message
|
||||
if length(a.chathistory) > maximumMsg
|
||||
summarize(a.chathistory)
|
||||
else
|
||||
d = Dict(:role=> role, :text=> text, :timestamp=> Dates.now())
|
||||
d = Dict(:name=> name, :text=> text, :timestamp=> Dates.now())
|
||||
push!(a.chathistory, d)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
""" Convert a chat dictionary into LLM model instruct format.
|
||||
|
||||
Arguments\n
|
||||
-----
|
||||
name::T
|
||||
message owner name e.f. "system", "user" or "assistant"
|
||||
text::T
|
||||
|
||||
Return\n
|
||||
-----
|
||||
formattedtext::String
|
||||
text formatted to model format
|
||||
|
||||
Example\n
|
||||
-----
|
||||
```jldoctest
|
||||
julia> using Revise
|
||||
julia> d = Dict(:name=> "system",:text=> "You are a helpful, respectful and honest assistant.",)
|
||||
julia> formattedtext = formatLLMtext_llama3instruct(d[:name], d[:text])
|
||||
```
|
||||
|
||||
Signature\n
|
||||
-----
|
||||
"""
|
||||
function formatLLMtext_llama3instruct(name::T, text::T) where {T<:AbstractString}
|
||||
formattedtext =
|
||||
if name == "system"
|
||||
"""<|begin_of_text|>
|
||||
<|start_header_id|>$name<|end_header_id|>
|
||||
$text
|
||||
<|eot_id|>
|
||||
"""
|
||||
else
|
||||
"""
|
||||
<|start_header_id|>$name<|end_header_id|>
|
||||
$text
|
||||
<|eot_id|>
|
||||
"""
|
||||
end
|
||||
|
||||
return formattedtext
|
||||
end
|
||||
|
||||
|
||||
|
||||
""" Convert a chat messages in vector of dictionary into LLM model instruct format.
|
||||
|
||||
Arguments\n
|
||||
-----
|
||||
messages::Vector{Dict{Symbol, T}}
|
||||
message owner name e.f. "system", "user" or "assistant"
|
||||
formatname::T
|
||||
format name to be used
|
||||
|
||||
Return\n
|
||||
-----
|
||||
formattedtext::String
|
||||
text formatted to model format
|
||||
|
||||
Example\n
|
||||
-----
|
||||
```jldoctest
|
||||
julia> using Revise
|
||||
julia> chatmessage = [
|
||||
Dict(:name=> "system",:text=> "You are a helpful, respectful and honest assistant.",),
|
||||
Dict(:name=> "user",:text=> "list me all planets in our solar system.",),
|
||||
]
|
||||
julia> formattedtext = formatLLMtext(chatmessage, "llama3instruct")
|
||||
```
|
||||
|
||||
Signature\n
|
||||
-----
|
||||
"""
|
||||
function formatLLMtext(messages::Vector{Dict{Symbol, T}},
|
||||
formatname::String="llama3instruct") where {T<:Any}
|
||||
f = if formatname == "llama3instruct"
|
||||
formatLLMtext_llama3instruct
|
||||
elseif formatname == "mistral"
|
||||
# not define yet
|
||||
else
|
||||
error("$formatname template not define yet")
|
||||
end
|
||||
|
||||
str = ""
|
||||
for t in messages
|
||||
str *= f(t[:name], t[:text])
|
||||
end
|
||||
|
||||
return str
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -168,7 +260,6 @@ end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
end # module util
|
||||
Reference in New Issue
Block a user