This commit is contained in:
2023-11-19 22:49:37 +00:00
parent 337f581da4
commit 08d8b28b99

View File

@@ -350,128 +350,11 @@ end
"""
Continuously run llm functions except when llm is getting Answer: or chatbox.
"""
function work(a::T, prompt::String, maxround::Int=3) where {T<:agent}
respond = nothing
while true
a.thoughtround += 1
@show a.thoughtround
toolname = nothing
toolinput = nothing
#WORKING force answer if thoughtround exceed limit
if a.thoughtround > a.thoughtlimit
a.thought *= "Thought $(a.thoughtround): I think I know the answer."
prompt = a.thought
end
@show prompt
respond = sendReceivePrompt(a, prompt)
headerToDetect = nothing
if a.thoughtround == 1
try
respond = split(respond, "Obs:")[1]
@show respond
headerToDetect = ["Question:", "Plan:", "Thought:", "Act:", "ActInput:", "Obs:", "...", "Answer:",
"Conclusion:", "Summary:"]
catch
end
else
try
respond = split(respond, "Obs $(a.thoughtround):")[1]
@show respond
headerToDetect = ["Question $(a.thoughtround):", "Plan $(a.thoughtround):",
"Thought $(a.thoughtround):", "Act $(a.thoughtround):",
"ActInput $(a.thoughtround):", "Obs $(a.thoughtround):",
"...", "Answer:",
"Conclusion:", "Summary:"]
catch
end
end
headers = detectCharacters(respond, headerToDetect)
chunkedtext = chunktext(respond, headers)
@show chunkedtext
Answer = findDetectedCharacter(headers, "Answer:")
AnswerInd = length(Answer) != 0 ? Answer[1] : nothing
Act = findDetectedCharacter(headers, "Act $(a.thoughtround):")
if length(Answer) == 1 && length(Act) == 0
a.thought = "nothing" # question finished, no more thought
a.thoughtround = 0
respond = chunkedtext[AnswerInd][:body]
_ = addNewMessage(a, "assistant", respond)
break
else
# check for tool being called
ActHeader = a.thoughtround == 1 ? "Act:" : "Act $(a.thoughtround):"
if length(findDetectedCharacter(headers, ActHeader)) != 0 # check whether there is Act: in a respond
ActInd = findDetectedCharacter(headers, ActHeader)[1]
toolname = toolNameBeingCalled(chunkedtext[ActInd][:body], a.tools)
end
ActInputHeader = a.thoughtround == 1 ? "ActInput:" : "ActInput $(a.thoughtround):"
if length(findDetectedCharacter(headers, ActInputHeader)) != 0 # check whether there is ActInput: in a respond
ActInputInd = findDetectedCharacter(headers, ActInputHeader)[1]
toolinput = chunkedtext[ActInputInd][:body]
end
# clean up
if occursin(" \"", toolinput)
toolinput = GeneralUtils.getStringBetweenCharacters(toolinput, " \"", "\"\n")
else
toolinput = GeneralUtils.getStringBetweenCharacters(toolinput, " ", "\n")
end
@show toolname #BUG llm not specify tools
@show toolinput
if toolname === nothing || toolinput === nothing
println("retry think")
a.thoughtround -= 1
continue
end
if a.thought == "nothing"
thought = ""
for i in chunkedtext
header = i[:header]
header = replace(header, ":"=>" $(a.thoughtround):") # add number so that llm not confused
body = i[:body]
thought *= "$header $body"
end
a.thought = thought #BUG should be prompt + thought
else
a.thought *= respond
end
if toolname == "chatbox" # chat with user
a.thought *= toolinput
respond = toolinput
_ = addNewMessage(a, "assistant", respond)
break
else # function call
println("//////////// $(a.thoughtround)")
f = a.tools[Symbol(toolname)][:func]
_result = f(toolinput)
if _result != "No info available." #TODO for use with wikisearch(). Not good for other tools
_result = makeSummary(a, _result)
end
result = "Obs $(a.thoughtround): $_result\n"
a.thought *= result
prompt = a.thought
end
end
end
@show respond
return respond
end
# function work(a::T, prompt::String, maxround::Int=3) where {T<:agent}
# respond = nothing
# while true
# a.thoughtround += 1
# @show a.thoughtround
# toolname = nothing
# toolinput = nothing
@@ -509,19 +392,6 @@ end
# chunkedtext = chunktext(respond, headers)
# @show chunkedtext
# if a.thought == "nothing"
# thought = ""
# for i in chunkedtext
# header = i[:header]
# header = replace(header, ":"=>" $(a.thoughtround):") # add number so that llm not confused
# body = i[:body]
# thought *= "$header $body"
# end
# a.thought = thought
# else
# a.thought *= respond
# end
# Answer = findDetectedCharacter(headers, "Answer:")
# AnswerInd = length(Answer) != 0 ? Answer[1] : nothing
# Act = findDetectedCharacter(headers, "Act $(a.thoughtround):")
@@ -532,11 +402,20 @@ end
# _ = addNewMessage(a, "assistant", respond)
# break
# else
# # check for tool being called
# ActHeader = a.thoughtround == 1 ? "Act:" : "Act $(a.thoughtround):"
# ActInd = findDetectedCharacter(headers, ActHeader)[1]
# toolname = toolNameBeingCalled(chunkedtext[ActInd][:body], a.tools)
# toolinput = chunkedtext[ActInd+1][:body]
# if length(findDetectedCharacter(headers, ActHeader)) != 0 # check whether there is Act: in a respond
# ActInd = findDetectedCharacter(headers, ActHeader)[1]
# toolname = toolNameBeingCalled(chunkedtext[ActInd][:body], a.tools)
# end
# ActInputHeader = a.thoughtround == 1 ? "ActInput:" : "ActInput $(a.thoughtround):"
# if length(findDetectedCharacter(headers, ActInputHeader)) != 0 # check whether there is ActInput: in a respond
# ActInputInd = findDetectedCharacter(headers, ActInputHeader)[1]
# toolinput = chunkedtext[ActInputInd][:body]
# end
# # clean up
# if occursin(" \"", toolinput)
# toolinput = GeneralUtils.getStringBetweenCharacters(toolinput, " \"", "\"\n")
# else
@@ -544,7 +423,28 @@ end
# end
# @show toolname #BUG llm not specify tools
# @show toolinput
# if toolname === nothing || toolinput === nothing
# println("retry think")
# a.thoughtround -= 1
# continue
# end
# if a.thought == "nothing"
# thought = ""
# for i in chunkedtext
# header = i[:header]
# header = replace(header, ":"=>" $(a.thoughtround):") # add number so that llm not confused
# body = i[:body]
# thought *= "$header $body"
# end
# a.thought = prompt * thought #BUG should be prompt + thought
# else
# a.thought *= respond
# end
# if toolname == "chatbox" # chat with user
# a.thought *= toolinput
@@ -568,6 +468,106 @@ end
# return respond
# end
function work(a::T, prompt::String, maxround::Int=3) where {T<:agent}
respond = nothing
while true
a.thoughtround += 1
toolname = nothing
toolinput = nothing
#WORKING force answer if thoughtround exceed limit
if a.thoughtround > a.thoughtlimit
a.thought *= "Thought $(a.thoughtround): I think I know the answer."
prompt = a.thought
end
@show prompt
respond = sendReceivePrompt(a, prompt)
headerToDetect = nothing
if a.thoughtround == 1
try
respond = split(respond, "Obs:")[1]
@show respond
headerToDetect = ["Question:", "Plan:", "Thought:", "Act:", "ActInput:", "Obs:", "...", "Answer:",
"Conclusion:", "Summary:"]
catch
end
else
try
respond = split(respond, "Obs $(a.thoughtround):")[1]
@show respond
headerToDetect = ["Question $(a.thoughtround):", "Plan $(a.thoughtround):",
"Thought $(a.thoughtround):", "Act $(a.thoughtround):",
"ActInput $(a.thoughtround):", "Obs $(a.thoughtround):",
"...", "Answer:",
"Conclusion:", "Summary:"]
catch
end
end
headers = detectCharacters(respond, headerToDetect)
chunkedtext = chunktext(respond, headers)
@show chunkedtext
if a.thought == "nothing"
thought = ""
for i in chunkedtext
header = i[:header]
header = replace(header, ":"=>" $(a.thoughtround):") # add number so that llm not confused
body = i[:body]
thought *= "$header $body"
end
a.thought = prompt * thought
else
a.thought *= respond
end
Answer = findDetectedCharacter(headers, "Answer:")
AnswerInd = length(Answer) != 0 ? Answer[1] : nothing
Act = findDetectedCharacter(headers, "Act $(a.thoughtround):")
if length(Answer) == 1 && length(Act) == 0
a.thought = "nothing" # question finished, no more thought
a.thoughtround = 0
respond = chunkedtext[AnswerInd][:body]
_ = addNewMessage(a, "assistant", respond)
break
else
# check for tool being called
ActHeader = a.thoughtround == 1 ? "Act:" : "Act $(a.thoughtround):"
ActInd = findDetectedCharacter(headers, ActHeader)[1]
toolname = toolNameBeingCalled(chunkedtext[ActInd][:body], a.tools)
toolinput = chunkedtext[ActInd+1][:body]
if occursin(" \"", toolinput)
toolinput = GeneralUtils.getStringBetweenCharacters(toolinput, " \"", "\"\n")
else
toolinput = GeneralUtils.getStringBetweenCharacters(toolinput, " ", "\n")
end
@show toolname #BUG llm not specify tools
@show toolinput
if toolname == "chatbox" # chat with user
a.thought *= toolinput
respond = toolinput
_ = addNewMessage(a, "assistant", respond)
break
else # function call
println("//////////// $(a.thoughtround)")
f = a.tools[Symbol(toolname)][:func]
_result = f(toolinput)
if _result != "No info available." #TODO for use with wikisearch(). Not good for other tools
_result = makeSummary(a, _result)
end
result = "Obs $(a.thoughtround): $_result\n"
a.thought *= result
prompt = a.thought
end
end
end
@show respond
return respond
end
"""
make a conversation summary.
@@ -768,7 +768,7 @@ end
)
```
"""
function sendReceivePrompt(a::T, prompt::String; timeout::Int=60) where {T<:agent}
function sendReceivePrompt(a::T, prompt::String; timeout::Int=120) where {T<:agent}
a.msgMeta[:msgId] = "$(uuid4())" # new msg id for each msg
msg = Dict(
:msgMeta=> a.msgMeta,