Reja1 Claude Opus 4.8 commited on
Commit
40ca96c
·
1 Parent(s): b635d72

feat: add --provider-only flag to pin OpenRouter routing

Browse files

Restricts OpenRouter routing to a specific provider (e.g. 'moonshotai'
for the official Kimi host) with fallbacks disabled, so a request fails
and is retried rather than silently routing to a third-party reseller
that may serve a different quantization.

Threaded through config["provider_only"] to both the initial call and
the re-prompt call. Adds two TDD tests for the request-body wiring.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

README.md CHANGED
@@ -135,6 +135,12 @@ Override the sampling temperature from the config:
135
  uv run python src/benchmark_runner.py --model "openai/gpt-5.5" --temperature 0.7
136
  ```
137
 
 
 
 
 
 
 
138
  Resume an interrupted run (skips already-completed questions):
139
 
140
  ```bash
 
135
  uv run python src/benchmark_runner.py --model "openai/gpt-5.5" --temperature 0.7
136
  ```
137
 
138
+ Pin OpenRouter routing to a specific provider — e.g. the model's official host instead of a third-party reseller. Fallbacks are disabled, so a request fails (and is retried) rather than silently routing elsewhere. Useful when resellers serve a different quantization than the official endpoint:
139
+
140
+ ```bash
141
+ uv run python src/benchmark_runner.py --model "moonshotai/kimi-k2.6" --exam_name JEE_ADVANCED --exam_year 2026 --provider-only moonshotai
142
+ ```
143
+
144
  Resume an interrupted run (skips already-completed questions):
145
 
146
  ```bash
src/benchmark_runner.py CHANGED
@@ -391,6 +391,7 @@ def process_question(
391
  request_timeout=config.get("request_timeout", 60),
392
  temperature=config.get("temperature", 0),
393
  reasoning_effort=config.get("reasoning_effort", "high"),
 
394
  )
395
 
396
  api_success = raw_response is not None # False if API returned empty content
@@ -410,6 +411,7 @@ def process_question(
410
  request_timeout=config.get("request_timeout", 60),
411
  temperature=config.get("temperature", 0),
412
  reasoning_effort=config.get("reasoning_effort", "high"),
 
413
  )
414
  if isinstance(parsed_answer_rp, list):
415
  parsed_answer_rp = [str(item) for item in parsed_answer_rp]
@@ -1228,6 +1230,14 @@ if __name__ == "__main__":
1228
  default=1,
1229
  help="Number of independent runs per model (default: 1). Use 3+ for publication-grade variance analysis."
1230
  )
 
 
 
 
 
 
 
 
1231
  args = parser.parse_args()
1232
 
1233
  # Dynamically update config path if user provides a different one
@@ -1263,6 +1273,10 @@ if __name__ == "__main__":
1263
  if args.temperature is not None:
1264
  config["temperature"] = args.temperature
1265
 
 
 
 
 
1266
  if args.model not in config.get("openrouter_models", []):
1267
  logging.error(f"The model '{args.model}' is not listed in '{args.config}'. Please check the model name or the config file.")
1268
  exit(1)
 
391
  request_timeout=config.get("request_timeout", 60),
392
  temperature=config.get("temperature", 0),
393
  reasoning_effort=config.get("reasoning_effort", "high"),
394
+ provider_only=config.get("provider_only"),
395
  )
396
 
397
  api_success = raw_response is not None # False if API returned empty content
 
411
  request_timeout=config.get("request_timeout", 60),
412
  temperature=config.get("temperature", 0),
413
  reasoning_effort=config.get("reasoning_effort", "high"),
414
+ provider_only=config.get("provider_only"),
415
  )
416
  if isinstance(parsed_answer_rp, list):
417
  parsed_answer_rp = [str(item) for item in parsed_answer_rp]
 
1230
  default=1,
1231
  help="Number of independent runs per model (default: 1). Use 3+ for publication-grade variance analysis."
1232
  )
1233
+ parser.add_argument(
1234
+ "--provider-only",
1235
+ type=str,
1236
+ default=None,
1237
+ help="Optional: restrict OpenRouter routing to a specific provider (e.g. 'moonshotai' "
1238
+ "for the official Kimi host). Comma-separated for multiple. Disables fallbacks, so "
1239
+ "the call fails rather than routing to a reseller."
1240
+ )
1241
  args = parser.parse_args()
1242
 
1243
  # Dynamically update config path if user provides a different one
 
1273
  if args.temperature is not None:
1274
  config["temperature"] = args.temperature
1275
 
