Build augmented prompts for an LLM
Use the prompts module when you need to assemble a grounded prompt from retrieved passages before sending a query to an LLM.
Prerequisites
- Python access to
src/attune_rag/prompts.py - A list of
RetrievalHitobjects to use as grounding context
Build and send an augmented prompt
-
Format your retrieved hits into a context string. Call
join_context()to concatenate hit contents into a single sentinel-wrapped string, or calljoin_context_numbered()if you want each passage labelled[P1],[P2], and so on inside<passage>tags:from attune_rag.prompts import join_context, join_context_numbered # Plain sentinel-wrapped context context = join_context(hits) # Numbered passage context context = join_context_numbered(hits)Both functions accept an optional
corpusand amax_charslimit to cap total context size. -
Build the augmented prompt. Pass your query string and the context string to
build_augmented_prompt(). Supply avariantargument to select a prompt style; the default is'baseline':from attune_rag.prompts import build_augmented_prompt prompt = build_augmented_prompt(query=query, context=context, variant="baseline")querymust be a non-empty string; otherwise aValueErroris raised.- If you pass an unrecognised
variant, aValueErrorlists the valid options.
-
Send the rendered prompt to your LLM. Pass the string returned by
build_augmented_prompt()directly to your model's API. The prompt already includes the injection-defence clause that instructs the model to treat content inside<passage>…</passage>tags as documentation, not as instructions. -
Run the prompt-related tests. Verify your integration with:
pytest -k "prompts"
Verify success
The task succeeded when:
build_augmented_prompt()returns a non-empty string containing your query and the sentinel-wrapped passages.- All
pytest -k "prompts"tests pass with no errors or failures.
Key files
src/attune_rag/prompts.py