展示 HN:Runprompt – 从命令行运行 .prompt 文件
Show HN: Runprompt – run .prompt files from the command line

原始链接: https://github.com/chr15m/runprompt

## RunPrompt:一个简单的提示运行器 RunPrompt 是一个 Python 脚本,用于轻松执行 `.prompt` 文件,并与大型语言模型 (LLM) 交互。它简化了与 Anthropic、OpenAI、Google AI 和 OpenRouter 等提供商的交互。 使用方法:下载并使脚本可执行,然后创建 `.prompt` 文件,定义 `model` 和提示内容。输入通过 `STDIN`(使用 `{{STDIN}}`)或作为 JSON 提供。RunPrompt 支持使用输出模式(定义 `name: type? description`)进行结构化数据提取,并支持在提示之间进行管道传输以进行链式操作。 配置灵活:API 密钥通过环境变量设置(例如 `ANTHROPIC_API_KEY`),前置信息值可以通过命令行参数(`--name "Alice"`)或以 `RUNPROMPT_` 前缀的环境变量覆盖。详细模式 (`-v`) 显示请求/响应详细信息,用于调试。 模型指定为 `provider/model-name`(例如 `anthropic/claude-sonnet-4-20250514`)。示例提示和进一步的文档可在项目的测试文件夹中找到。

## Runprompt:命令行LLM提示词执行 Runprompt是一个新的单文件Python脚本,旨在从命令行直接执行LLM提示词,灵感来自Google的Dotprompt格式。它允许用户将提示词视为可执行程序,从而实现类Unix风格的管道和链式操作,以进行复杂的工作流程。 主要功能包括:在提示词中定义结构化的JSON输出模式,通过管道将JSON输出作为输入将提示词链接在一起,以及无需依赖项——仅需要Python标准库。 Runprompt支持多种LLM提供商,如Anthropic、OpenAI、Google AI和OpenRouter,提供模型选择的灵活性。它旨在用于自动化诸如数据提取、报告生成和构建简单的代理工作流程等任务,而无需大型框架的开销。开发者欢迎反馈和贡献。
相关文章

原文

A single-file Python script for running .prompt files.

Quick start | Examples | Configuration | Providers

curl -O https://raw.githubusercontent.com/chr15m/runprompt/main/runprompt
chmod +x runprompt

Create hello.prompt:

---
model: anthropic/claude-sonnet-4-20250514
---
Say hello to {{name}}!

Run it:

export ANTHROPIC_API_KEY="your-key"
echo '{"name": "World"}' | ./runprompt hello.prompt

In addition to the following, see the tests folder for more example .prompt files.

---
model: anthropic/claude-sonnet-4-20250514
---
Summarize this text: {{STDIN}}
cat article.txt | ./runprompt summarize.prompt

The special {{STDIN}} variable always contains the raw stdin as a string.

Extract structured data using an output schema:

---
model: anthropic/claude-sonnet-4-20250514
input:
  schema:
    text: string
output:
  format: json
  schema:
    name?: string, the person's name
    age?: number, the person's age
    occupation?: string, the person's job
---
Extract info from: {{text}}
echo "John is a 30 year old teacher" | ./runprompt extract.prompt
# {"name": "John", "age": 30, "occupation": "teacher"}

Fields ending with ? are optional. The format is field: type, description.

Pipe structured output between prompts:

echo "John is 30" | ./runprompt extract.prompt | ./runprompt generate-bio.prompt

The JSON output from the first prompt becomes template variables in the second.

Override any frontmatter value from the command line:

./runprompt --model anthropic/claude-haiku-4-20250514 hello.prompt
./runprompt --name "Alice" hello.prompt

Set API keys for your providers:

export ANTHROPIC_API_KEY="..."
export OPENAI_API_KEY="..."
export GOOGLE_API_KEY="..."
export OPENROUTER_API_KEY="..."

Override any frontmatter value via environment variables prefixed with RUNPROMPT_:

export RUNPROMPT_MODEL="anthropic/claude-haiku-4-20250514"
./runprompt hello.prompt

This is useful for setting defaults across multiple prompt runs.

Use -v to see request/response details:

./runprompt -v hello.prompt

Models are specified as provider/model-name:

Provider Model format API key env var
Anthropic anthropic/claude-sonnet-4-20250514 ANTHROPIC_API_KEY
OpenAI openai/gpt-4o OPENAI_API_KEY
Google AI googleai/gemini-1.5-pro GOOGLE_API_KEY
OpenRouter openrouter/anthropic/claude-sonnet-4-20250514 OPENROUTER_API_KEY

OpenRouter provides access to models from many providers (Anthropic, Google, Meta, etc.) through a single API key.

联系我们 contact @ memedata.com