使用 @traceable 装饰器或 traceable 包装器进行追踪时,LangSmith支持上传二进制文件(如图片、音频、视频、PDF和CSV)以及追踪。这在使用多模态输入或输出的LLM管道时特别有用。
在 Python 和 TypeScript SDK中,您可以通过指定每个文件的MIME类型和二进制内容来向追踪添加附件。本页面解释如何使用 Attachment 类型在Python中以及 Uint8Array / ArrayBuffer 在TypeScript中定义和追踪附件。
Python
在 Python SDK中,您可以使用 Attachment 类型向追踪添加文件。每个 Attachment requires:
- -
mime_type(str):文件的MIME类型(例如,"image/png"). - -
data(bytes | Path):文件的二进制内容或文件路径。
您还可以使用以下形式的元组定义附件 (mime_type, data) 以方便使用。
提供文件数据有两种方式:
- - 自己加载字节并直接传递它们(适用于所有环境),或者
- - 传递一个
Path对象并通过设置让SDK读取文件,在您的dangerously_allow_filesystem=True上设置@traceabledecorator.
使用 @traceable 装饰函数,并包含您的 Attachment 实例作为参数。以下示例演示了两种方法:手动将文件字节加载到 Attachment中,以及传递一个带有 Path 的对象 dangerously_allow_filesystem=True:
from langsmith import traceable
from langsmith.schemas import Attachment
from pathlib import Path
# Must set dangerously_allow_filesystem to True if you want to use file paths
@traceable(dangerously_allow_filesystem=True)
def trace_with_attachments(
val: int,
text: str,
image: Attachment,
audio: Attachment,
video: Attachment,
pdf: Attachment,
csv: Attachment,
):
return f"Processed: {val}, {text}, {len(image.data)}, {len(audio.data)}, {len(video.data)}, {len(pdf.data), {len(csv.data)}}"
# Helper function to load files as bytes
def load_file(file_path: str) -> bytes:
with open(file_path, "rb") as f:
return f.read()
# Load files and create attachments
image_data = load_file("my_image.png")
audio_data = load_file("my_mp3.mp3")
video_data = load_file("my_video.mp4")
pdf_data = load_file("my_document.pdf")
image_attachment = Attachment(mime_type="image/png", data=image_data)
audio_attachment = Attachment(mime_type="audio/mpeg", data=audio_data)
video_attachment = Attachment(mime_type="video/mp4", data=video_data)
pdf_attachment = ("application/pdf", pdf_data) # Can just define as tuple of (mime_type, data)
csv_attachment = Attachment(mime_type="text/csv", data=Path(os.getcwd()) / "my_csv.csv")
# Define other parameters
val = 42
text = "Hello, world!"
# Call the function with traced attachments
result = trace_with_attachments(
val=val,
text=text,
image=image_attachment,
audio=audio_attachment,
video=video_attachment,
pdf=pdf_attachment,
csv=csv_attachment,
)
TypeScript
在 TypeScript SDK中,您可以使用 Uint8Array or ArrayBuffer 作为数据类型向追踪添加附件。每个附件的MIME类型在 extractAttachments:
- -
Uint8Array中指定:用于直接处理二进制数据。 - -
ArrayBuffer:表示固定长度的二进制数据,您可以将其转换为Uint8Array按需使用。
在TypeScript SDK中, extractAttachments 函数是 traceable 配置中的可选参数。当调用追踪包装的函数时,它会从输入中提取二进制数据(例如图片、音频文件),并与其他追踪数据一起记录,指定其MIME类型。
使用 traceable 包装您的函数,并在 extractAttachments 选项。签名是:
type AttachmentData = Uint8Array | ArrayBuffer;
type Attachments = Record<string, [string, AttachmentData]>;
extractAttachments?: (
...args: Parameters
) => [Attachments | undefined, KVMap];
以下示例展示了一个完整实现:
const traceableWithAttachments = traceable(
(
val: number,
text: string,
attachment: Uint8Array,
attachment2: ArrayBuffer,
attachment3: Uint8Array,
attachment4: ArrayBuffer,
attachment5: Uint8Array,
) =>
`Processed: ${val}, ${text}, ${attachment.length}, ${attachment2.byteLength}, ${attachment3.length}, ${attachment4.byteLength}, ${attachment5.byteLength}`,
{
name: "traceWithAttachments",
extractAttachments: (
val: number,
text: string,
attachment: Uint8Array,
attachment2: ArrayBuffer,
attachment3: Uint8Array,
attachment4: ArrayBuffer,
attachment5: Uint8Array,
) => [
{
"image inputs": ["image/png", attachment],
"mp3 inputs": ["audio/mpeg", new Uint8Array(attachment2)],
"video inputs": ["video/mp4", attachment3],
"pdf inputs": ["application/pdf", new Uint8Array(attachment4)],
"csv inputs": ["text/csv", new Uint8Array(attachment5)],
},
{ val, text },
],
}
);
const fs = Deno // or Node.js fs module
const image = await fs.readFile("my_image.png"); // Uint8Array
const mp3Buffer = await fs.readFile("my_mp3.mp3");
const mp3ArrayBuffer = mp3Buffer.buffer; // Convert to ArrayBuffer
const video = await fs.readFile("my_video.mp4"); // Uint8Array
const pdfBuffer = await fs.readFile("my_document.pdf");
const pdfArrayBuffer = pdfBuffer.buffer; // Convert to ArrayBuffer
const csv = await fs.readFile("test-vals.csv"); // Uint8Array
// Define example parameters
const val = 42;
const text = "Hello, world!";
// Call traceableWithAttachments with the files
const result = await traceableWithAttachments(
val, text, image, mp3ArrayBuffer, video, pdfArrayBuffer, csv
);