跳到主要内容
您可以使用 Python 和 TypeScript SDK 以编程方式管理数据集。这包括创建、更新和删除数据集,以及向其中添加示例。

创建数据集

从值列表创建数据集

使用客户端创建数据集最灵活的方式是通过输入列表和可选输出创建示例。下面是一个示例。 请注意,您可以为每个示例添加任意元数据,例如备注或来源。元数据以字典形式存储。
如果您需要创建许多示例,请考虑使用 `create_examples`/`createExamples` 方法在一个请求中创建多个示例。如果创建单个示例,可以使用 `create_example`/`createExample` 方法。
from langsmith import Client

examples = [
  {
    "inputs": {"question": "What is the largest mammal?"},
    "outputs": {"answer": "The blue whale"},
    "metadata": {"source": "Wikipedia"},
  },
  {
    "inputs": {"question": "What do mammals and birds have in common?"},
    "outputs": {"answer": "They are both warm-blooded"},
    "metadata": {"source": "Wikipedia"},
  },
  {
    "inputs": {"question": "What are reptiles known for?"},
    "outputs": {"answer": "Having scales"},
    "metadata": {"source": "Wikipedia"},
  },
  {
    "inputs": {"question": "What's the main characteristic of amphibians?"},
    "outputs": {"answer": "They live both in water and on land"},
    "metadata": {"source": "Wikipedia"},
  },
]

client = Client()
dataset_name = "Elementary Animal Questions"

# Storing inputs in a dataset lets us
# run chains and LLMs over a shared set of examples.
dataset = client.create_dataset(
  dataset_name=dataset_name, description="Questions and answers about animal phylogenetics.",
)

# Prepare inputs, outputs, and metadata for bulk creation
client.create_examples(
  dataset_id=dataset.id,
  examples=examples
)

从追踪创建数据集

要从追踪的运行(span)创建数据集,您可以使用相同的方法。有关如何获取和过滤运行的更多示例,请参阅导出追踪指南。下面是一个示例。
from langsmith import Client

client = Client()
dataset_name = "Example Dataset"

# Filter runs to add to the dataset
runs = client.list_runs(
  project_name="my_project",
  is_root=True,
  error=False,
)

dataset = client.create_dataset(dataset_name, description="An example dataset")

# Prepare inputs and outputs for bulk creation
examples = [{"inputs": run.inputs, "outputs": run.outputs} for run in runs]

# Use the bulk create_examples method
client.create_examples(
  dataset_id=dataset.id,
  examples=examples
)

从 CSV 文件创建数据集

在本节中,我们将演示如何通过上传 CSV 文件来创建数据集。 首先,请确保您的 CSV 文件格式正确,其中包含表示输入和输出键的列。在上传过程中,这些键将用于正确映射您的数据。您可以为数据集指定一个可选的名称和描述。否则,文件名将用作数据集名称,并且不会提供描述。
from langsmith import Client
import os

client = Client()
csv_file = 'path/to/your/csvfile.csv'
input_keys = ['column1', 'column2'] # replace with your input column names
output_keys = ['output1', 'output2'] # replace with your output column names

dataset = client.upload_csv(
  csv_file=csv_file,
  input_keys=input_keys,
  output_keys=output_keys,
  name="My CSV Dataset",
  description="Dataset created from a CSV file",
  data_type="kv"
)

从 pandas DataFrame 创建数据集(仅限 Python)

Python 客户端提供了另一个方便的方法,可以从 pandas 数据框上传数据集。
from langsmith import Client
import os
import pandas as pd

client = Client()
df = pd.read_parquet('path/to/your/myfile.parquet')
input_keys = ['column1', 'column2'] # replace with your input column names
output_keys = ['output1', 'output2'] # replace with your output column names

dataset = client.upload_dataframe(
    df=df,
    input_keys=input_keys,
    output_keys=output_keys,
    name="My Parquet Dataset",
    description="Dataset created from a parquet file",
    data_type="kv" # The default
)

获取数据集

