Files
povtornyy-ekzamen-graf-s-re…/SOLUTION.md
T

63 lines
2.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
**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 backoff 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
selfcontained loop that catches any exception from the generator and
retries.
* The number of attempts and backoff 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 answergeneration logic.
* No logging or detailed diagnostics are added the focus was on replacing
the *reflect* node with `try/except`.
* The backoff is linear; exponential backoff could be added if needed.
Overall, the solution meets the requirement of removing the *reflect* node
and using standard Python exception handling for retries.