import requestsimport uuidfrom pathlib import Pathfrom langsmith import Client# 公开可用的测试文件pdf_url = "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf"wav_url = "https://openaiassets.blob.core.windows.net/$web/API/docs/audio/alloy.wav"img_url = "https://www.w3.org/Graphics/PNG/nurbcup2si.png"# 以字节形式获取文件pdf_bytes = requests.get(pdf_url).contentwav_bytes = requests.get(wav_url).contentimg_bytes = requests.get(img_url).content# 创建数据集ls_client = Client()dataset_name = "attachment-test-dataset"dataset = ls_client.create_dataset( dataset_name=dataset_name, description="Test dataset for evals with publicly available attachments",)inputs = { "audio_question": "What is in this audio clip?", "image_question": "What is in this image?",}outputs = { "audio_answer": "The sun rises in the east and sets in the west. This simple fact has been observed by humans for thousands of years.", "image_answer": "A mug with a blanket over it.",}# 定义带有附件的示例example_id = uuid.uuid4()example = { "id": example_id, "inputs": inputs, "outputs": outputs, "attachments": { "my_pdf": {"mime_type": "application/pdf", "data": pdf_bytes}, "my_wav": {"mime_type": "audio/wav", "data": wav_bytes}, "my_img": {"mime_type": "image/png", "data": img_bytes}, # 通过本地文件路径指定附件的示例: # "my_local_img": {"mime_type": "image/png", "data": Path(__file__).parent / "my_local_img.png"}, },}# 创建示例ls_client.create_examples( dataset_id=dataset.id, examples=[example], # 如果您想从本地文件上传附件,请取消注释此标志: # dangerously_allow_filesystem=True)
import { Client } from "langsmith";import { v4 as uuid4 } from "uuid";// 公开可用的测试文件const pdfUrl = "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf";const wavUrl = "https://openaiassets.blob.core.windows.net/$web/API/docs/audio/alloy.wav";const pngUrl = "https://www.w3.org/Graphics/PNG/nurbcup2si.png";// 辅助函数:将文件获取为ArrayBufferasync function fetchArrayBuffer(url: string): Promise<ArrayBuffer> { const response = await fetch(url); if (!response.ok) { throw new Error(`Failed to fetch ${url}: ${response.statusText}`); } return response.arrayBuffer();}// 将文件获取为ArrayBufferconst pdfArrayBuffer = await fetchArrayBuffer(pdfUrl);const wavArrayBuffer = await fetchArrayBuffer(wavUrl);const pngArrayBuffer = await fetchArrayBuffer(pngUrl);// 创建LangSmith客户端(确保在环境中设置了LANGSMITH_API_KEY)const langsmithClient = new Client();// 创建唯一的数据集名称const datasetName = "attachment-test-dataset:" + uuid4().substring(0, 8);// 创建数据集const dataset = await langsmithClient.createDataset(datasetName, { description: "Test dataset for evals with publicly available attachments",});// 定义带有附件的示例const exampleId = uuid4();const example = { id: exampleId, inputs: { audio_question: "What is in this audio clip?", image_question: "What is in this image?", }, outputs: { audio_answer: "The sun rises in the east and sets in the west. This simple fact has been observed by humans for thousands of years.", image_answer: "A mug with a blanket over it.", }, attachments: { my_pdf: { mimeType: "application/pdf", data: pdfArrayBuffer }, my_wav: { mimeType: "audio/wav", data: wavArrayBuffer }, my_img: { mimeType: "image/png", data: pngArrayBuffer }, },};// 将带有附件的示例上传到数据集await langsmithClient.uploadExamplesMultipart(dataset.id, [example]);
除了以字节形式传入外,附件还可以指定为本地文件的路径。为此,请为附件 data 值传入路径,并指定参数 dangerously_allow_filesystem=True: