Mistral 임베딩

Mistral 임베딩 (Mistral)

Qdrant는 Mistral Embed와 그 공식 Python SDK와 호환돼요. 읽는 시간은 약 10분, 난이도는 초급 수준이에요. SDK를 설치하고 나면 Qdrant와 Mistral 클라이언트를 함께 만들어 문서를 임베딩하고 검색하는 전체 흐름을 따라 해볼 수 있어요.

출처: Qdrant 공식 문서 — mistral

설정 (Setup)

클라이언트 설치

pip install mistralai

그다음 Qdrant와 Mistral 클라이언트를 준비해요. QdrantClient(":memory:")는 인메모리 모드라 별도의 서버 없이 바로 실습해 볼 수 있어요.

from mistralai.client import MistralClient
from qdrant_client import QdrantClient
from qdrant_client.models import PointStruct, VectorParams, Distance

collection_name = "example_collection"
MISTRAL_API_KEY = "your_mistral_api_key"
client = QdrantClient(":memory:")
mistral_client = MistralClient(api_key=MISTRAL_API_KEY)

texts = [
    "Qdrant is the best vector search engine!",
    "Loved by Enterprises and everyone building for low latency, high performance, and scale.",
]

문서 임베딩

mistral-embed 모델로 문서를 임베딩해요. 반환된 결과에는 data 필드가 있고, 각 항목의 embedding 키에 임베딩 벡터(숫자 float 리스트)가 담겨요.

result = mistral_client.embeddings(
    model="mistral-embed",
    input=texts,
)

Qdrant Point로 변환

임베딩 결과와 원본 텍스트를 짝지어 Qdrant의 PointStruct 리스트를 만들어요. id에는 인덱스를, payload에는 원본 text를 넣어 나중에 검색 결과에서 함께 꺼낼 수 있게 해요.

points = [
    PointStruct(
        id=idx,
        vector=response.embedding,
        payload={"text": text},
    )
    for idx, (response, text) in enumerate(zip(result.data, texts))
]

컬렉션 생성과 문서 삽입

mistral-embed는 임베딩 차원이 1024라서 VectorParams(size=1024)로 컬렉션을 만들고, 거리 계산은 코사인 유사도(Distance.COSINE)를 사용해요. 그런 다음 upsert로 포인트를 넣어요.

client.create_collection(
    collection_name,
    vectors_config=VectorParams(
        size=1024,
        distance=Distance.COSINE,
    ),
)
client.upsert(collection_name, points)

Qdrant로 문서 검색

문서가 인덱싱되면 같은 모델로 검색 쿼리를 임베딩해서 가장 관련 있는 문서를 찾을 수 있어요.

client.search(
    collection_name=collection_name,
    query_vector=mistral_client.embeddings(
        model="mistral-embed",
        input=["What is the best to use for vector search scaling?"],
    ).data[0].embedding,
)

성능: Binary Quantization 활용

Mistral 임베딩 모델은 Binary Quantization과 함께 쓸 수 있어요. 이 기법은 임베딩 크기를 32배 줄이면서도 검색 품질을 크게 떨어뜨리지 않게 도와줘요.

공식 문서에 따르면 oversampling 3, limit 100에서 rescore를 켜면 정확한 최근접 이웃(exact nearest neighbors) 대비 recall 95%를 달성한다고 해요. 아래 표는 oversampling과 rescore 설정에 따른 recall 값을 보여줘요.

Oversampling 1 1 2 2 3 3
Rescore False True False True False True
Limit
10 0.53444 0.857778 0.534444 0.918889 0.533333 0.941111
20 0.508333 0.837778 0.508333 0.903889 0.508333 0.927778
50 0.492222 0.834444 0.492222 0.903556 0.492889 0.940889
100 0.499111 0.845444 0.498556 0.918333 0.497667 0.944556

rescore를 켤 때 recall이 눈에 띄게 올라가는 걸 확인할 수 있어요. 이제 Mistral 임베딩 모델을 Qdrant와 함께 쓸 준비가 된 거예요.

더 알아보기 (Learn more)