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

Build and send an augmented prompt

  1. Format your retrieved hits into a context string. Call join_context() to concatenate hit contents into a single sentinel-wrapped string, or call join_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 corpus and a max_chars limit to cap total context size.

  2. Build the augmented prompt. Pass your query string and the context string to build_augmented_prompt(). Supply a variant argument 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")
    
    • query must be a non-empty string; otherwise a ValueError is raised.
    • If you pass an unrecognised variant, a ValueError lists the valid options.
  3. 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.

  4. Run the prompt-related tests. Verify your integration with:

    pytest -k "prompts"
    

Verify success

The task succeeded when:

Key files