fix: conditional Ollama timeout — 5s when vLLM primary, 240s otherwise

#3
Files changed (1) hide show
  1. app/llm.py +19 -7
app/llm.py CHANGED
@@ -92,6 +92,18 @@ def _build_router() -> Router:
92
  fallbacks: list[dict[str, list[str]]] = []
93
  use_vllm = _PRIMARY == "vllm" and bool(_VLLM_BASE)
94
 
 
 
 
 
 
 
 
 
 
 
 
 
95
  for alias, (vllm_name, ollama_tag) in _LOGICAL.items():
96
  if use_vllm:
97
  model_list.append({
@@ -100,8 +112,8 @@ def _build_router() -> Router:
100
  "model": f"openai/{vllm_name}",
101
  "api_base": _VLLM_BASE,
102
  "api_key": _VLLM_KEY,
103
- "timeout": 240,
104
- "stream_timeout": 240,
105
  },
106
  })
107
  if _FALLBACK == "ollama":
@@ -111,8 +123,8 @@ def _build_router() -> Router:
111
  "litellm_params": {
112
  "model": f"ollama_chat/{ollama_tag}",
113
  "api_base": _OLLAMA_BASE,
114
- "timeout": 240,
115
- "stream_timeout": 240,
116
  },
117
  })
118
  fallbacks.append({alias: [fb_alias]})
@@ -122,8 +134,8 @@ def _build_router() -> Router:
122
  "litellm_params": {
123
  "model": f"ollama_chat/{ollama_tag}",
124
  "api_base": _OLLAMA_BASE,
125
- "timeout": 240,
126
- "stream_timeout": 240,
127
  },
128
  })
129
 
@@ -135,7 +147,7 @@ def _build_router() -> Router:
135
  fallbacks=fallbacks,
136
  num_retries=0, # Router fallback handles the failover; no point
137
  # burning seconds re-hitting a dead endpoint.
138
- timeout=240,
139
  )
140
 
141
 
 
92
  fallbacks: list[dict[str, list[str]]] = []
93
  use_vllm = _PRIMARY == "vllm" and bool(_VLLM_BASE)
94
 
95
+ # vLLM on RunPod can take 250+ seconds to cold-start (container boot +
96
+ # model load into GPU VRAM). The first-token timeout must exceed that.
97
+ # stream_timeout (per-chunk) stays tight since subsequent tokens are fast.
98
+ _vllm_first_token_timeout = int(
99
+ os.environ.get("RIPRAP_LITELLM_TIMEOUT_S", "360"))
100
+ # When vLLM is primary: Ollama fallback should fail fast so the mellea
101
+ # probe loop (not Ollama's timeout) controls retry timing.
102
+ # When Ollama is primary: use the full configured timeout (default 240s).
103
+ _ollama_timeout = int(os.environ.get(
104
+ "RIPRAP_OLLAMA_TIMEOUT_S",
105
+ "5" if use_vllm else "240"))
106
+
107
  for alias, (vllm_name, ollama_tag) in _LOGICAL.items():
108
  if use_vllm:
109
  model_list.append({
 
112
  "model": f"openai/{vllm_name}",
113
  "api_base": _VLLM_BASE,
114
  "api_key": _VLLM_KEY,
115
+ "timeout": _vllm_first_token_timeout,
116
+ "stream_timeout": 60,
117
  },
118
  })
119
  if _FALLBACK == "ollama":
 
123
  "litellm_params": {
124
  "model": f"ollama_chat/{ollama_tag}",
125
  "api_base": _OLLAMA_BASE,
126
+ "timeout": _ollama_timeout,
127
+ "stream_timeout": _ollama_timeout,
128
  },
129
  })
130
  fallbacks.append({alias: [fb_alias]})
 
134
  "litellm_params": {
135
  "model": f"ollama_chat/{ollama_tag}",
136
  "api_base": _OLLAMA_BASE,
137
+ "timeout": _ollama_timeout,
138
+ "stream_timeout": _ollama_timeout,
139
  },
140
  })
141
 
 
147
  fallbacks=fallbacks,
148
  num_retries=0, # Router fallback handles the failover; no point
149
  # burning seconds re-hitting a dead endpoint.
150
+ timeout=_vllm_first_token_timeout if use_vllm else _ollama_timeout,
151
  )
152
 
153