IBM watsonx.ai 통합

IBM watsonx.ai 통합

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

출처: 문서

본문

개요

이 문서는 Watsonx 문서 압축기를 시작하는 데 도움을 줘요. 모든 Watsonx 문서 압축기 기능과 구성에 대한 자세한 문서는 API 레퍼런스를 참고하세요.

통합 세부 정보

클래스 패키지 PY 지원 Downloads Version
WatsonxRerank @langchain/ibm NPM - Downloads NPM - Version

설정

IBM WatsonxAI 모델에 접근하려면 IBM watsonx.ai 계정을 만들고 API 키 또는 다른 종류의 자격 증명을 받은 뒤 @langchain/ibm 통합 패키지를 설치해야 해요.

자격 증명

IBM Cloud에서 IBM watsonx.ai에 가입하고 API 키를 생성하거나 아래 제시된 다른 인증 방식을 제공하세요.

IAM 인증

export WATSONX_AI_AUTH_TYPE=iam
export WATSONX_AI_APIKEY=<YOUR-APIKEY>

Bearer 토큰 인증

export WATSONX_AI_AUTH_TYPE=bearertoken
export WATSONX_AI_BEARER_TOKEN=<YOUR-BEARER-TOKEN>

IBM watsonx.ai 소프트웨어 인증

export WATSONX_AI_AUTH_TYPE=cp4d
export WATSONX_AI_USERNAME=<YOUR_USERNAME>
export WATSONX_AI_PASSWORD=<YOUR_PASSWORD>
export WATSONX_AI_URL=<URL>

이 값들이 환경 변수에 들어가고 객체가 초기화되면 인증이 자동으로 진행돼요.

이 값들을 새 인스턴스의 매개변수로 전달해 인증할 수도 있어요.

IAM 인증

import { WatsonxRerank } from "@langchain/ibm";

const props = {
  version: "YYYY-MM-DD",
  serviceUrl: "<SERVICE_URL>",
  projectId: "<PROJECT_ID>",
  watsonxAIAuthType: "iam",
  watsonxAIApikey: ***
};
const instance = new WatsonxRerank(props);

Bearer 토큰 인증

import { WatsonxRerank } from "@langchain/ibm";

const props = {
  version: "YYYY-MM-DD",
  serviceUrl: "<SERVICE_URL>",
  projectId: "<PROJECT_ID>",
  watsonxAIAuthType: "bearertoken",
  watsonxAIBearerToken: "<YOUR-BEARERTOKEN>",
};
const instance = new WatsonxRerank(props);

IBM watsonx.ai 소프트웨어 인증

import { WatsonxRerank } from "@langchain/ibm";

const props = {
  version: "YYYY-MM-DD",
  serviceUrl: "<SERVICE_URL>",
  projectId: "<PROJECT_ID>",
  watsonxAIAuthType: "cp4d",
  watsonxAIUsername: "<YOUR-USERNAME>",
  watsonxAIPassword: "<YOUR-PASSWORD>",
  watsonxAIUrl: "<url>",
};
const instance = new WatsonxRerank(props);

개별 쿼리의 자동 추적을 원한다면 아래 주석을 해제해 LangSmith API 키를 설정할 수도 있어요:

// process.env.LANGSMITH_API_KEY = "<YOUR API KEY HERE>";
// process.env.LANGSMITH_TRACING = "true";

설치

이 문서 압축기는 @langchain/ibm 패키지에 있어요:

```bash npm theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}} npm install @langchain/ibm @langchain/core ```
yarn add @langchain/ibm @langchain/core
pnpm add @langchain/ibm @langchain/core

인스턴스 생성

이제 압축기를 인스턴스화할 수 있어요:

import { WatsonxRerank } from "@langchain/ibm";

const watsonxRerank = new WatsonxRerank({
  version: "2024-05-31",
  serviceUrl: process.env.WATSONX_AI_SERVICE_URL,
  projectId: process.env.WATSONX_AI_PROJECT_ID,
  model: "cross-encoder/ms-marco-minilm-l-12-v2",
});

사용법

먼저 임베딩, 텍스트 분할기, 벡터 저장소로 기본 RAG 수집(ingest) 파이프라인을 설정하세요. 이를 사용해 선택한 쿼리에 대한 일부 문서를 리랭킹할 거예요:

import { readFileSync } from "node:fs";
import { MemoryVectorStore } from "@langchain/classic/vectorstores/memory";
import { WatsonxEmbeddings } from "@langchain/ibm";
import { CharacterTextSplitter } from "@langchain/textsplitters";

const embeddings = new WatsonxEmbeddings({
 version: "YYYY-MM-DD",
 serviceUrl: process.env.API_URL,
 projectId: "<PROJECT_ID>",
 spaceId: "<SPACE_ID>",
 model: "ibm/slate-125m-english-rtrvr",
});

const textSplitter = new CharacterTextSplitter({
  chunkSize: 400,
  chunkOverlap: 0,
});

