fastrag.query.prompts.templates#

The PromptTemplate: retrieved documents plus a question to messages.

Functions

group_sources(context)

Group retrieved chunks into the sources a prompt cites as [n].

page_url(document)

The link to the page document came from, if known.

Classes

PromptTemplate(template[, system])

Renders retrieved documents and a question into messages for a ChatModel.

class fastrag.query.prompts.templates.PromptTemplate(template, system=None)[source]#

Bases: object

Renders retrieved documents and a question into messages for a ChatModel.

Parameters:
  • template (str) – The user-message body. Must contain both {context} and {question}; any other text is copied through verbatim.

  • system (str | None) – Optional system message, sent unchanged ahead of the rendered template.

render(*, context, question)[source]#

Render context and question into the messages to send.

context is numbered by source, as group_sources() groups it — [n] is every chunk of the page in groups[n - 1] — so a “cite as [n]” system instruction cites each page by one number.

Parameters:
Return type:

list[Message]

fastrag.query.prompts.templates.group_sources(context)[source]#

Group retrieved chunks into the sources a prompt cites as [n].

Chunks sharing a page_url() (one page, and each page of a PDF apart) form one group, placed where the page’s best-ranked chunk was retrieved and holding its chunks in page order (chunk_index). A chunk with no URL is a group of its own. groups[n - 1] is what [n] cites: the serving layer maps an answer’s citations back to pages through this same function.

Parameters:

context (Sequence[Document])

Return type:

list[list[Document]]

fastrag.query.prompts.templates.page_url(document)[source]#

The link to the page document came from, if known.

A PDF parser records each page’s link, with its #page=N fragment, as source_url; any other document links to its source_uri.

Parameters:

document (Document)

Return type:

str | None