asmit dash
Open-source library

relapse

Open source · shipped to PyPI

Self-correcting LLM-call wrapper - ground-check, restructure, retry.

What it is

A self-correcting LLM call wrapper for RAG. relapse wraps any LLM call in a feedback loop: call → ground-check the answer → restructure the prompt with explicit findings → retry. The first call uses your prompt verbatim; only on hallucination does relapse rewrite and re-ask. The user sees the final grounded answer plus a transcript of what was rejected and why. Provider-agnostic - pass a function that takes a prompt and returns text.

Why it's needed

LLM hallucination at the answer layer is the failure mode you can't fix with better retrieval - the model has the right context but invents a fact anyway. The standard reflex is 'retry with feedback,' but most teams hand-roll it badly: they retry on a vague 'are you sure?' prompt, which often produces the same hallucination with more confidence. The retry isn't useful unless it tells the model exactly what was wrong.

What it does

relapse runs a deterministic check after each call (numbers, years, quoted spans must appear in the retrieved chunks) and feeds the specific failed spans back into the next prompt - not a vague nudge. The default check is a vendored copy of corroborate's deterministic spine; richer checks plug in via a (query, chunks, answer) -> GroundingResult function so you can use NLI scoring, entity matching, or even LLM-as-judge if you really want one.

How to use it

Install it:

pip install relapse

Then call it:

import relapse

def my_llm(prompt: str) -> str:
    return client.messages.create(...).content[0].text

result = relapse.run(
    query="When did WW2 end?",
    retrieved_chunks=["World War II ended in 1945 with Japan's surrender."],
    call_llm=my_llm,
    max_iterations=3,
)
print(result.answer)        # final grounded answer
print(result.iterations)    # how many tries it took
print(result.grounded)      # True if the final answer passed the check

Links