192 lines
7.1 KiB
Bash
Executable file
192 lines
7.1 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
# Configures new-api channels and token via the admin API.
|
|
# Run once after first boot: ./new-api/init-channels.sh
|
|
#
|
|
# Requires these env vars (or .env file in project root):
|
|
# NEW_API_USERNAME - admin username (default: root)
|
|
# NEW_API_PASSWORD - admin password
|
|
# DEEPINFRA_API_KEY
|
|
# SILICONFLOW_API_KEY
|
|
# OPENROUTER_API_KEY
|
|
# GROQ_API_KEY
|
|
# CEREBRAS_API_KEY
|
|
|
|
set -euo pipefail
|
|
|
|
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
|
ENV_FILE="${SCRIPT_DIR}/../.env"
|
|
|
|
# Load .env if present
|
|
if [[ -f "$ENV_FILE" ]]; then
|
|
set -a
|
|
# shellcheck disable=SC1090
|
|
source "$ENV_FILE"
|
|
set +a
|
|
fi
|
|
|
|
API_BASE="${NEW_API_BASE:-http://localhost:4000}"
|
|
USERNAME="${NEW_API_USERNAME:-root}"
|
|
PASSWORD="${NEW_API_PASSWORD:?Set NEW_API_PASSWORD to the admin password}"
|
|
COOKIE_JAR=$(mktemp)
|
|
trap 'rm -f "$COOKIE_JAR"' EXIT
|
|
|
|
# ── Login to get session cookie ─────────────────────────
|
|
login() {
|
|
echo "Logging in as ${USERNAME}..."
|
|
local resp
|
|
resp=$(curl -s -c "$COOKIE_JAR" "${API_BASE}/api/user/login" \
|
|
-H "Content-Type: application/json" \
|
|
-d "$(python3 -c "
|
|
import json, sys
|
|
print(json.dumps({'username': sys.argv[1], 'password': sys.argv[2]}))
|
|
" "$USERNAME" "$PASSWORD")")
|
|
|
|
local success
|
|
success=$(echo "$resp" | python3 -c "import sys,json; print(json.load(sys.stdin).get('success', False))")
|
|
if [[ "$success" != "True" ]]; then
|
|
echo "ERROR: Login failed: ${resp}"
|
|
exit 1
|
|
fi
|
|
echo " Logged in."
|
|
}
|
|
|
|
# ── Helper ──────────────────────────────────────────────
|
|
create_channel() {
|
|
local name="$1" type="$2" key="$3" base_url="$4" priority="$5" models="$6" model_mapping="$7"
|
|
|
|
echo "Creating channel: ${name} (priority ${priority})..."
|
|
|
|
local payload
|
|
payload=$(python3 -c "
|
|
import json, sys
|
|
print(json.dumps({
|
|
'type': int(sys.argv[1]),
|
|
'name': sys.argv[2],
|
|
'key': sys.argv[3],
|
|
'base_url': sys.argv[4],
|
|
'models': sys.argv[5],
|
|
'model_mapping': sys.argv[6],
|
|
'priority': int(sys.argv[7]),
|
|
'status': 1,
|
|
'group': 'default',
|
|
'weight': 1,
|
|
'auto_ban': 1
|
|
}))
|
|
" "$type" "$name" "$key" "$base_url" "$models" "$model_mapping" "$priority")
|
|
|
|
local resp success
|
|
resp=$(curl -s "${API_BASE}/api/channel/" \
|
|
-b "$COOKIE_JAR" \
|
|
-H "Content-Type: application/json" \
|
|
-d "$payload")
|
|
|
|
success=$(echo "$resp" | python3 -c "import sys,json; print(json.load(sys.stdin).get('success', False))")
|
|
if [[ "$success" == "True" ]]; then
|
|
echo " OK"
|
|
else
|
|
echo " FAILED: ${resp}" | head -c 500
|
|
echo
|
|
fi
|
|
}
|
|
|
|
# Wait for new-api to be ready
|
|
echo "Waiting for new-api at ${API_BASE}..."
|
|
for i in $(seq 1 30); do
|
|
if curl -sf "${API_BASE}/" > /dev/null 2>&1; then
|
|
echo "new-api is ready."
|
|
break
|
|
fi
|
|
if [[ "$i" == "30" ]]; then
|
|
echo "ERROR: new-api did not become ready in time."
|
|
exit 1
|
|
fi
|
|
sleep 2
|
|
done
|
|
|
|
# Login first
|
|
login
|
|
|
|
# ── Channel: DeepInfra (priority 1) ────────────────────
|
|
create_channel "DeepInfra" 1 \
|
|
"${DEEPINFRA_API_KEY:?}" \
|
|
"https://api.deepinfra.com/v1/openai" \
|
|
1 \
|
|
"deepseek-v3.2,deepseek-r1,gpt-oss,gpt-oss-20b,nemotron-super,nemotron-nano,devstral,glm-4.6,glm-4.7,glm-5,kimi-k2,kimi-k2.5" \
|
|
'{"deepseek-v3.2":"deepseek-ai/DeepSeek-V3.2","deepseek-r1":"deepseek-ai/DeepSeek-R1","gpt-oss":"openai/gpt-oss-120b","gpt-oss-20b":"openai/gpt-oss-20b","nemotron-super":"nvidia/Llama-3.3-Nemotron-Super-49B-v1.5","nemotron-nano":"nvidia/NVIDIA-Nemotron-Nano-9B-v2","devstral":"mistralai/Devstral-Small-2505","glm-4.6":"zai-org/GLM-4.6","glm-4.7":"zai-org/GLM-4.7","glm-5":"zai-org/GLM-5","kimi-k2":"moonshotai/Kimi-K2-Instruct-0905","kimi-k2.5":"moonshotai/Kimi-K2.5"}'
|
|
|
|
# ── Channel: SiliconFlow (priority 2) ──────────────────
|
|
create_channel "SiliconFlow" 1 \
|
|
"${SILICONFLOW_API_KEY:?}" \
|
|
"https://api.siliconflow.com/v1" \
|
|
2 \
|
|
"deepseek-v3.2,glm-4.7,kimi-k2,qwen3-coder,qwen3-coder-30b" \
|
|
'{"deepseek-v3.2":"deepseek-ai/DeepSeek-V3.2","glm-4.7":"THUDM/GLM-4-32B-0414","kimi-k2":"moonshotai/Kimi-K2-Instruct-0905","qwen3-coder":"Qwen/Qwen3-Coder-480B-A35B-Instruct","qwen3-coder-30b":"Qwen/Qwen3-Coder-30B-A3B-Instruct"}'
|
|
|
|
# ── Channel: OpenRouter (priority 3) ───────────────────
|
|
create_channel "OpenRouter" 1 \
|
|
"${OPENROUTER_API_KEY:?}" \
|
|
"https://openrouter.ai/api/v1" \
|
|
3 \
|
|
"deepseek-v3.2,deepseek-v3-free,kimi-k2.5,minimax-m2.5,gpt-4.1-mini,gpt-4.1,gemini-3-flash-preview,gemini-2.5-pro,claude-sonnet,trinity-large-preview" \
|
|
'{"deepseek-v3.2":"deepseek/deepseek-chat-v3-0324","deepseek-v3-free":"deepseek/deepseek-chat-v3-0324:free","kimi-k2.5":"moonshotai/kimi-k2.5","minimax-m2.5":"minimax/minimax-m2.5","gpt-4.1-mini":"openai/gpt-4.1-mini","gpt-4.1":"openai/gpt-4.1","gemini-3-flash-preview":"google/gemini-3-flash-preview","gemini-2.5-pro":"google/gemini-2.5-pro-preview","claude-sonnet":"anthropic/claude-sonnet-4","trinity-large-preview":"arcee-ai/trinity-large-preview"}'
|
|
|
|
# ── Channel: Groq (priority 1) ─────────────────────────
|
|
create_channel "Groq" 1 \
|
|
"${GROQ_API_KEY:?}" \
|
|
"https://api.groq.com/openai/v1" \
|
|
1 \
|
|
"llama-3.3-70b" \
|
|
'{"llama-3.3-70b":"llama-3.3-70b-versatile"}'
|
|
|
|
# ── Channel: Cerebras (priority 1) ─────────────────────
|
|
create_channel "Cerebras" 1 \
|
|
"${CEREBRAS_API_KEY:?}" \
|
|
"https://api.cerebras.ai/v1" \
|
|
1 \
|
|
"llama-3.3-70b-cerebras" \
|
|
'{"llama-3.3-70b-cerebras":"llama-3.3-70b"}'
|
|
|
|
# ── Create API token for Open WebUI ────────────────────
|
|
echo ""
|
|
echo "Creating API token for Open WebUI..."
|
|
TOKEN_RESP=$(curl -s "${API_BASE}/api/token/" \
|
|
-b "$COOKIE_JAR" \
|
|
-H "Content-Type: application/json" \
|
|
-d "$(python3 -c "
|
|
import json
|
|
print(json.dumps({
|
|
'name': 'open-webui',
|
|
'remain_quota': 0,
|
|
'unlimited_quota': True
|
|
}))
|
|
")")
|
|
|
|
TOKEN_KEY=$(echo "$TOKEN_RESP" | python3 -c "
|
|
import sys, json
|
|
data = json.load(sys.stdin)
|
|
if data.get('success'):
|
|
print(data['data']['key'])
|
|
else:
|
|
print('FAILED: ' + data.get('message', 'unknown error'))
|
|
" 2>/dev/null || echo "FAILED: could not parse response")
|
|
|
|
echo " Token: ${TOKEN_KEY}"
|
|
|
|
echo ""
|
|
echo "══════════════════════════════════════"
|
|
echo "Channel setup complete!"
|
|
echo ""
|
|
if [[ "$TOKEN_KEY" != FAILED* ]]; then
|
|
echo "Open WebUI API key: ${TOKEN_KEY}"
|
|
echo "Set OPENWEBUI_API_KEY=${TOKEN_KEY} in your .env"
|
|
fi
|
|
echo ""
|
|
echo "Next steps:"
|
|
echo " 1. Verify channels at ${API_BASE}"
|
|
echo " 2. Test a model:"
|
|
echo " curl ${API_BASE}/v1/chat/completions \\"
|
|
echo " -H 'Authorization: Bearer ${TOKEN_KEY}' \\"
|
|
echo " -H 'Content-Type: application/json' \\"
|
|
echo " -d '{\"model\":\"deepseek-v3.2\",\"messages\":[{\"role\":\"user\",\"content\":\"hi\"}]}'"
|
|
echo " 3. Restart Open WebUI with the new API key"
|
|
echo "══════════════════════════════════════"
|