add chunktext()

This commit is contained in:
2023-11-17 03:24:35 +00:00
parent 1d106582be
commit 204d546058

View File

@@ -2,7 +2,7 @@ module interface
export agentReact, addNewMessage, clearMessage, removeLatestMsg, generatePrompt_tokenPrefix,
generatePrompt_tokenSuffix, conversation, work
generatePrompt_tokenSuffix, conversation, work, detectCharacters, chunktext
using JSON3, DataStructures, Dates, UUIDs
using CommUtils, GeneralUtils
@@ -81,7 +81,7 @@ function agentReact(
Act: the action tool related to what you intend to do, should be one of {toolnames}
ActInput: the input to the action (pay attention to the tool's input)
Obs: the result of the action
... (this Plan/Thought/Act/ActInput/Obs loop can repeat N times.)
..... (this Plan/Thought/Act/ActInput/Obs loop can repeat N times.)
Thought: I think I know the answer
ANS: Answer of the original question and the rationale behind your answer
""",
@@ -98,7 +98,7 @@ function agentReact(
Act: the tool that match your thought, should be one of {toolnames}
ActInput: the input to the action (pay attention to the tool's input)
Obs: the result of the action
... (this Plan/Thought/Act/ActInput/Obs loop can repeat N times until you know the answer.)
..... (this Plan/Thought/Act/ActInput/Obs loop can repeat N times until you know the answer.)
ANS: Answer of the original question. You describe detailed benefits of each answer to user's preference.
Info used to select wine:
@@ -377,13 +377,13 @@ function conversation(a::T, usermsg::String) where {T<:agent}
_ = addNewMessage(a, "assistant", respond)
@show respond
elseif userintend == "wine" #WORKING
@show a.thought
if a.thought == "nothing" # new thought
a.context = conversationSummary(a)
_ = addNewMessage(a, "user", usermsg)
prompt = generatePrompt_react_mistral_openorca(a, usermsg)
@show prompt
# respond = sendReceivePrompt(a, prompt)
respond = work(a, prompt)
else # continue thought
@@ -418,8 +418,36 @@ function conversation(a::T, usermsg::String) where {T<:agent}
end
#WORKING
function work(a::T, usermsg::String) where {T<:agent}
function work(a::T, prompt::String) where {T<:agent}
respond = nothing
while true
respond = sendReceivePrompt(a, prompt)
@show respond
header = detectCharacters(respond,
["QTS:", "Plan:", "Thought:", "Act:", "ActInput:", "Obs:", ".....", "ANS:"])
end
end
"""
@@ -726,10 +754,10 @@ end
julia> characters = ["eat", "use", "i"]
julia> result = detectCharacters(text, characters)
4-element Vector{Any}:
(char = "i", startInd = 4, endInd = 4)
(char = "eat", startInd = 11, endInd = 13)
(char = "use", startInd = 26, endInd = 28)
(char = "i", startInd = 35, endInd = 35)
(char = "i", start = 4, stop = 4)
(char = "eat", start = 11, stop = 13)
(char = "use", start = 26, stop = 28)
(char = "i", start = 35, stop = 35)
```
"""
function detectCharacters(text::T1, characters::Vector{T2}) where {T1<:AbstractString, T2<:AbstractString}
@@ -744,7 +772,7 @@ function detectCharacters(text::T1, characters::Vector{T2}) where {T1<:AbstractS
# skip
else
if text[char_startInd: char_endInd] == char
push!(result, (char=char, startInd=char_startInd, endInd=char_endInd))
push!(result, (char=char, start=char_startInd, stop=char_endInd))
end
end
end
@@ -758,10 +786,10 @@ end
Output is character location index inside detectedCharacters
```jldoctest
julia a = [ (char = "i", startInd = 4, endInd = 4)
(char = "eat", startInd = 11, endInd = 13)
(char = "use", startInd = 26, endInd = 28)
(char = "i", startInd = 35, endInd = 35) ]
julia a = [ (char = "i", start = 4, stop = 4)
(char = "eat", start = 11, stop = 13)
(char = "use", start = 26, stop = 28)
(char = "i", start = 35, stop = 35) ]
julia> findDetectedCharacter(a, "i")
[1, 4]
```
@@ -771,7 +799,35 @@ function findDetectedCharacter(detectedCharacters, character)
return findall(isequal.(allchar, character))
end
"""
Chunk a text into smaller pieces by header.
```jldoctest
julia> text = "Plan: First, we need to find out what kind of wine the user wants."
julia> headers = detectCharacters(text, ["Nope", "sick", "First", "user", "Then", ])
3-element Vector{Any}:
(char = "First", start = 7, stop = 11)
(char = "user", start = 56, stop = 59)
(char = "Then", start = 102, stop = 105)
julia> chunkedtext = chunktext(text, headers)
```
"""
function chunktext(text::T, headers) where {T<:AbstractString}
result = []
for (i, v) in enumerate(headers)
if i < length(headers)
nextheader = headers[i+1]
body = text[v[:stop]+1: nextheader[:start]-1]
push!(result, (header=v[:char], body=body))
else
body = text[v[:stop]+1: end]
push!(result, (header=v[:char], body=body))
end
end
return result
end