Skip to content

Instantly share code, notes, and snippets.

@bombless
Last active March 10, 2026 02:21
Show Gist options
  • Select an option

  • Save bombless/6df3f5951b276fe9fda55074376615a2 to your computer and use it in GitHub Desktop.

Select an option

Save bombless/6df3f5951b276fe9fda55074376615a2 to your computer and use it in GitHub Desktop.
测试openclaw配置的模型
#!/usr/bin/env bash
# model-cli.sh - OpenClaw模型命令行工具
# 用法:
# ./model-cli.sh list # 列出所有可用接口
# ./model-cli.sh set <alias> # 设置当前使用的接口
# ./model-cli.sh ask "问题" # 向当前模型提问
set -euo pipefail
CONFIG_FILE="/home/openclaw/.openclaw/openclaw.json"
STATE_FILE="$HOME/.config/model-config.ini"
# 确保状态文件目录存在
mkdir -p "$(dirname "$STATE_FILE")"
# 读取当前配置的模型
get_current_model() {
if [[ -f "$STATE_FILE" ]]; then
grep -E "^current_model=" "$STATE_FILE" | cut -d= -f2
else
echo ""
fi
}
# 设置当前模型
set_current_model() {
local model="$1"
echo "current_model=$model" > "$STATE_FILE"
echo "✅ 已设置当前模型: $model"
}
# 列出所有可用接口
list_models() {
if [[ ! -f "$CONFIG_FILE" ]]; then
echo "❌ 配置文件不存在: $CONFIG_FILE"
exit 1
fi
local current=$(get_current_model)
echo "📋 可用接口列表:"
echo ""
# 遍历所有provider下的models
jq -r '.models.providers | to_entries[] | .key as $provider | .value.models[]? | "\($provider)/\(.id) # \(.name)"' "$CONFIG_FILE" | while read -r line; do
local model_id=$(echo "$line" | awk '{print $1}')
if [[ "$model_id" == "$current" ]]; then
echo " $line ⭐ (当前)"
else
echo " $line"
fi
done
echo ""
if [[ -n "$current" ]]; then
echo "当前模型: $current"
else
echo "当前模型: 未设置"
fi
}
# 向模型提问
ask_model() {
local question="$1"
local current=$(get_current_model)
if [[ -z "$current" ]]; then
echo "❌ 未设置当前模型,请先运行: $0 set <provider/model>"
exit 1
fi
if [[ ! -f "$CONFIG_FILE" ]]; then
echo "❌ 配置文件不存在: $CONFIG_FILE"
exit 1
fi
# 解析provider和model (格式: provider/model-id)
local provider=$(echo "$current" | cut -d/ -f1)
local model_id=$(echo "$current" | cut -d/ -f2-)
# 获取API配置
local base_url=$(jq -r ".models.providers[\"$provider\"].baseUrl // empty" "$CONFIG_FILE")
local api_key=$(jq -r ".models.providers[\"$provider\"].apiKey // empty" "$CONFIG_FILE")
if [[ -z "$base_url" || -z "$api_key" ]]; then
echo "❌ 无法获取 $provider 的API配置"
exit 1
fi
echo "🤖 使用模型: $current"
echo "💬 问题: $question"
echo ""
echo "📝 响应:"
echo "---"
# 调用API
curl -s -X POST "${base_url}/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $api_key" \
-d "{
\"model\": \"$model_id\",
\"messages\": [{\"role\": \"user\", \"content\": \"$question\"}],
\"stream\": false
}" | jq -r '.choices[0].message.content // "❌ 无法获取响应"'
echo ""
echo "---"
}
# 主逻辑
case "${1:-}" in
list)
list_models
;;
set)
if [[ -z "${2:-}" ]]; then
echo "❌ 用法: $0 set <alias>"
exit 1
fi
set_current_model "$2"
;;
ask)
if [[ -z "${2:-}" ]]; then
echo "❌ 用法: $0 ask \"问题\""
exit 1
fi
ask_model "$2"
;;
*)
echo "用法:"
echo " $0 list # 列出所有可用接口"
echo " $0 set <provider/model> # 设置当前使用的接口"
echo " $0 ask \"问题\" # 向当前模型提问"
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment