Cohere Rerank 모델 개요

Cohere Rerank 모델 개요

Cohere의 Rerank 모델이 어떻게 동작하는지 알아볼 거예요. 주어진 query와 documents 목록에 대해 Rerank가 문서들을 쿼리와의 의미적 관련성에 따라 높은 순부터 낮은 순으로 정렬해 준답니다.

Rerank API 엔드포인트는 Rerank 모델을 기반으로 하는, 시맨틱 검색을 위한 단순하면서도 매우 강력한 도구예요. query와 documents 목록이 주어지면 Rerank는 문서를 쿼리와 의미적으로 가장 관련이 높은 것부터 낮은 것까지 순서를 매겨요.

출처: 문서

시작하기

텍스트로 하는 예시

아래 예시에서는 Rerank API 엔드포인트를 사용해 documents 목록을 쿼리 "What is the capital of the United States?"에 대한 관련성 순으로 정렬해요.

요청 (Request)

이 예시에서는 문서를 문자열 목록으로 전달하고 있어요.

PYTHON

import cohere

co = cohere.ClientV2()

query = "What is the capital of the United States?"
docs = [
    "Carson City is the capital city of the American state of Nevada. At the 2010 United States Census, Carson City had a population of 55,274.",
    "The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean that are a political division controlled by the United States. Its capital is Saipan.",
    "Charlotte Amalie is the capital and largest city of the United States Virgin Islands. It has about 20,000 people. The city is on the island of Saint Thomas.",
    "Washington, D.C. (also known as simply Washington or D.C., and officially as the District of Columbia) is the capital of the United States. It is a federal district. The President of the USA and many major national government offices are in the territory. This makes it the political center of the United States of America.",
    "Capital punishment has existed in the United States since before the United States was a country. As of 2017, capital punishment is legal in 30 of the 50 states. The federal government (including the United States military) also uses capital punishment.",
]

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

cURL

curl --request POST \
  --url https://api.cohere.ai/v2/rerank \
  --header 'accept: application/json' \
  --header 'content-type: application/json' \
  --header "Authorization: bearer ***" \
  --data '{
    "model": "rerank-v4.0-pro",
    "query": "What is the capital of the United States?",
    "documents": [
      "Carson City is the capital city of the American state of Nevada. At the 2010 United States Census, Carson City had a population of 55,274.",
      "The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean that are a political division controlled by the United States. Its capital is Saipan.",
      "Charlotte Amalie is the capital and largest city of the United States Virgin Islands. It has about 20,000 people. The city is on the island of Saint Thomas.",
      "Washington, D.C. (also known as simply Washington or D.C., and officially as the District of Columbia) is the capital of the United States. It is a federal district. The President of the USA and many major national government offices are in the territory. This makes it the political center of the United States of America.",
      "Capital punishment has existed in the United States since before the United States was a country. As of 2017, capital punishment is legal in 30 of the 50 states. The federal government (including the United States military) also uses capital punishment."
    ],
    "top_n": 5
  }'

그러면 다음과 같은 V2RerankResponse 객체를 받게 돼요.

V2RerankResponse(
    id="2104ccd0-74b5-4951-9bb1-cc543b26720f",
    results=[
        V2RerankResponseResultsItem(
            index=3, relevance_score=0.943264
        ),
        V2RerankResponseResultsItem(
            index=2, relevance_score=0.62209207
        ),
        V2RerankResponseResultsItem(
            index=1, relevance_score=0.6054258
        ),
        V2RerankResponseResultsItem(
            index=0, relevance_score=0.59040135
        ),
        V2RerankResponseResultsItem(
            index=4, relevance_score=0.4664567
        ),
    ],
    meta=ApiMeta(
        api_version=ApiMetaApiVersion(
            version="2", is_deprecated=None, is_experimental=None
        ),
        billed_units=ApiMetaBilledUnits(
            images=None,
            input_tokens=None,
            output_tokens=None,
            search_units=1.0,
            classifications=None,
        ),
        tokens=None,
        cached_tokens=None,
        warnings=None,
    ),
)

index는 Python에서처럼 동작해서 index=0이 첫 번째 문서라는 점에 유의해 주세요. 그리고 V2RerankResponse 객체는 실제로는 더 컴팩트한데, 위 예시는 읽기 쉽도록 다시 포맷한 것이에요.

구조화된 데이터로 하는 예시

문서에 구조화된 데이터가 들어 있다면, 최상의 성능을 위해 YAML 문자열로 포맷하는 것을 권장해요.

요청 (Request)

PYTHON

import yaml
import cohere

co = cohere.ClientV2()

query = "What is the capital of the United States?"
docs = [
    {
        "Title": "Facts about Carson City",
        "Content": "Carson City is the capital city of the American state of Nevada. At the 2010 United States Census, Carson City had a population of 55,274.",
    },
    {
        "Title": "The Commonwealth of Northern Mariana Islands",
        "Content": "The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean that are a political division controlled by the United States. Its capital is Saipan.",
    },
    {
        "Title": "The Capital of United States Virgin Islands",
        "Content": "Charlotte Amalie is the capital and largest city of the United States Virgin Islands. It has about 20,000 people. The city is on the island of Saint Thomas.",
    },
    {
        "Title": "Washington D.C.",
        "Content": "Washington, D.C. (also known as simply Washington or D.C., and officially as the District of Columbia) is the capital of the United States. It is a federal district. The President of the USA and many major national government offices are in the territory. This makes it the political center of the United States of America.",
    },
    {
        "Title": "Capital Punishment in the US",
        "Content": "Capital punishment has existed in the United States since before the United States was a country. As of 2017, capital punishment is legal in 30 of the 50 states. The federal government (including the United States military) also uses capital punishment.",
    },
]

