From 801596fa7f89a0926a6444ed8e74bfed62b001de Mon Sep 17 00:00:00 2001 From: narawat Date: Fri, 24 Jul 2026 14:32:44 +0700 Subject: [PATCH] update --- Project.toml | 2 +- src/dbUtil.jl | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 3b61f16..7776dd1 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "GeneralUtils" uuid = "c6c72f09-b708-4ac8-ac7c-2084d70108fe" -version = "0.5.5" +version = "0.5.6" authors = ["tonaerospace "] [deps] diff --git a/src/dbUtil.jl b/src/dbUtil.jl index 0120dc8..afe5cb7 100644 --- a/src/dbUtil.jl +++ b/src/dbUtil.jl @@ -93,6 +93,67 @@ function get_embedding_nats(nats_conn::NATS.Connection, texts::Vector{String}, s return result end +""" find_text_vector_similarity + +Find the most similar text records in a PostgreSQL database using vector embeddings and cosine similarity. + +This function computes an embedding for the input text using the provided embedding function, +then queries the database to find records with the most similar vector representations using +PostgreSQL's cosine similarity operator (`<->`). + +# Arguments +- `text::AbstractString`: The input text to find similar records for +- `tablename::AbstractString`: Name of the database table containing the embedding column +- `embeddingColumnName::AbstractString`: Name of the column storing vector embeddings +- `executesql::Function`: Function that executes SQL queries and returns results +- `get_embedding::Function`: Function that generates embeddings for text inputs + +# Keyword Arguments +- `limit::Integer=1`: Maximum number of similar records to return + +# Returns +- `DataFrame`: Database records ordered by similarity (most similar first), including a `distance` column + where smaller values indicate higher similarity + +# Example +```julia +# Assume you have embedding and SQL execution functions +text = "a rich structured red wine" +tablename = "wine" +embeddingColumnName = "description_embedding" + +df = find_text_vector_similarity( + text, tablename, embeddingColumnName, + executesql, get_embedding; + limit = 5 +) + +# Result contains columns from the table plus a 'distance' column +# where distance = 1 - cosine_similarity (smaller = more similar) +``` +""" +function find_text_vector_similarity(text::T1, tablename::T2, embeddingColumnName::T3, + executesql::Function, get_embedding::Function; + limit::Integer=1 + )::DataFrame where {T1<:AbstractString, T2<:AbstractString, T3<:AbstractString} + # get embedding from LLM service + _embedding = get_embedding([text]) + _embedding = _embedding["data"][1]["embedding"] + _embedding = "$_embedding" + + embedding = _embedding[4:end] # remove 'Any' from Any[...] + + # check whether there is close enough vector already store in executesql. if no, add, else skip + sql = """ + SELECT *, $embeddingColumnName <-> '$embedding' as distance + FROM $tablename + ORDER BY distance LIMIT $limit; + """ + response = executesql(sql) + df = DataFrame(response) + + return df +end """