From 562f528c0195df06ab766227d056fb01720ce13a Mon Sep 17 00:00:00 2001 From: narawat lamaiin Date: Thu, 27 Mar 2025 13:09:20 +0700 Subject: [PATCH] update --- src/llmUtil.jl | 10 +++--- src/util.jl | 80 +++++++++++++++++++++++++++++++++++++++++++----- test/runtests.jl | 23 ++++++-------- 3 files changed, 87 insertions(+), 26 deletions(-) diff --git a/src/llmUtil.jl b/src/llmUtil.jl index ef9511c..53fbd29 100644 --- a/src/llmUtil.jl +++ b/src/llmUtil.jl @@ -103,22 +103,22 @@ function formatLLMtext_phi4(name::T, text::T; formattedtext = if name == "system" """ - <|im_start|>$name<|im_sep|> + <|system|> $text - <|im_end|> + <|end|> """ else """ - <|im_start|>$name<|im_sep|> + <|assistant|> $text - <|im_end|> + <|end|> """ end if assistantStarter formattedtext *= """ - <|im_start|>assistant<|im_sep|> + <|assistant|> """ end diff --git a/src/util.jl b/src/util.jl index 9f2024c..681a73b 100644 --- a/src/util.jl +++ b/src/util.jl @@ -6,7 +6,7 @@ export timedifference, showstracktrace, findHighestIndexKey, uuid4snakecase, rep dfToString, dataframe_to_json_list, dictToString, dictToString_noKey, dictToString_numbering, extract_triple_backtick_text, countGivenWords, remove_french_accents, detect_keyword, extractTextBetweenCharacter, - convertCamelSnakeKebabCase + convertCamelSnakeKebabCase, fitrange, lastElementsIndex using JSON3, DataStructures, Distributions, Random, Dates, UUIDs, MQTTClient, DataFrames @@ -1134,14 +1134,80 @@ true ```` """ function issomething(x) -return x === nothing ? false : true + return x === nothing ? false : true +end + + +""" Adjust a given range to fit within the bounds of a vector's length. + +# Arguments + - `v::T1` + the input vector to check against + - `range::UnitRange` + the original range to be adjusted + +# Return + - `adjusted_range::UnitRange` + a range that is constrained to the vector's length, preventing out-of-bounds indexing + +# Example + +julia> v = [1, 2, 3, 4, 5] +julia> fitrange(v, 3:10) +3:5 + +""" +function fitrange(v::T1, range::UnitRange) where {T1<:AbstractVector} + totalelements = length(v) + + startind = + # check if user put start range greater than total event + if range.start > totalelements + totalelements + else + range.start + end + + stopind = + if range.stop > totalelements + totalelements + else + range.stop + end + + return startind:stopind +end + + +""" Find a unit range for a vector given a number of the most recent elements of interest. + +# Arguments + - `v::AbstractVector` + the input vector to extract recent elements from + - `n::Integer` + the number of most recent elements to include in the range + +# Return + - `UnitRange` + a range representing the last `n` elements of the vector + +# Example +```jldoctest +julia> a = [1, 2, 3, 4, 5] +julia> lastElementsIndex(a, 3) +3:5 +``` +""" +function lastElementsIndex(v::AbstractVector, n::Integer) + len = length(v) + if n == 0 + return 1:len + end + + start = max(1, len - n + 1) + return start:len end - - - - - end # module util \ No newline at end of file diff --git a/test/runtests.jl b/test/runtests.jl index c00177a..5bc7609 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,23 +1,18 @@ using Test using GeneralUtils -@testset "detect_keyword tests" begin - @test GeneralUtils.detect_keyword(["test"], "this is a test string") == Dict("test" => 1) - @test GeneralUtils.detect_keyword(["hello", "world"], "hello world") == Dict("hello" => 1, "world" => 1) - @test GeneralUtils.detect_keyword(["missing"], "no keyword here") == Dict("missing" => nothing) - @test GeneralUtils.detect_keyword(["a", "b"], "a a b b b") == Dict("a" => 2, "b" => 3) - @test GeneralUtils.detect_keyword(String[], "empty keywords") == Dict{String, Any}() - @test GeneralUtils.detect_keyword(["keyword"], "") == Dict("keyword" => nothing) - @test GeneralUtils.detect_keyword(["case"], "CASE case Case cAsE") == Dict("case" => 4) - mixed_results = GeneralUtils.detect_keyword(["found", "notfound"], "found found found") - @test mixed_results["found"] == 3 - @test mixed_results["notfound"] === nothing - - special_chars = GeneralUtils.detect_keyword(["test!"], "test! test? test.") - @test special_chars["test!"] == 1 +@testset "lastElementsIndex" begin + @test GeneralUtils.lastElementsIndex([1,2,3,4,5], 3) == 3:5 + @test GeneralUtils.lastElementsIndex([1,2,3], 5) == 1:3 + @test GeneralUtils.lastElementsIndex([1], 1) == 1:1 + @test GeneralUtils.lastElementsIndex(collect(1:10), 4) == 7:10 + @test GeneralUtils.lastElementsIndex(Float64[], 2) == 1:0 + @test GeneralUtils.lastElementsIndex([1,2,3], 0) == length([1,2,3])+1:length([1,2,3]) + @test GeneralUtils.lastElementsIndex(["a","b","c"], 2) == 2:3 end +