> ## 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 允许您为数据集模式中的字段附加转换，这些转换会在数据添加到数据集之前应用于您的数据，无论是通过 UI、API 还是运行规则。

结合 [LangSmith 的预构建 JSON 模式类型](/langsmith/dataset-json-types)，这些转换允许您在将数据保存到数据集之前轻松进行预处理。

## 转换类型

| 转换类型                        | 目标类型                          | 功能                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| --------------------------- | ----------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `remove_system_messages`    | `Array[Message]`              | 过滤消息列表以移除任何系统消息。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| `convert_to_openai_message` | Message `Array[Message]`      | 使用 langchain 的 [`convert_to_openai_messages`](https://reference.langchain.com/python/langchain_core/utils/#langchain_core.utils.function_calling.convert_to_openai_messages) 将任何传入数据从 LangChain 的内部序列化格式转换为 OpenAI 的标准消息格式。如果目标字段被标记为必需，并且在输入时未找到匹配的消息，它将尝试从几种知名的 LangSmith 追踪格式（例如，任何已追踪的 LangChain [`BaseChatModel`](https://reference.langchain.com/python/langchain-core/language_models/chat_models/BaseChatModel) 运行或来自 [LangSmith OpenAI 包装器](/langsmith/annotate-code#use-%40traceable-%2F-traceable) 的已追踪运行）中提取消息（或消息列表），并移除包含该消息的原始键。 |
| `convert_to_openai_tool`    | `Array[Tool]` 仅在输入字典的顶级字段上可用。 | 使用 langchain 的 [`convert_to_openai_tool`](https://reference.langchain.com/python/langchain-core/utils/function_calling/convert_to_openai_tool) 将任何传入数据转换为 OpenAI 标准工具格式。如果存在/未在指定键找到工具，它将从运行的调用参数中提取工具定义。这很有用，因为 LangChain 聊天模型将工具定义追踪到运行的 `extra.invocation_params` 字段，而不是输入。                                                                                                                                                                                                                                                                  |
| `remove_extra_fields`       | `Object`                      | 移除此目标对象模式中未定义的任何字段。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |

## 聊天模型预构建模式

转换的主要用例是简化将生产追踪收集到数据集中的过程，使其格式可以在不同模型提供商之间标准化，用于下游评估/少样本提示等。

为了简化我们最终用户的转换设置，LangSmith 提供了一个预定义模式，它将执行以下操作：

* 从您收集的运行中提取消息，并将其转换为 OpenAI 标准格式，这使得它们与所有 LangChain 聊天模型和大多数模型提供商的 SDK 兼容，可用于下游评估和实验
* 提取您的 LLM 使用的任何工具，并将其添加到您的示例输入中，以便在下游评估中用于可重现性

<Check>
  希望迭代其系统提示的用户在使用我们的聊天模型模式时，通常也会在输入消息上添加“移除系统消息”转换，这将阻止您将系统提示保存到数据集。
</Check>

### 兼容性

LLM 运行收集模式旨在从 LangChain [`BaseChatModel`](https://reference.langchain.com/python/langchain-core/language_models/chat_models/BaseChatModel) 运行或来自 [LangSmith OpenAI 包装器](/langsmith/annotate-code#use-%40traceable-%2F-traceable) 的已追踪运行中收集数据。

如果您正在追踪的 LLM 运行不兼容，请通过 [support.langchain.com](https://support.langchain.com) 联系支持团队，我们可以扩展支持。

如果您想对其他类型的运行应用转换（例如，使用消息历史表示 LangGraph 状态），请直接定义您的模式并手动添加相关转换。

### 启用

当从追踪项目或标注队列向数据集添加运行时，如果它具有 LLM 运行类型，我们将默认应用聊天模型模式。

有关新数据集的启用，请参阅我们的[数据集管理操作指南](/langsmith/manage-datasets-in-application)。

### 规范

有关预构建模式的完整 API 规范，请参阅以下部分：

#### 输入模式

```json theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
{
  "type": "object",
  "properties": {
    "messages": {
      "type": "array",
      "items": {
        "$ref": "https://api.smith.langchain.com/public/schemas/v1/message.json"
      }
    },
    "tools": {
      "type": "array",
      "items": {
        "$ref": "https://api.smith.langchain.com/public/schemas/v1/tooldef.json"
      }
    }
  },
  "required": ["messages"]
}
```

#### 输出模式

```json theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
{
  "type": "object",
  "properties": {
    "message": {
      "$ref": "https://api.smith.langchain.com/public/schemas/v1/message.json"
    }
  },
  "required": ["message"]
}
```

#### 转换

转换如下所示：

```json theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
[
  {
    "path": ["inputs"],
    "transformation_type": "remove_extra_fields"
  },
  {
    "path": ["inputs", "messages"],
    "transformation_type": "convert_to_openai_message"
  },
  {
    "path": ["inputs", "tools"],
    "transformation_type": "convert_to_openai_tool"
  },
  {
    "path": ["outputs"],
    "transformation_type": "remove_extra_fields"
  },
  {
    "path": ["outputs", "message"],
    "transformation_type": "convert_to_openai_message"
  }
]
```

***

<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/dataset-transformations.mdx) 或 [提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
