Skip to contents

Create system and user messages for a RAG answer

Usage

make_rag_messages(
  question,
  chunks,
  store_location = RAG_STORE_LOCATION,
  system_prompt_file = NULL,
  system_prompt = NULL,
  use_saved_system_prompt = TRUE,
  output_instructions = NULL,
  output_instructions_file = NULL,
  context_instructions =
    paste("Use only the retrieved context below. Do not invent citations, facts, dates, locations, or measurements.",
    "When evidence is absent, ambiguous, or not tied to a source, state that limitation.",
    "Return both the required summary response and the required structured JSON data.",
    sep = "\n")
)

Arguments

question

User question.

chunks

Retrieved chunks.

store_location

Path to the ragnar store.

system_prompt_file, system_prompt, use_saved_system_prompt

System prompt controls.

output_instructions, output_instructions_file

Output instruction controls.

context_instructions

Instructions prepended to retrieved context.

Value

A list with system and user character elements.

Examples

chunks <- tibble::tibble(
  source_type = "local_file",
  source_title = "Example report",
  file_name = "example_report.pdf",
  origin = "local-file://example_report.pdf",
  text = "This is an example retrieved passage."
)
make_rag_messages(
  question = "What does the source say?",
  chunks = chunks,
  system_prompt = "Answer only from retrieved context.",
  use_saved_system_prompt = FALSE
)
#> $system
#> [1] "Answer only from retrieved context.\n\nReturn the answer in exactly two top-level sections:\n\n## Summary response\nWrite a concise but evidence-rich Markdown summary organized around the user's question. Include important limitations and distinguish direct evidence from indirect or contextual evidence.\n\n## Structured data\nReturn a single valid JSON array inside a fenced ```json code block. Use null for unknown values and do not use trailing commas. Each JSON object must represent one evidence item, source annotation, or explicit limitation.\n\nEach JSON object must include these fields:\ntopic, evidence_type, source_label, reference_id, resource_id, file_name, source_url, local_path, location, date_or_time_period, evidence_summary, relevance, limitations, confidence.\n\nAllowed evidence_type values are: direct_evidence, contextual_evidence, source_metadata, limitation.\nAllowed confidence values are: high, medium, low.\nUse source labels that match the retrieved context labels, such as [1], [2], or [3]. Do not include claims in either section unless they are supported by the retrieved context."
#> 
#> $user
#> [1] "Use only the retrieved context below. Do not invent citations, facts, dates, locations, or measurements.\nWhen evidence is absent, ambiguous, or not tied to a source, state that limitation.\nReturn both the required summary response and the required structured JSON data.\n\nQuestion:\nWhat does the source say?\n\nRetrieved context:\n[1] Local file example_report.pdf\nOrigin: local-file://example_report.pdf\nURL: \nLocal path: \nContext headings: \nScreening term hits: \n\nThis is an example retrieved passage."
#>