63 lines
2.2 KiB
Markdown
63 lines
2.2 KiB
Markdown
**What was implemented**
|
||
The original project used a special *reflect* node to retry answer generation.
|
||
In this version the retry logic is replaced by a plain `try/except` loop inside
|
||
`get_answer_with_retry`. The function now attempts to call a generator up to
|
||
`max_retries` times, sleeping a short back‑off between attempts, and raises a
|
||
`GenerationError` only after all attempts fail.
|
||
|
||
**Why the main parts satisfy the assignment**
|
||
* The retry mechanism is implemented without any external node – it is a
|
||
self‑contained loop that catches any exception from the generator and
|
||
retries.
|
||
* The number of attempts and back‑off are configurable, matching the
|
||
behaviour that the original *reflect* node provided.
|
||
* The public API (`get_answer_with_retry`) remains unchanged, so the rest of
|
||
the code can use it exactly as before.
|
||
|
||
**Key code excerpts**
|
||
|
||
*`src/index.py` – retry loop*
|
||
```python
|
||
while attempt < max_retries:
|
||
try:
|
||
answer = generator()
|
||
return answer
|
||
except Exception as exc:
|
||
attempt += 1
|
||
if attempt >= max_retries:
|
||
raise GenerationError(
|
||
f"Answer generation failed after {max_retries} attempts"
|
||
) from exc
|
||
wait_time = backoff_factor * attempt
|
||
time.sleep(wait_time)
|
||
```
|
||
|
||
*`src/index.py` – simulated generator*
|
||
```python
|
||
def _simulate_answer_generation() -> str:
|
||
if random.random() < 0.3:
|
||
raise RuntimeError("Simulated generation failure")
|
||
time.sleep(0.1)
|
||
return "Generated answer content"
|
||
```
|
||
|
||
*`src/index.py` – entry point*
|
||
```python
|
||
def main() -> None:
|
||
try:
|
||
answer = get_answer_with_retry()
|
||
print("Answer generated successfully:")
|
||
print(answer)
|
||
except GenerationError as err:
|
||
print(f"Error: {err}")
|
||
```
|
||
|
||
**Limitations**
|
||
* The generator is a simple simulation; in a real system it would be replaced
|
||
by the actual answer‑generation logic.
|
||
* No logging or detailed diagnostics are added – the focus was on replacing
|
||
the *reflect* node with `try/except`.
|
||
* The back‑off is linear; exponential back‑off could be added if needed.
|
||
|
||
Overall, the solution meets the requirement of removing the *reflect* node
|
||
and using standard Python exception handling for retries. |