安装 belt CLI 技能:
npx skills add belt-sh/cli
AI 自动化工作流
通过 inference.sh CLI 构建自动化 AI 工作流。

快速开始
需要 installation.sh CLI (
belt)。安装指南
belt login
# 简单自动化:每日生成图片
belt app run falai/flux-dev --input '{
"prompt": "Inspirational quote background, minimalist design, date: '"$(date +%Y-%m-%d)"'"
}'
自动化模式
模式 1:批处理
使用相同工作流处理多个项目。
#!/bin/bash
# batch_images.sh - 为多个提示生成图片
PROMPTS=(
"Mountain landscape at sunrise"
"Ocean waves at sunset"
"Forest path in autumn"
"Desert dunes at night"
)
for prompt in "${PROMPTS[@]}"; do
echo "正在生成:$prompt"
belt app run falai/flux-dev --input "{
\"prompt\": \"$prompt, professional photography, 4K\"
}" > "output_${prompt// /_}.json"
sleep 2 # 限速
done
模式 2:顺序管道
串联多个 AI 操作。
#!/bin/bash
# content_pipeline.sh - 完整的内容创作管道
TOPIC="AI in healthcare"
# 步骤 1:调研
echo "正在调研..."
RESEARCH=$(belt app run tavily/search-assistant --input "{
\"query\": \"$TOPIC latest developments\"
}")
# 步骤 2:撰写文章
echo "正在写文章..."
ARTICLE=$(belt app run openrouter/claude-sonnet-45 --input "{
\"prompt\": \"Write a 500-word blog post about $TOPIC based on: $RESEARCH\"
}")
# 步骤 3:生成图片
echo "正在生成图片..."
IMAGE=$(belt app run falai/flux-dev --input "{
\"prompt\": \"Blog header image for article about $TOPIC, modern, professional\"
}")
# 步骤 4:生成社交媒体帖子
echo "正在创建社交媒体帖子..."
SOCIAL=$(belt app run openrouter/claude-haiku-45 --input "{
\"prompt\": \"Write a Twitter thread (5 tweets) summarizing: $ARTICLE\"
}")
echo "管道完成!"
模式 3:并行处理
同时运行多个操作。
#!/bin/bash
# parallel_generation.sh - 并行生成多个资源
# 在后台启动所有任务
belt app run falai/flux-dev --input '{"prompt": "Hero image..."}' > hero.json &
PID1=$!
belt app run falai/flux-dev --input '{"prompt": "Feature image 1..."}' > feature1.json &
PID2=$!
belt app run falai/flux-dev --input '{"prompt": "Feature image 2..."}' > feature2.json &
PID3=$!
# 等待所有任务完成
wait $PID1 $PID2 $PID3
echo "所有图片已生成!"
模式 4:条件工作流
根据结果进行分支。
#!/bin/bash
# conditional_workflow.sh - 基于内容分析进行处理
INPUT_TEXT="$1"
# 分析内容
ANALYSIS=$(belt app run openrouter/claude-haiku-45 --input "{
\"prompt\": \"Classify this text as: positive, negative, or neutral. Return only the classification.\n\n$INPUT_TEXT\"
}")
# 根据结果分支
case "$ANALYSIS" in
*positive*)
echo "正在生成庆祝图片..."
belt app run falai/flux-dev --input '{"prompt": "Celebration, success, happy"}'
;;
*negative*)
echo "正在生成支持性消息..."
belt app run openrouter/claude-sonnet-45 --input "{
\"prompt\": \"Write a supportive, encouraging response to: $INPUT_TEXT\"
}"
;;
*)
echo "正在生成中性确认..."
;;
esac
模式 5:带降级的重试
优雅地处理失败。
#!/bin/bash
# retry_workflow.sh - 重试失败的操作
generate_with_retry() {
local prompt="$1"
local max_attempts=3
local attempt=1
while [ $attempt -le $max_attempts ]; do
echo "第 $attempt 次尝试..."
result=$(belt app run falai/flux-dev --input "{\"prompt\": \"$prompt\"}" 2>&1)
if [ $? -eq 0 ]; then
echo "$result"
return 0
fi
echo "失败,正在重试..."
((attempt++))
sleep $((attempt * 2)) # 指数退避
done
# 降级到其他模型
echo "降级到备用模型..."
belt app run google/imagen-3 --input "{\"prompt\": \"$prompt\"}"
}
generate_with_retry "A beautiful sunset over mountains"
定时自动化
Cron 任务设置
# 编辑 crontab
crontab -e
# 每天上午 9 点生成内容
0 9 * * * /path/to/daily_content.sh >> /var/log/ai-automation.log 2>&1
# 每周一上午 8 点生成周报
0 8 * * 1 /path/to/weekly_report.sh >> /var/log/ai-automation.log 2>&1
# 每 6 小时生成社交媒体内容
0 */6 * * * /path/to/social_content.sh >> /var/log/ai-automation.log 2>&1
每日内容脚本
#!/bin/bash
# daily_content.sh - 每天上午 9 点运行
DATE=$(date +%Y-%m-%d)
OUTPUT_DIR="/output/$DATE"
mkdir -p "$OUTPUT_DIR"
# 生成每日名言图片
belt app run falai/flux-dev --input '{
"prompt": "Motivational quote background, minimalist, morning vibes"
}' > "$OUTPUT_DIR/quote_image.json"
# 生成每日提示
belt app run openrouter/claude-haiku-45 --input '{
"prompt": "Give me one actionable productivity tip for today. Be concise."
}' > "$OUTPUT_DIR/daily_tip.json"
# 发布到社交媒体(可选)
# belt app run twitter/post-tweet --input "{...}"
echo "每日内容已生成:$DATE"
监控与日志
日志包装器
#!/bin/bash
# logged_workflow.sh - 带全面日志记录
LOG_FILE="/var/log/ai-workflow-$(date +%Y%m%d).log"
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}
log "开始工作流"
# 跟踪执行时间
START_TIME=$(date +%s)
# 运行工作流
log "正在生成图片..."
RESULT=$(belt app run falai/flux-dev --input '{"prompt": "test"}' 2>&1)
STATUS=$?
if [ $STATUS -eq 0 ]; then
log "成功:图片已生成"
else
log "错误:$RESULT"
fi
END_TIME=$(date +%s)
DURATION=$((END_TIME - START_TIME))
log "在 ${DURATION} 秒内完成"
错误告警
#!/bin/bash
# monitored_workflow.sh - 带错误告警
run_with_alert() {
local result
result=$("$@" 2>&1)
local status=$?
if [ $status -ne 0 ]; then
# 发送告警(webhook、邮件等)
curl -X POST "https://your-webhook.com/alert" \
-H "Content-Type: application/json" \
-d "{\"error\": \"$result\", \"command\": \"$*\"}"
fi
echo "$result"
return $status
}
run_with_alert belt app run falai/flux-dev --input '{"prompt": "test"}'
Python SDK 自动化
#!/usr/bin/env python3
# automation.py - 基于 Python 的工作流
import subprocess
import json
from datetime import datetime
from pathlib import Path
def run_infsh(app_id: str, input_data: dict) -> dict:
"""运行 inference.sh 应用并返回结果。"""
result = subprocess.run(
["belt", "app", "run", app_id, "--input", json.dumps(input_data)],
capture_output=True,
text=True
)
return json.loads(result.stdout) if result.returncode == 0 else None
def daily_content_pipeline():
"""生成每日内容。"""
date_str = datetime.now().strftime("%Y-%m-%d")
output_dir = Path(f"output/{date_str}")
output_dir.mkdir(parents=True, exist_ok=True)
# 生成图片
image = run_infsh("falai/flux-dev", {
"prompt": f"Daily inspiration for {date_str}, beautiful, uplifting"
})
(output_dir / "image.json").write_text(json.dumps(image))
# 生成标题
caption = run_infsh("openrouter/claude-haiku-45", {
"prompt": "Write an inspiring caption for a daily motivation post. 2-3 sentences."
})
(output_dir / "caption.json").write_text(json.dumps(caption))
print(f"已生成 {date_str} 的内容")
if __name__ == "__main__":
daily_content_pipeline()
工作流模板
内容日历自动化
#!/bin/bash
# content_calendar.sh - 生成一周的内容
TOPICS=("productivity" "wellness" "technology" "creativity" "leadership")
DAYS=("Monday" "Tuesday" "Wednesday" "Thursday" "Friday")
for i in "${!DAYS[@]}"; do
DAY=${DAYS[$i]}
TOPIC=${TOPICS[$i]}
echo "正在生成 $DAY 关于 $TOPIC 的内容..."
# 图片
belt app run falai/flux-dev --input "{
\"prompt\": \"$TOPIC theme, $DAY motivation, social media style\"
}" > "content/${DAY}_image.json"
# 标题
belt app run openrouter/claude-haiku-45 --input "{
\"prompt\": \"Write a $DAY motivation post about $TOPIC. Include hashtags.\"
}" > "content/${DAY}_caption.json"
done
数据处理管道
#!/bin/bash
# data_processing.sh - 处理和分析数据文件
INPUT_DIR="./data/raw"
OUTPUT_DIR="./data/processed"
for file in "$INPUT_DIR"/*.txt; do
filename=$(basename "$file" .txt)
# 分析内容
belt app run openrouter/claude-haiku-45 --input "{
\"prompt\": \"Analyze this data and provide key insights in JSON format: $(cat $file)\"
}" > "$OUTPUT_DIR/${filename}_analysis.json"
done
最佳实践
- 限速 - 在 API 调用之间添加延迟
- 错误处理 - 始终检查返回码
- 日志记录 - 跟踪所有操作
- 幂等性 - 设计为可安全重跑
- 监控 - 失败时发送告警
- 备份 - 保存中间结果
- 超时 - 设置合理的限制
相关技能
# 内容管道
npx skills add inference-sh/skills@ai-content-pipeline
# RAG 管道
npx skills add inference-sh/skills@ai-rag-pipeline
# 社交媒体自动化
npx skills add inference-sh/skills@ai-social-media-content
# 完整平台技能
npx skills add inference-sh/skills@infsh-cli
浏览所有应用:belt app list