Files
YiemAgent/src/type.jl
narawat lamaiin e8c13015ea update
2024-04-19 11:51:56 +07:00

228 lines
5.1 KiB
Julia

module type
export agent, sommelier
using Dates, UUIDs, DataStructures, JSON3
using GeneralUtils
# ---------------------------------------------- 100 --------------------------------------------- #
abstract type agent end
""" A sommelier agent.
Arguments\n
-----
mqttClient::Client
MQTTClient's client
msgMeta::Dict{Symbol, Any}
A dict contain info about a message.
config::Dict{Symbol, Any}
Config info for an agent. Contain mqtt topic for internal use and other info.
Keyword Arguments\n
-----
name::String
Agent's name
id::String
Agent's ID
tools::Dict{Symbol, Any}
Agent's tools
maxHistoryMsg::Integer
max history message
Return\n
-----
nothing
Example:
```jldoctest
julia> using YiemAgent, MQTTClient, GeneralUtils
julia> msgMeta = GeneralUtils.generate_msgMeta(
"N/A",
replyTopic = "/testtopic/prompt"
)
julia> tools= Dict(
:chatbox=>Dict(
:name => "chatbox",
:description => "Useful only for when you need to ask the user for more info or context. Do not ask the user their own question.",
:input => "Input should be a text.",
:output => "" ,
:func => nothing,
),
)
julia> agentConfig = Dict(
:receiveprompt=>Dict(
:mqtttopic=> "/testtopic/prompt", # topic to receive prompt i.e. frontend send msg to this topic
),
:receiveinternal=>Dict(
:mqtttopic=> "/testtopic/internal", # receive topic for model's internal
),
:text2text=>Dict(
:mqtttopic=> "/text2text/receive",
),
)
julia> client, connection = MakeConnection("test.mosquitto.org", 1883)
julia> agent = YiemAgent.bsommelier(
client,
msgMeta,
agentConfig,
name= "assistant",
id= "555", # agent instance id
tools=tools,
)
```
Signature\n
-----
""" #[] update docstring
@kwdef mutable struct sommelier <: agent
name::String
id::String
config::Dict
tools::Dict
thinkinglimit::Integer # thinking round limit
thinkingcount::Integer # used to count attempted round of a task
# memory
# 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}} = Vector{Dict{Symbol, Any}}() # store messages history in the format :name=>"message"
maxHistoryMsg::Integer # 31th and earlier messages will get summarized
keywordinfo::Dict{Symbol, Any} = Dict{Symbol, Any}(
:userinfo => Dict{Symbol, Any}(),
:retailerinfo => Dict{Symbol, Any}(),
)
mctstree::Dict{Symbol, Any} = Dict{Symbol, Any}()
# 1-historyPoint is in Dict{Symbol, Any} and compose of:
# state, statevalue, thought, action, observation
plan::Dict{Symbol, Any} = Dict{Symbol, Any}(
# store 3 to 5 best plan AI frequently used to avoid having to search MCTS all the time
# each plan is in [historyPoint_1, historyPoint_2, ...] format
:existingplan => Vector(),
:activeplan => Vector{Dict{Symbol, Any}}(), # current using plan
:currenttrajectory=> Vector{Dict{Symbol, Any}}(), # store
)
# communication
msgMeta::Dict{Symbol, Any} # a template for msgMeta
# put incoming message here. waiting for further processing
receiveUserMsgChannel::Channel{Dict} = Channel{Dict}(8) # for incoming user communication
receiveInternalMsgChannel::Channel{Dict} = Channel{Dict}(8) # for internal communication
end
function sommelier(
receiveUserMsgChannel::Channel,
receiveInternalMsgChannel::Channel,
msgMeta::Dict= GeneralUtils.generate_msgMeta("N/A"),
config::Dict = Dict(
:frontend=> Dict(
:mqtttopic=> nothing
),
:internal=> Dict(
:mqtttopic=> nothing
),
:text2text=> Dict(
:mqtttopic=> "txt2text/api/v1/prompt/gpu",
),
)
;
name::String= "Assistant",
id::String= string(uuid4()),
tools::Dict= Dict(
:chatbox=> Dict(
:name => "chatbox",
:description => "Useful for when you need to communicate with the user.",
:input => "Input should be a conversation to the user.",
:output => "" ,
:func => nothing,
),
),
maxHistoryMsg::Integer= 20,
thinkinglimit::Integer= 5,
thinkingcount::Integer= 0,
)
#[NEXTVERSION] publish to a.config[:configtopic] to get a config.
#[NEXTVERSION] get a config message in a.mqttMsg_internal
#[NEXTVERSION] set agent according to config
newAgent = sommelier(
receiveUserMsgChannel= receiveUserMsgChannel,
receiveInternalMsgChannel= receiveInternalMsgChannel,
name= name,
id= id,
config= config,
msgMeta= msgMeta,
maxHistoryMsg= maxHistoryMsg,
tools= tools,
thinkinglimit= thinkinglimit,
thinkingcount= thinkingcount,
)
return newAgent
end
end # module type