Skip to main content

批量推理 JSONL 文件生成工具,快速构建阿里云批量推理请求,支持 API 调用和 CLI 命令行。

Project description

batch-swallow

批量推理 JSONL 文件生成工具 —— 快速构建阿里云批量推理请求。

同时支持作为 Python 库导入命令行工具(CLI) 两种方式使用。


安装

pip install batch-swallow

或从源码安装:

git clone https://github.com/your-username/batch-swallow.git
cd batch-swallow
pip install -e .

作为 Python 库使用

生成 Prompt

from batch_swallow import generate_prompt, generate_prompt_safe

# 标准模式
prompt = generate_prompt("你好,{user}!今天天气{weather}。", user="小明", weather="晴朗")
print(prompt)  # 你好,小明!今天天气晴朗。

# 安全模式:缺失占位符不会抛出异常
prompt = generate_prompt_safe("Hello, {name}! You are {age} years old.", name="Alice")
print(prompt)  # Hello, Alice! You are  years old.

构造批量请求并生成 JSONL

from batch_swallow import generate_requests_data, generate_batch_jsonl

# 1. 构造请求数据
questions = ["什么是人工智能?", "What is 2+2?"]
requests_data = generate_requests_data(
    questions,
    system_prompt="You are a helpful assistant."
)

# 2. 生成 JSONL 文件
jsonl_str = generate_batch_jsonl(
    requests=requests_data,
    model="qwen-max",
    output_file="batch.jsonl",
    enable_thinking=False,
)

print(f"已生成 {len(jsonl_str.splitlines())} 条请求")

高级用法:自定义消息模板

from batch_swallow import generate_requests_data, generate_batch_jsonl

# 多轮对话场景
template = [
    {"role": "system", "content": "你是一个翻译助手。"},
    {"role": "user", "content": "请翻译:你好"},
    {"role": "assistant", "content": "Hello"},
    {"role": "user", "content": "请翻译:再见"},
]
requests_data = generate_requests_data(
    questions=["placeholder"],  # 占位,数量由 questions 长度决定
    message_template=template,
)

jsonl_str = generate_batch_jsonl(requests_data, model="qwen-max", output_file="multi-turn.jsonl")

命令行工具(CLI)

安装后可直接在终端使用 batch-swallow 命令。

查看帮助

batch-swallow --help

生成 JSONL 文件

首先准备输入文件 questions.json

{
  "questions": [
    "什么是人工智能?",
    "What is the capital of France?",
    "请用 Python 实现快速排序"
  ]
}

然后运行:

batch-swallow generate-jsonl \
  --input questions.json \
  --model qwen-max \
  --output batch.jsonl \
  --system-prompt "You are a helpful assistant."

支持开启思考模式:

batch-swallow generate-jsonl \
  --input questions.json \
  --model qwen-max \
  --output batch.jsonl \
  --enable-thinking \
  --thinking-budget 1024

输入文件也支持直接传入请求列表格式:

[
  {"messages": [{"role": "user", "content": "你好"}]},
  {"messages": [{"role": "user", "content": "再见"}]}
]

生成 Prompt

batch-swallow generate-prompt \
  --template "请翻译以下内容为{lang}:{text}" \
  -p lang=英文 \
  -p text=你好世界

使用安全模式(缺失占位符替换为空字符串):

batch-swallow generate-prompt \
  --template "Hello, {name}! Age: {age}" \
  -p name=Alice \
  --safe

API 参考

函数 说明
generate_prompt(template, **kwargs) 根据模板生成 prompt,缺失占位符抛出异常
generate_prompt_safe(template, default, **kwargs) 安全版本,缺失占位符使用默认值
render_template(template, context) 使用字典上下文渲染模板
generate_requests_data(questions, system_prompt, message_template) 构造批量推理请求数据
generate_batch_jsonl(requests, model, output_file, ...) 生成 JSONL 格式文件

异常类

异常 说明
BatchInferenceError 模块基础异常
ValidationError 参数校验失败

许可证

MIT License

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

batch_swallow-0.1.0.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

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

batch_swallow-0.1.0-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: batch_swallow-0.1.0.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.3

File hashes

Hashes for batch_swallow-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b5c3f789a528b54c7a18f9e5027a70eb69072ca9acb9d34ab9ecdbd309585df7
MD5 607552a8f73db6c1a2b1d009d96d29c0
BLAKE2b-256 c2abb862d8cc53993a2aba43e17a4dce327e4b794a7876b7112c5f67be43596b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: batch_swallow-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.3

File hashes

Hashes for batch_swallow-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fc5699a9fc77ed56df5655e2662fd23ea9d5879103ecefe848399207cb0a831e
MD5 9411f25b2aececdaf031c7c0b7dcad17
BLAKE2b-256 e07aa96164712b050cc3a26495ec93610d79435d24489c5bb1ba8de5bf1e0423

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