Skip to main content

Data synthesis toolkit - batch generate high-quality training data from seed examples using LLMs

Project description

DataSynth

数据合成工具 — 基于种子数据批量生成高质量训练数据 Seed-to-scale synthetic data engine for LLM training workflows

PyPI Python 3.10+ License: MIT MCP

快速开始 · 交互模式 · MCP Server · Data Pipeline 生态


GitHub Topics: synthetic-data, data-generation, llm, mcp, ai-data-pipeline

基于少量种子数据和 Schema 定义,使用 LLM 批量生成高质量训练数据。支持 API 模式和交互模式。

核心能力 / Core Capabilities

Schema + 种子数据 (50条) → LLM 合成 → 批量数据 (1000+条) → 质检筛选

输入 / 输出示例 / Input & Output Samples

// seed.json
{
  "schema": {
    "fields": [
      {"name": "instruction", "type": "text"},
      {"name": "response", "type": "text"},
      {"name": "quality", "type": "int", "range": [1,5]}
    ]
  },
  "samples": [{"instruction": "解释 COT", "response": "Chain-of-thought...", "quality": 5}]
}

// output/11_合成数据/synthetic.json
[{"instruction": "给出反事实...", "response": "...", "quality": 4 }]

解决的问题 / Problems Solved

痛点 传统方案 DataSynth
成本 人工标注 $0.1-$10/条 LLM 生成 $0.001-$0.01/条
速度 人工 100条/天 自动 10000条/小时
规模 需要招人、培训 按需弹性生成
一致性 标注员理解差异 规则 + 模板保证一致

工作模式 / Modes

模式 说明 适用场景
API 模式 直接调用 LLM API 有 API key,批量生成
交互模式 生成 Prompt,手动调用 Claude Code 中使用,无需 API key

安装 / Installation

pip install knowlyr-datasynth

可选依赖:

pip install knowlyr-datasynth[anthropic]  # Anthropic Claude
pip install knowlyr-datasynth[openai]     # OpenAI GPT
pip install knowlyr-datasynth[llm]        # 两者都装
pip install knowlyr-datasynth[mcp]        # MCP 服务器
pip install knowlyr-datasynth[all]        # 全部功能

快速开始 / Quick Start

API 模式 (需要 API key) / API Mode

# 设置 API key
export ANTHROPIC_API_KEY=your_key

# 从 DataRecipe 分析结果生成
knowlyr-datasynth generate ./analysis_output/my_dataset/ -n 100

# 估算成本
knowlyr-datasynth generate ./analysis_output/my_dataset/ -n 1000 --dry-run
输出示例
正在从 ./analysis_output/my_dataset/ 生成合成数据...
  目标数量: 100
  模型: claude-sonnet-4-20250514
  进度: 100/100
✓ 生成成功: ./analysis_output/my_dataset/11_合成数据/synthetic.json
  生成数量: 100
  失败数量: 0
  Token 用量: 45,230
  预计成本: $0.1823
  耗时: 42.3s

交互模式 (无需 API key) / Interactive Mode

# 生成 Prompt
knowlyr-datasynth prepare ./analysis_output/my_dataset/ -n 10

# 将 Prompt 复制到 Claude,获取结果后解析

在 Claude Code 中使用更方便,见 MCP Server 章节。


成本估算 / Costing

knowlyr-datasynth estimate -n 1000
成本估算:
  目标数量: 1000
  预计批次: 200
  预计输入 Token: 400,000
  预计输出 Token: 600,000
  预计成本: $10.20
  模型: claude-sonnet-4-20250514

不同规模的成本参考 / Scale Reference

数量 预计成本 预计时间
100 ~$1 ~1 分钟
1,000 ~$10 ~10 分钟
10,000 ~$100 ~2 小时

质量闭环 / Quality Loop

DataRecipe 输出 (Schema + Rubric)
      ↓
DataLabel 人工校准 50 条种子
      ↓
DataSynth 批量合成
      ↓
DataCheck 质检 + 回写报告

在 CLI 中可通过 knowlyr-datasynth generate ... --post-hook "knowlyr-datacheck validate {analysis_dir}" 自动触发后置质检。


交互模式 / Interactive Workflow

