diff --git a/src/interface.jl b/src/interface.jl index f945f8e..49388f9 100644 --- a/src/interface.jl +++ b/src/interface.jl @@ -1,6 +1,6 @@ module interface -# export +export addNewMessage using JSON3, DataStructures, Dates, UUIDs, HTTP, Random using GeneralUtils @@ -47,7 +47,7 @@ using ..type, ..util, ..llmfunction Return\n ----- - messageleft + message left Example\n ----- @@ -72,38 +72,25 @@ using ..type, ..util, ..llmfunction msgMeta, agentConfig, ) - julia> addNewMessage(a, "user", "hello") + julia> YiemAgent.addNewMessage(a, "user", "hello") ``` Signature\n ----- """ #WORKING function addNewMessage(a::T1, role::String, text::T2; - maximumMsg::Integer=20)::Integer where {T1<:agent, T2<:AbstractString} - if role ∉ a.availableRole # guard against typo + maximumMsg::Integer=20) where {T1<:agent, T2<:AbstractString} + if role ∉ ["system", "user", "assistant"] # guard against typo error("role is not in agent.availableRole $(@__LINE__)") end - # check whether user messages exceed limit - userMsg = 0 - for i in a.messages - if i[:role] == "user" - userMsg += 1 - end - end - messageleft = 0 - - if userMsg > maximumMsg # delete all conversation - clearMessage(a) - messageleft = maximumMsg + #TODO summarize the oldest 10 message + if length(a.chathistory) > maximumMsg + summarize(a.chathistory) else - userMsg += 1 d = Dict(:role=> role, :text=> text, :timestamp=> Dates.now()) - push!(a.messages, d) - messageleft = maximumMsg - userMsg + push!(a.chathistory, d) end - - return messageleft end diff --git a/src/type.jl b/src/type.jl index 8286035..9d11975 100644 --- a/src/type.jl +++ b/src/type.jl @@ -80,7 +80,7 @@ abstract type agent end # Ref: Chat prompt format https://huggingface.co/TheBloke/Llama-2-7B-Chat-GGML/discussions/3 # messages= [Dict(:role=>"system", :content=> "", :timestamp=> Dates.now()),] 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}( :userinfo => Dict{Symbol, Any}(), :retailerinfo => Dict{Symbol, Any}(), @@ -119,7 +119,7 @@ function sommelier( :text2text=>Dict( :mqtttopic=> "txt2text/api/v1/prompt/gpu", ), - ), + ) ; name::String="Assistant", id::String=string(uuid4()), @@ -131,29 +131,8 @@ function sommelier( :output => "" , :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. @@ -166,23 +145,38 @@ function sommelier( config = config, mqttClient = mqttClient, msgMeta = msgMeta, - maxUserMsg = maxUserMsg, + maxHistoryMsg = maxHistoryMsg, tools = tools, attemptlimit = 5, attemptcount = 0, ) - return newAgent end -function initAgentMemory(a::T) where {T<:agent} - a.chathistory = Dict{String,Any}() - a.mctstree = Dict{Symbol, Any}() - a.plan[:activeplan] = Dict{Symbol, Any}() - a.plan[:currenttrajectory] = Dict{Symbol, Any}() +# function initAgentMemory(a::T) where {T<:agent} +# a.chathistory = Dict{String,Any}() +# a.mctstree = Dict{Symbol, Any}() +# a.plan[:activeplan] = 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 @@ -239,10 +233,6 @@ end - - - -