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

# JSON agent 工具包集成

> 使用 LangChain JavaScript 集成 JSON 智能体工具包。

这个示例展示了如何使用 JSON 工具包加载和使用智能体。

> [!TIP]
>
> 参见 [有关安装 LangChain 包的通用说明部分](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/install.md).

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

```typescript theme={null}
import * as fs from "fs";
import * as yaml from "js-yaml";
import { OpenAI } from "@langchain/openai";
import { JsonSpec, JsonObject } from "@langchain/classic/tools";
import { JsonToolkit, createJsonAgent } from "@langchain/classic/agents";

export const run = async () => {
  let data: JsonObject;
  try {
    const yamlFile = fs.readFileSync("openai_openapi.yaml", "utf8");
    data = yaml.load(yamlFile) as JsonObject;
    if (!data) {
      throw new Error("Failed to load OpenAPI spec");
    }
  } catch (e) {
    console.error(e);
    return;
  }

  const toolkit = new JsonToolkit(new JsonSpec(data));
  const model = new OpenAI({ temperature: 0 });
  const executor = createJsonAgent(model, toolkit);

  const input = `What are the required parameters in the request body to the /completions endpoint?`;

  console.log(`Executing with input "${input}"...`);

  const result = await executor.invoke({ input });

  console.log(`Got output ${result.output}`);

  console.log(
    `Got intermediate steps ${JSON.stringify(
      result.intermediateSteps,
      null,
      2
    )}`
  );
};
```

***

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


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