交互模式适合在 Claude Code 中使用,不需要 API key:

步骤 1: 准备 Prompt

knowlyr-datasynth prepare ./analysis_output/my_dataset/ -n 10

步骤 2: 将 Prompt 发送给 Claude

复制输出的 Prompt,发送给 Claude 生成数据。

步骤 3: 解析结果

使用 MCP 工具 parse_synthesis_result 解析 Claude 的回复。


Prompt 指南 / Prompt Guide

模板 / Template

You are a data generation engine...
- Field definitions: {{schema}}
- Style guide: {{rubric}}
- Examples ({{seed_count}}): {{seed_examples}}
Generate {{batch_size}} samples in JSONL format.
  • batch_size 建议 ≤20,过大容易触发限流或超时。
  • 根据任务敏感度添加「不得输出 PII / 不得引用真实用户」等约束,降低审查失败概率。
  • 冗长 schema 可分块粘贴 (schema://chunk/<n>) 以保持 Prompt < 8k tokens。

失败重试策略 / Retry Strategy

knowlyr-datasynth generate ... --max-retries 5 --retry-delay 3 --temperature 0.4
  • --max-retries:应对 429/5xx。
  • --retry-delay:大型模型常见 2-5 秒冷却时间。
  • --temperature:偏低→一致性,偏高→多样性;可在重试时递增 0.05。

MCP Server / Claude Integration

在 Claude Desktop / Claude Code 中直接使用。

配置 / Config

添加到 ~/Library/Application Support/Claude/claude_desktop_config.json

{
  "mcpServers": {
    "knowlyr-datasynth": {
      "command": "uv",
      "args": ["--directory", "/path/to/data-synth", "run", "python", "-m", "datasynth.mcp_server"]
    }
  }
}

可用工具 / Tools

工具 功能
prepare_synthesis 准备合成 Prompt(交互模式)
parse_synthesis_result 解析 LLM 生成结果并保存
synthesize_data 直接调用 LLM 生成(需要 API key)
estimate_synthesis_cost 估算生成成本

使用示例 (交互模式) / Usage Example

用户: 帮我基于 ./output/SVGEditBench 生成 20 条合成数据

Claude: [调用 prepare_synthesis]
        生成 Prompt...

        [Claude 自己执行 Prompt 生成数据]

        [调用 parse_synthesis_result]
        ✓ 合成数据已保存:
        - 输出路径: ./output/SVGEditBench/11_合成数据/synthetic.json
        - 生成数量: 20

Data Pipeline 生态 / Ecosystem

DataSynth 是 Data Pipeline 生态的合成组件:

graph LR
    Radar["🔍 Radar<br/>情报发现"] --> Recipe["📋 Recipe<br/>逆向分析"]
    Recipe --> Synth["🔄 Synth<br/>数据合成"]
    Recipe --> Label["🏷️ Label<br/>数据标注"]
    Synth --> Check["✅ Check<br/>数据质检"]
    Label --> Check
    Check --> Audit["🔬 Audit<br/>模型审计"]
    Audit --> Hub["🎯 Hub<br/>编排层"]
    Hub --> Sandbox["📦 Sandbox<br/>执行沙箱"]
    Sandbox --> Recorder["📹 Recorder<br/>轨迹录制"]
    Recorder --> Reward["⭐ Reward<br/>过程打分"]
    style Synth fill:#0969da,color:#fff,stroke:#0969da

生态项目

项目 PyPI 包 说明 仓库
情报 AI Dataset Radar knowlyr-radar 数据集竞争情报、趋势分析 GitHub
分析 DataRecipe knowlyr-datarecipe 逆向分析、Schema 提取、成本估算 GitHub
生产 DataSynth knowlyr-datasynth LLM 批量合成、种子数据扩充 You are here
生产 DataLabel knowlyr-datalabel 轻量标注工具、多标注员合并 GitHub
质检 DataCheck knowlyr-datacheck 规则验证、重复检测、分布分析 GitHub
质检 ModelAudit knowlyr-modelaudit 蒸馏检测、模型指纹、身份验证 GitHub
Agent knowlyr-agent knowlyr-sandbox / recorder / reward / hub 沙箱 + 轨迹录制 + Reward + 编排 GitHub

端到端工作流 / End-to-end Flow

# 1. DataRecipe: 分析数据集,生成 Schema 和样例
knowlyr-datarecipe deep-analyze tencent/CL-bench -o ./output

# 2. DataLabel: 生成标注界面,人工标注/校准种子数据
knowlyr-datalabel generate ./output/tencent_CL-bench/

# 3. DataSynth: 基于种子数据批量合成
knowlyr-datasynth generate ./output/tencent_CL-bench/ -n 1000

# 4. DataCheck: 质量检查
knowlyr-datacheck validate ./output/tencent_CL-bench/

四合一 MCP 配置 / Quad MCP Config

{
  "mcpServers": {
    "knowlyr-datarecipe": {
      "command": "uv",
      "args": ["--directory", "/path/to/data-recipe", "run", "knowlyr-datarecipe-mcp"]
    },
    "knowlyr-datalabel": {
      "command": "uv",
      "args": ["--directory", "/path/to/data-label", "run", "python", "-m", "datalabel.mcp_server"]
    },
    "knowlyr-datasynth": {
      "command": "uv",
      "args": ["--directory", "/path/to/data-synth", "run", "python", "-m", "datasynth.mcp_server"]
    },
    "knowlyr-datacheck": {
      "command": "uv",
      "args": ["--directory", "/path/to/data-check", "run", "python", "-m", "datacheck.mcp_server"]
    }
  }
}

命令参考

命令 功能
knowlyr-datasynth generate <dir> 从 DataRecipe 分析结果生成 (API 模式)
knowlyr-datasynth generate <dir> --dry-run 仅估算成本
knowlyr-datasynth create <schema> <seeds> -o <out> 从自定义文件生成
knowlyr-datasynth prepare <dir> 准备 Prompt (交互模式)
knowlyr-datasynth estimate -n <count> 估算成本

生成选项

选项 说明 默认值
-n, --count 生成数量 100
-m, --model LLM 模型 claude-sonnet-4-20250514
-p, --provider 提供商 anthropic
-t, --temperature 采样温度 0.8
--batch-size 每批数量 5

API 使用

from datasynth import DataSynthesizer, SynthesisConfig

# 配置
config = SynthesisConfig(
    target_count=100,
    model="claude-sonnet-4-20250514",
    provider="anthropic",
    temperature=0.8,
)

# 生成
synthesizer = DataSynthesizer(config)
result = synthesizer.synthesize_from_datarecipe(
    analysis_dir="./output/my_dataset/",
)

print(f"生成数量: {result.generated_count}")
print(f"成本: ${result.estimated_cost:.4f}")

项目架构

src/datasynth/
├── synthesizer.py    # 核心合成器
├── prompts.py        # Prompt 模板和解析
├── config.py         # 配置和 Schema
├── cli.py            # CLI 命令行
└── mcp_server.py     # MCP Server (4 工具)

License

MIT

knowlyr 数据工程生态 · 种子到规模化数据合成

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

knowlyr_datasynth-0.1.0.tar.gz (95.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

knowlyr_datasynth-0.1.0-py3-none-any.whl (22.1 kB view details)

Uploaded Python 3

File details

Details for the file knowlyr_datasynth-0.1.0.tar.gz.

File metadata

  • Download URL: knowlyr_datasynth-0.1.0.tar.gz
  • Upload date:
  • Size: 95.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for knowlyr_datasynth-0.1.0.tar.gz
Algorithm Hash digest
SHA256 733a93f0d0ff998f9771f796d43e293bf67653c65fb1148aeea9280bb6a6254c
MD5 421820a62be800bffb5fb46569592c7d
BLAKE2b-256 e02824ec262bed061ae84cc4f6951b58a410709c50786643c5974be0d20f9d64

See more details on using hashes here.

File details

Details for the file knowlyr_datasynth-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: knowlyr_datasynth-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 22.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for knowlyr_datasynth-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b06646b4f654e65bb5dbc272aca67ec3d94dd240dccf4663b37a9cf6f110de3c
MD5 5a7555a16e48e64692d76eb4b2d435a7
BLAKE2b-256 f7a28dc5ee3135634b9edf3787416729b349d9557e182cc8de87541e05830066

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page