renminwansui1976 commited on
Commit
beaa858
·
unverified ·
1 Parent(s): f301510

更新 start.sh

Browse files
Files changed (1) hide show
  1. start.sh +22 -19
start.sh CHANGED
@@ -1,9 +1,17 @@
1
  #!/usr/bin/env bash
2
  set -euo pipefail
3
 
4
- # ── 1. Validate required LiteLLM variables ────────────────────────────────────
5
- : "${LITELLM_API_KEY:?Secret LITELLM_API_KEY is required}"
6
- : "${LITELLM_MODEL:?Secret LITELLM_MODEL is required (e.g. openai/gpt-4o)}"
 
 
 
 
 
 
 
 
7
 
8
  # ── 2. Write LiteLLM proxy config ─────────────────────────────────────────────
9
  LITELLM_CONFIG=/tmp/litellm_config.yaml
@@ -24,40 +32,35 @@ LITELLM_CONFIG=/tmp/litellm_config.yaml
24
  echo " request_timeout: 120"
25
  } > "$LITELLM_CONFIG"
26
 
27
- echo "[start.sh] LiteLLM config:"
28
- cat "$LITELLM_CONFIG"
29
 
30
  # ── 3. Start LiteLLM proxy in the background ──────────────────────────────────
31
  litellm --config "$LITELLM_CONFIG" --port 4000 --host 127.0.0.1 &
32
  LITELLM_PID=$!
33
  echo "[start.sh] LiteLLM started (pid=$LITELLM_PID)"
34
 
35
- # Ensure LiteLLM is killed when this script exits for any reason.
36
- # Note: exec below replaces this shell, so the trap only fires if we exit
37
- # before exec (e.g. LiteLLM health-check failure).
38
- trap 'echo "[start.sh] stopping LiteLLM"; kill "$LITELLM_PID" 2>/dev/null || true' EXIT
 
 
 
39
 
40
  # ── 4. Wait for LiteLLM to be healthy (max 60 s) ─────────────────────────────
41
  MAX_WAIT=60
42
  WAITED=0
43
  until curl -sf http://127.0.0.1:4000/health/liveliness > /dev/null 2>&1; do
44
  if [ "$WAITED" -ge "$MAX_WAIT" ]; then
45
- echo "[start.sh] ERROR: LiteLLM not healthy after ${MAX_WAIT}s — aborting"
46
- exit 1
47
  fi
48
  sleep 1
49
  WAITED=$((WAITED + 1))
50
  done
51
  echo "[start.sh] LiteLLM healthy after ${WAITED}s"
52
 
53
- # ── 5. Start OpenClaw gateway ─────────────────────────────────────────────────
54
- # Pass the port as a CLI flag (--port 7860) so we don't depend on OpenClaw
55
- # reading any particular env var name for its port — env var names are not
56
- # documented and differ across versions.
57
- #
58
- # OPENAI_API_KEY / OPENAI_BASE_URL: standard OpenAI-SDK env vars that OpenClaw
59
- # uses to reach its AI backend — pointing them at the LiteLLM proxy.
60
- # OPENCLAW_DEFAULT_MODEL=default: matches model_name in litellm_config.yaml.
61
  exec env \
62
  OPENCLAW_DEFAULT_MODEL=default \
63
  OPENAI_API_KEY=litellm-proxy \
 
1
  #!/usr/bin/env bash
2
  set -euo pipefail
3
 
4
+ LITELLM_PID=""
5
+
6
+ # ── 1. Check if LiteLLM should be enabled ─────────────────────────────────────
7
+ # Both LITELLM_API_KEY and LITELLM_MODEL must be set to enable the proxy.
8
+ # If either is missing, OpenClaw starts without a pre-configured AI backend
9
+ # and the user can configure providers through OpenClaw's own UI.
10
+ if [ -z "${LITELLM_API_KEY:-}" ] || [ -z "${LITELLM_MODEL:-}" ]; then
11
+ echo "[start.sh] LITELLM_API_KEY or LITELLM_MODEL not set — starting OpenClaw without LiteLLM proxy"
12
+ echo "[start.sh] You can configure AI providers through OpenClaw's UI after startup"
13
+ exec openclaw gateway --port 7860 --allow-unconfigured
14
+ fi
15
 
16
  # ── 2. Write LiteLLM proxy config ─────────────────────────────────────────────
17
  LITELLM_CONFIG=/tmp/litellm_config.yaml
 
32
  echo " request_timeout: 120"
33
  } > "$LITELLM_CONFIG"
34
 
35
+ echo "[start.sh] LiteLLM config written for model: ${LITELLM_MODEL}"
 
36
 
37
  # ── 3. Start LiteLLM proxy in the background ──────────────────────────────────
38
  litellm --config "$LITELLM_CONFIG" --port 4000 --host 127.0.0.1 &
39
  LITELLM_PID=$!
40
  echo "[start.sh] LiteLLM started (pid=$LITELLM_PID)"
41
 
42
+ cleanup() {
43
+ if [ -n "$LITELLM_PID" ]; then
44
+ echo "[start.sh] stopping LiteLLM (pid=$LITELLM_PID)"
45
+ kill "$LITELLM_PID" 2>/dev/null || true
46
+ fi
47
+ }
48
+ trap cleanup EXIT
49
 
50
  # ── 4. Wait for LiteLLM to be healthy (max 60 s) ─────────────────────────────
51
  MAX_WAIT=60
52
  WAITED=0
53
  until curl -sf http://127.0.0.1:4000/health/liveliness > /dev/null 2>&1; do
54
  if [ "$WAITED" -ge "$MAX_WAIT" ]; then
55
+ echo "[start.sh] WARNING: LiteLLM not healthy after ${MAX_WAIT}s — starting OpenClaw without proxy"
56
+ exec openclaw gateway --port 7860 --allow-unconfigured
57
  fi
58
  sleep 1
59
  WAITED=$((WAITED + 1))
60
  done
61
  echo "[start.sh] LiteLLM healthy after ${WAITED}s"
62
 
63
+ # ── 5. Start OpenClaw pointing at LiteLLM proxy ───────────────────────────────
 
 
 
 
 
 
 
64
  exec env \
65
  OPENCLAW_DEFAULT_MODEL=default \
66
  OPENAI_API_KEY=litellm-proxy \