Skip to content
LangSmith 文档版本 跟随上游 源快照 08ff5eba 更新于 2026-10-18

数据集转换

LangSmith 允许你为数据集 schema 中的字段附加转换,这些转换会在数据加入数据集之前应用,无论数据来自 UI、API 还是 run 规则。

结合 LangSmith 预置的 JSON schema 类型,你可以在数据保存到数据集之前轻松完成预处理。

转换类型

转换类型目标类型功能
remove_system_messagesArray[Message]过滤消息列表,移除所有 system 消息。
convert_to_openai_messageMessage Array[Message]使用 LangChain 的 convert_to_openai_messages 将任何传入数据从 LangChain 内部序列化格式转换为 OpenAI 的标准消息格式。如果目标字段标记为必需,且进入时未找到匹配的消息,它会尝试从几种知名的 LangSmith 追踪格式(例如任何被追踪的 LangChain BaseChatModel run,或来自 LangSmith OpenAI 包装器的被追踪 run)中提取一条消息(或消息列表),并移除包含该消息的原始键。
convert_to_openai_toolArray[Tool] 仅适用于 inputs 字典中的顶层字段。在此使用 LangChain 的 convert_to_openai_tool 将任何传入数据转换为 OpenAI 标准工具格式。会在存在时从 run 的 invocation parameters 中提取工具定义,也会在指定键处找不到工具时这样做。这很有用,因为 LangChain chat model 会把工具定义追踪到 run 的 extra.invocation_params 字段,而不是 inputs。
remove_extra_fieldsObject移除此目标对象 schema 中未定义的任何字段。

Chat model 预置 schema

转换的主要用例是简化将生产追踪收集到数据集的过程,将其转为可跨模型提供商标准化的格式,用于下游的评估 / few-shot 提示等。

为简化终端用户的转换设置,LangSmith 提供了一个预定义 schema,它会执行以下操作:

  • 从收集到的 run 中提取消息,并将其转换为 OpenAI 标准格式,使其兼容所有 LangChain ChatModel 以及大多数模型提供商的 SDK,用于下游评估和实验
  • 提取你的 LLM 使用的任何工具,并将其添加到示例的输入中,以便在下游评估中复现

TIP

在使用我们的 Chat Model schema 时,想要迭代系统提示词的用户通常还会在输入消息上添加 Remove System Messages 转换,这将防止系统提示词被保存到数据集中。

兼容性

LLM run 收集 schema 用于从 LangChain BaseChatModel run 或 LangSmith OpenAI 包装器的被追踪 run 中收集数据。

如果你追踪的某个 LLM run 不兼容,请通过 support.langchain.com 联系我们寻求支持,我们可以扩展兼容范围。

如果你想对其他类型的 run 应用转换(例如用消息历史表示 LangGraph state),请直接定义你的 schema,并手动添加相关转换。

启用方式

从追踪项目或标注队列向数据集添加 run 时,如果该 run 是 LLM 类型,我们会默认应用 Chat Model schema。

在新数据集上的启用方式,请参阅我们的数据集管理操作指南。

规格

预置 schema 的完整 API 规格见以下各节:

输入 schema

json
{
  "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"]
}

输出 schema

json
{
  "type": "object",
  "properties": {
    "message": {
      "$ref": "https://api.smith.langchain.com/public/schemas/v1/message.json"
    }
  },
  "required": ["message"]
}

转换

转换的内容如下:

json
[
  {
    "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"
  }
]

BytesRouter 是社区翻译站点,所有译文仅供参考,一切以官方英文原文为准。