Skip to content

Commit d7faa8b

Browse files
committed
feat: enhance query logic to handle empty content scenarios and add error handling for judge responses
1 parent 94ff4e0 commit d7faa8b

3 files changed

Lines changed: 33 additions & 8 deletions

File tree

api/routers/query.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,8 @@ async def close_stream(stream):
420420
record_chunk()
421421
yield emit("chunk", {"chunk": chunk})
422422

423-
if (start_timeout or timed_out) and not full_content.strip():
423+
no_chunks = chunk_count == 0 and not full_content.strip()
424+
if (start_timeout or timed_out or no_chunks) and not full_content.strip():
424425
fallback_used = True
425426
try:
426427
fallback_content = await asyncio.wait_for(

api/services/ensemble.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,22 @@ async def judge_responses(topic: str, responses: list[str], mode: str = LEARNING
6767
telemetry_sink = kwargs.get("telemetry_sink") if isinstance(kwargs.get("telemetry_sink"), dict) else None
6868

6969
model_start = time.perf_counter()
70-
result = await create_chat_completion(
71-
model="judge",
72-
messages=[{"role": "user", "content": prompt}],
73-
temperature=0,
74-
max_tokens=512,
75-
request_id=request_id,
76-
)
70+
try:
71+
result = await create_chat_completion(
72+
model="judge",
73+
messages=[{"role": "user", "content": prompt}],
74+
temperature=0,
75+
max_tokens=512,
76+
request_id=request_id,
77+
)
78+
except Exception as exc:
79+
logger.warning(
80+
"judge_completion_failed",
81+
request_id=request_id,
82+
user_id_hash=anonymized_user_id,
83+
error=str(exc),
84+
)
85+
return responses[0]
7786
usage = None
7887
usage_obj = getattr(result, "usage", None)
7988
if usage_obj is not None:

api/tests/test_ensemble.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,18 @@ async def fake_generate_explanation(_topic, _level, _model, **_kwargs):
4444

4545
with pytest.raises(RuntimeError):
4646
await ensemble_module.ensemble_generate("topic", "eli5", use_premium=False, mode="learning")
47+
48+
49+
@pytest.mark.asyncio
50+
async def test_ensemble_falls_back_when_judge_fails(monkeypatch):
51+
async def fake_generate_explanation(_topic, _level, _model, **_kwargs):
52+
return "candidate"
53+
54+
async def fake_create_chat_completion(*_args, **_kwargs):
55+
raise RuntimeError("judge down")
56+
57+
monkeypatch.setattr(ensemble_module, "generate_explanation", fake_generate_explanation)
58+
monkeypatch.setattr(ensemble_module, "create_chat_completion", fake_create_chat_completion)
59+
60+
result = await ensemble_module.ensemble_generate("topic", "eli5", use_premium=False, mode="learning")
61+
assert result == "candidate"

0 commit comments

Comments
 (0)