Voyage AI 임베딩

Voyage AI 임베딩 (Voyage AI)

Qdrant는 Voyage AI 임베딩과 함께 사용할 수 있어요. 지원되는 모델 목록은 Voyage AI 페이지에서 확인할 수 있고, API 키는 Voyage AI 대시보드에서 발급받아 요청 인증에 써요. Python과 TypeScript 두 방식 모두 지원돼요.

출처: Qdrant 공식 문서 — voyage

Qdrant와 Voyage 클라이언트 준비

파이썬에서는 voyageai 패키지로 Voyage AI 클라이언트를 만들고, Qdrant 클라이언트와 함께 준비해요.

from qdrant_client import QdrantClient
import voyageai

VOYAGE_API_KEY = "<YOUR_VOYAGEAI_API_KEY>"

qclient = QdrantClient(":memory:")
vclient = voyageai.Client(api_key=VOYAGE_API_KEY)

texts = [
    "Qdrant is the best vector search engine!",
    "Loved by Enterprises and everyone building for low latency, high performance, and scale.",
]
import {QdrantClient} from '@qdrant/js-client-rest';

const VOYAGEAI_BASE_URL = "https://api.voyageai.com/v1/embeddings"
const VOYAGEAI_API_KEY = "<YOUR_VOYAGEAI_API_KEY>"

const client = new QdrantClient({ url: 'http://localhost:6333' });

const headers = {
    "Authorization": "Bearer " + VOYAGEAI_API_KEY,
    "Content-Type": "application/json"
}

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

문서 임베딩

다음 예시는 크기 1536의 문장 임베딩을 생성하는 voyage-large-2 모델로 문서를 임베딩해요. input_typedocument로 주어 문서에 대한 임베딩을 만들고 있어요.

response = vclient.embed(texts, model="voyage-large-2", input_type="document")
let body = {
    "input": texts,
    "model": "voyage-large-2",
    "input_type": "document",
}

let response = await fetch(VOYAGEAI_BASE_URL, {
    method: "POST",
    body: JSON.stringify(body),
    headers
});

let response_body = await response.json();

모델 출력을 Qdrant Point로 변환

파이썬에서는 response.embeddings에서, TypeScript에서는 response_body.data에서 각 임베딩을 꺼내 원본 텍스트와 함께 PointStruct로 만들어요.

from qdrant_client.models import PointStruct

points = [
    PointStruct(
        id=idx,
        vector=embedding,
        payload={"text": text},
    )
    for idx, (embedding, text) in enumerate(zip(response.embeddings, texts))
]
let points = response_body.data.map((data, i) => {
    return {
        id: i,
        vector: data.embedding,
        payload: {
            text: texts[i]
        }
    }
});

컬렉션 생성과 문서 삽입

voyage-large-2는 임베딩 차원이 1536이므로 VectorParams(size=1536)로 컬렉션을 만들고, 코사인 유사도(Distance.COSINE)로 검색하도록 설정해요.

from qdrant_client.models import VectorParams, Distance

COLLECTION_NAME = "example_collection"

qclient.create_collection(
    COLLECTION_NAME,
    vectors_config=VectorParams(
        size=1536,
        distance=Distance.COSINE,
    ),
)
qclient.upsert(COLLECTION_NAME, points)
const COLLECTION_NAME = "example_collection"

await client.createCollection(COLLECTION_NAME, {
    vectors: {
        size: 1536,
        distance: 'Cosine',
    }
});

await client.upsert(COLLECTION_NAME, {
    wait: true,
    points
});

Qdrant로 문서 검색

문서를 추가한 뒤에는 가장 관련 있는 문서를 검색할 수 있어요. 검색 쿼리도 같은 모델로 임베딩해 벡터 차원을 맞춰야 해요.

response = vclient.embed(
    ["What is the best to use for vector search scaling?"],
    model="voyage-large-2",
    input_type="query",
)

qclient.search(
    collection_name=COLLECTION_NAME,
    query_vector=response.embeddings[0],
)

더 알아보기 (Learn more)