> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-docs-zh.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# IBM watsonx.ai 集成

> 使用 LangChain 集成 IBM watsonx.ai 文档压缩器。

## 概述

这将帮助您开始使用 Watsonx [文档压缩器](https://docs.langchain.com/oss/concepts/#document_compressors)。有关 Watsonx 文档压缩器所有功能和配置的详细文档，请访问 [API 参考](https://reference.langchain.com/javascript/langchain-ibm/WatsonxRerank)。

### 集成详情

| 类 | 包 | [PY 支持](https://langchain-docs-zh.mintlify.site/langchain/oss/python/integrations/retrievers/ibm_watsonx_ranker.md) | 下载 | 版本 |
| :- | :- | :-: | :-: | :-: |
| [`WatsonxRerank`](https://reference.langchain.com/javascript/langchain-ibm/WatsonxRerank) | [`@langchain/ibm`](https://www.npmjs.com/package/@langchain/ibm) | ✅ | ![NPM - 下载](https://img.shields.io/npm/dm/@langchain/ibm?style=flat-square\&label=%20&) | ![NPM - 版本](https://img.shields.io/npm/v/@langchain/ibm?style=flat-square\&label=%20&) |

## 设置

要访问 IBM WatsonxAI 模型，您需要创建一个 IBM watsonx.ai 账户，获取 API 密钥或任何其他类型的凭证，并安装 `@langchain/ibm` 集成包。

### 凭证

前往 [IBM Cloud](https://cloud.ibm.com/login) 注册 IBM watsonx.ai 并生成 API 密钥，或提供下方所示的其他任何形式的身份验证。

#### IAM 身份验证

```bash theme={null}
export WATSONX_AI_AUTH_TYPE=iam
export WATSONX_AI_APIKEY=<YOUR-APIKEY>
```

#### Bearer 令牌身份验证

```bash theme={null}
export WATSONX_AI_AUTH_TYPE=bearertoken
export WATSONX_AI_BEARER_TOKEN=<YOUR-BEARER-TOKEN>
```

#### IBM watsonx.ai 软件身份验证

```bash theme={null}
export WATSONX_AI_AUTH_TYPE=cp4d
export WATSONX_AI_USERNAME=<YOUR_USERNAME>
export WATSONX_AI_PASSWORD=<YOUR_PASSWORD>
export WATSONX_AI_URL=<URL>
```

将它们放置在您的环境变量中，并初始化对象后，身份验证将自动进行。

也可以通过将这些值作为参数传递给新实例来完成身份验证。

## IAM 身份验证

```typescript theme={null}
import { WatsonxRerank } from "@langchain/ibm";

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

## Bearer 令牌身份验证

```typescript theme={null}
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 软件身份验证

```typescript theme={null}
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](https://docs.langchain.com/langsmith/observability) API 密钥：

```typescript theme={null}
// process.env.LANGSMITH_API_KEY = "<YOUR API KEY HERE>";
// process.env.LANGSMITH_TRACING = "true";
```

### 安装

此文档压缩器位于 `@langchain/ibm` 包中：

```bash npm theme={null}
npm install @langchain/ibm @langchain/core
```

```bash yarn theme={null}
yarn add @langchain/ibm @langchain/core
```

```bash pnpm theme={null}
pnpm add @langchain/ibm @langchain/core
```

## 实例化

现在我们可以实例化我们的压缩器：

```javascript theme={null}
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 摄取管道，包含嵌入、文本分割器和向量存储。我们将使用此管道对与所选查询相关的文档进行重新排序：

```javascript theme={null}
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);
```

```javascript theme={null}
[
  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
  }
]
```

将选定的文档传递给重新排序，并为每个文档接收特定分数

```javascript theme={null}
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);
```

```text theme={null}
[
  { index: 0, relevanceScore: 0.726995587348938 },
  { index: 1, relevanceScore: 0.5758284330368042 },
  { index: 2, relevanceScore: 0.5479092597961426 },
  { index: 3, relevanceScore: 0.5468723773956299 }
]
```

或者，您也可以将文档与结果一起返回，为此请使用 .compressDocuments() 方法，如下所示。

```javascript theme={null}
const compressedWithResults = await reranker.compressDocuments(result, query);
console.log(compressedWithResults);
```

```javascript theme={null}
[
  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 参考](https://reference.langchain.com/javascript/langchain-ibm/WatsonxRerank)。

***

> 官方原文：[src/oss/javascript/integrations/document\_compressors/ibm.mdx](https://github.com/langchain-ai/docs/blob/main/src/oss/javascript/integrations/document_compressors/ibm.mdx) · 非官方翻译，译文由 AI 生成，以英文原文为准。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.