Cohere 임베딩

Cohere 임베딩 (Cohere)

Qdrant는 Cohere의 co.embed API와 공식 Python SDK와 호환돼요. SDK는 다른 패키지처럼 pip으로 설치하면 되고요, co.embed API가 반환한 임베딩을 그대로 Qdrant 클라이언트의 upsert 호출에 넣어 쓰면 돼요. 벡터 데이터베이스와 임베딩 모델을 따로따로 관리할 필요가 없어서 흐름이 단순해져요.

출처: Qdrant 공식 문서 — cohere

설치 (Setup)

pip install cohere

기본 사용법

cohere.Client로 모델을 호출해 임베딩을 얻고, 그 결과를 qdrant_clientupsert에 넣는 기본 패턴이에요.

import cohere
import qdrant_client
from qdrant_client.models import Batch

cohere_client = cohere.Client("<< your_api_key >>")
qdrant_client = qdrant_client.QdrantClient()

qdrant_client.upsert(
    collection_name="MyCollection",
    points=Batch(
        ids=[1],
        vectors=cohere_client.embed(
            model="large",
            texts=["The best vector database"],
        ).embeddings,
    ),
)

co.embed API와 Qdrant로 만든 end-to-end 프로젝트를 보고 싶다면 “Question Answering as a Service with Cohere and Qdrant” 문서를 참고해요.

Embed v3 모델 사용하기

Embed v3는 2023년 11월에 공개된 Cohere 모델의 새 계열이에요. 이 모델들은 API 호출에 input_type 파라미터를 추가로 전달해야 하는데, 이 값이 임베딩을 어떤 용도로 쓸지 결정해요.

  • input_type="search_document" — Qdrant에 저장할 문서용
  • input_type="search_query" — 관련 문서를 찾기 위한 검색 쿼리용
  • input_type="classification" — 분류(classification) 작업용
  • input_type="clustering" — 텍스트 클러스터링용

RAG 같은 의미 기반 검색 애플리케이션을 만들 때는 저장할 문서에 search_document, 검색 쿼리에 search_query를 사용하는 게 원칙이에요. 다음 예시는 Embed v3 모델로 문서를 인덱싱하는 코드예요.

import cohere
import qdrant_client
from qdrant_client.models import Batch

cohere_client = cohere.Client("<< your_api_key >>")
client = qdrant_client.QdrantClient()

client.upsert(
    collection_name="MyCollection",
    points=Batch(
        ids=[1],
        vectors=cohere_client.embed(
            model="embed-english-v3.0",      # New Embed v3 model
            input_type="search_document",    # Input type for documents
            texts=["Qdrant is the a vector database written in Rust"],
        ).embeddings,
    ),
)

문서를 인덱싱했다면 같은 Embed v3 모델로 가장 관련 있는 문서를 검색할 수 있어요. 검색할 때는 input_type="search_query"를 쓰는 점이 달라요.

client.query_points(
    collection_name="MyCollection",
    query=cohere_client.embed(
        model="embed-english-v3.0",       # New Embed v3 model
        input_type="search_query",        # Input type for search queries
        texts=["The best vector database"],
    ).embeddings[0],
)

더 알아보기 (Learn more)