COSINE_DISTANCE (Lakehouse v1)
이 문서는 PlaidCloud Lakehouse의 cosine_distance 함수에 대한 개요를 제공하고, 이 함수를 사용하여 문서 유사도를 측정하는 방법을 설명합니다.
PlaidCloud Lakehouse의 cosine_distance 함수는 두 벡터 사이의 코사인 거리를 계산하는 기본 제공 함수입니다. 문서 유사도나 추천 시스템과 같은 자연어 처리 작업에 흔히 사용됩니다.
코사인 거리는 두 벡터 사이 각도의 코사인을 기반으로 한 유사도 척도입니다. 이 함수는 두 입력 벡터를 받아 0에서 1 사이의 값을 반환하며, 0은 동일한 벡터를, 1은 직교하는(완전히 유사하지 않은) 벡터를 나타냅니다.
Analyze 구문
섹션 제목: “Analyze 구문”func.cosine_distance(<vector1>, <vector2>)SQL 예제
섹션 제목: “SQL 예제”테이블 생성 및 샘플 데이터 삽입
샘플 텍스트 문서와 해당 임베딩을 저장할 테이블을 만들어 보겠습니다.
CREATE TABLE articles ( id INT, title VARCHAR, content VARCHAR, embedding ARRAY(FLOAT32));이제 테이블에 샘플 문서를 삽입합니다.
INSERT INTO articles (id, title, content, embedding)VALUES (1, 'Python for Data Science', 'Python is a versatile programming language widely used in data science...', ai_embedding_vector('Python is a versatile programming language widely used in data science...')), (2, 'Introduction to R', 'R is a popular programming language for statistical computing and graphics...', ai_embedding_vector('R is a popular programming language for statistical computing and graphics...')), (3, 'Getting Started with SQL', 'Structured Query Language (SQL) is a domain-specific language used for managing relational databases...', ai_embedding_vector('Structured Query Language (SQL) is a domain-specific language used for managing relational databases...'));유사한 문서 쿼리
이제 cosine_distance 함수를 사용하여 주어진 쿼리와 가장 유사한 문서를 찾아 보겠습니다.
SELECT id, title, content, cosine_distance(embedding, ai_embedding_vector('How to use Python in data analysis?')) AS similarityFROM articlesORDER BY similarity ASC LIMIT 3;결과:
┌──────┬──────────────────────────┬─────────────────────────────────────────────────────────────────────────────────────────────────────────┬────────────┐│ id │ title │ content │ similarity │├──────┼──────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────┼────────────┤│ 1 │ Python for Data Science │ Python is a versatile programming language widely used in data science... │ 0.1142081 ││ 2 │ Introduction to R │ R is a popular programming language for statistical computing and graphics... │ 0.18741018 ││ 3 │ Getting Started with SQL │ Structured Query Language (SQL) is a domain-specific language used for managing relational databases... │ 0.25137568 │└──────┴──────────────────────────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────┴────────────┘