Skip to main content

数据集转换

LangSmith 允许您为数据集模式中的字段附加转换操作,这些转换会在数据(无论来自用户界面、API 还是运行规则)被添加到数据集之前应用到您的数据上。

结合LangSmith 预置的 JSON 模式类型,这些功能可让您在将数据保存到数据集之前轻松完成预处理。

转换类型

转换类型目标类型功能
remove_system_messagesArray[Message]Filters a list of messages to remove any system messages.
convert_to_openai_messageMessage
Array[Message]
Converts any incoming data from LangChain's internal serialization format to OpenAI's standard message format using langchain's convert_to_openai_messages.

If the target field is marked as required, and no matching message is found upon entry, it will attempt to extract a message (or list of messages) from several well-known LangSmith tracing formats (e.g., any traced LangChain BaseChatModel run or traced run from the LangSmith OpenAI wrapper), and remove the original key containing the message.
convert_to_openai_toolArray[Tool]

Only available on top level fields in the inputs dictionary.
Converts any incoming data into OpenAI standard tool formats here using langchain's 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 field of the run rather than inputs.
remove_extra_fieldsObjectRemoves any field not defined in the schema for this target object.

聊天模型预构建模式

转换功能的主要使用场景是简化将生产环境中的追踪数据收集为数据集的过程,使其格式能够标准化,以便在不同模型提供商之间统一应用于评估、少样本提示等下游任务。

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

  • 从您收集的运行记录中提取消息,并将其转换为 OpenAI 标准格式,从而使其兼容所有 LangChain 聊天模型(ChatModels)以及大多数模型提供商的 SDK,便于下游评估与实验。
  • 提取您的大语言模型所使用的任何工具,并将它们添加到示例的输入中,以便在下游评估中实现可复现性。
提示

希望迭代系统提示词的用户,在使用我们的聊天模型(Chat Model)架构时,通常还会在输入消息上添加“移除系统消息”(Remove System Messages)转换操作,这将导致您无法将系统提示词保存到数据集中。

兼容性

大语言模型(LLM)运行集合模式旨在收集来自 LangChain BaseChatModel 的运行数据,或来自 LangSmith OpenAI 封装器 的已追踪运行数据。

如果您的大型语言模型(LLM)运行实例在追踪过程中存在兼容性问题,请联系 support@langchain.dev,我们将为您扩展支持。

如果您希望对其他类型的运行(例如,使用消息历史记录表示 LangGraph 状态)应用转换,请直接定义您的模式,并手动添加相关的转换。

赋能

在将追踪项目或标注队列中的运行(run)添加到数据集时,如果该运行的类型为 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"
}
]

这个页面对你有帮助吗?


您可以留下详细的反馈 在 GitHub 上.