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

# VectorStoreToolkit 集成

> 通过LangChain JavaScript与VectorStoreToolkit工具集成。

这将帮助您开始使用 `VectorStoreToolkit` [工具包](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/tools.md#预构建工具)。有关所有 `VectorStoreToolkit` 功能和配置的详细文档，请访问 [API 参考](https://reference.langchain.com/javascript/langchain-core/vectorstores/VectorStore)。

`VectorStoreToolkit` 是一个工具包，它接收一个向量存储，并将其转换为可以调用的工具，然后传递给大型语言模型、智能体等。

## 安装

如果您希望从单个工具的运行中获得自动跟踪，您也可以通过取消注释以下内容来设置您的 [LangSmith](https://docs.langchain.com/langsmith/observability) API 密钥：

```typescript theme={null}
process.env.LANGSMITH_TRACING="true"
process.env.LANGSMITH_API_KEY="your-api-key"
```

### 安装

此工具包位于 `langchain` 包中：

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

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

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

## 实例化

现在我们可以实例化我们的工具包。首先，我们需要定义工具包中使用的 LLM。

```typescript theme={null}
// @lc-docs-hide-cell

import { ChatOpenAI } from "@langchain/openai";

const llm = new ChatOpenAI({
  model: "gpt-5.4-mini",
  temperature: 0,
})
```

```typescript theme={null}
import { VectorStoreToolkit, VectorStoreInfo } from "@langchain/classic/agents/toolkits"
import { OpenAIEmbeddings } from "@langchain/openai"
import { MemoryVectorStore } from "@langchain/classic/vectorstores/memory"
import { RecursiveCharacterTextSplitter } from "@langchain/textsplitters";
import fs from "fs";

// Load a text file to use as our data source.
const text = fs.readFileSync("../../../../../examples/state_of_the_union.txt", "utf8");

// Split the text into chunks before inserting to our store
const textSplitter = new RecursiveCharacterTextSplitter({ chunkSize: 1000 });
const docs = await textSplitter.createDocuments([text]);

const vectorStore = await MemoryVectorStore.fromDocuments(docs, new OpenAIEmbeddings());

const vectorStoreInfo: VectorStoreInfo = {
  name: "state_of_union_address",
  description: "the most recent state of the Union address",
  vectorStore,
};

const toolkit = new VectorStoreToolkit(vectorStoreInfo, llm);
```

## 工具

在这里，我们可以看到它将我们的向量存储转换为工具：

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

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

```text theme={null}
[
  {
    name: 'state_of_union_address',
    description: 'Useful for when you need to answer questions about state_of_union_address. Whenever you need information about the most recent state of the Union address you should ALWAYS use this. Input should be a fully formed question.'
  }
]
```

## 在智能体中使用

首先，确保您已安装 LangGraph：

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

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

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

然后，实例化智能体：

```typescript theme={null}
import { createAgent } from "@langchain/classic"

const agentExecutor = createAgent({ llm, tools });
```

```typescript theme={null}
const exampleQuery = "What did biden say about Ketanji Brown Jackson is the state of the union address?"

const stream = await agentExecutor.streamEvents(
  { messages: [["user", 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}
[
  {
    name: 'state_of_union_address',
    args: {
      input: 'What did Biden say about Ketanji Brown Jackson in the State of the Union address?'
    },
    type: 'tool_call',
    id: 'call_glJSWLNrftKHa92A6j8x4jhd'
  }
]
In the State of the Union address, Biden mentioned that he nominated Circuit Court of Appeals Judge Ketanji Brown Jackson, describing her as one of the nation’s top legal minds who will continue Justice Breyer’s legacy of excellence. He highlighted her background as a former top litigator in private practice, a former federal public defender, and noted that she comes from a family of public school educators and police officers. He also pointed out that she has received a broad range of support since her nomination.
In the State of the Union address, President Biden spoke about Ketanji Brown Jackson, stating that he nominated her as one of the nation’s top legal minds who will continue Justice Breyer’s legacy of excellence. He highlighted her experience as a former top litigator in private practice and a federal public defender, as well as her background coming from a family of public school educators and police officers. Biden also noted that she has received a broad range of support since her nomination.
```

***

## API 参考

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

***

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


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