This commit is contained in:
narawat lamaiin
2024-04-23 23:24:51 +07:00
parent 9f5efb2948
commit c2e3d1bae5
3 changed files with 51 additions and 58 deletions

View File

@@ -48,6 +48,7 @@ julia> addNewMessage(agent1, "user", "Where should I go to buy snacks")
```
"""
function addNewMessage(a::T1, name::String, content::T2) where {T1<:agent, T2<:AbstractString}
if name a.availableRole # guard against typo
error("name is not in agent.availableRole $(@__LINE__)")
end
@@ -181,18 +182,14 @@ function chat_mistral_openorca(a::agentReflex, prompttemplate="llama3")
{longterm memory}
"
"""
conversation = formatLLMtext(a.messages, "llama3instruct")
prompt =
"""
$conversation
"""
response = sendReceivePrompt(a, prompt, a.config[:text2textchat][:mqtttopic],
_response = sendReceivePrompt(a, prompt, a.config[:text2textchat][:mqtttopic],
timeout=180, stopword=["<|", "</"])
response = split(response, "<|")[1]
response = split(response, "</")[1]
response = _response
return response
end
@@ -1275,20 +1272,17 @@ 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
# 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.
@@ -1297,10 +1291,8 @@ function conversation(a::agentReflex, usermsg::String; attemptlimit::Int=3)
response = split(response, "\n\n")[1]
response = split(response, "\n\n")[1]
end
response = removeTrailingCharacters(response)
_ = addNewMessage(a, "assistant", response)
return response
end

View File

@@ -230,7 +230,7 @@ function agentReflex(
roleSpecificInstruction = roleSpecificInstruction,
)
systemChatMsg = Dict(:name=> "system",:text=> "You are a helpful, respectful and honest assistant.",)
systemChatMsg = Dict(:name=> "system", :text=> "You are a helpful, respectful and honest assistant.",)
push!(newAgent.messages, systemChatMsg)

View File

@@ -53,7 +53,6 @@ function sendReceivePrompt(a::T1, prompt::String, sendtopic::String;
max_tokens::Integer=256, timeout::Integer=120, temperature::AbstractFloat=0.2,
stopword::T2=["nostopwordyet"],
seed=nothing) where {T1<:agent, T2<:Vector{<:AbstractString}}
println("---> 4")
# copy a.msgMeta instead of using GeneralUtils.generate_msgMeta because if I want to custom
# msgMeta for some communication I can do it during that agent instantiation and the custom
# msgMeta will effect all of the communication in that agent without effecting all agent
@@ -379,50 +378,6 @@ end
2. objective # what LLM going to do
"""
function isUsePlans(a::agentReflex)
toollines = ""
for (toolname, v) in a.tools
if toolname ["chatbox"] # LLM will always use chatbox
toolline = "$toolname is $(v[:description])\n"
toollines *= toolline
end
end
conversation = messagesToString(a.messages)
aboutYourself =
"""
Your name is $(a.name)
$(a.roles[a.role])
"""
prompt =
"""
<|system|>
<About yourself>
$aboutYourself
</About yourself>
<You have access to the following tools>
$toollines
</You have access to the following tools>
<Your earlier conversation with the user>
$conversation
</Your earlier conversation with the user>
<Your job>
Your job is to decide whether you need think thoroughly or use tools in order to respond to the user.
Use the following format:
Thought: Do you need to think thoroughly or use tools before responding to the user?
</Your job>
<Example 1>
user: Hello!. How are you?
assistant: The user is greeting me, I don't need to think about it.
</Example 1>
<Example 2>
user: "What's tomorrow weather like?"
assistant: I will need to use weather tools to check for tomorrow's temperature.
</Example 2>
</s>
<|assistant|>
"""
isuseplan = false
@show a.role
if length(a.memory[:shortterm]) != 0
@@ -432,6 +387,51 @@ function isUsePlans(a::agentReflex)
elseif a.role == :sommelier
isuseplan = true
else
toollines = ""
for (toolname, v) in a.tools
if toolname ["chatbox"] # LLM will always use chatbox
toolline = "$toolname is $(v[:description])\n"
toollines *= toolline
end
end
conversation = messagesToString(a.messages)
aboutYourself =
"""
Your name is $(a.name)
$(a.roles[a.role])
"""
prompt =
"""
<|system|>
<About yourself>
$aboutYourself
</About yourself>
<You have access to the following tools>
$toollines
</You have access to the following tools>
<Your earlier conversation with the user>
$conversation
</Your earlier conversation with the user>
<Your job>
Your job is to decide whether you need think thoroughly or use tools in order to respond to the user.
Use the following format:
Thought: Do you need to think thoroughly or use tools before responding to the user?
</Your job>
<Example 1>
user: Hello!. How are you?
assistant: The user is greeting me, I don't need to think about it.
</Example 1>
<Example 2>
user: "What's tomorrow weather like?"
assistant: I will need to use weather tools to check for tomorrow's temperature.
</Example 2>
</s>
<|assistant|>
"""
# if LLM mentions any tools, use Plan/Thought/Act loop
response = sendReceivePrompt(a, prompt, a.config[:text2textchat][:mqtttopic], max_tokens=64,
timeout=180, stopword=["<|", "</"])
@@ -706,7 +706,7 @@ function removeTrailingCharacters(text; charTobeRemoved::AbstractVector{T}=['\n'
end
end
return text[1:end-nouse]
return String(text[1:end-nouse])
end
@@ -1174,6 +1174,7 @@ function formatLLMtext(messages::Vector{Dict{Symbol, T}},
for t in messages
str *= f(t[:name], t[:text])
end
return str
end