yaml_docs = [yaml.dump(doc, sort_keys=False) for doc in docs]

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

cURL

curl --request POST \
  --url https://api.cohere.ai/v2/rerank \
  --header 'accept: application/json' \
  --header 'content-type: application/json' \
  --header "Authorization: bearer ***" \
  --data '{
    "model": "rerank-v4.0-pro",
    "query": "What is the capital of the United States?",
    "documents": [
      "Title: Facts about Carson City\\nContent: Carson City is the capital city of the American state of Nevada. At the 2010 United States Census, Carson City had a population of 55,274.\\n",
      "Title: The Commonwealth of Northern Mariana Islands\\nContent: The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean that are a political division controlled by the United States. Its capital is Saipan.\\n",
      "Title: The Capital of United States Virgin Islands\\nContent: Charlotte Amalie is the capital and largest city of the United States Virgin Islands. It has about 20,000 people. The city is on the island of Saint Thomas.\\n",
      "Title: Washington D.C.\\nContent: Washington, D.C. (also known as simply Washington or D.C., and officially as the District of Columbia) is the capital of the United States. It is a federal district. The President of the USA and many major national government offices are in the territory. This makes it the political center of the United States of America.\\n",
      "Title: Capital Punishment in the US\\nContent: Capital punishment has existed in the United States since before the United States was a country. As of 2017, capital punishment is legal in 30 of the 50 states. The federal government (including the United States military) also uses capital punishment.\\n"
    ],
    "top_n": 5
  }'

documents 매개변수에 구조화된 데이터를 나타내는 YAML 문자열 목록을 전달하고 있어요.

앞서와 마찬가지로 다음과 같은 V2RerankResponse 객체를 받게 돼요.

V2RerankResponse(
    id="df4d8720-8265-4868-a8f5-0bcee7a35bd0",
    results=[
        V2RerankResponseResultsItem(
            index=3, relevance_score=0.9497813
        ),
        V2RerankResponseResultsItem(
            index=2, relevance_score=0.69064254
        ),
        V2RerankResponseResultsItem(
            index=0, relevance_score=0.57901955
        ),
        V2RerankResponseResultsItem(
            index=1, relevance_score=0.5482865
        ),
        V2RerankResponseResultsItem(
            index=4, relevance_score=0.49375027
        ),
    ],
    meta=ApiMeta(
        api_version=ApiMetaApiVersion(
            version="2", is_deprecated=None, is_experimental=None
        ),
        billed_units=ApiMetaBilledUnits(
            images=None,
            input_tokens=None,
            output_tokens=None,
            search_units=1.0,
            classifications=None,
        ),
        tokens=None,
        cached_tokens=None,
        warnings=None,
    ),
)

다국어 Reranking

Cohere의 Rerank 모델은 100개 이상의 언어에서 좋은 성능을 내도록 훈련됐어요.

모델을 선택할 때 다음 언어 지원 사항을 참고해 주세요.

  • Rerank 4.0 ('fast'와 'pro' 모두): 단일 다국어 모델 (rerank-v4.0-pro와 rerank-v4.0-fast)
  • Rerank 3.5: 단일 다국어 모델 (rerank-v3.5)
  • Rerank 3.0: 영어 전용 모델과 다국어 모델이 분리 (rerank-english-v3.0과 rerank-multilingual-v3.0)

다음 표는 Rerank 모델이 지원하는 언어 목록이에요. 언어별로 성능이 다를 수 있다는 점에 유의해 주세요.

ISO Code Language Name
af Afrikaans
am Amharic
ar Arabic
as Assamese
az Azerbaijani
be Belarusian
bg Bulgarian
bn Bengali
bo Tibetan
bs Bosnian
ca Catalan
ceb Cebuano
co Corsican
cs Czech
cy Welsh
da Danish
de German
el Greek
en English
eo Esperanto
es Spanish
et Estonian
eu Basque
fa Persian
fi Finnish
fr French
fy Frisian
ga Irish
gd Scots_gaelic
gl Galician
gu Gujarati
ha Hausa
haw Hawaiian
he Hebrew
hi Hindi
hmn Hmong
hr Croatian
ht Haitian_creole
hu Hungarian
hy Armenian
id Indonesian
ig Igbo
is Icelandic
it Italian
ja Japanese
jv Javanese
ka Georgian
kk Kazakh
km Khmer
kn Kannada
ko Korean
ku Kurdish
ky Kyrgyz
La Latin
Lb Luxembourgish
Lo Laothian
Lt Lithuanian
Lv Latvian
mg Malagasy
mi Maori
mk Macedonian
ml Malayalam
mn Mongolian
mr Marathi
ms Malay
mt Maltese
my Burmese
ne Nepali
nl Dutch
no Norwegian
ny Nyanja
or Oriya
pa Punjabi
pl Polish
pt Portuguese
ro Romanian
ru Russian
rw Kinyarwanda
si Sinhalese
sk Slovak
sl Slovenian
sm Samoan
sn Shona
so Somali
sq Albanian
sr Serbian
st Sesotho
su Sundanese
sv Swedish
sw Swahili
ta Tamil
te Telugu
tg Tajik
th Thai
tk Turkmen
tl Tagalog
tr Turkish
tt Tatar
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zh Chinese
zu Zulu

더 알아보기 (Learn more)