This commit is contained in:
2023-12-19 16:30:32 +00:00
parent d90ff8d3fc
commit 59e3b3027f
3 changed files with 222 additions and 172 deletions

View File

@@ -4,7 +4,7 @@ export makeSummary, sendReceivePrompt, chunktext, extractStepFromPlan, checkTota
detectCharacters, findDetectedCharacter, extract_number, toolNameBeingCalled,
isUseTools, conversationSummary, checkReasonableness, replaceHeaders,
addShortMem!, splittext, dictToString, removeHeaders, keepOnlyKeys, experience,
messagesToString, messagesToString_nomark, removeTrailingCharacters
messagesToString, messagesToString_nomark, removeTrailingCharacters, dictLatestStep
using UUIDs, Dates, DataStructures
using CommUtils, GeneralUtils
@@ -378,35 +378,40 @@ Return:
function isUseTools(a::agentReflex)
toollines = ""
for (toolname, v) in a.tools
if toolname ["chatbox"]
if toolname ["chatbox"] # LLM will always use chatbox
toolline = "$toolname: $(v[:description]) $(v[:input]) $(v[:output])\n"
toollines *= toolline
end
end
conversation = messagesToString_nomark(a.messages)
conversation = messagesToString(a.messages)
prompt =
"""
<|im_start|>system
$(a.roles[a.role])
<|system|>
You are a helpful assistant.
You have access to the following tools:
$toollines
Your conversation with the user:
Your task is to decide whether you need think thoroughly in order to respond to the user according to your conversation with the user and tools you have.
So for instance the following:
user: Hello!. How are you?
assistant: {No}, the user is greeting me, I could respond right away.
user: "I want a bottle of wine."
assistant: {Yes}, I need to think thoroughly about the user stimulus.
</s>
$conversation
From the conversation, ask yourself what do you intend to do now?
<|im_end|>
<|assistant|>
"""
# if LLM mentions any tools, use Plan/Thought/Act loop
isusetool = false
response = sendReceivePrompt(a, prompt, temperature=0.0)
response = split(response, "<|im_end|>")[1]
response = sendReceivePrompt(a, prompt, temperature=0.2, max_tokens=64)
response = split(response, "<|assistant|>")[1]
response = split(response, "<|user|>")[1]
@show response
for (toolname, v) in a.tools
if occursin(toolname, String(response))
if occursin("Yes", String(response))
isusetool = true
break
end
@@ -415,17 +420,14 @@ function isUseTools(a::agentReflex)
if length(a.memory[:shortterm]) != 0
isusetool = true
end
return isusetool
end
# function isUseTools(a::agentReflex)
# toollines = ""
# for (toolname, v) in a.tools
# if toolname ∉ ["chatbox"]
# if toolname ∉ ["chatbox"] # LLM will always use chatbox
# toolline = "$toolname: $(v[:description]) $(v[:input]) $(v[:output])\n"
# toollines *= toolline
# end
@@ -443,74 +445,30 @@ end
# Your conversation with the user:
# $conversation
# From your conversation, ask yourself what do you need to do now?
# From the conversation, ask yourself what do you intend to do now?
# <|im_end|>
# """
# # if LLM mentions any tools, use Plan/Thought/Act loop
# isusetool = false
# result = sendReceivePrompt(a, prompt, temperature=0.2)
# response = sendReceivePrompt(a, prompt, temperature=0.0)
# response = split(response, "<|im_end|>")[1]
# for (toolname, v) in a.tools
# if occursin(toolname, result)
# if occursin(toolname, String(response))
# isusetool = true
# break
# end
# end
# @show prompt
# whattodo = result
# @show whattodo
# return isusetool, whattodo
# if length(a.memory[:shortterm]) != 0
# isusetool = true
# end
# return isusetool
# end
# function isUseTools(a::agentReflex, usermsg::String)
# toollines = ""
# for (toolname, v) in a.tools
# if toolname ∉ ["chatbox"]
# toolline = "$toolname: $(v[:description]) $(v[:input]) $(v[:output])\n"
# toollines *= toolline
# end
# end
# prompt =
# """
# <|im_start|>system
# $(a.roles[a.role])
# You have access to the following tools:
# $toollines
# Your earlier conversation with the user:
# None
# User's message:
# $usermsg
# From the user's message, ask yourself what do you need to do?
# <|im_end|>
# <|im_start|>assistant
# """
# # if LLM mentions any tools, use Plan/Thought/Act loop
# isusetool = false
# result = sendReceivePrompt(a, prompt, temperature=0.2)
# for (toolname, v) in a.tools
# if occursin(toolname, result)
# isusetool = true
# break
# end
# end
# whattodo = result
# @show whattodo
# return isusetool, whattodo
# end
"""
make a conversation summary.
@@ -614,9 +572,9 @@ function messagesToString(messages::AbstractVector{T}; addressAIas="assistant")
end
if role == "user"
conversation *= "<|im_start|>$role: $(content[1:end-nouse])\n<|im_end|>"
conversation *= "<|$role|>\n $(content[1:end-nouse])\n</s>"
elseif role == "assistant"
conversation *= "<|im_start|>$addressAIas: $(content[1:end-nouse])\n<|im_end|>"
conversation *= "<|$addressAIas|>\n $(content[1:end-nouse])\n</s>"
else
error("undefied condition role = $role $(@__LINE__)")
end
@@ -627,6 +585,36 @@ function messagesToString(messages::AbstractVector{T}; addressAIas="assistant")
return conversation
end
# function messagesToString(messages::AbstractVector{T}; addressAIas="assistant") where {T<:AbstractDict}
# conversation = ""
# if length(messages)!= 0
# for msg in messages
# role = msg[:role]
# content = msg[:content]
# nouse = 0
# for i in reverse(content)
# if i == '\n' || i == ' '
# nouse += 1
# else
# break
# end
# end
# if role == "user"
# conversation *= "<|im_start|>$role: $(content[1:end-nouse])\n<|im_end|>"
# elseif role == "assistant"
# conversation *= "<|im_start|>$addressAIas: $(content[1:end-nouse])\n<|im_end|>"
# else
# error("undefied condition role = $role $(@__LINE__)")
# end
# end
# else
# conversation = "N/A"
# end
# return conversation
# end
""" Convert a vector of dict into 1-continous string.
@@ -1028,6 +1016,14 @@ function experience(dict::T) where {T<:AbstractDict}
end
function dictLatestStep(dict::T) where {T<:AbstractDict}
@show dict
_latest_step = keys(dict)
_latest_step = [i for i in _latest_step]
_latest_step = _latest_step[end]
latest_step = parse(Int, _latest_step[end-2:end-1])
return latest_step
end