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

# 在提示词中使用工具

工具使语言模型能够与外部系统交互，并执行超越单纯文本生成的操作。在 Playground 中，您可以使用两种类型的工具：

1. [**内置工具**](#内置工具)：由模型提供商（如 OpenAI 和 Anthropic）提供的预配置工具，开箱即用。当您需要常见功能（如网络搜索或代码解释）时，请使用内置工具。
2. [**自定义工具**](#创建自定义工具)：您定义的用于执行特定任务的函数。当您需要与自己的系统集成或创建专门功能时，这些工具非常有用。当您在 Playground 中定义自定义工具时，可以验证模型是否正确识别并使用正确的参数调用这些工具。

LangSmith 会自动将您创建的工具保存到工作区范围的[工具注册表](#使用注册表管理工具)中，这使得它们可以在您所有的提示词和会话中重复使用。

## 内置工具

Playground 原生支持来自 OpenAI 和 Anthropic 的各种工具。如果您想使用 Playground 中未明确列出的工具，仍然可以通过手动指定其 `type` 和任何必需的参数来添加它。

### OpenAI 工具

* **网络搜索**：[搜索网络以获取实时信息](https://platform.openai.com/docs/guides/tools-web-search?api-mode=responses)。
* **图像生成**：[基于文本提示生成图像](https://platform.openai.com/docs/guides/tools-image-generation)。
* **MCP**：[为模型提供对远程 MCP 服务器上托管的工具的访问权限](https://platform.openai.com/docs/guides/tools-remote-mcp)。
* [查看所有 OpenAI 工具](https://platform.openai.com/docs/guides/tools?api-mode=responses)。

### Anthropic 工具

* **网络搜索**：[搜索网络以获取最新信息](https://platform.claude.com/docs/en/agents-and-tools/tool-use/web-search-tool)。
* [查看所有 Anthropic 工具](https://platform.claude.com/docs/en/agents-and-tools/tool-use/overview)。

## 添加和使用工具

Playground 允许您一键快速[添加工具](#添加工具)到任何提示词。您可以选择由 OpenAI 和 Anthropic 等模型提供商提供的内置工具，也可以定义适合您特定需求的[自定义工具](#创建自定义工具)。一旦创建了自定义工具，它会自动添加到工作区范围的[工具注册表](#使用注册表管理工具)中，您可以在不同的提示词中启用、禁用或编辑它，而无需重新创建。

### 添加工具

要向提示词添加工具，请点击提示词编辑器底部的 **+ 工具** 按钮。

<img className="block dark:hidden" src="https://mintcdn.com/hhh-8c10bf0c/dGi5Qyx6ZNfUuqyP/langsmith/images/add-tool-light.png?fit=max&auto=format&n=dGi5Qyx6ZNfUuqyP&q=85&s=2ee47d6eb116dfc9063892e86039eebb" alt="提示词界面，编辑框后跟着 + 工具按钮。" width="644" height="316" data-path="langsmith/images/add-tool-light.png" />

<img className="hidden dark:block" src="https://mintcdn.com/hhh-8c10bf0c/dGi5Qyx6ZNfUuqyP/langsmith/images/add-tool-dark.png?fit=max&auto=format&n=dGi5Qyx6ZNfUuqyP&q=85&s=9f194541187081cc4ce2db812a059e83" alt="提示词界面，编辑框后跟着 + 工具按钮。" width="642" height="311" data-path="langsmith/images/add-tool-dark.png" />

### 使用内置工具

1. 在工具部分，选择您要使用的内置工具。您只会看到与您选择的提供商和模型兼容的工具。
2. 当模型调用该工具时，Playground 将显示响应。

   <img src="https://mintcdn.com/hhh-8c10bf0c/jRI9Uh24bT9O5tSI/langsmith/images/web-search-tool.gif?s=7516b21ebf82242cfb663b0c6e3a962e" alt="网络搜索工具" width="1036" height="720" data-path="langsmith/images/web-search-tool.gif" />

### 创建自定义工具

要创建自定义工具，您需要提供：

* **名称**：工具的描述性名称。
* **描述**：对工具功能的清晰说明。
* **参数**：您的工具所需的输入。

<img src="https://mintcdn.com/hhh-8c10bf0c/lw0BeSKlKZkqgDHv/langsmith/images/custom-tool.gif?s=3c20926aa72ba13feaf42114eefcf68b" alt="自定义工具" width="1028" height="720" data-path="langsmith/images/custom-tool.gif" />

在 Playground 中运行自定义工具时，模型将返回一个包含工具名称和工具调用的 JSON 对象。

<img src="https://mintcdn.com/hhh-8c10bf0c/BCyPqRNhtAjzdCmk/langsmith/images/tool-call.png?fit=max&auto=format&n=BCyPqRNhtAjzdCmk&q=85&s=99d9b6cce8d4cbf3800dbd2d41fe1573" alt="工具调用" width="1488" height="747" data-path="langsmith/images/tool-call.png" />

### 使用注册表管理工具

Playground 包含一个[工作区](/langsmith/administration-overview#workspaces)范围的**工具注册表**，它能在提示词和会话之间持久保存自定义和内置工具。当您创建自定义工具或添加内置工具时，它会自动保存到您的工作区注册表中，并可在任何提示词中重复使用。您可以按提示词启用或禁用工具，以控制每个特定提示词的活动工具；在编辑共享工具时，您可以选择更新注册表版本或另存为新工具。

点击 Playground 中的 **+ 工具** 按钮以打开**管理工具**。您可以执行以下操作：

* 在**可用工具**选项卡中选择和查看现有工具。
* 使用**启用**开关切换单个工具的开启/关闭状态。
* 通过点击列表中的工具来编辑现有工具。
* 使用**管理工具**底部的**删除**按钮删除工具。

<img className="block dark:hidden" src="https://mintcdn.com/hhh-8c10bf0c/BCyPqRNhtAjzdCmk/langsmith/images/tool-registry-manage-light.png?fit=max&auto=format&n=BCyPqRNhtAjzdCmk&q=85&s=79a8afb0ab275e09e1faee6c1df60499" alt="管理工具界面，显示可用工具列表、启用开关和编辑功能。" width="863" height="807" data-path="langsmith/images/tool-registry-manage-light.png" />

<img className="hidden dark:block" src="https://mintcdn.com/hhh-8c10bf0c/BCyPqRNhtAjzdCmk/langsmith/images/tool-registry-manage-dark.png?fit=max&auto=format&n=BCyPqRNhtAjzdCmk&q=85&s=11b6cf05164c813cc4f8f3a86a592e41" alt="管理工具界面，显示可用工具列表、启用开关和编辑功能。" width="858" height="815" data-path="langsmith/images/tool-registry-manage-dark.png" />

工具与其完整配置（包括名称、描述、参数和元数据）一起存储。注册表支持自定义函数工具和内置工具配置。

## 工具选择设置

某些模型提供对调用哪些工具的控制。要配置此功能：

1. 在提示词编辑器下选择 **+ 工具**。
2. 导航到**工具选择设置**选项卡。
3. 选择您的工具选择。

要了解可用的工具选择选项，请查阅您特定提供商的文档。例如，[OpenAI 关于工具选择的文档](https://platform.openai.com/docs/guides/function-calling/function-calling-behavior?api-mode=responses#tool-choice)。

<img className="block dark:hidden" src="https://mintcdn.com/hhh-8c10bf0c/BCyPqRNhtAjzdCmk/langsmith/images/tool-choice-light.png?fit=max&auto=format&n=BCyPqRNhtAjzdCmk&q=85&s=d1745e8582e66de09f41fbc326674fd4" alt="从工具选择设置选项卡中选择工具。" width="1732" height="1156" data-path="langsmith/images/tool-choice-light.png" />

<img className="hidden dark:block" src="https://mintcdn.com/hhh-8c10bf0c/BCyPqRNhtAjzdCmk/langsmith/images/tool-choice-dark.png?fit=max&auto=format&n=BCyPqRNhtAjzdCmk&q=85&s=2be046b21ddbd1b4e8278a8956e5daa5" alt="从工具选择设置选项卡中选择工具。" width="1724" height="1118" data-path="langsmith/images/tool-choice-dark.png" />

***

<div className="source-links">
  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/i18n\zh-CN\langsmith\use-tools.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>

  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>
</div>
