Created at : 2025-04-18 07:52
Auther: Soo.Y
๐๋ฉ๋ชจ
Day2 ์๋ฃ
- PDF : Embeddings & Vector Stores | Kaggle
- Code : Day 2 - Document Q&A with RAG
- Code : Day 2 - Embeddings and similarity scores
- Code : Day 2 - Classifying embeddings with Keras
Introduction
ํ๋ ๋จธ์ ๋ฌ๋์ ์ด๋ฏธ์ง, ํ ์คํธ, ์ค๋์ค ๋ฑ ๋ค์ํ ํํ์ ๋ฐ์ดํฐ์ ๊ธฐ๋ฐํ์ฌ ๋ฐ์ ํ๊ณ ์๋ค. ์ด ๋ฐฑ์(whitepaper)์์๋ ์ด๋ฌํ ์ด์ง์ ์ธ ๋ฐ์ดํฐ๋ฅผ ํ๋์ ๋ฒกํฐ ํํ์ผ๋ก ํตํฉํ์ฌ ๋ค์ํ ์์ฉ ๋ถ์ผ์์ ์ํ ํ ์ฌ์ฉํ ์ ์๋๋ก ๋์์ฃผ๋ ์๋ฒ ๋ฉ์ ๊ฐ๋ ฅํจ์ ์๊ฐํ๊ณ ์๋ค.
์ด ๋ฐฑ์์์ ๋ค๋ฃจ๋ ์ฃผ์ ๋ด์ฉ
- ์๋ฒ ๋ฉ ์ดํดํ๊ธฐ : ๋ฉํฐ๋ชจ๋ฌ ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌํ๋ ๋ฐ ์ ์๋ฒ ๋ฉ์ด ์ค์ํ์ง, ๊ทธ๋ฆฌ๊ณ ์๋ฒ ๋ฉ์ด ํ์ฉ๋๋ ๋ค์ํ ์ผ์ฉ ์ฌ๋ก๋ฅผ ์ค๋ช ํ๋ค.
- ์๋ฒ ๋ฉ ๊ธฐ๋ฒ : ์๋ก ๋ค๋ฅธ ๋ฐ์ดํฐ ์ ํ(์: ํ ์คํธ, ์ด๋ฏธ์ง ์ค๋์ค ๋ฑ)์ ๊ณตํต๋ ๋ฒกํฐ ๊ณต๊ฐ์ผ๋ก ๋งคํํ๋ ๋ค์ํ ๊ธฐ๋ฒ์ ์๊ฐํ๋ค.
- ํจ์จ์ ์ธ ์ ๋ฒ ๋ฉ ๊ด๋ฆฌ : ๋๊ท๋ชจ ์๋ฒ ๋ฉ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅ, ๊ฒ์, ํ์ํ๋ ๋ฐฉ๋ฒ์ ๋๋ค๋ฃฌ๋ค.
- ๋ฒกํฐ ๋ฐ์ดํฐ๋ฒ ์ด์ค(Vector Databases) : ์๋ฒ ๋ฉ์ ํจ์จ์ ์ผ๋ก ๊ด๋ฆฌํ๊ณ ์ฟผ๋ฆฌํ ์ ์๋ ํนํ๋ ์์คํ ๋ค์ ์๊ฐํ๋ฉฐ, ์ค์ ์ด์ ํ๊ฒฝ์์์ ๊ณ ๋ ค์ฌํญ๋ ํจ๊ป ์ค๋ช ํ๋ค.
- ์ค์ ํ์ฉ ์ฌ๋ก : ์๋ฒ ๋ฉ๊ณผ ๋ฒกํฐ ๋ฐ์ดํฐ๋ฒ ์ด์ค๊ฐ ๋ํ ์ธ์ด ๋ชจ๋ธ๊ณผ ๊ฒฐํฉ๋์ด ํ์ค ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ๊ตฌ์ฒด์ ์ธ ์์ ๋ฅผ ์ดํด๋ณธ๋ค.
๋ฐฑ์ ์ ์ฒด๋ฅผ ํตํด, ํต์ฌ ๊ฐ๋ ์ ์ฒดํํ ์ ์๋ ์ฝ๋ ์์๋ ํจ๊ป ์ ๊ณต๋๋ค.
Why embeddings are important
์๋ฒ ๋ฉ์ด๋, ํ ์คํธ, ์์ฑ, ์ด๋ฏธ์ง, ์์๊ณผ ๊ฐ์ ํ์ค ์ธ๊ณ์ ๋ฐ์ดํฐ๋ฅผ ์์นํํ ํํ์ด๋ค. ์๋ฒ ๋ฉ์ด๋ผ๋ ์ด๋ฆ์ ํ ๊ณต๊ฐ์ ๋ค๋ฅธ ๊ณต๊ฐ์ผ๋ก ๋งคํํ๊ฑฐ๋ ์ฝ์ ํ๋ ์ํ์ ๊ฐ๋ ์์ ์ ๋๋์๋ค. ์๋ฅผ ๋ค์ด ์๋ BERT ๋ชจ๋ธ์ ํ ์คํธ๋ฅผ 768๊ฐ์ ์ซ์ ๋ฒกํฐ๋ก ์๋ฒ ๋ฉํ๋ค. ์ฆ, ๋ชจ๋ ๋ฌธ์ฅ์ ๊ณ ์ฐจ์ ๊ณต๊ฐ์์ 768์ฐจ์์ ์ ์ฐจ์ ๊ณต๊ฐ์ผ๋ก ๋งตํํ๋ ๊ฒ์ด๋ค.
์๋ฒ ๋ฉ์ ํต์ฌ ๊ฐ๋ ์๋ฒ ๋ฉ์ ์ ์ฐจ์ ๋ฒกํฐ๋ก ํํ๋๋ฉฐ, ๋ ๋ฒกํฐ ๊ฐ์ ๊ธฐํํ์ ๊ฑฐ๋ฆฌ(์ ํด๋ฆฌ๋ ๊ฑฐ๋ฆฌ ๋ฑ)๋ ์ค์ ์ธ๊ณ ๊ฐ์ฒด ๊ฐ์ ์๋ฏธ์ ์ ์ฌ์ฑ์ ๋ฐ์ํ๋ค. ์๋ฅผ ๋ค์ด computer๋ผ๋ ๋จ์ด๋ ์ปดํจํฐ ์ด๋ฏธ์ง์๋ ์ ์ฌํ๊ณ laptop๊ณผ๋ ๋น์ทํฎใท๋๋ง car์๋ ์ ์ฌํ์ง ์๋ค. ์ฆ, ์๋ก ๋ค๋ฅธ ์ ํ์ ๋ฐ์ดํฐ๋ฅผ ์ซ์ ๊ณต๊ฐ์์ ๋น๊ตํ๊ณ ๊ฒ์ํ ์ ์๊ฒ ํด์ฃผ๋ ๊ฒ์ด ์๋ฒ ๋ฉ์ ๊ฐ์ฅ ํฐ ์ฅ์ ์ด๋ค.
์๋ฒ ๋ฉ์ ์ผ์ข ์ ์ ๋ณด ์์ถ
- ์๋ฒ ๋ฉ์ ์๋ณธ ๋ฐ์ดํฐ๋ฅผ ์์ค ์๋ ๋ฐฉ์์ผ๋ก ์์ถํ๋ฉด์๋ ์์ง๋จน ํน์ง์ ์ ์งํ๋ค.
- ์ด๋ ๋๊ท๋ชจ ๋ฐ์ดํฐ ์ฒ๋ฆฌ์ ์ ์ฅ์ ํจ์จ์ ์ผ๋ก ๋ง๋ค๊ธฐ ์ํ ํต์ฌ ๋๊ตฌ์ด๋ค.
์ง๊ด์ ์ธ ์์: ์๋์ ๊ฒฝ๋
- ์ง๊ตฌ์์ ์์น๋ฅผ ๋ ์ซ์๋ก ํํํ๋ ๊ฒ๋ ํ๋์ ์๋ฒ ๋ฉ์ด๋ผ๊ณ ๋ณผ ์ ์๋ค.
- ๋ ์์น์ ์๊ฒฝ๋๋ฅผ ๋น๊ตํ๋ฉด ์๋ฆฌ, ์ธ์ ์์น, ์ ์ฌ์ฑ ๋ฑ์ ๊ณ์ฐํ ์ ์๋ค.
- ๋ง์ฐฌ๊ฐ์ง๋ก ํ ์คํธ ์๋ฒ ๋ฉ์์๋ ๋ฒกํฐ ๊ณต๊ฐ์์ ๊ฐ๊น์ด ์์น์ ์๋ ํ ์คํธ๋ ์๋ฏธ์ ์ผ๋ก ์ ์ฌํ ํ ์คํธ์์ ๋ํ๋ธ๋ค.
๊ฒ์๊ณผ ์ถ์ฒ์์์ ์ค์์ฑ RAG, ๊ฒ์, ์ถ์ฒ ์์คํ , ๊ด๊ณ , ์ด์ ํ์ง ๋ฑ ๋ค์ํ ์ค๋ฌด ์์ญ์์ ์๋ฒ ๋ฉ์ ํต์ฌ ์์์ด๋ค. ๋ฒกํฐ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ํตํด, ๊ฑฐ๋ํ ๋ฐ์ดํฐ์ ๋ด์์ ๋น ๋ฅด๊ฒ ์ ์ฌ ํญ๋ชฉ์ ์ฐพ์๋ผ ์ ์๋ ๊ฒ์ด ์ค๋๋ ์ ์ค์๊ฐ ์์คํ ๊ตฌํ์ ๊ฐ๋ฅํ๊ฒ ํ๋ค.
- ๋จ, ์๋/๊ฒฝ๋๋ ์ง๊ตฌ์ ๊ตฌํ ๊ตฌ์กฐ์ ๋ฐ๋ผ ์ค๊ณ๋ ๊ฒ์ด์ง๋ง, ํ ์คํธ ์๋ฒ ๋ฉ ๊ณต๊ฐ์ ์ ๊ฒฝ๋ง์ด ํ์ต์ ํตํด ์๋์ผ๋ก ๋ง๋ค์ด๋ธ ๊ณต๊ฐ์ด๋ค.
์ค์! ์๋ก ๋ค๋ฅธ ๋ชจ๋ธ์์ ์์ฑํ ์๋ฒ ๋ฉ์ ์ง์ ๋น๊ต๊ฐ ๋ถ๊ฐ๋ฅํ๋ฏ๋ก, ํธํ์ฑ๊ณผ ์ผ๊ด์ฑ์ ์ ์งํ ์๋ฒ ๋ฉ ๋ฒ์ ์ ์ฌ์ฉํ๋ ๊ฒ์ด ๋งค์ฐ ์ค์ํ๋ค.
์๋ฒ ๋ฉ ๊ธฐ๋ฐ ๊ฒ์ ์์คํ ์ 3๋จ๊ณ
- ๊ฒ์ ๊ณต๊ฐ์ ์กด์ฌํ๋ ์์ญ์ต ๊ฐ ์์ดํ ์ ๋ํด ์ฌ์ ์๋ฒ ๋ฉ ์ํ
- ์ฟผ๋ฆฌ๋ฅผ ๋์ผํ ์๋ฒ ๋ฉ ๊ณต๊ฐ์ผ๋ก ๋งตํ
- ์ฟผ๋ฆฌ ์๋ฒ ๋ฉ๊ณผ ๊ฐ์ฅ ๊ฐ๊น์ด ์ด์(Nearest Neighbors)์ ์ฐพ์ ๋น ๋ฅด๊ฒ ๋ฐํ
๋ฉํฐ๋ชจ๋ฌ ๋ฐ์ดํฐ์๋ ์ ํฉ ํ๋ ์ ํ๋ฆฌ์ผ์ด์ ์ ํ ์คํธ, ์์ฑ, ์ด๋ฏธ์ง, ์์ ๋ฑ ๋ค์ํ ๋ฐ์ดํฐ ์ ํ์ ํจ๊ป ๋ค๋ฃจ๋ฉฐ, ์ด๋ฐ ํ๊ฒฝ์์๋ ๊ณตํต ์๋ฒ ๋ฉ ๊ณต๊ฐ์ด ํนํ ์ ์ฉํฉ๋๋ค.
์๋ฒ ๋ฉ์ ํต์ฌ ํน์ง ์์ฝ
- ์๋ฏธ์ ์ผ๋ก ์ ์ฌํ ๊ฐ์ฒด๋ฅผ ๊ฐ๊น์ด ์์น์ ๋งคํ
- ๋ค์ด์คํธ๋ฆผ ์์ ์์ ์ ์ฉํ ์์ฝ๋ ์๋ฏธ ํํ์ผ๋ก ์ฌ์ฉ ๊ฐ๋ฅ
- ML ๋ชจ๋ธ ์ ๋ ฅ, ์ถ์ฒ ์์คํ , ๊ฒ์ ์์ง, ๋ถ๋ฅ ๋ชจ๋ธ ๋ฑ์์ ํ์ฉ ๊ฐ๋ฅ
- ์๋ฒ ๋ฉ์ ์์ ์ ๋ฐ๋ผ ์ต์ ํ๋ ํํ์ ๊ฐ์ง ์ ์์ โ ๊ฐ์ ๊ฐ์ฒด๋ผ๋ ๋ชฉ์ ์ ๋ฐ๋ผ ๋ค๋ฅธ ์๋ฒ ๋ฉ ์์ฑ ๊ฐ๋ฅ
Evaluating Embedding Quality
์๋ฒ ๋ฉ ๋ชจ๋ธ์ ํ์ง์ ์ฌ์ฉํ๋ ์์ ์ ๋ฐ๋ผ ์๋ก ๋ค๋ฅธ ๋ฐฉ์์ผ๋ก ํ๊ฐ๋๋ค. ๋๋ถ๋ถ์ ์ผ๋ฐ์ ์ธ ํ๊ฐ ์งํ๋, ๋น์ทํ ํญ๋ชฉ์ ์ ์ฐพ์๋ด๊ณ , ๊ด๋ จ ์๋ ํญ๋ชฉ์ ์ ์ธํ๋ ๋ฅ๋ ฅ์ ์ด์ ์ ๋๋ค. ์ด๋ฌํ ํ๊ฐ๋ ๋ณดํต ์ ๋ต์ด ๋ ์ด๋ธ๋ง๋ ๋ฐ์ดํฐ์ ์ด ํ์ํ๋ฉฐ, ์๋ฅผ ๋ค์ด Snippet 0์์๋ NFCorpus ๋ฐ์ดํฐ์ ์ ์ฌ์ฉํ์ฌ ๋ค์ํ ์งํ๋ฅผ ์ค๋ช ํ๋ค.
๊ฒ์ ๊ณผ์ ์์์ ์ฃผ์ ํ๊ฐ ์งํ
- ์ ํ๋(Precision) : ๊ฒ์๋ ๋ฌธ์ ์ค์์ ์ผ๋ง๋ ๋ง์ ๋ฌธ์๊ฐ ์ค์ ๋ก ๊ด๋ จ์๋ ๋ฌธ์์ธ์ง ํ๊ฐ
- ์: 10๊ฐ ๋ฌธ์ ์ค 7๊ฐ๊ฐ ๊ด๋ จ ์๋ค๋ฉด โ
precision@10 = 7/10 = 0.7
- ์: 10๊ฐ ๋ฌธ์ ์ค 7๊ฐ๊ฐ ๊ด๋ จ ์๋ค๋ฉด โ
- ์ฌํ์จ(Recall) : ์ ์ฒด ๊ด๋ จ ๋ฌธ์ ์ค์์ ์ผ๋ง๋ ๋ง์ด ๊ฒ์๋์๋์ง ํ๊ฐ
- ์: ๊ด๋ จ ๋ฌธ์๊ฐ ์ด 6๊ฐ์ธ๋ฐ ๊ทธ ์ค 3๊ฐ๊ฐ ๊ฒ์๋์๋ค๋ฉด โ
recall@K = 3/6 = 0.5
- ์: ๊ด๋ จ ๋ฌธ์๊ฐ ์ด 6๊ฐ์ธ๋ฐ ๊ทธ ์ค 3๊ฐ๊ฐ ๊ฒ์๋์๋ค๋ฉด โ
์ด์์ ์ธ ์๋ฒ ๋ฉ ๋ชจ๋ธ์ด๋ผ๋ฉด
- ๋ชจ๋ ๊ด๋ จ ๋ฌธ์๋ ๋น ์ง์์ด ๊ฒ์ํ๊ณ
- ๊ด๋ จ ์๋ ๋ฌธ์๋ ํ๋๋ ํฌํจ๋์ง ์์์ผ ํ๋ค. ํ์ง๋ง ํ์ค์์๋ ์ผ๋ถ ๊ด๋ จ ๋ฌธ์๋ฅผ ๋์น๊ธฐ๋ ํ๊ณ , ๊ด๋ จ ์๋ ๋ฌธ์๊ฐ ํฌํจ๋๊ธฐ๋ ํ๋ฏ๋ก ์ ๋์ ์ธ ๊ธฐ์ค์ด ํ์ํ๋ค.
์์ ์ ๋ณด๊ฐ ์๋ ๊ฒฝ์ฐ์ ํ๊ฐ
- Precision/Recall์ ๊ด๋ จ์ฑ ์ฌ๋ถ๊ฐ ์ด์ง(Binary)์ผ ๋ ์ ์ฉํ์ง๋ง, ์ค์ ๊ฒ์ ํ๊ฒฝ์์๋ ๋ ๊ด๋ จ์ฑ ๋์ ๋ฌธ์๊ฐ ์์ ๋์ค๋ ๊ฒ์ด ๋งค์ฐ ์ค์ํ๋ค.
- ์ด๋ด ๋ ์ฌ์ฉํ๋ ๋ํ์ ์ธ ์งํ๊ฐ ๋ฐ๋ก โ์ ๊ทํ ํ ์ธ ๋์ ์ด๋โ(nDCG)์ด๋ค.
nDCG(Normalized Discounted Cumulative Gain)
- ๊ฐ ๋ฌธ์์ ๊ด๋ จ์ฑ ์ ์(reli)๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๊ณ์ฐ
- ํ๋จ์ ์์นํ ๋ฌธ์๋ ํจ๋ํฐ๋ฅผ ๋ถ์ฌ
- ์ด์์ ์ธ ์์๋ฅผ ๊ธฐ์ค์ผ๋ก ์ ๊ทํํ์ฌ 0.0 ~ 1.0 ์ฌ์ด์ ๊ฐ์ผ๋ก ํํํ์ฌ ์๋ก ๋ค๋ฅธ ์ฟผ๋ฆฌ๋ ์์คํ ๊ฐ ๊ณต์ ํ ๋น๊ต ๊ฐ๋ฅ
์์ ์ ๋ฆฌ
โ DCG@p (์ ๋ ฌ๋ ๊ฒฐ๊ณผ์ ํ ์ธ ๋์ ์ด๋)
-
โ: ์์ iii์ ์๋ ๋ฌธ์์ ๊ด๋ จ์ฑ ์ ์
-
: ๊ฒ์ ๊ฒฐ๊ณผ์ ์์ (1๋ถํฐ ์์)
-
: ํ๊ฐํ ์์ ๋ฌธ์ ์ (ex: @10์ด๋ฉด ์์ 10๊ฐ ๋ฌธ์๋ง ์ฌ์ฉ)
โก IDCG@p (์ด์์ ์ธ DCG)
- โ: ๊ด๋ จ์ฑ ์ ์๋ฅผ ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌํ ์ด์์ ์์
โข nDCG@p (์ ๊ทํ๋ ์ ์)
-
๊ฐ ๋ฒ์: 0.0 ~ 1.0
-
1์ ๊ฐ๊น์ธ์๋ก โ ์ด์์ ์ธ ์์์ ๊ฐ๊น์
import numpy as np
def dcg_at_k(relevance_scores, k):
"""
relevance_scores: ๊ด๋ จ์ฑ ์ ์ ๋ฆฌ์คํธ (์: [3, 2, 3, 0, 1])
k: ์์ ๋ช ๊ฐ๊น์ง ํ๊ฐํ ์ง (์: k=5)
"""
relevance_scores = np.asfarray(relevance_scores)[:k]
if relevance_scores.size == 0:
return 0.0
return np.sum((2 ** relevance_scores - 1) / np.log2(np.arange(2, relevance_scores.size + 2)))
def ndcg_at_k(predicted_relevance, ideal_relevance, k):
"""
predicted_relevance: ์์ธก๋ ์์๋๋ก ์ ๋ ฌ๋ ๊ด๋ จ์ฑ ์ ์ ๋ฆฌ์คํธ
ideal_relevance: ์ด์์ ์ธ ์ ๋ ฌ ์์์ ๊ด๋ จ์ฑ ์ ์ ๋ฆฌ์คํธ (๋ด๋ฆผ์ฐจ์)
"""
dcg = dcg_at_k(predicted_relevance, k)
idcg = dcg_at_k(sorted(ideal_relevance, reverse=True), k)
return dcg / idcg if idcg != 0 else 0.0
# ์์ธก๋ ๊ฒ์ ๊ฒฐ๊ณผ์ ๊ด๋ จ์ฑ ์ ์ (์: ๋ชจ๋ธ ์ถ๋ ฅ ์์ ๊ธฐ์ค)
pred = [3, 2, 0, 1, 0]
# ํด๋น ์ฟผ๋ฆฌ์ ๋ํด ์ ๋ต ๋ฌธ์์ ์ด์์ ๊ด๋ จ์ฑ ์ ์ ์์
ideal = [3, 3, 2, 1, 0]
# ํ๊ฐํ k๊ฐ (์: ์์ 5๊ฐ ๋ฌธ์ ๊ธฐ์ค)
k = 5
print(f"nDCG@{k}:", round(ndcg_at_k(pred, ideal, k), 4))
๊ณต๊ฐ ๋ฒค์น๋งํฌ
-
BEIR: ๊ฒ์/์ง๋ฌธ์๋ต ๋ฑ ๋ค์ํ ์์ ์ ํ๊ฐํ๋ ๋ํ ๋ฒค์น๋งํฌ
-
MTEB (Massive Text Embedding Benchmark): ๋๊ท๋ชจ ์๋ฒ ๋ฉ ํ์ง ๋น๊ต๋ฅผ ์ํ ๋ฒค์น๋งํฌ
-
**TREC (Text REtrieval Conference)**์์ ์ ์ํ
trec_eval
์ด๋ Python ๋ํผ์ธpytrec_eval
์ ํตํด
Precision, Recall, nDCG ๋ฑ ๋ค์ํ ์งํ๋ฅผ ์ผ๊ด๋๊ฒ ๊ณ์ฐ ๊ฐ๋ฅ
์์ฉ ํ๊ฒฝ์์์ ์ต์ ํ๊ฐ
- ์ด๋ค ์๋ฒ ๋ฉ ๋ชจ๋ธ์ด โ์ต์ โ์ธ์ง๋ ์ ์ฉ ๋ถ์ผ์ ๋ฐ๋ผ ๋ฌ๋ผ์ง ์ ์์ต๋๋ค.
ํ์ง๋ง ๋๋ถ๋ถ์ ๊ฒฝ์ฐ ๋ค์๊ณผ ๊ฐ์ ์ง๊ด์ด ์ข์ ์ถ๋ฐ์ ์ด ๋ฉ๋๋ค:
โ์๋ก ๋น์ทํ ๊ฐ์ฒด๋ ์๋ฒ ๋ฉ ๊ณต๊ฐ์์๋ ๊ฐ๊น์ด ์์นํด์ผ ํ๋ค.โ
๊ทธ ์ธ ๊ณ ๋ ค ์์
- ๋ชจ๋ธ ํฌ๊ธฐ (Model Size)
- ์๋ฒ ๋ฉ ์ฐจ์ ์ (Embedding Dimension Size)
- ์๋ต ์ง์ฐ ์๊ฐ (Latency)
- ์ ์ฒด ์์คํ ๋น์ฉ (Total Cost)
์ค์ ์ ํ ํ๊ฒฝ์์ ์๋ฒ ๋ฉ ๋ชจ๋ธ์ ์ ํํ ๋ ๋งค์ฐ ์ค์ํ ์์์ ๋๋ค.
Search Example
Types of embeddings
Text embeddings
Word embeddings
Document embeddings
Shallow BoW models
Deeper pretrained large language models
Images & multimodal embeddings
Structured data embeddings
General structured data
User/item structured data
Graph embeddings
Training Embeddings
Vector search
Important vector search algorithms
Locality sensitive hashing & trees
Hierarchical navigable small worlds
ScaNN
Vector databases
Operational considerations
Applications
Q&A with sources(retrieval augmented generation)
RAG(Retrieval-Augmented Generation)์ ์ ๋ณด ๊ฒ์(Retrieval)๊ณผ ํ ์คํธ ์์ฑ(Generation)์ ์ฅ์ ์ ๊ฒฐํฉํ Q&A๋ฐฉ์์ด๋ค.
์ด๋ป๊ฒ ์๋ํ๋์?
- ์ง์ ๋ฒ ์ด์ค์์ ๊ด๋ จ ๋ฌธ์๋ฅผ ๋จผ์ ๊ฒ์ํ๋ค.
- ๊ฒ์๋ ์ ๋ณด๋ฅผ ํ๋กฌํํธ์ ์ถ๊ฐํ๋ค.
- ํ์ฅ๋ ํ๋กฌํํธ๋ฅผ ๋ฐํ์ผ๋ก LLM์ด ์๋ต์ ์์ฑํ๋ค.
Prompt Expansion์ด๋?
- ํ๋กฌํํธ ํ์ฅ(Prompt Expansion)์ ๋ฐ์ดํฐ๋ฒ ์ด์ค ๊ฒ์ ๊ฒฐ๊ณผ(์ฃผ๋ก ์๋ฏธ ๊ธฐ๋ฐ ๊ฒ์ + ๋น์ง๋์ค ๊ท์น)์ ์๋ ํ๋กฌํํธ์ ๋ง๋ถ์ด๋ ๊ธฐ์ ์ด๋ค.
RAG๋ ์ด๋ค ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ๊น? LLM์ด ๋ํ์ ์ธ ๋ ๊ฐ์ง ๋ฌธ์ ๋ฅผ ์ํํ๋ ๋ฐ ์ ์ฉํ๋ค.
- ํ๊ฐ(hallucination)
- LLM์ด ์ฌ์ค์ด ์๋ ๋ด์ฉ์ ๊ทธ๋ด๋ฏํ๊ฒ ๋ง๋ค์ด๋ด๋ ํ์
- RAG๋ ์ ๋ขฐํ ์ ์๋ ๋ฌธ์ ๊ธฐ๋ฐ์ผ๋ก ๋ต๋ณ์ ์์ฑํ์ฌ ์ด๋ฅผ ์ค์ฌ์ค๋ค.
- ์์ฃผ ์ฌํ์ตํด์ผ ํ๋ ๋ฌธ์
- ์ต์ ์ ๋ณด๋ฅผ ๋ฐ์ํ๋ ค๋ฉด ๋ชจ๋ธ์ ์ฌํ๋ จํด์ผ ํ๋ ๋น์ฉ์ด ํฌ๋ค.
- ํ์ง๋ง RAG๋ ์ต์ ์ ๋ณด๋ฅผ ํ๋กฌํํธ๋ก ์ง์ ์ ๋ฌํ ์ ์๊ธฐ ๋๋ฌธ์, ์ฌํ๋ จ ์์ด๋ ์ต์ ์๋ต ์์ฑ ๊ฐ๋ฅ
๋จ, RAG๊ฐ ํ๊ฐ์ ์์ ํ ์ ๊ฑฐํ์ง ์๋๋ค.
ํด๊ฒฐ์ฑ : ์ถ์ฒ(source) ๋ฐํ + ์ ํ์ฑ ๊ฒ์ฌ
- ํ๊ฐ์ ๋ ์ค์ด๊ธฐ ์ํด์๋ ๊ฒ์๋ ์ถ์ฒ๋ฅผ ํจ๊ป ๋ฐํํ๊ณ ์ฌ๋์ด๋ LLM์ด ํด๋น ์ถ์ฒ์ ์๋ต์ ์ผ๊ด์ฑ์ ํ์ธํ๋ ๊ฒ์ด ์ข๋ค.
- ์ด๋ ๊ฒ ํ๋ฉด LLM์ด ์์ฑํ ์๋ต์ด ์ค์ ๋ก ์๋ฏธ์ ์ผ๋ก ์ ์ฌํ ์ถ์ฒ ์ ๋ณด์ ์ผ์นํ๋์ง ๊ฒ์ฆํ ์ ์๋ค.
# Before you start run this command:
# pip install --upgrade --user --quiet google-cloud-aiplatform langchain_google_vertexai
# after running pip install make sure you restart your kernel
# TODO : Set values as per your requirements
# Project and Storage Constants
PROJECT_ID = "<my_project_id>"
REGION = "<my_region>"
BUCKET = "<my_gcs_bucket>"
BUCKET_URI = f"gs://{BUCKET}"
# The number of dimensions for the text-embedding-005 is 768
# If other embedder is used, the dimensions would probably need to change.
DIMENSIONS = 768
# Index Constants
DISPLAY_NAME = "<my_matching_engine_index_id>"
DEPLOYED_INDEX_ID = "yourname01" # you set this. Start with a letter.
from google.cloud import aiplatform
from langchain_google_vertexai import VertexAIEmbeddings
from langchain_google_vertexai import VertexAI
from langchain_google_vertexai import (
VectorSearchVectorStore,
VectorSearchVectorStoreDatastore,
)
from langchain.chains import RetrievalQA
from langchain.prompts.chat import (
ChatPromptTemplate,
SystemMessagePromptTemplate,
HumanMessagePromptTemplate,
)
from IPython.display import display, Markdown
aiplatform.init(project=PROJECT_ID, location=REGION, staging_bucket=BUCKET_URI)
embedding_model = VertexAIEmbeddings(model_name="text-embedding-005")
# NOTE : This operation can take upto 30 seconds
my_index = aiplatform.MatchingEngineIndtex.create_tree_ah_index(
display_name=DISPLAY_NAME,
dimensions=DIMENSIONS,
approximate_neighbors_count=150,
distance_measure_type="DOT_PRODUCT_DISTANCE",
index_update_method="STREAM_UPDATE", # allowed values BATCH_UPDATE , STREAM_UPDATE
)
# Create an endpoint
my_index_endpoint = aiplatform.MatchingEngineIndexEndpoint.create(
display_name=f"{DISPLAY_NAME}-endpoint", public_endpoint_enabled=True
)
# NOTE : This operation can take upto 20 minutes
my_index_endpoint = my_index_endpoint.deploy_index(
index=my_index, deployed_index_id=DEPLOYED_INDEX_ID
)
my_index_endpoint = my_index_endpoint.deploy_index(
index=my_index, deployed_index_id=DEPLOYED_INDEX_ID
)
my_index_endpoint.deployed_indexes
# TODO : replace 1234567890123456789 with your acutial index ID
my_index = aiplatform.MatchingEngineIndex("1234567890123456789")
# TODO : replace 1234567890123456789 with your acutial endpoint ID
# Be aware that the Index ID differs from the endpoint ID
my_index_endpoint = aiplatform.MatchingEngineIndexEndpoint("1234567890123456789")
from langchain_google_vertexai import (
VectorSearchVectorStore,
VectorSearchVectorStoreDatastore,
)
# Input texts
texts = [
"The cat sat on",
"the mat.",
"I like to",
"eat pizza for",
"dinner.",
"The sun sets",
"in the west.",
]
# Create a Vector Store
vector_store = VectorSearchVectorStore.from_components(
project_id=PROJECT_ID,
region=REGION,
gcs_bucket_name=BUCKET,
index_id=my_index.name,
endpoint_id=my_index_endpoint.name,
embedding=embedding_model,
stream_update=True,
)
# Add vectors and mapped text chunks to your vectore store
vector_store.add_texts(texts=texts)
# Initialize the vectore_store as retriever
retriever = vector_store.as_retriever()
# perform simple similarity search on retriever
retriever.invoke("What are my options in breathable fabric?")