以编程方式使用文档

LangSmith 允许您将转换附加到数据集模式中的字段,这些转换会在数据添加到数据集之前应用,无论数据来自 UI、API 还是运行规则。

结合 LangSmith 预构建的 JSON 模式类型,这些允许您在将数据保存到数据集之前轻松地进行预处理。

转换类型

转换类型目标类型功能
remove_system_messagesArray[Message]过滤消息列表,删除任何系统消息。
convert_to_openai_message消息 Array[Message]使用 langchain 的将任何来自 LangChain 内部序列化格式的传入数据转换为 OpenAI 标准消息格式 convert_to_openai_messages。如果目标字段标记为必填,且在录入时找不到匹配的消息,它将尝试从多个知名 LangSmith 追踪格式中提取消息(或消息列表)(例如,任何追踪的 LangChain BaseChatModel 运行或来自 LangSmith OpenAI 包装器的追踪运行),并删除包含原始消息的密钥。
convert_to_openai_toolArray[Tool] 仅适用于输入字典中的顶级字段。使用 langchain 的将任何传入数据转换为 OpenAI 标准工具格式convert_to_openai_tool] Will extract tool definitions from a run's invocation parameters if present / no tools are found at the specified key. This is useful because LangChain chat models trace tool definitions to the extra.invocation_params 的运行字段,而不是输入。
remove_extra_fieldsObject删除目标对象模式中未定义的任何字段。

聊天模型预构建模式

The main use case for transformations is to simplify collecting production traces into datasets in a format that can be standardized across model providers for usage in evaluations / few shot prompting / etc downstream.

为简化最终用户的转换设置,LangSmith 提供了一个预定义模式,可执行以下操作:

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

兼容性

LLM 运行收集架构旨在从 LangChain BaseChatModel 运行或从 LangSmith OpenAI 包装器.

如果您的 LLM 运行未被兼容追踪,请通过以下方式联系支持团队 support.langchain.com ,我们可以扩展支持。

如果要将转换应用于其他类型的运行(例如,用消息历史表示 LangGraph 状态),请直接定义架构并手动添加相关转换。

启用

将追踪项目或标注队列中的运行添加到数据集时,如果该运行具有 LLM 运行类型,我们将默认应用 Chat Model 架构。

有关新数据集的启用,请参阅我们的 数据集管理操作指南.

规格

有关预构建架构的完整 API 规格,请参阅以下部分:

输入架构

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

输出架构

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

转换

转换如下所示:

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