使用命令行界面进行批处理作业管理¶
instructor CLI 提供了在 OpenAI 和 Anthropic 平台管理批处理作业的功能。这种双重支持允许用户利用两个提供商的优势来满足其批处理需求。
支持的提供商¶
- OpenAI:利用 OpenAI 强大的批处理能力。
- Anthropic:利用 Anthropic 的高级语言模型进行批处理操作。
要切换提供商,请在相关命令中使用 --use-anthropic
标志。
$ instructor batch --help
Usage: instructor batch [OPTIONS] COMMAND [ARGS]...
Manage OpenAI and Anthropic Batch jobs
╭─ Options ───────────────────────────────────────────────────────────────────────────╮
│ --help Show this message and exit. │
╰─────────────────────────────────────────────────────────────────────────────────────╯
╭─ Commands ──────────────────────────────────────────────────────────────────────────╮
│ cancel Cancel a batch job │
│ create-from-file Create a batch job from a file │
│ download-file Download the file associated with a batch job │
│ list See all existing batch jobs │
╰─────────────────────────────────────────────────────────────────────────────────────╯
创建批处理作业¶
查看作业¶
$ instructor batch list --help
Usage: instructor batch list [OPTIONS]
See all existing batch jobs
╭─ Options ───────────────────────────────────────────────────────────────────────────╮
│ --limit INTEGER Total number of batch jobs to show │
│ [default: 10] │
│ --poll INTEGER Time in seconds to wait for the batch job to │
│ complete │
│ [default: 10] │
│ --screen --no-screen Enable or disable screen output │
│ [default: no-screen] │
│ --use-anthropic Use Anthropic API instead of OpenAI │
│ [default: False] │
│ --help Show this message and exit. │
╰─────────────────────────────────────────────────────────────────────────────────────╯
这将返回作业列表,如下所示
$ instructor batch list --limit 5
OpenAI Batch Jobs
┏━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━┓
┃ Batch ID ┃ Created At ┃ Status ┃ Failed ┃ Completed ┃ Total ┃
┡━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━┩
│ batch_BSMSiMMy8on2D… │ 2024-06-19 15:10:21 │ cancelled │ 0 │ 298 │ 300 │
│ batch_pD5dqHmqjWYF5… │ 2024-06-19 15:09:38 │ completed │ 0 │ 15 │ 15 │
│ batch_zsTSsWVLgpEan… │ 2024-06-19 15:06:05 │ completed │ 0 │ 15 │ 15 │
│ batch_igaa2j9VBVw2Z… │ 2024-06-19 15:01:59 │ completed │ 0 │ 300 │ 300 │
│ batch_HcjI2wG46Y1LY… │ 2024-06-12 15:45:37 │ completed │ 0 │ 3 │ 3 │
└──────────────────────┴─────────────────────┴───────────┴────────┴───────────┴───────┘
从文件创建¶
您需要提供一个有效的 .jsonl 文件来创建批处理作业。以下是使用 Instructor 创建文件的方法
from instructor.batch import BatchJob
from pydantic import BaseModel, Field
from typing import Literal
class Classification(BaseModel):
label: Literal["SPAM", "NOT_SPAM"] = Field(
..., description="Whether the email is spam or not"
)
emails = [
"Hello there I'm a Nigerian prince and I want to give you money",
"Meeting with Thomas has been set at Friday next week",
"Here are some weekly product updates from our marketing team",
]
messages = [
[
{
"role": "system",
"content": f"Classify the following email {email}",
}
]
for email in emails
]
import json
with open("output.jsonl", "w") as f:
for line in BatchJob.create_from_messages(
messages,
model="gpt-3.5-turbo",
response_model=Classification,
max_tokens=100,
):
f.write(json.dumps(line) + "\n")
$ instructor batch create-from-file --help
Usage: instructor batch create-from-file [OPTIONS]
Create a batch job from a file
╭─ Options ───────────────────────────────────────────────────────────────────────────╮
│ * --file-path TEXT File containing the batch job requests [default: None] │
│ [required] │
│ --use-anthropic Use Anthropic API instead of OpenAI │
│ [default: False] │
│ --help Show this message and exit. │
╰─────────────────────────────────────────────────────────────────────────────────────╯
使用示例
取消批处理作业¶
您可以使用 cancel
命令取消未完成的批处理作业
$ instructor batch cancel --help
Usage: instructor batch cancel [OPTIONS]
Cancel a batch job
╭─ Options ───────────────────────────────────────────────────────────────────────────╮
│ * --batch-id TEXT Batch job ID to cancel [default: None] [required] │
│ --use-anthropic Use Anthropic API instead of OpenAI │
│ [default: False] │
│ --help Show this message and exit. │
╰─────────────────────────────────────────────────────────────────────────────────────╯
使用示例
下载批处理作业结果¶
要下载已完成批处理作业的结果
$ instructor batch download-file --help
Usage: instructor batch download-file [OPTIONS]
Download the file associated with a batch job
╭─ Options ───────────────────────────────────────────────────────────────────────────╮
│ * --batch-id TEXT Batch job ID to download [default: None] [required] │
│ * --download-file-path TEXT Path to download file to [default: None] [required] │
│ --use-anthropic Use Anthropic API instead of OpenAI │
│ [default: False] │
│ --help Show this message and exit. │
╰─────────────────────────────────────────────────────────────────────────────────────╯
使用示例
这套全面的命令允许您高效地管理批处理作业,无论您使用 OpenAI 还是 Anthropic 作为您的提供商。