This commit is contained in:
narawat lamaiin
2025-05-04 20:56:36 +07:00
parent 150ddac2c0
commit d8ea4b70a9
2 changed files with 50 additions and 27 deletions

View File

@@ -1,6 +1,6 @@
module llmUtil
export formatLLMtext, formatLLMtext_llama3instruct, jsoncorrection, deFormatLLMtext
export formatLLMtext, formatLLMtext_llama3instruct, jsoncorrection, deFormatLLMtext, extractthink
using UUIDs, JSON3, Dates
using GeneralUtils
@@ -255,7 +255,7 @@ julia> normalText = YiemAgent.deFormatLLMtext(response, "granite3")
"This is a sample system instruction."
```
"""
function deFormatLLMtext(text::String, formatname::String
function deFormatLLMtext(text::String, formatname::String; includethink::Bool=false
)::String
f =
if formatname == "granite3"
@@ -306,28 +306,32 @@ function deFormatLLMtext_granite3(text::String)::Union{Nothing, String}
end
function deFormatLLMtext_qwen3(text::String; includethink::Bool=false)::Union{Nothing, String}
think = nothing
str = nothing
if occursin("<think>", text)
r = GeneralUtils.extractTextBetweenString(text, "<think>", "</think>")
if r[:success]
think = r[:text]
end
str = string(split(text, "</think>")[2])
end
if includethink == true && occursin("<think>", text)
result = "ModelThought: $think $str"
return result
elseif includethink == false && occursin("<think>", text)
result = str
return result
else
function deFormatLLMtext_qwen3(text::String)::Union{Nothing, String}
return text
end
end
# function deFormatLLMtext_qwen3(text::String; includethink::Bool=false)::Union{Nothing, String}
# think = nothing
# str = nothing
# if occursin("<think>", text)
# r = GeneralUtils.extractTextBetweenString(text, "<think>", "</think>")
# if r[:success]
# think = r[:text]
# end
# str = string(split(text, "</think>")[2])
# end
# if includethink == true && occursin("<think>", text)
# result = "ModelThought: $think $str"
# return result
# elseif includethink == false && occursin("<think>", text)
# result = str
# return result
# else
# return text
# end
# end
""" Attemp to correct LLM response's incorrect JSON response.
@@ -419,7 +423,20 @@ function jsoncorrection(config::T1, input::T2, correctJsonExample::T3;
end
function extractthink(text::String)
think = nothing
str = nothing
if occursin("<think>", text)
r = GeneralUtils.extractTextBetweenString(text, "<think>", "</think>")
if r[:success]
think = r[:text]
end
str = string(split(text, "</think>")[2])
else
str = text
end
return think, str
end

View File

@@ -1305,13 +1305,19 @@ julia> recentElementsIndex(length(a), 0)
5:5
```
"""
function recentElementsIndex(vectorlength::Integer, n::Integer)
function recentElementsIndex(vectorlength::Integer, n::Integer; includelatest::Bool=false)
if n == 0
error("n must be greater than 0")
end
if includelatest
start = max(1, vectorlength - n + 1)
return start:vectorlength
else
startind = max(1, vectorlength - n)
endind = vectorlength -1
return startind:endind
end
end