const query = "What did the president say about Ketanji Brown Jackson";
const text = readFileSync("state_of_the_union.txt", "utf8");

const docs = await textSplitter.createDocuments([text]);
const vectorStore = await MemoryVectorStore.fromDocuments(docs, embeddings);
const vectorStoreRetriever = vectorStore.asRetriever();

const result = await vectorStoreRetriever.invoke(query);
console.log(result);
[
  Document {
    pageContent: 'And I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation’s top legal minds, who will continue Justice Breyer’s legacy of excellence.',
    metadata: { loc: [Object] },
    id: undefined
  },
  Document {
    pageContent: 'I spoke with their families and told them that we are forever in debt for their sacrifice, and we will carry on their mission to restore the trust and safety every community deserves. \n' +
      '\n' +
      'I’ve worked on these issues a long time. \n' +
      '\n' +
      'I know what works: Investing in crime preventionand community police officers who’ll walk the beat, who’ll know the neighborhood, and who can restore trust and safety.',
    metadata: { loc: [Object] },
    id: undefined
  },
  Document {
    pageContent: 'We are the only nation on Earth that has always turned every crisis we have faced into an opportunity. \n' +
      '\n' +
      'The only nation that can be defined by a single word: possibilities. \n' +
      '\n' +
      'So on this night, in our 245th year as a nation, I have come to report on the State of the Union. \n' +
      '\n' +
      'And my report is this: the State of the Union is strong—because you, the American people, are strong.',
    metadata: { loc: [Object] },
    id: undefined
  },
  Document {
    pageContent: 'And I’m taking robust action to make sure the pain of our sanctions  is targeted at Russia’s economy. And I will use every tool at our disposal to protect American businesses and consumers. \n' +
      '\n' +
      'Tonight, I can announce that the United States has worked with 30 other countries to release 60 Million barrels of oil from reserves around the world.',
    metadata: { loc: [Object] },
    id: undefined
  }
]

선택한 문서를 리랭킹에 전달하고 각각에 대한 특정 점수를 받으세요

import { WatsonxRerank } from "@langchain/ibm";

const reranker = new WatsonxRerank({
  version: "2024-05-31",
  serviceUrl: process.env.WATSONX_AI_SERVICE_URL,
  projectId: process.env.WATSONX_AI_PROJECT_ID,
  model: "cross-encoder/ms-marco-minilm-l-12-v2",
});
const compressed = await reranker.rerank(result, query);
console.log(compressed);
[
  { index: 0, relevanceScore: 0.726995587348938 },
  { index: 1, relevanceScore: 0.5758284330368042 },
  { index: 2, relevanceScore: 0.5479092597961426 },
  { index: 3, relevanceScore: 0.5468723773956299 }
]

또는 결과와 함께 문서를 반환받고 싶다면 아래와 같이 .compressDocuments() 메서드를 사용하세요.

const compressedWithResults = await reranker.compressDocuments(result, query);
console.log(compressedWithResults);
[
  Document {
    pageContent: 'And I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation’s top legal minds, who will continue Justice Breyer’s legacy of excellence.',
    metadata: { loc: [Object], relevanceScore: 0.726995587348938 },
    id: undefined
  },
  Document {
    pageContent: 'I spoke with their families and told them that we are forever in debt for their sacrifice, and we will carry on their mission to restore the trust and safety every community deserves. \n' +
      '\n' +
      'I’ve worked on these issues a long time. \n' +
      '\n' +
      'I know what works: Investing in crime preventionand community police officers who’ll walk the beat, who’ll know the neighborhood, and who can restore trust and safety.',
    metadata: { loc: [Object], relevanceScore: 0.5758284330368042 },
    id: undefined
  },
  Document {
    pageContent: 'We are the only nation on Earth that has always turned every crisis we have faced into an opportunity. \n' +
      '\n' +
      'The only nation that can be defined by a single word: possibilities. \n' +
      '\n' +
      'So on this night, in our 245th year as a nation, I have come to report on the State of the Union. \n' +
      '\n' +
      'And my report is this: the State of the Union is strong—because you, the American people, are strong.',
    metadata: { loc: [Object], relevanceScore: 0.5479092597961426 },
    id: undefined
  },
  Document {
    pageContent: 'And I’m taking robust action to make sure the pain of our sanctions  is targeted at Russia’s economy. And I will use every tool at our disposal to protect American businesses and consumers. \n' +
      '\n' +
      'Tonight, I can announce that the United States has worked with 30 other countries to release 60 Million barrels of oil from reserves around the world.',
    metadata: { loc: [Object], relevanceScore: 0.5468723773956299 },
    id: undefined
  }
]

API 레퍼런스

모든 Watsonx 문서 압축기 기능과 구성에 대한 자세한 문서는 API 레퍼런스를 참고하세요.


[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/ibm.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).

더 알아보기