synth-ai 0.2.17__py3-none-any.whl → 0.2.19__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of synth-ai might be problematic. Click here for more details.
- examples/baseline/banking77_baseline.py +204 -0
- examples/baseline/crafter_baseline.py +407 -0
- examples/baseline/pokemon_red_baseline.py +326 -0
- examples/baseline/simple_baseline.py +56 -0
- examples/baseline/warming_up_to_rl_baseline.py +239 -0
- examples/blog_posts/gepa/README.md +355 -0
- examples/blog_posts/gepa/configs/banking77_gepa_local.toml +95 -0
- examples/blog_posts/gepa/configs/banking77_gepa_test.toml +82 -0
- examples/blog_posts/gepa/configs/banking77_mipro_local.toml +52 -0
- examples/blog_posts/gepa/configs/hotpotqa_gepa_local.toml +59 -0
- examples/blog_posts/gepa/configs/hotpotqa_gepa_qwen.toml +36 -0
- examples/blog_posts/gepa/configs/hotpotqa_mipro_local.toml +53 -0
- examples/blog_posts/gepa/configs/hover_gepa_local.toml +59 -0
- examples/blog_posts/gepa/configs/hover_gepa_qwen.toml +36 -0
- examples/blog_posts/gepa/configs/hover_mipro_local.toml +53 -0
- examples/blog_posts/gepa/configs/ifbench_gepa_local.toml +59 -0
- examples/blog_posts/gepa/configs/ifbench_gepa_qwen.toml +36 -0
- examples/blog_posts/gepa/configs/ifbench_mipro_local.toml +53 -0
- examples/blog_posts/gepa/configs/pupa_gepa_local.toml +60 -0
- examples/blog_posts/gepa/configs/pupa_mipro_local.toml +54 -0
- examples/blog_posts/gepa/deploy_banking77_task_app.sh +41 -0
- examples/blog_posts/gepa/gepa_baseline.py +204 -0
- examples/blog_posts/gepa/query_prompts_example.py +97 -0
- examples/blog_posts/gepa/run_gepa_banking77.sh +87 -0
- examples/blog_posts/gepa/task_apps.py +105 -0
- examples/blog_posts/gepa/test_gepa_local.sh +67 -0
- examples/blog_posts/gepa/verify_banking77_setup.sh +123 -0
- examples/blog_posts/pokemon_vl/configs/eval_gpt5nano.toml +26 -0
- examples/blog_posts/pokemon_vl/configs/eval_qwen3_vl.toml +12 -10
- examples/blog_posts/pokemon_vl/configs/train_rl_from_sft.toml +1 -0
- examples/blog_posts/pokemon_vl/extract_images.py +239 -0
- examples/blog_posts/pokemon_vl/pokemon_vl_baseline.py +326 -0
- examples/blog_posts/pokemon_vl/run_eval_extract_images.py +209 -0
- examples/blog_posts/pokemon_vl/run_qwen_eval_extract_images.py +212 -0
- examples/blog_posts/pokemon_vl/text_box_analysis.md +106 -0
- examples/blog_posts/warming_up_to_rl/ARCHITECTURE.md +195 -0
- examples/blog_posts/warming_up_to_rl/FINAL_TEST_RESULTS.md +127 -0
- examples/blog_posts/warming_up_to_rl/INFERENCE_SUCCESS.md +132 -0
- examples/blog_posts/warming_up_to_rl/SMOKE_TESTING.md +164 -0
- examples/blog_posts/warming_up_to_rl/SMOKE_TEST_COMPLETE.md +253 -0
- examples/blog_posts/warming_up_to_rl/configs/eval_baseline_qwen32b_10x20.toml +25 -0
- examples/blog_posts/warming_up_to_rl/configs/eval_ft_qwen4b_10x20.toml +26 -0
- examples/blog_posts/warming_up_to_rl/configs/filter_high_reward_dataset.toml +1 -1
- examples/blog_posts/warming_up_to_rl/configs/smoke_test.toml +75 -0
- examples/blog_posts/warming_up_to_rl/configs/train_rl_from_sft.toml +60 -10
- examples/blog_posts/warming_up_to_rl/configs/train_sft_qwen4b.toml +1 -1
- examples/blog_posts/warming_up_to_rl/warming_up_to_rl_baseline.py +187 -0
- examples/multi_step/configs/VERILOG_REWARDS.md +4 -0
- examples/multi_step/configs/VERILOG_RL_CHECKLIST.md +4 -0
- examples/multi_step/configs/crafter_rl_outcome.toml +1 -0
- examples/multi_step/configs/crafter_rl_stepwise_shaped.toml +1 -0
- examples/multi_step/configs/crafter_rl_stepwise_simple.toml +1 -0
- examples/rl/configs/rl_from_base_qwen17.toml +1 -0
- examples/swe/task_app/hosted/inference/openai_client.py +0 -34
- examples/swe/task_app/hosted/policy_routes.py +17 -0
- examples/swe/task_app/hosted/rollout.py +4 -2
- examples/task_apps/banking77/__init__.py +6 -0
- examples/task_apps/banking77/banking77_task_app.py +841 -0
- examples/task_apps/banking77/deploy_wrapper.py +46 -0
- examples/task_apps/crafter/CREATE_SFT_DATASET.md +4 -0
- examples/task_apps/crafter/FILTER_COMMAND_STATUS.md +4 -0
- examples/task_apps/crafter/FILTER_COMMAND_SUCCESS.md +4 -0
- examples/task_apps/crafter/task_app/grpo_crafter.py +24 -2
- examples/task_apps/crafter/task_app/synth_envs_hosted/hosted_app.py +49 -0
- examples/task_apps/crafter/task_app/synth_envs_hosted/inference/openai_client.py +355 -58
- examples/task_apps/crafter/task_app/synth_envs_hosted/policy_routes.py +68 -7
- examples/task_apps/crafter/task_app/synth_envs_hosted/rollout.py +78 -21
- examples/task_apps/crafter/task_app/synth_envs_hosted/utils.py +194 -1
- examples/task_apps/gepa_benchmarks/__init__.py +7 -0
- examples/task_apps/gepa_benchmarks/common.py +260 -0
- examples/task_apps/gepa_benchmarks/hotpotqa_task_app.py +507 -0
- examples/task_apps/gepa_benchmarks/hover_task_app.py +436 -0
- examples/task_apps/gepa_benchmarks/ifbench_task_app.py +563 -0
- examples/task_apps/gepa_benchmarks/pupa_task_app.py +460 -0
- examples/task_apps/pokemon_red/README_IMAGE_ONLY_EVAL.md +4 -0
- examples/task_apps/pokemon_red/task_app.py +254 -36
- examples/warming_up_to_rl/configs/rl_from_base_qwen4b.toml +1 -0
- examples/warming_up_to_rl/task_app/grpo_crafter.py +53 -4
- examples/warming_up_to_rl/task_app/synth_envs_hosted/hosted_app.py +49 -0
- examples/warming_up_to_rl/task_app/synth_envs_hosted/inference/openai_client.py +152 -41
- examples/warming_up_to_rl/task_app/synth_envs_hosted/policy_routes.py +31 -1
- examples/warming_up_to_rl/task_app/synth_envs_hosted/rollout.py +33 -3
- examples/warming_up_to_rl/task_app/synth_envs_hosted/utils.py +67 -0
- examples/workflows/math_rl/configs/rl_from_base_qwen17.toml +1 -0
- synth_ai/api/train/builders.py +90 -1
- synth_ai/api/train/cli.py +396 -21
- synth_ai/api/train/config_finder.py +13 -2
- synth_ai/api/train/configs/__init__.py +15 -1
- synth_ai/api/train/configs/prompt_learning.py +442 -0
- synth_ai/api/train/configs/rl.py +29 -0
- synth_ai/api/train/task_app.py +1 -1
- synth_ai/api/train/validators.py +277 -0
- synth_ai/baseline/__init__.py +25 -0
- synth_ai/baseline/config.py +209 -0
- synth_ai/baseline/discovery.py +214 -0
- synth_ai/baseline/execution.py +146 -0
- synth_ai/cli/__init__.py +85 -17
- synth_ai/cli/__main__.py +0 -0
- synth_ai/cli/claude.py +70 -0
- synth_ai/cli/codex.py +84 -0
- synth_ai/cli/commands/__init__.py +1 -0
- synth_ai/cli/commands/baseline/__init__.py +12 -0
- synth_ai/cli/commands/baseline/core.py +637 -0
- synth_ai/cli/commands/baseline/list.py +93 -0
- synth_ai/cli/commands/eval/core.py +13 -10
- synth_ai/cli/commands/filter/core.py +53 -17
- synth_ai/cli/commands/help/core.py +0 -1
- synth_ai/cli/commands/smoke/__init__.py +7 -0
- synth_ai/cli/commands/smoke/core.py +1436 -0
- synth_ai/cli/commands/status/subcommands/pricing.py +22 -0
- synth_ai/cli/commands/status/subcommands/usage.py +203 -0
- synth_ai/cli/commands/train/judge_schemas.py +1 -0
- synth_ai/cli/commands/train/judge_validation.py +1 -0
- synth_ai/cli/commands/train/validation.py +0 -57
- synth_ai/cli/demo.py +35 -3
- synth_ai/cli/deploy/__init__.py +40 -25
- synth_ai/cli/deploy.py +162 -0
- synth_ai/cli/legacy_root_backup.py +14 -8
- synth_ai/cli/opencode.py +107 -0
- synth_ai/cli/root.py +9 -5
- synth_ai/cli/task_app_deploy.py +1 -1
- synth_ai/cli/task_apps.py +53 -53
- synth_ai/environments/examples/crafter_classic/engine_deterministic_patch.py +7 -4
- synth_ai/environments/examples/crafter_classic/engine_serialization_patch_v3.py +9 -5
- synth_ai/environments/examples/crafter_classic/world_config_patch_simple.py +4 -3
- synth_ai/judge_schemas.py +1 -0
- synth_ai/learning/__init__.py +10 -0
- synth_ai/learning/prompt_learning_client.py +276 -0
- synth_ai/learning/prompt_learning_types.py +184 -0
- synth_ai/pricing/__init__.py +2 -0
- synth_ai/pricing/model_pricing.py +57 -0
- synth_ai/streaming/handlers.py +53 -4
- synth_ai/streaming/streamer.py +19 -0
- synth_ai/task/apps/__init__.py +1 -0
- synth_ai/task/config.py +2 -0
- synth_ai/task/tracing_utils.py +25 -25
- synth_ai/task/validators.py +44 -8
- synth_ai/task_app_cfgs.py +21 -0
- synth_ai/tracing_v3/config.py +162 -19
- synth_ai/tracing_v3/constants.py +1 -1
- synth_ai/tracing_v3/db_config.py +24 -38
- synth_ai/tracing_v3/storage/config.py +47 -13
- synth_ai/tracing_v3/storage/factory.py +3 -3
- synth_ai/tracing_v3/turso/daemon.py +113 -11
- synth_ai/tracing_v3/turso/native_manager.py +92 -16
- synth_ai/types.py +8 -0
- synth_ai/urls.py +11 -0
- synth_ai/utils/__init__.py +30 -1
- synth_ai/utils/agents.py +74 -0
- synth_ai/utils/bin.py +39 -0
- synth_ai/utils/cli.py +149 -5
- synth_ai/utils/env.py +17 -17
- synth_ai/utils/json.py +72 -0
- synth_ai/utils/modal.py +283 -1
- synth_ai/utils/paths.py +48 -0
- synth_ai/utils/uvicorn.py +113 -0
- {synth_ai-0.2.17.dist-info → synth_ai-0.2.19.dist-info}/METADATA +102 -4
- {synth_ai-0.2.17.dist-info → synth_ai-0.2.19.dist-info}/RECORD +162 -88
- synth_ai/cli/commands/deploy/__init__.py +0 -23
- synth_ai/cli/commands/deploy/core.py +0 -614
- synth_ai/cli/commands/deploy/errors.py +0 -72
- synth_ai/cli/commands/deploy/validation.py +0 -11
- synth_ai/cli/deploy/core.py +0 -5
- synth_ai/cli/deploy/errors.py +0 -23
- synth_ai/cli/deploy/validation.py +0 -5
- {synth_ai-0.2.17.dist-info → synth_ai-0.2.19.dist-info}/WHEEL +0 -0
- {synth_ai-0.2.17.dist-info → synth_ai-0.2.19.dist-info}/entry_points.txt +0 -0
- {synth_ai-0.2.17.dist-info → synth_ai-0.2.19.dist-info}/licenses/LICENSE +0 -0
- {synth_ai-0.2.17.dist-info → synth_ai-0.2.19.dist-info}/top_level.txt +0 -0
|
@@ -7,7 +7,9 @@ import logging
|
|
|
7
7
|
import os
|
|
8
8
|
import time
|
|
9
9
|
from typing import Any
|
|
10
|
+
from urllib.parse import urlparse, urlunparse
|
|
10
11
|
|
|
12
|
+
import click
|
|
11
13
|
import httpx
|
|
12
14
|
|
|
13
15
|
logger = logging.getLogger(__name__)
|
|
@@ -148,11 +150,169 @@ class OpenAIClient:
|
|
|
148
150
|
OpenAI-compatible chat completion response
|
|
149
151
|
"""
|
|
150
152
|
base = (base_url or self.base_url).rstrip("/")
|
|
151
|
-
#
|
|
152
|
-
|
|
153
|
+
# Ensure processed_request is defined for error logging paths
|
|
154
|
+
processed_request: dict[str, Any] = dict(request or {})
|
|
155
|
+
|
|
156
|
+
# Bulletproof normalization BEFORE any parsing
|
|
157
|
+
def _local_force_normalize(u: str) -> str:
|
|
158
|
+
if not isinstance(u, str) or not u:
|
|
159
|
+
return u
|
|
160
|
+
p = urlparse(u)
|
|
161
|
+
path = (p.path or "").rstrip("/")
|
|
162
|
+
q = p.query or ""
|
|
163
|
+
# If query contains a path segment, extract and repair
|
|
164
|
+
if q and "/" in q:
|
|
165
|
+
before, after = q.split("/", 1)
|
|
166
|
+
# Split off any extra query parameters that were appended after the path
|
|
167
|
+
cut_positions = [i for i in [after.find("&"), after.find("?")] if i >= 0]
|
|
168
|
+
cut = min(cut_positions) if cut_positions else len(after)
|
|
169
|
+
path_from_query = "/" + after[:cut]
|
|
170
|
+
extra_query = after[cut + 1 :] if cut < len(after) else ""
|
|
171
|
+
merged_query = before
|
|
172
|
+
if extra_query:
|
|
173
|
+
merged_query = f"{merged_query}&{extra_query}" if merged_query else extra_query
|
|
174
|
+
# Ensure final path
|
|
175
|
+
final_path = path_from_query if path_from_query.startswith("/v1/chat/completions") else f"{path_from_query.rstrip('/')}/v1/chat/completions"
|
|
176
|
+
p = p._replace(path=final_path, query=merged_query)
|
|
177
|
+
u = urlunparse(p)
|
|
178
|
+
p = urlparse(u)
|
|
179
|
+
path = p.path or ""
|
|
180
|
+
q = p.query or ""
|
|
181
|
+
if not path.endswith("/v1/chat/completions"):
|
|
182
|
+
new_path = f"{path}/v1/chat/completions" if path else "/v1/chat/completions"
|
|
183
|
+
p = p._replace(path=new_path)
|
|
184
|
+
u = urlunparse(p)
|
|
185
|
+
p = urlparse(u)
|
|
186
|
+
q = p.query or ""
|
|
187
|
+
if q and "/" in q:
|
|
188
|
+
# Last-resort: drop anything after first '/'
|
|
189
|
+
safe_q = q.split("/")[0]
|
|
190
|
+
p = p._replace(query=safe_q)
|
|
191
|
+
u = urlunparse(p)
|
|
192
|
+
return u
|
|
193
|
+
|
|
194
|
+
norm_base = None
|
|
195
|
+
try:
|
|
196
|
+
# Try importing shared normalizer first
|
|
197
|
+
from examples.task_apps.crafter.task_app.synth_envs_hosted.utils import (
|
|
198
|
+
force_normalize_chat_completions_url,
|
|
199
|
+
)
|
|
200
|
+
norm_base = force_normalize_chat_completions_url(base)
|
|
201
|
+
except Exception:
|
|
202
|
+
norm_base = _local_force_normalize(base)
|
|
203
|
+
base = norm_base or base
|
|
204
|
+
# Parse URL to handle query parameters correctly
|
|
205
|
+
parsed = urlparse(base)
|
|
206
|
+
path = parsed.path.rstrip("/")
|
|
207
|
+
query = parsed.query
|
|
208
|
+
|
|
209
|
+
# Debug: Log URL parsing
|
|
210
|
+
logger.error(f"[URL_PARSE] base={base} parsed.path={parsed.path} parsed.query={parsed.query}")
|
|
211
|
+
|
|
212
|
+
# CRITICAL FIX: Handle malformed URLs where path is incorrectly in the query string
|
|
213
|
+
# Example: https://host?cid=trace_123/v1/chat/completions
|
|
214
|
+
# Should be: https://host/v1/chat/completions?cid=trace_123
|
|
215
|
+
|
|
216
|
+
# ALWAYS check for malformed URLs - this is CRITICAL
|
|
217
|
+
# CRASH IMMEDIATELY if URL is malformed - don't let it through!
|
|
218
|
+
if query and "/" in query:
|
|
219
|
+
logger.error(f"[URL_FATAL] MALFORMED URL DETECTED AT START: base={base} query={query}")
|
|
220
|
+
# Try to fix it
|
|
221
|
+
logger.error(f"[URL_FIX_TRIGGERED] Query contains '/': query={query}")
|
|
222
|
+
# This is a malformed URL - extract path from query and fix it
|
|
223
|
+
logger.error(
|
|
224
|
+
f"[URL_FIX] Malformed URL detected: {base}\n"
|
|
225
|
+
f"Query contains path segments. Fixing..."
|
|
226
|
+
)
|
|
227
|
+
|
|
228
|
+
# Find where the path starts in the query string
|
|
229
|
+
# The query format is: "cid=value/path" or similar
|
|
230
|
+
# We need to find the first "/" that starts a path segment
|
|
231
|
+
query_parts = query.split("/", 1)
|
|
232
|
+
if len(query_parts) == 2:
|
|
233
|
+
# query_parts[0] is the actual query (e.g., "cid=trace_123")
|
|
234
|
+
# query_parts[1] is the path that was incorrectly put in query
|
|
235
|
+
actual_query = query_parts[0]
|
|
236
|
+
path_and_more = query_parts[1] # Could be "v1/chat/completions" or "v1/chat/completions&foo=bar"
|
|
237
|
+
|
|
238
|
+
# Extract the path part (everything before "&" or "?" if present)
|
|
239
|
+
# Handle both "&" (query param separator) and "?" (another malformed query separator)
|
|
240
|
+
if "&" in path_and_more:
|
|
241
|
+
# Path is followed by more query params (separated by &)
|
|
242
|
+
path_segment, extra_query = path_and_more.split("&", 1)
|
|
243
|
+
path_in_query = "/" + path_segment # Restore leading slash
|
|
244
|
+
# Merge extra query params with actual_query
|
|
245
|
+
actual_query = f"{actual_query}&{extra_query}"
|
|
246
|
+
elif "?" in path_and_more:
|
|
247
|
+
# Path is followed by more query params (separated by ?, which is malformed)
|
|
248
|
+
path_segment, extra_query = path_and_more.split("?", 1)
|
|
249
|
+
path_in_query = "/" + path_segment # Restore leading slash
|
|
250
|
+
# Merge extra query params with actual_query (use & as separator)
|
|
251
|
+
actual_query = f"{actual_query}&{extra_query}"
|
|
252
|
+
else:
|
|
253
|
+
# No extra query params, just the path
|
|
254
|
+
path_in_query = "/" + path_and_more # Restore leading slash
|
|
255
|
+
|
|
256
|
+
# If the path_in_query already contains /v1/chat/completions, use it
|
|
257
|
+
# Otherwise, append /v1/chat/completions
|
|
258
|
+
if path_in_query.startswith("/v1/chat/completions"):
|
|
259
|
+
final_path = path_in_query
|
|
260
|
+
else:
|
|
261
|
+
# Append /v1/chat/completions to whatever path we found
|
|
262
|
+
final_path = path_in_query.rstrip("/") + "/v1/chat/completions"
|
|
263
|
+
|
|
264
|
+
# Reconstruct URL correctly: path comes before query
|
|
265
|
+
parsed = parsed._replace(path=final_path, query=actual_query)
|
|
266
|
+
url = urlunparse(parsed)
|
|
267
|
+
logger.warning(f"[URL_FIX] Fixed malformed URL:\n FROM: {base}\n TO: {url}")
|
|
268
|
+
else:
|
|
269
|
+
# Can't parse, fall through to normal processing
|
|
270
|
+
logger.error(f"[URL_FIX] Could not parse malformed query: {query}")
|
|
271
|
+
path = parsed.path.rstrip("/")
|
|
272
|
+
if not path.endswith("/v1/chat/completions"):
|
|
273
|
+
new_path = f"{path}/v1/chat/completions" if path else "/v1/chat/completions"
|
|
274
|
+
parsed = parsed._replace(path=new_path)
|
|
275
|
+
url = urlunparse(parsed)
|
|
276
|
+
else:
|
|
277
|
+
url = base
|
|
278
|
+
# Normal case: query params are separate from path
|
|
279
|
+
elif path.endswith("/v1/chat/completions"):
|
|
153
280
|
url = base
|
|
154
281
|
else:
|
|
155
|
-
|
|
282
|
+
# Append /v1/chat/completions to the path, preserving query params
|
|
283
|
+
new_path = f"{path}/v1/chat/completions" if path else "/v1/chat/completions"
|
|
284
|
+
parsed = parsed._replace(path=new_path)
|
|
285
|
+
url = urlunparse(parsed)
|
|
286
|
+
logger.debug(f"[URL_CONSTRUCT] Added path to URL: {base} -> {url}")
|
|
287
|
+
|
|
288
|
+
# FINAL VALIDATION: Ensure the constructed URL is correct
|
|
289
|
+
final_parsed = urlparse(url)
|
|
290
|
+
final_path = final_parsed.path or ""
|
|
291
|
+
final_query = final_parsed.query or ""
|
|
292
|
+
|
|
293
|
+
# Verify path is correct
|
|
294
|
+
if not final_path.endswith("/v1/chat/completions"):
|
|
295
|
+
error_msg = (
|
|
296
|
+
f"FATAL [OpenAIClient]: URL missing /v1/chat/completions path!\n"
|
|
297
|
+
f"Original: {base}\n"
|
|
298
|
+
f"Constructed: {url}\n"
|
|
299
|
+
f"Path: {final_path}\n"
|
|
300
|
+
)
|
|
301
|
+
logger.error(error_msg)
|
|
302
|
+
raise ValueError(error_msg)
|
|
303
|
+
|
|
304
|
+
# Verify query doesn't contain path segments
|
|
305
|
+
if final_query and "/" in final_query:
|
|
306
|
+
error_msg = (
|
|
307
|
+
f"FATAL [OpenAIClient]: Query still contains path segments after fix!\n"
|
|
308
|
+
f"Original: {base}\n"
|
|
309
|
+
f"Constructed: {url}\n"
|
|
310
|
+
f"Query: {final_query}\n"
|
|
311
|
+
f"This indicates a bug in URL construction logic."
|
|
312
|
+
)
|
|
313
|
+
logger.error(error_msg)
|
|
314
|
+
raise ValueError(error_msg)
|
|
315
|
+
|
|
156
316
|
timeout = timeout_s or self.timeout_s
|
|
157
317
|
|
|
158
318
|
# Merge headers
|
|
@@ -233,31 +393,97 @@ class OpenAIClient:
|
|
|
233
393
|
logger.debug(f"🔊 [OPENAI_CLIENT_POST_FIX] Message[1] content value: {msg1_content_post if not isinstance(msg1_content_post, list) else f'list[{len(msg1_content_post)}]'}")
|
|
234
394
|
|
|
235
395
|
# Log request (redact messages in production)
|
|
396
|
+
# CRITICAL: Verify URL is correct BEFORE making HTTP request
|
|
397
|
+
final_parsed_check = urlparse(url)
|
|
398
|
+
logger.error(f"[URL_FINAL_CHECK] Before HTTP request: url={url} path={final_parsed_check.path} query={final_parsed_check.query}")
|
|
399
|
+
|
|
400
|
+
# CRASH IF URL IS STILL MALFORMED - DO NOT PROCEED
|
|
401
|
+
if final_parsed_check.query and "/" in final_parsed_check.query:
|
|
402
|
+
error_msg = (
|
|
403
|
+
f"FATAL [OpenAIClient]: URL IS STILL MALFORMED AFTER FIX ATTEMPT!\n"
|
|
404
|
+
f"Original base_url: {base_url or self.base_url}\n"
|
|
405
|
+
f"Constructed URL: {url}\n"
|
|
406
|
+
f"Path: {final_parsed_check.path}\n"
|
|
407
|
+
f"Query (contains path): {final_parsed_check.query}\n"
|
|
408
|
+
f"This will cause a 404 error. CRASHING NOW to prevent bad request."
|
|
409
|
+
)
|
|
410
|
+
logger.error(error_msg)
|
|
411
|
+
raise ValueError(error_msg)
|
|
412
|
+
|
|
413
|
+
# Verify path is correct
|
|
414
|
+
if not final_parsed_check.path.endswith("/v1/chat/completions"):
|
|
415
|
+
error_msg = (
|
|
416
|
+
f"FATAL [OpenAIClient]: URL missing /v1/chat/completions path!\n"
|
|
417
|
+
f"URL: {url}\n"
|
|
418
|
+
f"Path: {final_parsed_check.path}\n"
|
|
419
|
+
)
|
|
420
|
+
logger.error(error_msg)
|
|
421
|
+
raise ValueError(error_msg)
|
|
422
|
+
|
|
423
|
+
# Log request with detailed prompts/tools preview and sampling settings (Authorization is not logged)
|
|
236
424
|
logger.info(f"Inference POST target: {url}")
|
|
237
425
|
if extra_headers:
|
|
238
426
|
logger.info(f"Extra headers: {extra_headers}")
|
|
239
427
|
with contextlib.suppress(Exception):
|
|
240
428
|
keys_preview = sorted(processed_request.keys())
|
|
241
429
|
logger.info(f"Request keys: {keys_preview}")
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
430
|
+
|
|
431
|
+
# Detailed IO log: messages/tools/sampling and final payload fields
|
|
432
|
+
try:
|
|
433
|
+
import json as _json
|
|
434
|
+
|
|
435
|
+
def _truncate(text: str, limit: int = 2000) -> str:
|
|
436
|
+
return text if len(text) <= limit else text[:limit] + "…"
|
|
437
|
+
|
|
438
|
+
def _messages_preview(msgs: Any) -> str:
|
|
439
|
+
try:
|
|
440
|
+
out: list[dict[str, Any]] = []
|
|
441
|
+
if isinstance(msgs, list):
|
|
442
|
+
for m in msgs:
|
|
443
|
+
if not isinstance(m, dict):
|
|
444
|
+
continue
|
|
445
|
+
role = m.get("role")
|
|
446
|
+
content = m.get("content")
|
|
447
|
+
if isinstance(content, str):
|
|
448
|
+
text = content
|
|
449
|
+
elif isinstance(content, list):
|
|
450
|
+
parts: list[str] = []
|
|
451
|
+
for seg in content:
|
|
452
|
+
if isinstance(seg, dict) and isinstance(seg.get("text"), str):
|
|
453
|
+
parts.append(seg["text"])
|
|
454
|
+
text = "\n".join(parts)
|
|
257
455
|
else:
|
|
258
|
-
|
|
259
|
-
|
|
456
|
+
text = ""
|
|
457
|
+
out.append({"role": role, "content": _truncate(str(text), 4000)})
|
|
458
|
+
return _json.dumps(out)
|
|
459
|
+
except Exception:
|
|
460
|
+
return "[]"
|
|
260
461
|
|
|
462
|
+
def _tools_preview(tools: Any) -> str:
|
|
463
|
+
try:
|
|
464
|
+
return _truncate(_json.dumps(tools), 4000)
|
|
465
|
+
except Exception:
|
|
466
|
+
return "[]"
|
|
467
|
+
|
|
468
|
+
msgs = processed_request.get("messages") if isinstance(processed_request, dict) else None
|
|
469
|
+
tools = processed_request.get("tools") if isinstance(processed_request, dict) else None
|
|
470
|
+
io_log: dict[str, Any] = {
|
|
471
|
+
"llm.call": True,
|
|
472
|
+
"model": processed_request.get("model") if isinstance(processed_request, dict) else None,
|
|
473
|
+
"tool_choice": processed_request.get("tool_choice") if isinstance(processed_request, dict) else None,
|
|
474
|
+
"parallel_tool_calls": processed_request.get("parallel_tool_calls") if isinstance(processed_request, dict) else None,
|
|
475
|
+
"stop_after_tool_calls": processed_request.get("stop_after_tool_calls") if isinstance(processed_request, dict) else None,
|
|
476
|
+
"temperature": processed_request.get("temperature") if isinstance(processed_request, dict) else None,
|
|
477
|
+
"top_p": processed_request.get("top_p") if isinstance(processed_request, dict) else None,
|
|
478
|
+
"max_tokens": processed_request.get("max_tokens") if isinstance(processed_request, dict) else None,
|
|
479
|
+
"max_completion_tokens": processed_request.get("max_completion_tokens") if isinstance(processed_request, dict) else None,
|
|
480
|
+
"messages_preview": _messages_preview(msgs),
|
|
481
|
+
"tools_preview": _tools_preview(tools),
|
|
482
|
+
}
|
|
483
|
+
logger.info(io_log)
|
|
484
|
+
except Exception:
|
|
485
|
+
pass
|
|
486
|
+
|
|
261
487
|
# Final hard-guard for OpenAI/Groq: drop unsupported field
|
|
262
488
|
try:
|
|
263
489
|
low_url = url.lower()
|
|
@@ -329,10 +555,70 @@ class OpenAIClient:
|
|
|
329
555
|
logger.info(
|
|
330
556
|
f"Inference response status=200, content-type={content_type}, bytes={len(body_text)}"
|
|
331
557
|
)
|
|
332
|
-
|
|
558
|
+
if body_text:
|
|
559
|
+
# Log raw output with generous preview to debug no-tool-call issues
|
|
560
|
+
preview_len = min(4000, len(body_text))
|
|
561
|
+
logger.info({
|
|
562
|
+
"llm.raw_response": True,
|
|
563
|
+
"bytes": len(body_text),
|
|
564
|
+
"preview": body_text[:preview_len],
|
|
565
|
+
})
|
|
333
566
|
|
|
334
567
|
result = response.json()
|
|
335
568
|
logger.info(f"Inference response parsed_type={type(result).__name__}")
|
|
569
|
+
|
|
570
|
+
tool_call_count = -1
|
|
571
|
+
# Normalize tool calls so downstream always sees a function tool call
|
|
572
|
+
try:
|
|
573
|
+
if isinstance(result, dict):
|
|
574
|
+
choices = result.get("choices")
|
|
575
|
+
if isinstance(choices, list) and choices:
|
|
576
|
+
msg = choices[0].get("message")
|
|
577
|
+
if isinstance(msg, dict):
|
|
578
|
+
# Prefer tool_calls; if missing but function_call is present, synthesize tool_calls
|
|
579
|
+
tc = msg.get("tool_calls")
|
|
580
|
+
fc = msg.get("function_call")
|
|
581
|
+
if (not isinstance(tc, list) or not tc) and isinstance(fc, dict):
|
|
582
|
+
name = fc.get("name") or "interact_many"
|
|
583
|
+
args = fc.get("arguments") or "{}"
|
|
584
|
+
msg["tool_calls"] = [
|
|
585
|
+
{
|
|
586
|
+
"id": "call_norm",
|
|
587
|
+
"type": "function",
|
|
588
|
+
"function": {"name": name, "arguments": args},
|
|
589
|
+
}
|
|
590
|
+
]
|
|
591
|
+
if isinstance(choices[0], dict):
|
|
592
|
+
choices[0]["finish_reason"] = "tool_calls"
|
|
593
|
+
# Log tool call count for debugging
|
|
594
|
+
try:
|
|
595
|
+
tc2 = msg.get("tool_calls")
|
|
596
|
+
count = len(tc2) if isinstance(tc2, list) else 0
|
|
597
|
+
logger.info({
|
|
598
|
+
"llm.tool_calls": True,
|
|
599
|
+
"count": count,
|
|
600
|
+
"finish_reason": choices[0].get("finish_reason") if isinstance(choices[0], dict) else None,
|
|
601
|
+
})
|
|
602
|
+
if count == 0:
|
|
603
|
+
click.echo(
|
|
604
|
+
"[openai-client] ✗ upstream response missing tool_calls; dumping preview to logs",
|
|
605
|
+
err=True,
|
|
606
|
+
)
|
|
607
|
+
logger.error(
|
|
608
|
+
"Inference response missing tool_calls; failing fast. Raw body preview: %s",
|
|
609
|
+
body_text[:500] if body_text else "<empty>",
|
|
610
|
+
)
|
|
611
|
+
raise ValueError("Inference response missing tool_calls")
|
|
612
|
+
tool_call_count = count
|
|
613
|
+
except Exception:
|
|
614
|
+
pass
|
|
615
|
+
except Exception:
|
|
616
|
+
pass
|
|
617
|
+
|
|
618
|
+
click.echo(
|
|
619
|
+
f"[openai-client] ✓ response ok with tool_calls={tool_call_count}",
|
|
620
|
+
err=True,
|
|
621
|
+
)
|
|
336
622
|
return result
|
|
337
623
|
|
|
338
624
|
except httpx.TimeoutException:
|
|
@@ -341,11 +627,31 @@ class OpenAIClient:
|
|
|
341
627
|
except httpx.HTTPStatusError as e:
|
|
342
628
|
status = e.response.status_code if e.response is not None else None
|
|
343
629
|
text = e.response.text if e.response is not None else str(e)
|
|
344
|
-
# Log
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
630
|
+
# Log full body and request diagnostics for debugging remote failures
|
|
631
|
+
try:
|
|
632
|
+
redacted_headers = dict(headers)
|
|
633
|
+
if "Authorization" in redacted_headers:
|
|
634
|
+
redacted_headers["Authorization"] = "***REDACTED***"
|
|
635
|
+
logger.error(
|
|
636
|
+
{
|
|
637
|
+
"openai_http_error": True,
|
|
638
|
+
"status": status,
|
|
639
|
+
"url": url,
|
|
640
|
+
"body": text,
|
|
641
|
+
}
|
|
642
|
+
)
|
|
643
|
+
logger.error(
|
|
644
|
+
{
|
|
645
|
+
"request_debug": True,
|
|
646
|
+
"status": status,
|
|
647
|
+
"target": url,
|
|
648
|
+
"headers": redacted_headers,
|
|
649
|
+
"payload": processed_request,
|
|
650
|
+
}
|
|
651
|
+
)
|
|
652
|
+
except Exception:
|
|
653
|
+
logger.error(f"HTTP error from {url}: {status} - {text}")
|
|
654
|
+
# Special case: token budget exceeded handled below, else 422 degrade, else re-raise
|
|
349
655
|
try:
|
|
350
656
|
if status == 400 and e.response is not None:
|
|
351
657
|
data = e.response.json()
|
|
@@ -398,6 +704,8 @@ class OpenAIClient:
|
|
|
398
704
|
logger.warning(
|
|
399
705
|
{
|
|
400
706
|
"token_budget_recovery": True,
|
|
707
|
+
"messages_tokens": messages_tokens,
|
|
708
|
+
"model_limit": model_limit,
|
|
401
709
|
"retry_max_tokens": new_max,
|
|
402
710
|
}
|
|
403
711
|
)
|
|
@@ -412,35 +720,6 @@ class OpenAIClient:
|
|
|
412
720
|
pass
|
|
413
721
|
except Exception:
|
|
414
722
|
pass
|
|
415
|
-
# Gracefully degrade on 422 so rollouts can still produce a trajectory
|
|
416
|
-
if status == 422:
|
|
417
|
-
try:
|
|
418
|
-
# Best-effort parse of error for diagnostics
|
|
419
|
-
err = None
|
|
420
|
-
try:
|
|
421
|
-
err = e.response.json()
|
|
422
|
-
except Exception:
|
|
423
|
-
err = {"error": "unprocessable"}
|
|
424
|
-
logger.warning({"inference_422_recovered": True})
|
|
425
|
-
except Exception:
|
|
426
|
-
pass
|
|
427
|
-
# Return a minimal OpenAI-compatible response with no tool_calls/content
|
|
428
|
-
import time as _t
|
|
429
|
-
|
|
430
|
-
return {
|
|
431
|
-
"id": f"cmpl-{int(_t.time())}",
|
|
432
|
-
"object": "chat.completion",
|
|
433
|
-
"created": int(_t.time()),
|
|
434
|
-
"model": processed_request.get("model") or "unknown",
|
|
435
|
-
"choices": [
|
|
436
|
-
{
|
|
437
|
-
"index": 0,
|
|
438
|
-
"message": {"role": "assistant", "content": "", "tool_calls": []},
|
|
439
|
-
"finish_reason": "stop",
|
|
440
|
-
}
|
|
441
|
-
],
|
|
442
|
-
"usage": {"prompt_tokens": 0, "completion_tokens": 0, "total_tokens": 0},
|
|
443
|
-
}
|
|
444
723
|
raise
|
|
445
724
|
except Exception as e:
|
|
446
725
|
logger.error(f"Unexpected error calling {url}: {e}")
|
|
@@ -506,14 +785,29 @@ class OpenAIClient:
|
|
|
506
785
|
OpenAI-compatible chat completion response
|
|
507
786
|
"""
|
|
508
787
|
last_error = None
|
|
788
|
+
processed_request: dict[str, Any] = dict(request or {})
|
|
509
789
|
wait_time = 1.0
|
|
510
790
|
|
|
511
791
|
for attempt in range(max_retries + 1):
|
|
512
792
|
try:
|
|
513
793
|
# Apply parameter fixes to the request
|
|
794
|
+
# CRITICAL: Use proper URL parsing, not string concatenation!
|
|
795
|
+
target_base = base_url or self.base_url
|
|
796
|
+
if target_base:
|
|
797
|
+
parsed_target = urlparse(target_base)
|
|
798
|
+
target_path = parsed_target.path.rstrip("/")
|
|
799
|
+
if not target_path.endswith("/v1/chat/completions"):
|
|
800
|
+
new_target_path = f"{target_path}/v1/chat/completions" if target_path else "/v1/chat/completions"
|
|
801
|
+
parsed_target = parsed_target._replace(path=new_target_path)
|
|
802
|
+
target_url = urlunparse(parsed_target)
|
|
803
|
+
else:
|
|
804
|
+
target_url = target_base
|
|
805
|
+
else:
|
|
806
|
+
target_url = None
|
|
807
|
+
|
|
514
808
|
processed_request = self._fix_model_parameters(
|
|
515
809
|
request,
|
|
516
|
-
target_url=
|
|
810
|
+
target_url=target_url,
|
|
517
811
|
)
|
|
518
812
|
return await self.generate(
|
|
519
813
|
request=processed_request,
|
|
@@ -619,7 +913,9 @@ class OpenAIClient:
|
|
|
619
913
|
await asyncio.sleep(wait_time)
|
|
620
914
|
wait_time *= backoff_factor
|
|
621
915
|
|
|
622
|
-
|
|
916
|
+
if last_error is not None:
|
|
917
|
+
raise last_error
|
|
918
|
+
raise RuntimeError("RL inference retries exhausted with no captured exception")
|
|
623
919
|
|
|
624
920
|
|
|
625
921
|
def create_inference_client(
|
|
@@ -694,7 +990,8 @@ def create_inference_client(
|
|
|
694
990
|
) -> dict[str, Any]:
|
|
695
991
|
return {"status": "ok", "dummy": True}
|
|
696
992
|
|
|
697
|
-
|
|
993
|
+
import typing as _t
|
|
994
|
+
return _t.cast(OpenAIClient, _DummyClient())
|
|
698
995
|
|
|
699
996
|
return OpenAIClient(
|
|
700
997
|
base_url=task_app.vllm_base_url,
|
|
@@ -4,6 +4,7 @@ import contextlib
|
|
|
4
4
|
import logging
|
|
5
5
|
import os
|
|
6
6
|
from datetime import datetime
|
|
7
|
+
import asyncio
|
|
7
8
|
from typing import Any
|
|
8
9
|
|
|
9
10
|
from fastapi import APIRouter, HTTPException, Request
|
|
@@ -35,6 +36,13 @@ logger = logging.getLogger(__name__)
|
|
|
35
36
|
|
|
36
37
|
router = APIRouter()
|
|
37
38
|
|
|
39
|
+
# Global concurrency limit for outbound inference to avoid backend overload/timeouts
|
|
40
|
+
try:
|
|
41
|
+
_INFERENCE_CONCURRENCY = int(os.getenv("INFERENCE_CONCURRENCY", "2") or "2")
|
|
42
|
+
except Exception: # pragma: no cover
|
|
43
|
+
_INFERENCE_CONCURRENCY = 2
|
|
44
|
+
_inference_sem = asyncio.Semaphore(max(1, _INFERENCE_CONCURRENCY))
|
|
45
|
+
|
|
38
46
|
|
|
39
47
|
class PolicyCreateRequest(BaseModel):
|
|
40
48
|
policy_name: str
|
|
@@ -250,6 +258,11 @@ async def step_policy(
|
|
|
250
258
|
task_app = req.app.state.task_app
|
|
251
259
|
policy = handle.policy
|
|
252
260
|
tracing_context = getattr(req.state, "rollout_tracing", None)
|
|
261
|
+
if tracing_context is None:
|
|
262
|
+
print(
|
|
263
|
+
f"[TRACE_DEBUG] Missing tracing context on policy step; policy_id={request.policy_id}",
|
|
264
|
+
flush=True,
|
|
265
|
+
)
|
|
253
266
|
|
|
254
267
|
obs_text = request.observation
|
|
255
268
|
if isinstance(request.observation, dict):
|
|
@@ -546,6 +559,14 @@ async def step_policy(
|
|
|
546
559
|
|
|
547
560
|
# Ensure meta carries the final target URL for downstream logging/clients
|
|
548
561
|
with contextlib.suppress(Exception):
|
|
562
|
+
# Bulletproof normalizer at the call site (in addition to client-side)
|
|
563
|
+
try:
|
|
564
|
+
from examples.task_apps.crafter.task_app.synth_envs_hosted.utils import (
|
|
565
|
+
force_normalize_chat_completions_url,
|
|
566
|
+
)
|
|
567
|
+
target_url = force_normalize_chat_completions_url(target_url)
|
|
568
|
+
except Exception:
|
|
569
|
+
pass
|
|
549
570
|
sanitized_target = ensure_chat_completions_url(target_url)
|
|
550
571
|
if sanitized_target and sanitized_target != target_url:
|
|
551
572
|
logger.warning(
|
|
@@ -594,6 +615,28 @@ async def step_policy(
|
|
|
594
615
|
except Exception:
|
|
595
616
|
api_key_override = None
|
|
596
617
|
|
|
618
|
+
# Fallback: If target is OpenAI but OPENAI_API_KEY is missing, route to Synth API
|
|
619
|
+
try:
|
|
620
|
+
import os as _os2
|
|
621
|
+
_low = str(target_url or "").lower()
|
|
622
|
+
if ("api.openai.com" in _low) and not (_os2.getenv("OPENAI_API_KEY")):
|
|
623
|
+
# Prefer task_app.synth_base_url if available; else default
|
|
624
|
+
synth_base = getattr(task_app, "synth_base_url", None)
|
|
625
|
+
if isinstance(synth_base, str) and synth_base.strip():
|
|
626
|
+
base = synth_base.rstrip("/")
|
|
627
|
+
fallback = base + "/inference/v1/chat/completions"
|
|
628
|
+
else:
|
|
629
|
+
fallback = "https://api.synth.run/api/inference/v1/chat/completions"
|
|
630
|
+
fixed = ensure_chat_completions_url(fallback)
|
|
631
|
+
logger.warning(
|
|
632
|
+
"POLICY_STEP: OPENAI key missing; falling back to Synth route %s",
|
|
633
|
+
fixed,
|
|
634
|
+
)
|
|
635
|
+
meta["inference_url"] = fixed
|
|
636
|
+
target_url = fixed
|
|
637
|
+
except Exception:
|
|
638
|
+
pass
|
|
639
|
+
|
|
597
640
|
if api_key_override:
|
|
598
641
|
try:
|
|
599
642
|
masked = f"{api_key_override[:6]}…{api_key_override[-4:]}"
|
|
@@ -975,13 +1018,14 @@ async def step_policy(
|
|
|
975
1018
|
|
|
976
1019
|
_t_start = _t.time()
|
|
977
1020
|
call_started_at = datetime.utcnow()
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
1021
|
+
async with _inference_sem:
|
|
1022
|
+
inference_response = await client.generate_with_retries(
|
|
1023
|
+
request=meta["inference_request"],
|
|
1024
|
+
base_url=meta["inference_url"],
|
|
1025
|
+
max_retries=12,
|
|
1026
|
+
backoff_factor=2.0,
|
|
1027
|
+
extra_headers=extra_headers,
|
|
1028
|
+
)
|
|
985
1029
|
meta["inference_ms"] = int((_t.time() - _t_start) * 1000)
|
|
986
1030
|
call_completed_at = datetime.utcnow()
|
|
987
1031
|
|
|
@@ -1061,6 +1105,23 @@ async def step_policy(
|
|
|
1061
1105
|
except Exception as exc:
|
|
1062
1106
|
logger.debug(f"TRACING_LLM_FAIL: {exc}")
|
|
1063
1107
|
|
|
1108
|
+
if not tool_calls:
|
|
1109
|
+
preview = ""
|
|
1110
|
+
try:
|
|
1111
|
+
preview = str(meta.get("raw_response") or "")[:400]
|
|
1112
|
+
except Exception:
|
|
1113
|
+
preview = "<unavailable>"
|
|
1114
|
+
logger.error(
|
|
1115
|
+
{
|
|
1116
|
+
"rollout.policy_step": True,
|
|
1117
|
+
"policy_id": request.policy_id,
|
|
1118
|
+
"error": "no_tool_calls",
|
|
1119
|
+
"inference_url": meta.get("inference_url"),
|
|
1120
|
+
"raw_preview": preview,
|
|
1121
|
+
}
|
|
1122
|
+
)
|
|
1123
|
+
raise RuntimeError("Policy step produced no tool calls; inference response unusable.")
|
|
1124
|
+
|
|
1064
1125
|
return PolicyStepResponse(
|
|
1065
1126
|
tool_calls=tool_calls,
|
|
1066
1127
|
meta=meta,
|