Cohere rerank 통합

Cohere rerank 통합

LangChain JavaScript로 Cohere rerank 문서 압축기와 통합하는 방법을 안내할게요.

출처: 문서

본문

문서 리랭킹은 모든 RAG 애플리케이션과 문서 검색 시스템을 크게 개선할 수 있어요.

높은 수준에서 rerank API는 문서를 분석하고 주어진 쿼리에 대한 관련성을 기준으로 재정렬하는 언어 모델이에요.

Cohere는 문서 리랭킹을 위한 API를 제공해요. 이 예제에서는 사용 방법을 보여줄게요.

설정

See [this section for general instructions on installing LangChain packages](/oss/javascript/langchain/install).

API 키를 설정하세요:

export COHERE_API_KEY="your-api-key"
```bash npm theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}} npm install @langchain/cohere @langchain/core ```
yarn add @langchain/cohere @langchain/core
pnpm add @langchain/cohere @langchain/core
import { CohereRerank } from "@langchain/cohere";
import { Document } from "@langchain/core/documents";

const query = "What is the capital of the United States?";
const docs = [
  new Document({
    pageContent:
      "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.",
  }),
  new Document({
    pageContent:
      "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.",
  }),
  new Document({
    pageContent:
      "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.",
  }),
  new Document({
    pageContent:
      "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.",
  }),
  new Document({
    pageContent:
      "Capital punishment (the death penalty) 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.",
  }),
];

const cohereRerank = new CohereRerank({
  apiKey: proces...KEY, // Default
  model: "rerank-english-v2.0",
});

const rerankedDocuments = await cohereRerank.rerank(docs, query, {
  topN: 5,
});

console.log(rerankedDocuments);
/**
[
  { index: 3, relevanceScore: 0.9871293 },
  { index: 1, relevanceScore: 0.29961726 },
  { index: 4, relevanceScore: 0.27542195 },
  { index: 0, relevanceScore: 0.08977329 },
  { index: 2, relevanceScore: 0.041462272 }
]
 */

여기서 .rerank() 메서드는 문서의 인덱스(입력 문서의 인덱스와 일치)와 관련성 점수만 반환하는 것을 볼 수 있어요.

메서드 자체에서 문서를 반환받고 싶다면 .compressDocuments() 메서드를 사용할 수 있어요.

import { CohereRerank } from "@langchain/cohere";
import { Document } from "@langchain/core/documents";

const query = "What is the capital of the United States?";
const docs = [
  new Document({
    pageContent:
      "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.",
  }),
  new Document({
    pageContent:
      "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.",
  }),
  new Document({
    pageContent:
      "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.",
  }),
  new Document({
    pageContent:
      "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.",
  }),
  new Document({
    pageContent:
      "Capital punishment (the death penalty) 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.",
  }),
];

const cohereRerank = new CohereRerank({
  apiKey: proces...KEY, // Default
  topN: 3, // Default
  model: "rerank-english-v2.0",
});

const rerankedDocuments = await cohereRerank.compressDocuments(docs, query);

console.log(rerankedDocuments);
/**
[
  Document {
    pageContent: '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.',
    metadata: { relevanceScore: 0.9871293 }
  },
  Document {
    pageContent: '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.',
    metadata: { relevanceScore: 0.29961726 }
  },
  Document {
    pageContent: 'Capital punishment (the death penalty) 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.',
    metadata: { relevanceScore: 0.27542195 }
  }
]
 */

결과에서 상위 3개 문서를 반환하고 각각에 relevanceScore를 할당한 것을 볼 수 있어요.

예상대로 가장 높은 relevanceScore를 가진 문서는 워싱턴 D.C.를 언급하는 문서로, 점수가 98.7%예요!

CohereClient와 함께 사용하기

Azure, AWS Bedrock 또는 독립 인스턴스에서 Cohere를 사용한다면 CohereClient를 사용해 엔드포인트로 CohereRerank 인스턴스를 만들 수 있어요.

import { CohereRerank } from "@langchain/cohere";
import { CohereClient } from "cohere-ai";
import { Document } from "@langchain/core/documents";

const query = "What is the capital of the United States?";
const docs = [
  new Document({
    pageContent:
      "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.",
  }),
  new Document({
    pageContent:
      "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.",
  }),
  new Document({
    pageContent:
      "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.",
  }),
  new Document({
    pageContent:
      "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.",
  }),
  new Document({
    pageContent:
      "Capital punishment (the death penalty) 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.",
  }),
];

const client = new CohereClient({
  token: process.env.COHERE_API_KEY,
  environment: "<your-cohere-deployment-url>", // optional
  // other params
});

const cohereRerank = new CohereRerank({
  client, // apiKey will be ignored even if provided
  model: "rerank-english-v2.0",
});

const rerankedDocuments = await cohereRerank.rerank(docs, query, {
  topN: 5,
});

console.log(rerankedDocuments);

/*
  [
    { index: 3, relevanceScore: 0.9871293 },
    { index: 1, relevanceScore: 0.29961726 },
    { index: 4, relevanceScore: 0.27542195 },
    { index: 0, relevanceScore: 0.08977329 },
    { index: 2, relevanceScore: 0.041462272 }
  ]
*/

[Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers. [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/oss/javascript/integrations/document_compressors/cohere_rerank.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).

더 알아보기