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
Metadata
Release files for batch-swallow 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| batch_swallow-0.1.0.tar.gz | 10.0 kB | Without provenance | Details for batch_swallow-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| batch_swallow-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for batch_swallow-0.1.0-py3-none-any.whl |
Total release size: 20.8 kB
Release files / batch_swallow-0.1.0.tar.gz
| Download URL | batch_swallow-0.1.0.tar.gz |
|---|---|
| Size | 10.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b5c3f789a528b54c7a18f9e5027a70eb69072ca9acb9d34ab9ecdbd309585df7
|
|
BLAKE2b-256 checksum How to use checksums |
c2abb862d8cc53993a2aba43e17a4dce327e4b794a7876b7112c5f67be43596b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.3
|
Release files / batch_swallow-0.1.0-py3-none-any.whl
| Download URL | batch_swallow-0.1.0-py3-none-any.whl |
|---|---|
| Size | 10.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
fc5699a9fc77ed56df5655e2662fd23ea9d5879103ecefe848399207cb0a831e
|
|
BLAKE2b-256 checksum How to use checksums |
e07aa96164712b050cc3a26495ec93610d79435d24489c5bb1ba8de5bf1e0423
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.3
|
Security for batch-swallow 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.