Standalone-question rewriting

A user asks "what about chapter four?" Without context, retrieval has no idea which book or topic. Standalone-question rewriting takes the chat history plus the new question and produces a query that stands on its own. Retrieval becomes accurate again.

Where rewriting sits in the chain

The rewrite step runs before retrieval. The rewritten query, not the raw user message, is what searches the vector store.

booktutor.py
python
CONDENSE_PROMPT = '''Given the conversation and a follow-up question,
rewrite the follow-up as a self-contained question that can be searched
without the conversation context.

Conversation:
{history}

Follow-up question: {question}

Standalone question:'''

async def standalone_question(history: list, question: str) -> str:
    formatted = '\n'.join(f'{m.role}: {m.content}' for m in history[-6:])
    raw = await llm.complete(CONDENSE_PROMPT.format(history=formatted, question=question))
    return raw.strip()

Trim history to the last few turns. The prompt does the rewriting and the result is a fresh query the retriever can use without any conversational baggage.

Quiz: Quiz

Loading practice…