벡터 유사도 검색 수행하기
벡터 유사도 검색 수행하기
Fixed-Length Arrays 기능은 DuckDB 버전 0.10.0에서 추가됐어요. 이 덕분에 DuckDB 테이블에서 벡터 임베딩을 쓸 수 있어 데이터 분석을 더욱 강력하게 만들어요.
출처: 문서
본문
또한 array_cosine_similarity 함수가 도입됐어요. 이 함수는 두 벡터 사이 각도의 코사인을 측정해 유사도를 나타내요. 값 1은 완전히 정렬됨, 0은 수직, -1은 완전히 반대를 의미해요.
이 함수를 유사도 검색에 어떻게 쓰는지 살펴봐요. 이 섹션에서 DuckDB로 유사도 검색을 수행하는 방법을 보여줄 거예요.
asoria/awesome-chatgpt-prompts-embeddings 데이터셋을 사용할 거예요.
먼저 데이터셋에서 몇 개 레코드를 미리 봐요:
FROM 'hf://datasets/asoria/awesome-chatgpt-prompts-embeddings/data/*.parquet' SELECT act, prompt, len(embedding) as embed_len LIMIT 3;
┌──────────────────────┬──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┬───────────┐
│ act │ prompt │ embed_len │
│ varchar │ varchar │ int64 │
├──────────────────────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼───────────┤
│ Linux Terminal │ I want you to act as a linux terminal. I will type commands and you will reply with what the terminal should show. I want you to only reply with the terminal output insid… │ 384 │
│ English Translator… │ I want you to act as an English translator, spelling corrector and improver. I will speak to you in any language and you will detect the language, translate it and answer… │ 384 │
│ `position` Intervi… │ I want you to act as an interviewer. I will be the candidate and you will ask me the interview questions for the `position` position. I want you to only reply as the inte… │ 384 │
└──────────────────────┴──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┴───────────┘
다음으로 유사도 검색에 쓸 임베딩을 하나 골라요:
FROM 'hf://datasets/asoria/awesome-chatgpt-prompts-embeddings/data/*.parquet' SELECT embedding WHERE act = 'Linux Terminal';
┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ embedding │
│ float[] │
├─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ [-0.020781303, -0.029143505, -0.0660217, -0.00932716, -0.02601602, -0.011426172, 0.06627567, 0.11941507, 0.0013917526, 0.012889079, 0.053234346, -0.07380514, 0.04871567, -0.043601237, -0.0025319182, 0.0448… │
└─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
이제 선택한 임베딩으로 유사한 레코드를 찾아요:
SELECT act,
prompt,
array_cosine_similarity(embedding::float[384], (SELECT embedding FROM 'hf://datasets/asoria/awesome-chatgpt-prompts-embeddings/data/*.parquet' WHERE act = 'Linux Terminal')::float[384]) AS similarity
FROM 'hf://datasets/asoria/awesome-chatgpt-prompts-embeddings/data/*.parquet'
ORDER BY similarity DESC
LIMIT 3;
┌──────────────────────┬─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┬────────────┐
│ act │ prompt │ similarity │
│ varchar │ varchar │ float │
├──────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼────────────┤
│ Linux Terminal │ I want you to act as a linux terminal. I will type commands and you will reply with what the terminal should show. I want you to only reply with the terminal output insi… │ 1.0 │
│ JavaScript Console │ I want you to act as a javascript console. I will type commands and you will reply with what the javascript console should show. I want you to only reply with the termin… │ 0.7599728 │
│ R programming Inte… │ I want you to act as a R interpreter. I'll type commands and you'll reply with what the terminal should show. I want you to only reply with the terminal output inside on… │ 0.7303775 │
└──────────────────────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┴────────────┘
그게 다예요! DuckDB로 벡터 유사도 검색을 성공적으로 수행했어요.
더 알아보기 (Learn more)
- DuckDB를 통한 데이터셋 쿼리로 DuckDB와 Hub 데이터셋 통합을 익혀 보세요.
hf datasets sql로 CLI에서 같은 쿼리를 날려 보세요.