commit b65517c6ad799420b6c02f11f247379e9c535b3c Author: hz4th_coder Date: Fri May 29 11:15:46 2026 +0800 初始化零散开发集合,添加 llama-inference 脚本 diff --git a/README.md b/README.md new file mode 100644 index 0000000..1ec9156 --- /dev/null +++ b/README.md @@ -0,0 +1,24 @@ +# 零散开发集合 + +存放非独立完整项目的小工具、脚本、代码片段。 + +## 目录结构 + +``` +snippets/ +├── llama-inference/ # llama.cpp 服务启动 + 推理任务 +│ ├── llama_inference.sh +│ └── README.md +├── .../ +└── README.md # 本文件 +``` + +## 已有内容 + +| 目录 | 说明 | +|------|------| +| llama-inference | llama.cpp 服务启动后自动执行推理任务 | + +--- + +*黄庄4号程序员 维护* \ No newline at end of file diff --git a/llama-inference/README.md b/llama-inference/README.md new file mode 100644 index 0000000..b2f313e --- /dev/null +++ b/llama-inference/README.md @@ -0,0 +1,46 @@ +# llama.cpp 服务启动 + 推理任务 + +自动化脚本:启动 llama.cpp 模型服务,等待就绪后执行推理任务。 + +## 文件 + +| 文件 | 说明 | +|------|------| +| `llama_inference.sh` | 主脚本 | + +## 用法 + +```bash +# 1. 编辑脚本,修改配置区域 +vim llama_inference.sh + +# 2. 运行 +./llama_inference.sh +``` + +## 配置项 + +脚本顶部配置区域: + +```bash +LLAMA_CMD="./llama-server -m /path/to/model.gguf --port 8080" # 启动命令 +LLAMA_PORT="8080" # 服务端口 +HEALTH_URL="http://localhost:${LLAMA_PORT}/health" # 健康检查地址 +MAX_WAIT=300 # 最长等待秒数 + +# 推理任务 +TASKS=( + 'curl -s http://localhost:8080/completion -d "{\"prompt\": \"你好\", \"n_predict\": 50}"' +) +``` + +## 逻辑 + +1. 后台启动 llama.cpp 服务 +2. 轮询健康检查接口等待服务就绪 +3. 依次执行 TASKS 数组里的推理任务 +4. 完成后可选择关闭服务 + +--- + +*黄庄4号程序员* \ No newline at end of file diff --git a/llama-inference/llama_inference.sh b/llama-inference/llama_inference.sh new file mode 100755 index 0000000..158ab26 --- /dev/null +++ b/llama-inference/llama_inference.sh @@ -0,0 +1,55 @@ +#!/bin/bash +# llama.cpp 服务启动 + 推理任务脚本 +# 用法: ./llama_inference.sh + +set -e + +# ==================== 配置区域 ==================== +LLAMA_CMD="./llama-server -m /path/to/model.gguf --port 8080" # 启动命令 +LLAMA_PORT="8080" # 服务端口 +HEALTH_URL="http://localhost:${LLAMA_PORT}/health" # 健康检查地址 +MAX_WAIT=300 # 最长等待秒数 + +# 推理任务(按顺序执行) +TASKS=( + 'curl -s http://localhost:8080/completion -d "{\"prompt\": \"你好\", \"n_predict\": 50}"' + 'curl -s http://localhost:8080/completion -d "{\"prompt\": \"测试\", \"n_predict\": 50}"' +) + +# ==================== 启动服务 ==================== +echo "[$(date '+%H:%M:%S')] 启动 llama.cpp 服务..." +$LLAMA_CMD & +LLAMA_PID=$! +echo "[$(date '+%H:%M:%S')] 服务 PID: $LLAMA_PID" + +# ==================== 等待服务就绪 ==================== +echo "[$(date '+%H:%M:%S')] 等待模型加载..." +WAITED=0 +while ! curl -s "$HEALTH_URL" > /dev/null 2>&1; do + if [ $WAITED -ge $MAX_WAIT ]; then + echo "[$(date '+%H:%M:%S')] ❌ 等待超时,服务未就绪" + kill $LLAMA_PID 2>/dev/null || true + exit 1 + fi + sleep 2 + WAITED=$((WAITED + 2)) + echo "[$(date '+%H:%M:%S')] 已等待 ${WAITED}s..." +done +echo "[$(date '+%H:%M:%S')] ✅ 服务已就绪" + +# ==================== 执行推理任务 ==================== +echo "[$(date '+%H:%M:%S')] 开始执行推理任务..." +TASK_NUM=1 +for TASK in "${TASKS[@]}"; do + echo "[$(date '+%H:%M:%S')] --- 任务 $TASK_NUM ---" + eval "$TASK" + echo "" + TASK_NUM=$((TASK_NUM + 1)) +done + +# ==================== 清理(可选)==================== +# 如果任务执行完要关闭服务,取消下面注释 +# echo "[$(date '+%H:%M:%S')] 关闭服务..." +# kill $LLAMA_PID + +echo "[$(date '+%H:%M:%S')] ✅ 全部完成" \ No newline at end of file