qfa.services.hierarchical_prompts#

Prompt builders for the hierarchical (map-reduce) analyse path.

Reuses the #117 prompt envelope and guardrails so feedback records remain data, not instructions at BOTH the map (leaf) and reduce (synthesis) levels. The map step reuses build_analyze_user_message() verbatim; the reduce step combines leaf partial analyses with the deterministic coding-trend table (the faithfulness anchor).

Functions

build_map_system_message([output_language])

Build the leaf (map) system message: role + guardrails + map action.

build_reduce_system_message([output_language])

Build the synthesis (reduce) system message: role + guardrails + reduce action.

build_reduce_user_message(*, analyst_prompt, ...)

Build the reduce user message from partials and the (optional) trend table.

qfa.services.hierarchical_prompts.build_map_system_message(output_language: str | None = None) str[source]#

Build the leaf (map) system message: role + guardrails + map action.

output_language, when given, is appended here too, so each partial is already produced in the target language rather than leaving translation of a whole mixed-language corpus to the single final reduce call.

qfa.services.hierarchical_prompts.build_reduce_system_message(output_language: str | None = None) str[source]#

Build the synthesis (reduce) system message: role + guardrails + reduce action.

The reduce step produces the final, user-facing analysis, so the optional output_language directive is appended here too, to honour output_language (#154) even if a partial slips through in another language.

qfa.services.hierarchical_prompts.build_reduce_user_message(*, analyst_prompt: str, partial_analyses: tuple[str, ...], trend_table: CodingTrendTable | None) str[source]#

Build the reduce user message from partials and the (optional) trend table.

Each partial analysis is wrapped in a <partial_analysis> block inside <partial_analyses>; the rendered trend table (when present) goes in a <coding_trends> block. All untrusted text is escaped for the tag envelope, consistent with the map path.