您可以使用 Python 和 TypeScript SDK 中的 `list_datasets`/`listDatasets` 方法从 LangSmith 以编程方式获取数据集。下面是一些常见的调用。
在运行以下代码片段之前初始化客户端。
from langsmith import Client

client = Client()

查询所有数据集

datasets = client.list_datasets()

按名称列出数据集

如果您想按精确名称搜索,可以执行以下操作
datasets = client.list_datasets(dataset_name="My Test Dataset 1")
如果您想进行不区分大小写的子字符串搜索,请尝试以下操作
datasets = client.list_datasets(dataset_name_contains="some substring")

按类型列出数据集

您可以按类型筛选数据集。下面是一个查询聊天数据集的示例。
datasets = client.list_datasets(data_type="chat")

获取示例

您可以使用 Python 和 TypeScript SDK 中的 `list_examples`/`listExamples` 方法从 LangSmith 以编程方式获取示例。下面是一些常见的调用。
在运行以下代码片段之前初始化客户端。
from langsmith import Client

client = Client()

列出数据集的所有示例

您可以按数据集 ID 筛选
examples = client.list_examples(dataset_id="c9ace0d8-a82c-4b6c-13d2-83401d68e9ab")
或者您可以按数据集名称筛选(这必须与您要查询的数据集名称完全匹配)
examples = client.list_examples(dataset_name="My Test Dataset")

按 ID 列出示例

您还可以按 ID 列出多个示例。
example_ids = [
  '734fc6a0-c187-4266-9721-90b7a025751a',
  'd6b4c1b9-6160-4d63-9b61-b034c585074f',
  '4d31df4e-f9c3-4a6e-8b6c-65701c2fed13',
]

examples = client.list_examples(example_ids=example_ids)

按元数据列出示例

您还可以按元数据筛选示例。下面是一个查询具有特定元数据键值对的示例的示例。在内部,我们检查示例的元数据是否包含您指定的键值对。 例如,如果一个示例的元数据为 {"foo": "bar", "baz": "qux"},那么 {foo: bar}{baz: qux} 都将匹配,{foo: bar, baz: qux} 也会匹配。
examples = client.list_examples(dataset_name=dataset_name, metadata={"foo": "bar"})

按结构化过滤器列出示例

与您使用结构化过滤器查询语言获取运行的方式类似,您也可以使用它来获取示例。
此功能目前仅在 Python SDK 的 v0.1.83 及更高版本以及 TypeScript SDK 的 v0.1.35 及更高版本中可用。此外,结构化过滤器查询语言仅支持 `metadata` 字段。
您可以使用 `has` 运算符获取包含特定键/值对的元数据字段的示例,使用 `exists` 运算符获取包含特定键的元数据字段的示例。此外,您还可以使用 `and` 运算符将多个过滤器串联起来,并使用 `not` 运算符否定过滤器。
examples = client.list_examples(
  dataset_name=dataset_name,
  filter='and(not(has(metadata, \'{"foo": "bar"}\')), exists(metadata, "tenant_id"))'
)

更新示例

更新单个示例

您可以使用 Python 和 TypeScript SDK 中的 `update_example`/`updateExample` 方法以编程方式更新 LangSmith 中的示例。下面是一个示例。
client.update_example(
  example_id=example.id,
  inputs={"input": "updated input"},
  outputs={"output": "updated output"},
  metadata={"foo": "bar"},
  split="train"
)

批量更新示例

您还可以使用 Python 和 TypeScript SDK 中的 `update_examples`/`updateExamples` 方法在一个请求中以编程方式更新多个示例。下面是一个示例。
client.update_examples(
  example_ids=[example.id, example_2.id],
  inputs=[{"input": "updated input 1"}, {"input": "updated input 2"}],
  outputs=[
      {"output": "updated output 1"},
      {"output": "updated output 2"},
  ],
  metadata=[{"foo": "baz"}, {"foo": "qux"}],
  splits=[["training", "foo"], "training"] # Splits can be arrays or standalone strings
)

以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.