This commit is contained in:
narawat lamaiin
2024-04-17 07:49:02 +07:00
parent 673c64b223
commit bcd525a7d9
3 changed files with 271 additions and 91 deletions

View File

@@ -1,6 +1,6 @@
module interface
export addNewMessage
export addNewMessage, conversation
using JSON3, DataStructures, Dates, UUIDs, HTTP, Random
using GeneralUtils
@@ -33,64 +33,89 @@ using ..type, ..util, ..llmfunction
# ---------------------------------------------- 100 --------------------------------------------- #
""" Add new message to agent.
""" Chat with llm.
Arguments\n
-----
a::agent
an agent
role::String
message sender role i.e. system, user or assistant
text::String
message text
Return\n
-----
nothing
None
Example\n
-----
```jldoctest
julia> using YiemAgent, MQTTClient, GeneralUtils
julia> client, connection = MakeConnection("test.mosquitto.org", 1883)
julia> connect(client, connection)
julia> msgMeta = GeneralUtils.generate_msgMeta("testtopic")
julia> agentConfig = Dict(
:receiveprompt=>Dict(
:mqtttopic=> "testtopic/receive",
),
:receiveinternal=>Dict(
:mqtttopic=> "testtopic/internal",
),
:text2text=>Dict(
:mqtttopic=> "testtopic/text2text",
),
)
julia> a = YiemAgent.sommelier(
client,
msgMeta,
agentConfig,
)
julia> YiemAgent.addNewMessage(a, "user", "hello")
julia> using JSON3, UUIDs, Dates, FileIO, MQTTClient, ChatAgent
julia> const mqttBroker = "mqtt.yiem.cc"
julia> mqttclient, connection = MakeConnection(mqttBroker, 1883)
julia> tools=Dict( # update input format
"askbox"=>Dict(
:description => "<askbox tool description>Useful for when you need to ask the user for more context. Do not ask the user their own question.</askbox tool description>",
:input => "<input>Input is a text in JSON format.</input><input example>{\"Q1\": \"How are you doing?\", \"Q2\": \"How may I help you?\"}</input example>",
:output => "" ,
:func => nothing,
),
)
julia> msgMeta = Dict(
:msgPurpose=> "updateStatus",
:from=> "agent",
:to=> "llmAI",
:requestresponse=> "request",
:sendto=> "", # destination topic
:replyTo=> "agent/api/v0.1.0/txt/response", # requester ask responseer to send reply to this topic
:repondToMsgId=> "", # responseer is responseing to this msg id
:taskstatus=> "", # "complete", "fail", "waiting" or other status
:timestamp=> Dates.now(),
:msgId=> "$(uuid4())",
)
julia> a = ChatAgent.agentReflex(
"Jene",
mqttclient,
msgMeta,
agentConfigTopic, # I need a function to send msg to config topic to get load balancer
role=:sommelier,
tools=tools
)
julia> newAgent = ChatAgent.agentReact(agent)
julia> response = ChatAgent.conversation(newAgent, "Hi! how are you?")
```
Signature\n
-----
"""
function addNewMessage(a::T1, role::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__)")
function conversation(a::agentReflex, usermsg::String; attemptlimit::Int=3)
a.attemptlimit = attemptlimit
workstate = nothing
response = nothing
_ = addNewMessage(a, "user", usermsg)
isuseplan = isUsePlans(a)
# newinfo = extractinfo(a, usermsg)
# a.env = newinfo !== nothing ? updateEnvState(a, newinfo) : a.env
@show isuseplan
if isuseplan # use plan before responding
if haskey(a.memory[:shortterm], "User:") == false #TODO should change role if user want to buy wine.
a.memory[:shortterm]["User:"] = usermsg
end
workstate, response = work(a)
end
#TODO summarize the oldest 10 message
if length(a.chathistory) > maximumMsg
summarize(a.chathistory)
# if LLM using askbox, use returning msg form askbox as conversation response
if workstate == "askbox" || workstate == "formulatedUserResponse"
#TODO paraphrase msg so that it is human friendlier word.
else
d = Dict(:role=> role, :text=> text, :timestamp=> Dates.now())
push!(a.chathistory, d)
response = chat_mistral_openorca(a)
response = split(response, "\n\n")[1]
response = split(response, "\n\n")[1]
end
response = removeTrailingCharacters(response)
_ = addNewMessage(a, "assistant", response)
return response
end
@@ -100,4 +125,53 @@ end
end # module interface