1276
+ # Apply CLI provider-only override (restrict OpenRouter routing)
1277
+ if args.provider_only:
1278
+ config["provider_only"] = [p.strip() for p in args.provider_only.split(",") if p.strip()]
1279
+
1280
  if args.model not in config.get("openrouter_models", []):
1281
  logging.error(f"The model '{args.model}' is not listed in '{args.config}'. Please check the model name or the config file.")
1282
  exit(1)
src/llm_interface.py CHANGED
@@ -104,6 +104,7 @@ def get_openrouter_prediction(
104
  request_timeout: int = 60,
105
  temperature: float = 0.0,
106
  reasoning_effort: str = "high",
 
107
  ) -> tuple[list[str] | str | None, str | None, dict | None]:
108
  """
109
  Gets a prediction from an OpenRouter model. Handles initial image prompts and text-only re-prompts.
@@ -176,6 +177,12 @@ def get_openrouter_prediction(
176
  # - z-ai GLM-V: reasoning toggle
177
  data["reasoning"] = {"effort": reasoning_effort}
178
 
 
 
 
 
 
 
179
  request_start_time = time.time()
180
  response = requests.post(
181
  OPENROUTER_API_ENDPOINT,
 
104
  request_timeout: int = 60,
105
  temperature: float = 0.0,
106
  reasoning_effort: str = "high",
107
+ provider_only: str | list[str] | None = None,
108
  ) -> tuple[list[str] | str | None, str | None, dict | None]:
109
  """
110
  Gets a prediction from an OpenRouter model. Handles initial image prompts and text-only re-prompts.
 
177
  # - z-ai GLM-V: reasoning toggle
178
  data["reasoning"] = {"effort": reasoning_effort}
179
 
180
+ if provider_only:
181
+ # Restrict OpenRouter routing to specific provider(s) (e.g. the model's
182
+ # official host) and fail rather than silently fall back to a reseller.
183
+ only = [provider_only] if isinstance(provider_only, str) else list(provider_only)
184
+ data["provider"] = {"only": only, "allow_fallbacks": False}
185
+
186
  request_start_time = time.time()
187
  response = requests.post(
188
  OPENROUTER_API_ENDPOINT,
tests/test_llm_interface.py CHANGED
@@ -90,3 +90,58 @@ def test_non_claude_model_no_thinking_param():
90
 
91
  assert "thinking" not in captured["json"]
92
  assert captured["json"]["temperature"] == 0.0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
 
91
  assert "thinking" not in captured["json"]
92
  assert captured["json"]["temperature"] == 0.0
93
+
94
+
95
+ # --- provider_only: restrict OpenRouter routing to a specific provider ---
96
+
97
+ def test_provider_only_sets_provider_routing():
98
+ """When provider_only is given, the request restricts routing to that provider
99
+ with fallbacks disabled."""
100
+ captured = {}
101
+ def fake_post(url, headers, json, timeout):
102
+ captured["json"] = json
103
+ raise RuntimeError("stop after capture")
104
+
105
+ with patch("llm_interface.requests.post", side_effect=fake_post):
106
+ try:
107
+ from llm_interface import get_openrouter_prediction
108
+ get_openrouter_prediction(
109
+ model_identifier="moonshotai/kimi-k2.6",
110
+ api_key="test",
111
+ image=_make_test_image(),
112
+ exam_name="JEE_ADVANCED", exam_year="2026",
113
+ question_type="MCQ_SINGLE_CORRECT",
114
+ max_tokens=10000,
115
+ provider_only="moonshotai",
116
+ )
117
+ except RuntimeError:
118
+ pass
119
+
120
+ assert captured["json"]["provider"] == {
121
+ "only": ["moonshotai"],
122
+ "allow_fallbacks": False,
123
+ }
124
+
125
+
126
+ def test_no_provider_only_omits_provider_routing():
127
+ """Without provider_only, no provider routing is sent (OpenRouter default)."""
128
+ captured = {}
129
+ def fake_post(url, headers, json, timeout):
130
+ captured["json"] = json
131
+ raise RuntimeError("stop after capture")
132
+
133
+ with patch("llm_interface.requests.post", side_effect=fake_post):
134
+ try:
135
+ from llm_interface import get_openrouter_prediction
136
+ get_openrouter_prediction(
137
+ model_identifier="moonshotai/kimi-k2.6",
138
+ api_key="test",
139
+ image=_make_test_image(),
140
+ exam_name="JEE_ADVANCED", exam_year="2026",
141
+ question_type="MCQ_SINGLE_CORRECT",
142
+ max_tokens=10000,
143
+ )
144
+ except RuntimeError:
145
+ pass
146
+
147
+ assert "provider" not in captured["json"]