Upstage Solar Embeddings 연동

Upstage Solar Embeddings 연동 (upstage)

Qdrant는 UpstageSolar Embeddings API와 함께 동작해요. Solar Embeddings API는 통합된 벡터 공간 안에서 사용자 쿼리용 모델문서 임베딩용 모델, 두 개의 듀얼 모델을 제공하며, 성능 좋은 텍스트 처리를 위해 설계되었어요.

요청을 인증하기 위한 API 키는 Upstage Console에서 생성할 수 있습니다.

출처: Qdrant 공식 문서 — upstage

Qdrant 클라이언트와 Upstage 세션 설정하기

import requests
from qdrant_client import QdrantClient

UPSTAGE_BASE_URL = "https://api.upstage.ai/v1/solar/embeddings"

UPSTAGE_API_KEY = "<YOUR_API_KEY>"

upstage_session = requests.Session()

client = QdrantClient(url="http://localhost:6333")

headers = {
    "Authorization": f"Bearer {UPSTAGE_API_KEY}",
    "Accept": "application/json",
}

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 UPSTAGE_BASE_URL = "https://api.upstage.ai/v1/solar/embeddings"
const UPSTAGE_API_KEY = "<YOUR_API_KEY>"

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

const headers = {
    "Authorization": "Bearer " + UPSTAGE_API_KEY,
    "Accept": "application/json",
    "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.",
]

아래 예제는 권장되는 solar-embedding-1-large-passagesolar-embedding-1-large-query 모델로 문서를 임베딩하는 방법을 보여줘요. 이 모델들은 크기 4096의 문장 임베딩을 만들어 냅니다.

문서 임베딩 (Embedding documents)

body = {
    "input": texts,
    "model": "solar-embedding-1-large-passage",
}

response_body = upstage_session.post(
    UPSTAGE_BASE_URL, headers=headers, json=body
).json()
let body = {
    "input": texts,
    "model": "solar-embedding-1-large-passage",
}

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

let response_body = await response.json()

모델 출력을 Qdrant points로 변환하기

from qdrant_client.models import PointStruct

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

문서를 삽입할 컬렉션 생성하기

from qdrant_client.models import VectorParams, Distance

collection_name = "example_collection"

client.create_collection(
    collection_name,
    vectors_config=VectorParams(
        size=4096,
        distance=Distance.COSINE,
    ),
)
client.upsert(collection_name, points)
const COLLECTION_NAME = "example_collection"

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

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

Qdrant로 문서 검색하기

모든 문서가 추가된 뒤에는 가장 관련성 높은 문서를 검색할 수 있어요. 검색할 때는 문서 임베딩이 아니라 쿼리용 모델(solar-embedding-1-large-query)을 사용한다는 점을 주목하세요.

body = {
    "input": "What is the best to use for vector search scaling?",
    "model": "solar-embedding-1-large-query",
}

response_body = upstage_session.post(
    UPSTAGE_BASE_URL, headers=headers, json=body
).json()

client.query_points(
    collection_name=collection_name,
    query=response_body["data"][0]["embedding"],
)
body = {
    "input": "What is the best to use for vector search scaling?",
    "model": "solar-embedding-1-large-query",
}

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

response_body = await response.json()

await client.query(COLLECTION_NAME, {
    query: response_body.data[0].embedding,
});

더 알아보기 (Learn more)