> ## 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.

# WatsonxToolkit集成

> 使用 LangChain JavaScript 集成 WatsonxToolkit 工具。

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

工具包包含以下工具：

| 名称 | 描述 |
| - | - |
| `GoogleSearch` | 搜索在线趋势、新闻、当前事件、实时信息或研究主题。 |
| `WebCrawler` | 当您需要总结网页时很有用。不要用于 Web 搜索。 |
| `SDXLTurbo` | 使用 Stability.ai 从文本生成图像 |
| `Weather` | 查询城市的天气。 |
| `RAGQuery` | 搜索向量索引中的文档。 |

### 集成细节

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

## 设置

如果您希望从单个工具的运行中获得自动跟踪，请设置您的 [LangSmith](https://docs.langchain.com/langsmith/observability) API 密钥：

```bash theme={null}
export LANGSMITH_TRACING="true"
export LANGSMITH_API_KEY="your-api-key"
```

### 安装

此工具包位于 `@langchain/ibm` 包中：

> [!TIP]
>
> 请参阅 [有关安装 LangChain 包的一般说明](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/install.md)。

```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
```

## 实例化

现在我们可以实例化我们的工具包：

```typescript theme={null}
import { WatsonxToolkit } from "@langchain/ibm";
import "dotenv/config"

const toolkit = await WatsonxToolkit.init({
   version: '2024-05-31',
   serviceUrl: process.env.WATSONX_AI_SERVICE_URL
});
```

```text theme={null}
[Module: null prototype] { default: {} }
```

## 工具

查看可用工具：

```typescript theme={null}
const tools = toolkit.getTools();

console.log(tools.map((tool) => ({
   name: tool.name,
   description: tool.description,
})))
```

```text theme={null}
[
 {
 name: "GoogleSearch",
 description: "Search for online trends, news, current events, real-time information, or research topics."
 },
 {
 name: "WebCrawler",
 description: "Useful for when you need to summarize a webpage. Do not use for Web search."
 },
 {
 name: "SDXLTurbo",
 description: "Generate an image from text using Stability.ai"
 },
 { name: "Weather", description: "Find the weather for a city." },
 {
 name: "RAGQuery",
 description: "Search the documents in a vector index."
 }
]
```

有关工具的详细信息，请访问 [watsonx.ai API 文档](https://cloud.ibm.com/apidocs/watsonx-ai#get-utility-agent-tools)

## 在智能体中使用

安装 `langchain` 包以使用 `createAgent`：

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

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

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

然后，实例化您要在智能体中使用的 LLM：

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

const llm = new ChatWatsonx({
   version: '2024-05-31',
   serviceUrl: process.env.WATSONX_AI_SERVICE_URL,
   model: 'ibm/granite-3-8b-instruct',
   projectId: process.env.WATSONX_AI_PROJECT_ID
});
```

```typescript theme={null}
import { createAgent } from "langchain";

const agent = createAgent({ model: llm, tools });
```

```typescript theme={null}
const exampleQuery = "Who won F1 championship in 2022?"

const stream = await agent.streamEvents(
  { messages: [{ role: "user", content: exampleQuery }] },
  { version: "v3" },
);

for await (const snapshot of stream.values) {
  const lastMsg = snapshot.messages[snapshot.messages.length - 1];
  if (lastMsg.tool_calls?.length) {
    console.dir(lastMsg.tool_calls, { depth: null });
  } else if (lastMsg.content) {
    console.log(lastMsg.content);
  }
}
```

```text theme={null}
Who won F1 championship in 2022?
[
 {
 name: "GoogleSearch",
 args: { input: "F1 championship 2022" },
 type: "tool_call",
 id: "chatcmpl-tool-9da3456b9bbc475fb822296fdb8353a8"
 }
]
[{"title":"2022 DRIVER STANDINGS","description":"Official F1® Race Programme · Modern Slavery Statement; Do Not Sell or Share My Personal Information. Formula 1. © 2003-2025 Formula One World Championship ...","url":"https://www.formula1.com/en/results/2022/drivers"},{"title":"2022 Formula One World Championship - Wikipedia","description":"2022 Formula One World Championship · Max Verstappen won his second consecutive World Drivers' Championship driving for Red Bull Racing. · Charles Leclerc ...","url":"https://en.wikipedia.org/wiki/2022_Formula_One_World_Championship"},{"title":"2022 Formula One World Championship - Simple English Wikipedia ...","description":"Max Verstappen, who was the reigning Drivers' Champion, claimed his second title at the Japanese Grand Prix, while his team, Red Bull Racing, achieved their ...","url":"https://simple.wikipedia.org/wiki/2022_Formula_One_World_Championship"},{"title":"Max Verstappen wins the 2022 F1 Drivers World Championship : r ...","description":"Oct 9, 2022 ... One day this guy will win a championship just by finishing the race and celebrating with a world championship lap on the day he won.","url":"https://www.reddit.com/r/sports/comments/xzg8pf/max_verstappen_wins_the_2022_f1_drivers_world/"},{"title":"Red Bull Simulator Championship Edition | F1 Authentics","description":"Based on the livery of the 2022 Oracle Red Bull Racing Championship-winning RB18, this F1 simulator has been expertly engineered and manufactured by Memento ...","url":"https://www.f1authentics.com/products/red-bull-simulator-championship-edition"},{"title":"F1 2022 Drivers Championship without Max Verstappen : r/formula1","description":"Nov 26, 2022 ... 1.1K votes, 65 comments. 5.2M subscribers in the formula1 community. Welcome to r/Formula1, the best independent online Formula 1 community!","url":"https://www.reddit.com/r/formula1/comments/z5cwl7/f1_2022_drivers_championship_without_max/"},{"title":"F1® Sim Racing World Championship 2022","description":"World Championship 2022 Bahrain International Circuit Bahrain International Circuit Race Distance: 29 laps Track Length: 5.412 km","url":"https://f1esports.com/world/results/2022"},{"title":"Our personal F1 2022 Predictions Championship : r/formula1","description":"May 5, 2022 ... F1 2025 Driver Predictions from mathematical model. Leclerc and Sainz predicted to beat Hamilton and Albon. r/formula1 - F1 2025 Driver ...","url":"https://www.reddit.com/r/formula1/comments/uitbbu/our_personal_f1_2022_predictions_championship/"},{"title":"Haas F1 Team Esports announces roster for 2022 F1 Esports Series ...","description":"Sep 13, 2022 ... Haas F1 Team is ready to commence battle in the 2022 F1 Esports Series Pro Championship featuring an updated line-up for this season.","url":"https://www.haasf1team.com/news/haas-f1-team-esports-announces-roster-2022-f1-esports-series-pro-championship"},{"title":"2022 F1 Deconstructors Championship : r/formula1","description":"Nov 22, 2022 ... Congratulations to our 2022 champion Carlos Sainz. Alonso put in a late surge but had to settle for second place. Alfa put in the best effort ...","url":"https://www.reddit.com/r/formula1/comments/z1pkkx/2022_f1_deconstructors_championship/"}]
Max Verstappen won the 2022 F1 Championship, driving for Red Bull Racing. He claimed his second title at the Japanese Grand Prix.
```

***

## API 参考

有关所有 `WatsonxToolkit` 功能和配置的详细文档，请访问 [API 参考](https://reference.langchain.com/javascript/langchain-ibm/WatsonxToolkit)。

***

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


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