This commit is contained in:
narawat lamaiin
2024-04-15 20:44:20 +07:00
parent 5d95cfbb16
commit 4cb2353213
2 changed files with 34 additions and 57 deletions

View File

@@ -1,6 +1,6 @@
module interface module interface
# export export addNewMessage
using JSON3, DataStructures, Dates, UUIDs, HTTP, Random using JSON3, DataStructures, Dates, UUIDs, HTTP, Random
using GeneralUtils using GeneralUtils
@@ -47,7 +47,7 @@ using ..type, ..util, ..llmfunction
Return\n Return\n
----- -----
messageleft message left
Example\n Example\n
----- -----
@@ -72,38 +72,25 @@ using ..type, ..util, ..llmfunction
msgMeta, msgMeta,
agentConfig, agentConfig,
) )
julia> addNewMessage(a, "user", "hello") julia> YiemAgent.addNewMessage(a, "user", "hello")
``` ```
Signature\n Signature\n
----- -----
""" #WORKING """ #WORKING
function addNewMessage(a::T1, role::String, text::T2; function addNewMessage(a::T1, role::String, text::T2;
maximumMsg::Integer=20)::Integer where {T1<:agent, T2<:AbstractString} maximumMsg::Integer=20) where {T1<:agent, T2<:AbstractString}
if role a.availableRole # guard against typo if role ["system", "user", "assistant"] # guard against typo
error("role is not in agent.availableRole $(@__LINE__)") error("role is not in agent.availableRole $(@__LINE__)")
end end
# check whether user messages exceed limit #TODO summarize the oldest 10 message
userMsg = 0 if length(a.chathistory) > maximumMsg
for i in a.messages summarize(a.chathistory)
if i[:role] == "user"
userMsg += 1
end
end
messageleft = 0
if userMsg > maximumMsg # delete all conversation
clearMessage(a)
messageleft = maximumMsg
else else
userMsg += 1
d = Dict(:role=> role, :text=> text, :timestamp=> Dates.now()) d = Dict(:role=> role, :text=> text, :timestamp=> Dates.now())
push!(a.messages, d) push!(a.chathistory, d)
messageleft = maximumMsg - userMsg
end end
return messageleft
end end

View File

@@ -80,7 +80,7 @@ abstract type agent end
# Ref: Chat prompt format https://huggingface.co/TheBloke/Llama-2-7B-Chat-GGML/discussions/3 # Ref: Chat prompt format https://huggingface.co/TheBloke/Llama-2-7B-Chat-GGML/discussions/3
# messages= [Dict(:role=>"system", :content=> "", :timestamp=> Dates.now()),] # messages= [Dict(:role=>"system", :content=> "", :timestamp=> Dates.now()),]
chathistory = Vector{Dict{Symbol, Any}}() # store messages history in the format :name=>"message" chathistory = Vector{Dict{Symbol, Any}}() # store messages history in the format :name=>"message"
maxUserMsg::Int = 10 # 31th and earlier messages will get summarized maxHistoryMsg::Int = 20 # 31th and earlier messages will get summarized
keywordinfo = Dict{Symbol, Any}( keywordinfo = Dict{Symbol, Any}(
:userinfo => Dict{Symbol, Any}(), :userinfo => Dict{Symbol, Any}(),
:retailerinfo => Dict{Symbol, Any}(), :retailerinfo => Dict{Symbol, Any}(),
@@ -119,7 +119,7 @@ function sommelier(
:text2text=>Dict( :text2text=>Dict(
:mqtttopic=> "txt2text/api/v1/prompt/gpu", :mqtttopic=> "txt2text/api/v1/prompt/gpu",
), ),
), )
; ;
name::String="Assistant", name::String="Assistant",
id::String=string(uuid4()), id::String=string(uuid4()),
@@ -131,29 +131,8 @@ function sommelier(
:output => "" , :output => "" ,
:func => nothing, :func => nothing,
), ),
# :wikisearch=>Dict(
# :name => "wikisearch",
# :description => "Useful for when you need to search an encyclopedia",
# :input => "Input is keywords and not a question.",
# :output => "",
# :func => wikisearch, # put function here
# ),
# :wineStock=>Dict(
# :name => "wineStock",
# :description => "useful for when you need to search for wine by your description, price, name or ID.",
# :input => "Input should be a search query with as much details as possible.",
# :output => "" ,
# :func => nothing,
# ),
# :NTHING=>Dict(
# :name => "NTHING",
# :description => "useful for when you don't need to use tools or actions",
# :input => "No input is needed",
# :output => "" ,
# :func => nothing,
# ),
), ),
maxUserMsg::Int=20, maxHistoryMsg::Int=20,
) )
#NEXTVERSION publish to a.config[:configtopic] to get a config. #NEXTVERSION publish to a.config[:configtopic] to get a config.
@@ -166,23 +145,38 @@ function sommelier(
config = config, config = config,
mqttClient = mqttClient, mqttClient = mqttClient,
msgMeta = msgMeta, msgMeta = msgMeta,
maxUserMsg = maxUserMsg, maxHistoryMsg = maxHistoryMsg,
tools = tools, tools = tools,
attemptlimit = 5, attemptlimit = 5,
attemptcount = 0, attemptcount = 0,
) )
return newAgent return newAgent
end end
function initAgentMemory(a::T) where {T<:agent} # function initAgentMemory(a::T) where {T<:agent}
a.chathistory = Dict{String,Any}() # a.chathistory = Dict{String,Any}()
a.mctstree = Dict{Symbol, Any}() # a.mctstree = Dict{Symbol, Any}()
a.plan[:activeplan] = Dict{Symbol, Any}() # a.plan[:activeplan] = Dict{Symbol, Any}()
a.plan[:currenttrajectory] = Dict{Symbol, Any}() # a.plan[:currenttrajectory] = Dict{Symbol, Any}()
# end
function clearMessage(a::T) where {T<:agent}
for i in eachindex(a.messages)
if length(a.messages) > 0
pop!(a.messages)
else
break
end
end
a.memory = newAgentMemory()
# @show a.messages
# @show a.memory
end end
@@ -239,10 +233,6 @@ end