This commit is contained in:
narawat lamaiin
2024-04-27 08:35:35 +07:00
parent aacae344c2
commit 4a41b0feb4
5 changed files with 121 additions and 123 deletions

View File

@@ -1,6 +1,6 @@
module interface module interface
export addNewMessage, conversation export addNewMessage, conversation, decisionMaker
using JSON3, DataStructures, Dates, UUIDs, HTTP, Random, MQTTClient using JSON3, DataStructures, Dates, UUIDs, HTTP, Random, MQTTClient
using GeneralUtils using GeneralUtils
@@ -60,7 +60,7 @@ using ..type, ..util, ..llmfunction, ..mcts
Signature\n Signature\n
----- -----
""" """
function decisionMaker(state::T) where {T<:AbstractDict} function decisionMaker(a::T1, state::T2) where {T1<:agent, T2<:AbstractDict}
customerinfo = customerinfo =
""" """
I will give you the following information about customer: I will give you the following information about customer:
@@ -73,8 +73,8 @@ function decisionMaker(state::T) where {T<:AbstractDict}
$(JSON3.write(state[:storeinfo])) $(JSON3.write(state[:storeinfo]))
""" """
#[x] #[WORKING]
prompt = _prompt =
""" """
You are a helpful sommelier working for a wine store. You are a helpful sommelier working for a wine store.
You helps users by searching wine that match the user preferences from your inventory. You helps users by searching wine that match the user preferences from your inventory.
@@ -84,9 +84,9 @@ function decisionMaker(state::T) where {T<:AbstractDict}
You must follow the You must follow the
""" """
prompt = formatLLMtext_llama3instruct("system", _prompt)
thought = iterativeprompting(a, prompt, syntaxcheck_json)
thought = iterativeprompting(prompt, syntaxcheck_json)
error("--> decisionMaker") error("--> decisionMaker")
@@ -227,7 +227,8 @@ function conversation(a::T, userinput::Dict) where {T<:agent}
initialState = Dict( initialState = Dict(
# deepcopy the info to prevent modifying the info unintentionally during MCTS planning # deepcopy the info to prevent modifying the info unintentionally during MCTS planning
:info=> deepcopy(a.keywordinfo), :customerinfo=> deepcopy(a.keywordinfo[:customerinfo]),
:storeinfo=> deepcopy(a.keywordinfo[:storeinfo]),
:thoughtHistory=> Dict{Symbol, Any}( # contain question, thought_1, action_1, observation_1, thought_2, ... :thoughtHistory=> Dict{Symbol, Any}( # contain question, thought_1, action_1, observation_1, thought_2, ...
:question=> userinput[:text], :question=> userinput[:text],

View File

@@ -267,106 +267,6 @@ end
isLeaf(node::MCTSNode)::Bool = isempty(node.children) isLeaf(node::MCTSNode)::Bool = isempty(node.children)
"""
Arguments\n
-----
Return\n
-----
Example\n
-----
```jldoctest
julia>
```
TODO\n
-----
[] update docstring
[WORKING] implement the function
Signature\n
-----
"""
function iterativeprompting(a::T, prompt::String, verification::Function) where {T<:agent}
msgMeta = GeneralUtils.generate_msgMeta(
a.config[:thirdPartyService][:text2textinstruct],
senderName= "iterativeprompting",
senderId= a.id,
receiverName= "text2textinstruct",
)
outgoingMsg = Dict(
:msgMeta,
:payload=> Dict(
:text=> prompt,
)
)
# iteration loop
while true
# send prompt to LLM
response = GeneralUtils.sendReceiveMqttMsg()
# check for correctness and get feedback
if correct
break
else
# get LLM critique
# add critique to prompt
end
end
return
end
""" Check JSON format correctness, provide
Arguments\n
-----
Return\n
-----
Example\n
-----
```jldoctest
julia>
```
TODO\n
-----
[] update docstring
[WORKING] implement the function
Signature\n
-----
"""
function syntaxcheck_json(jsonstring::String)::NamedTuple
success, result, errormsg, st = GeneralUtils.showstracktrace(JSON3.read, jsonstring)
if !success # gives feedback
else
end
return (success, result, critique)
end
""" """
Arguments\n Arguments\n

View File

@@ -115,9 +115,6 @@ abstract type agent end
:currenttrajectory=> Vector{Dict{Symbol, Any}}(), # store :currenttrajectory=> Vector{Dict{Symbol, Any}}(), # store
) )
# communication
msgMeta::Dict{Symbol, Any} # a template for msgMeta
# put incoming message here. waiting for further processing # put incoming message here. waiting for further processing
receiveUserMsgChannel::Channel{Dict} = Channel{Dict}(8) # for incoming user communication receiveUserMsgChannel::Channel{Dict} = Channel{Dict}(8) # for incoming user communication
receiveInternalMsgChannel::Channel{Dict} = Channel{Dict}(8) # for internal communication receiveInternalMsgChannel::Channel{Dict} = Channel{Dict}(8) # for internal communication
@@ -167,7 +164,6 @@ function sommelier(
name= name, name= name,
id= id, id= id,
config= config, config= config,
msgMeta= msgMeta,
maxHistoryMsg= maxHistoryMsg, maxHistoryMsg= maxHistoryMsg,
tools= tools, tools= tools,
thinkinglimit= thinkinglimit, thinkinglimit= thinkinglimit,

View File

@@ -1,6 +1,7 @@
module util module util
export clearhistory, addNewMessage, formatLLMtext, formatLLMtext_llama3instruct export clearhistory, addNewMessage, formatLLMtext, formatLLMtext_llama3instruct,
syntaxcheck_json, iterativeprompting
using UUIDs, Dates, DataStructures, HTTP, MQTTClient, JSON3 using UUIDs, Dates, DataStructures, HTTP, MQTTClient, JSON3
using GeneralUtils using GeneralUtils
@@ -228,7 +229,105 @@ end
""" Check JSON format correctness, provide
Arguments\n
-----
jsonstring::T
Return\n
-----
Example\n
-----
```jldoctest
julia>
```
TODO\n
-----
[] update docstring
[WORKING] implement the function
Signature\n
-----
"""
function syntaxcheck_json(jsonstring::T)::NamedTuple where {T<:AbstractString}
error("--> syntaxcheck_json")
# success, result, errormsg, st = GeneralUtils.showstracktrace(JSON3.read, jsonstring)
# if !success # gives feedback
# else
# end
# return (success, result, critique)
end
"""
Arguments\n
-----
Return\n
-----
Example\n
-----
```jldoctest
julia>
```
TODO\n
-----
[] update docstring
[WORKING] implement the function
Signature\n
-----
"""
function iterativeprompting(a::T, prompt::String, verification::Function) where {T<:agent}
msgMeta = GeneralUtils.generate_msgMeta(
a.config[:externalService][:text2textinstruct],
senderName= "iterativeprompting",
senderId= a.id,
receiverName= "text2textinstruct",
)
outgoingMsg = Dict(
:msgMeta=> msgMeta,
:payload=> Dict(
:text=> prompt,
)
)
# iteration loop
while true
# send prompt to LLM
response = GeneralUtils.sendReceiveMqttMsg(outgoingMsg)
error("--> iterativeprompting")
# check for correctness and get feedback
# if correct
# break
# else
# # get LLM critique
# # add critique to prompt
# end
end
return response
end

View File

@@ -20,16 +20,19 @@ msgMeta = GeneralUtils.generate_msgMeta(
) )
agentConfig = Dict( agentConfig = Dict(
:receiveprompt=>Dict( :mqttServerInfo=> Dict(
:mqtttopic=> config[:servicetopic][:value], # topic to receive prompt i.e. frontend send msg to this topic :broker=> config[:mqttServerInfo][:value][:broker],
:port=> config[:mqttServerInfo][:value][:port],
), ),
:receiveinternal=>Dict( :receivemsg=> Dict(
:mqtttopic=> instanceInternalTopic, # receive topic for model's internal :prompt=> config[:servicetopic][:value], # topic to receive prompt i.e. frontend send msg to this topic
:internal=> instanceInternalTopic,
), ),
:text2text=>Dict( :externalService=> Dict(
:mqtttopic=> config[:text2text][:value], :text2textinstruct=> config[:externalService][:text2textinstruct][:value],
:text2textchat=> config[:externalService][:text2textchat][:value],
), ),
) )
# Instantiate an agent # Instantiate an agent
tools=Dict( # update input format tools=Dict( # update input format
@@ -56,7 +59,6 @@ tools=Dict( # update input format
a = YiemAgent.sommelier( a = YiemAgent.sommelier(
receiveUserMsgChannel, receiveUserMsgChannel,
receiveInternalMsgChannel, receiveInternalMsgChannel,
msgMeta,
agentConfig, agentConfig,
name= "assistant", name= "assistant",
id= "randomSessionID", # agent instance id id= "randomSessionID", # agent instance id