리랭킹(Reranking) - 퀵스타트

리랭킹(Reranking) - 퀵스타트

Cohere의 리랭킹 모델(v2 API)로 리랭킹을 수행하는 퀵스타트 가이드예요.

출처: 문서

본문

리랭킹이란

Cohere의 리랭킹 모델은 Rerank 엔드포인트를 통해 사용할 수 있어요. 이 엔드포인트는 어떤 키워드 검색이나 벡터 검색 시스템의 검색 품질에도 강력한 의미 기반(semantic) 부스트를 더해 줘요.

이 퀵스타트 가이드는 Rerank 엔드포인트로 리랭킹을 수행하는 방법을 보여드려요.

설정

먼저 다음 명령으로 Cohere Python SDK를 설치해요.

pip install -U cohere

다음으로 라이브러리를 import하고 클라이언트를 만들어요.

Cohere Platform

PYTHON

import cohere

co = cohere.ClientV2(
    "COHERE_API_KEY"
)  # Get your free API key here: https://dashboard.cohere.com/api-keys

Private Deployment

PYTHON

import cohere

co = cohere.ClientV2(
    api_key="",  # Leave this blank
    base_url="<YOUR_DEPLOYMENT_URL>",
)

Bedrock

PYTHON

import cohere

co = cohere.BedrockClientV2(
    aws_region="AWS_REGION",
    aws_access_key="AWS_ACCESS_KEY_ID",
    aws_secret_key="AWS_SECRET_ACCESS_KEY",
    aws_session_token="AWS_SESSION_TOKEN",
)

# Get the model name: https://docs.aws.amazon.com/bedrock/latest/userguide/models-supported.html

SageMaker

PYTHON

import cohere

co = cohere.SagemakerClientV2(
    aws_region="AWS_REGION",
    aws_access_key="AWS_ACCESS_KEY_ID",
    aws_secret_key="AWS_SECRET_ACCESS_KEY",
    aws_session_token="AWS_SESSION_TOKEN",
)

Azure AI

PYTHON

import cohere

co = cohere.ClientV2(
    api_key="AZURE_API_KEY",
    base_url="AZURE_ENDPOINT",  # example: "https://cohere-command-r-plus-08-2024-xyz.eastus.models.ai.azure.com/"
)

리랭킹할 문서(Retrieved Documents)

먼저 리랭킹할 문서 목록을 정의해요.

PYTHON

documents = [
    "Reimbursing Travel Expenses: Easily manage your travel expenses by submitting them through our finance tool. Approvals are prompt and straightforward.",
    "Working from Abroad: Working remotely from another country is possible. Simply coordinate with your manager and ensure your availability during core hours.",
    "Health and Wellness Benefits: We care about your well-being and offer gym memberships, on-site yoga classes, and comprehensive health insurance.",
    "Performance Reviews Frequency: We conduct informal check-ins every quarter and formal performance reviews twice a year.",
]

리랭킹 수행

그런 다음, Rerank 엔드포인트에 문서들과 사용자 쿼리를 전달해서 리랭킹을 수행해요.

Cohere Platform

PYTHON

# Add the user query
query = "Are there fitness-related perks?"

# Rerank the documents

results = co.rerank(
    model="rerank-v4.0-pro", query=query, documents=documents, top_n=2
)

for result in results.results:
    print(result)

Private Deployment

PYTHON

# Add the user query
query = "Are there fitness-related perks?"

# Rerank the documents
results = co.rerank(
    model="rerank-v4.0-pro", query=query, documents=documents, top_n=2
)

for result in results.results:
    print(result)

Bedrock

PYTHON

# Add the user query
query = "Are there fitness-related perks?"

# Rerank the documents

results = co.rerank(
    model="YOUR_MODEL_NAME", query=query, documents=documents, top_n=2
)

for result in results.results:
    print(result)

SageMaker

PYTHON

# Add the user query
query = "Are there fitness-related perks?"

# Rerank the documents
results = co.rerank(
    model="YOUR_ENDPOINT_NAME",
    query=query,
    documents=documents,
    top_n=2,
)

for result in results.results:
    print(result)

Azure AI

PYTHON

# Add the user query
query = "Are there fitness-related perks?"

# Rerank the documents

results = co.rerank(
    model="model",  # Pass a dummy string
    query=query,
    documents=documents,
    top_n=2,
)

for result in results.results:
    print(result)
document=None index=2 relevance_score=0.115670934
document=None index=1 relevance_score=0.01729751

더 보기(Further Resources)

더 알아보기 (Learn more)