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

# 创建提示词

在 [LangSmith 界面](https://smith.langchain.com) 中，从左侧边栏或应用主页导航至 **演练场**。

<img className="block dark:hidden" src="https://mintcdn.com/other-405835d4/2a1kkFnfjfW0Zb-b/langsmith/images/empty-playground-light.png?fit=max&auto=format&n=2a1kkFnfjfW0Zb-b&q=85&s=687c05935a1405bce79a92ce8975cf36" alt="空演练场" width="1436" height="529" data-path="langsmith/images/empty-playground-light.png" />

<img className="hidden dark:block" src="https://mintcdn.com/other-405835d4/2a1kkFnfjfW0Zb-b/langsmith/images/empty-playground-dark.png?fit=max&auto=format&n=2a1kkFnfjfW0Zb-b&q=85&s=9a42c6f2059f2ed1646535aa4424758f" alt="空演练场" width="1436" height="521" data-path="langsmith/images/empty-playground-dark.png" />

## 组合你的提示词

演练场的左侧面板是提示词的可编辑视图。

提示词由消息组成，每条消息都有一个 *角色*，包括：

* **系统**：相当于“说明书”。用于定义 AI 的角色、语气和基本规则（例如，“你是一个乐于助人的助手，能像解释天气一样解释事物”）。
* **人类**：代表“用户”。这代表向 AI 提问或提供指令的人。
* **AI**：代表“助手”。这是模型的响应。在演练场中，你可以使用此角色提供“少样本”示例——向 AI 展示你希望它如何响应。
* **工具 / 函数**：这些角色代表来自外部工具（如计算器或搜索引擎）的输出。它们帮助你测试 AI 在接收特定数据后应如何表现。
* **聊天**：一个通用角色，通常在导入日志或对话历史记录时使用，当未分配特定标签时使用。
* **消息列表**：一个动态占位符。允许你添加一个包含整个先前消息列表的变量，便于管理长对话历史记录。

### 模板格式

默认的[模板格式](/langsmith/prompt-template-format)是 f-string，但你可以通过点击提示词框下方的下拉框将提示词模板格式更改为 mustache。

### 添加模板变量

当你在提示词中添加变量时，提示词会变得特别有用。你可以使用变量向提示词添加动态内容。通过以下两种方式之一添加模板变量：

* 在你的提示词中添加 `{variable_name}`（f-string 格式每侧一个花括号，mustache 格式每侧两个花括号）。

  <img className="block dark:hidden" src="https://mintcdn.com/other-405835d4/l3RGLkJgudfiL0L9/langsmith/images/prompt-with-variable-light.png?fit=max&auto=format&n=l3RGLkJgudfiL0L9&q=85&s=fb076f1801c5a8f5b21a515a3750b7fa" alt="提示词框中的变量。" width="1616" height="584" data-path="langsmith/images/prompt-with-variable-light.png" />

  <img className="hidden dark:block" src="https://mintcdn.com/other-405835d4/l3RGLkJgudfiL0L9/langsmith/images/prompt-with-variable-dark.png?fit=max&auto=format&n=l3RGLkJgudfiL0L9&q=85&s=61549cac53af41ceeeb0a3475fc1f3c4" alt="提示词框中的变量。" width="1610" height="568" data-path="langsmith/images/prompt-with-variable-dark.png" />

* 高亮显示你想要模板化的文本，然后点击显示的 **转换为变量** 工具提示按钮。输入变量名称并进行转换。

  <img className="block dark:hidden" src="https://mintcdn.com/other-405835d4/jwwDDYly65_K9YMB/langsmith/images/convert-to-variable-light.png?fit=max&auto=format&n=jwwDDYly65_K9YMB&q=85&s=f4f1596b635451baf94d7b830c6abc36" alt="双击提示词会显示变量图标。" width="1590" height="624" data-path="langsmith/images/convert-to-variable-light.png" />

  <img className="hidden dark:block" src="https://mintcdn.com/other-405835d4/jwwDDYly65_K9YMB/langsmith/images/convert-to-variable-dark.png?fit=max&auto=format&n=jwwDDYly65_K9YMB&q=85&s=262dfc099650d78662b24af2007cdae7" alt="双击提示词会显示变量图标。" width="1562" height="634" data-path="langsmith/images/convert-to-variable-dark.png" />

添加变量后，演练场的右侧面板将出现一个 **输入** 框，用于输入提示词变量的示例输入。填入这些值以测试提示词。

<Callout icon="book" color="#4F46E5" iconType="regular">
  有关提示词模板格式的更多详细信息以及两种语法的示例，请参阅[提示词模板格式](/langsmith/prompt-template-format)指南。
</Callout>

### 结构化输出

为你的提示词添加输出模式，将获得结构化格式的输出。了解更多关于[结构化输出](/langsmith/prompt-engineering-concepts#structured-output)的信息。

### 工具

你也可以通过点击提示词编辑器底部的 **+ 工具** 按钮来添加工具。有关如何使用工具的更多信息，请参阅[使用工具](/langsmith/use-tools)。

<Callout type="info" icon="feather">
  在演练场中使用 **[Polly](/langsmith/polly)** AI 助手来生成工具、创建输出模式，并在 AI 辅助下优化你的提示词。
</Callout>

## 运行提示词

要运行提示词，请使用演练场右侧面板顶部的 <Icon icon="player-play" /> **开始** 按钮。

## 保存你的提示词

要保存你的提示词，请点击 **保存** 按钮并为你的提示词命名。

你在演练场设置中选择的模型和配置将与提示词一起保存。当你重新打开提示词时，模型和配置将自动从保存的版本加载。

<Check>
  首次创建公共提示词时，系统会要求你设置一个 LangChain Hub 句柄。你所有的公共提示词都将链接到此句柄。在共享工作区中，此句柄将为整个工作区设置。
</Check>

## 查看你的提示词

创建提示词后，你可以在左侧边栏的 **提示词** 下查看提示词表格。

## 添加元数据

要为你的提示词添加元数据，请点击页面右上角的 <Icon icon="dots-vertical" /> **更多** 图标，然后从下拉菜单中点击 <Icon icon="pencil" /> **更新元数据**。这将带你到一个页面，你可以在其中添加有关提示词的附加信息，包括描述和 README。

# 后续步骤

现在你已经创建了提示词，可以在应用程序代码中使用它。请参阅[如何以编程方式拉取提示词](/langsmith/manage-prompts-programmatically#pull-a-prompt)。

***

<div className="source-links">
  <Callout icon="terminal-2">
    [连接这些文档](/use-these-docs)到 Claude、VSCode 等，通过 MCP 获取实时答案。
  </Callout>

  <Callout icon="edit">
    [在 GitHub 上编辑此页面](https://github.com/langchain-ai/docs/edit/main/src/langsmith/create-a-prompt.mdx) 或 [提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
