내부 문서나 지원 이력을 검색할 때는 단어가 다르면 키워드 검색으로 결과를 놓치게 됩니다. 표현이 다르면 같은 질문에 답하는 문단도 찾지 못할 수 있습니다. 두 문단이 서로 다른 단어로 같은 내용을 말한다는 것은 알아볼 수 있지만, 단순한 문자열 매칭으로는 그 차이를 인식할 방법이 없습니다. txtai는 텍스트를 의미 벡터로 변환해 검색하는 임베딩 데이터베이스입니다. 검색어와 결과에 공통된 단어가 하나도 없어도 일치하는 내용을 찾을 수 있는지 설치해서 테스트했습니다.
txtai는 임베딩 기반 검색과 LLM 워크플로를 구축하기 위한 올인원 프레임워크입니다. 이 프로젝트는 Apache 2.0 License(*1)로 소스 코드를 공개합니다.
1. 설치하기
네트워크 액세스를 활성화한 빌드 과정에서 임베딩 모델(sentence-transformers/all-MiniLM-L6-v2)을 가져옵니다.
FROM docker.io/library/python:3.12.13-slim
RUN pip install --no-cache-dir --quiet 'txtai==9.11.0'
COPY install_model.py /tmp/install_model.py
RUN python /tmp/install_model.py
WORKDIR /work
# install_model.py
import txtai
embeddings = txtai.Embeddings(path="sentence-transformers/all-MiniLM-L6-v2")
embeddings.index([(0, "warm up the model cache", None)])
print("warmup ok")
podman build -t txtai-sandbox .
2. 공통 단어가 없는 검색 테스트하기
짧은 문장 다섯 개를 인덱싱한 다음, 어느 문장과도 공통된 단어가 없는 검색어로 검색했습니다. 네트워크 연결을 끊은 상태에서 실행했습니다.
import txtai
embeddings = txtai.Embeddings(path="sentence-transformers/all-MiniLM-L6-v2")
documents = [
(0, "The cat sat quietly on the windowsill in the sun.", None),
(1, "Stock markets fell sharply after the interest rate announcement.", None),
(2, "A kitten curled up and slept on the warm windowsill.", None),
(3, "The central bank raised rates to fight inflation.", None),
(4, "She baked a loaf of sourdough bread this morning.", None),
]
embeddings.index(documents)
query = "a small cat napping by a window"
results = embeddings.search(query, 3)
for uid, score in results:
print(f"score={score:.3f} {documents[uid][1]}")
podman run --rm --network none --memory 2g --cpus 2 --pids-limit 512 \
-v "$(pwd)":/work -w /work txtai-sandbox \
python search_demo.py
-v 옵션을 사용하면 컨테이너에서 지정한 디렉터리를 읽고 쓸 수 있습니다. 이 실험은 별도로 마련한 빈 디렉터리에서 실행하고, 그곳에 개인 키나 인증 정보를 두지 마세요.
명령은 다음 결과를 반환했습니다.
QUERY: a small cat napping by a window
score=0.638 A kitten curled up and slept on the warm windowsill.
score=0.612 The cat sat quietly on the windowsill in the sun.
score=0.243 She baked a loaf of sourdough bread this morning.
검색어의 napping은 두 문장 어디에도 나오지 않습니다. 검색어의 small도 kitten과 일치하지 않습니다. 고양이에 관한 두 문장이 1위와 2위를 차지했고, 관련 없는 빵 문장은 훨씬 낮은 점수를 받았습니다. 금리 인상과 주가에 관한 금융 문장 두 개는 상위 3개에 들지 못했습니다. txtai는 문자열이 하나도 일치하지 않는 상황에서도 의미적 유사도만으로 결과 순서를 바꿨습니다.
일치하는 키워드 표현 없이 검색하기
인덱싱된 텍스트와 같은 단어를 사용할 필요는 없습니다. 임베딩 검색에서는 원하는 표현을 사용할 수 있습니다. 시스템이 표현의 차이를 고려해 의미가 가까운 콘텐츠를 반환합니다. 어떤 단어를 입력해야 결과가 나오는지 검색창 앞에서 추측하는 시간을 줄일 수 있습니다.
출처
*1 txtai (GitHub), https://github.com/neuml/txtai, Apache 2.0 License