Haider67 commited on
Commit
9c34f78
·
verified ·
1 Parent(s): 87ba2f1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -9
app.py CHANGED
@@ -74,14 +74,19 @@ def generate_openrouter_response(prompt: str) -> str:
74
  {"role": "system", "content": SYSTEM_PROMPT},
75
  {"role": "user", "content": prompt},
76
  ],
77
- "max_tokens": 2048,
 
 
 
 
 
78
  }
79
 
80
  response = requests.post(
81
  OPENROUTER_API_URL,
82
  headers=get_openrouter_headers(),
83
  json=payload,
84
- timeout=120,
85
  )
86
 
87
  if not response.ok:
@@ -95,12 +100,21 @@ def generate_openrouter_response(prompt: str) -> str:
95
  f"OpenRouter request failed ({response.status_code}): {error_message}"
96
  )
97
 
98
- result = response.json()
 
 
 
 
 
 
99
  choices = result.get("choices") or []
100
  if not choices:
101
- raise RuntimeError(f"OpenRouter returned no choices: {result}")
 
 
102
 
103
- message = choices[0].get("message") or {}
 
104
  answer = message.get("content")
105
 
106
  # Some providers return content as a list of text blocks.
@@ -111,10 +125,27 @@ def generate_openrouter_response(prompt: str) -> str:
111
  if isinstance(block, dict)
112
  )
113
 
114
- if not isinstance(answer, str) or not answer.strip():
115
- raise RuntimeError("OpenRouter returned an empty answer.")
116
-
117
- return answer.strip()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
 
119
 
120
  def generate_answers(prompts: list[str]) -> list[str]:
 
74
  {"role": "system", "content": SYSTEM_PROMPT},
75
  {"role": "user", "content": prompt},
76
  ],
77
+ # Reasoning tokens and the visible answer share this output budget.
78
+ # A 2,048-token limit can be consumed entirely by model reasoning.
79
+ "max_tokens": 8192,
80
+ "reasoning": {
81
+ "effort": "medium",
82
+ },
83
  }
84
 
85
  response = requests.post(
86
  OPENROUTER_API_URL,
87
  headers=get_openrouter_headers(),
88
  json=payload,
89
+ timeout=180,
90
  )
91
 
92
  if not response.ok:
 
100
  f"OpenRouter request failed ({response.status_code}): {error_message}"
101
  )
102
 
103
+ try:
104
+ result = response.json()
105
+ except ValueError as error:
106
+ raise RuntimeError(
107
+ f"OpenRouter returned invalid JSON: {response.text[:1000]}"
108
+ ) from error
109
+
110
  choices = result.get("choices") or []
111
  if not choices:
112
+ raise RuntimeError(
113
+ f"OpenRouter returned no choices. Response: {json.dumps(result)[:2000]}"
114
+ )
115
 
116
+ choice = choices[0] or {}
117
+ message = choice.get("message") or {}
118
  answer = message.get("content")
119
 
120
  # Some providers return content as a list of text blocks.
 
125
  if isinstance(block, dict)
126
  )
127
 
128
+ if isinstance(answer, str) and answer.strip():
129
+ return answer.strip()
130
+
131
+ # Do not expose or submit private reasoning as the benchmark answer.
132
+ # Instead, report safe diagnostics that identify truncation/budget issues.
133
+ finish_reason = choice.get("finish_reason", "unknown")
134
+ usage = result.get("usage") or {}
135
+ completion_details = usage.get("completion_tokens_details") or {}
136
+ reasoning_tokens = completion_details.get("reasoning_tokens", "unknown")
137
+ completion_tokens = usage.get("completion_tokens", "unknown")
138
+ message_keys = sorted(message.keys())
139
+
140
+ raise RuntimeError(
141
+ "OpenRouter returned no visible answer text. "
142
+ f"finish_reason={finish_reason}; "
143
+ f"completion_tokens={completion_tokens}; "
144
+ f"reasoning_tokens={reasoning_tokens}; "
145
+ f"message_fields={message_keys}. "
146
+ "If finish_reason is 'length' and reasoning_tokens is close to "
147
+ "completion_tokens, the model used its output budget for reasoning."
148
+ )
149
 
150
 
151
  def generate_answers(prompts: list[str]) -> list[str]: