Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Spcl Graph of thoughts KeywordCountingPrompter

From Leeroopedia
Knowledge Sources
Domains Prompt_Engineering, Keyword_Counting
Source File examples/keyword_counting/keyword_counting.py, Lines 157-853
Superclass graph_of_thoughts.prompter.Prompter (ABC)
Implements Principle Principle:Spcl_Graph_of_thoughts_Keyword_Counting_Prompt_Design
Last Updated 2026-02-14

Overview

KeywordCountingPrompter is a domain-specific prompter that generates prompt strings for LLM-based country name frequency counting in text passages. It is the most comprehensive prompter in the GoT framework, containing 10 class-level prompt templates and supporting 7 reasoning approaches (IO, CoT, ToT, ToT2, GoT4, GoT8, GoTx). The class subclasses the abstract Prompter base class. It is defined in the keyword counting example file, not as a package import.

Description

The class manages a rich set of prompt templates and routes to the appropriate one based on the reasoning method name and execution phase. All counting prompts produce JSON-formatted frequency dictionaries. The class uniquely supports a validation-and-improve cycle for aggregation results, with a dedicated improve_prompt method (unlike sorting, where it is unimplemented).

Class Attribute Prompt Templates

Attribute Purpose Target Method(s)
count_prompt Full-text direct counting, JSON output IO
count_prompt_cot CoT decomposition into paragraphs CoT, ToT (initial), GoT4/GoT8 (phase 1)
count_prompt_sentence Word-by-word counting for short text GoTx (phase 1)
tot_improve_prompt Fix incorrect frequency dict given full text ToT, GoT4/GoT8 (phase 2)
sentence_improve_prompt Fix incorrect dict given single sentence GoTx (phase 2)
got_split_prompt Split into 4 paragraphs as JSON GoT4 (phase 0)
got_split_prompt2 Split into 8 paragraphs as JSON GoT8 (phase 0)
got_split_prompt3 Split into individual sentences as JSON GoTx (phase 0)
got_aggregate_prompt Merge two frequency dictionaries GoT4/GoT8/GoTx (aggregation)
got_improve_aggregate_prompt Fix incorrect dictionary merge GoT4/GoT8/GoTx (improve)

Code Reference

Key Methods

class KeywordCountingPrompter(prompter.Prompter):
    def generate_prompt(self, num_branches: int, original: str, current: str, method: str, **kwargs) -> str:
        """
        Routes to appropriate template based on method and phase.
        - IO: count_prompt
        - CoT: count_prompt_cot
        - ToT: count_prompt_cot (initial) or tot_improve_prompt (refinement)
        - GoT phase 0: got_split_prompt (4), got_split_prompt2 (8), or got_split_prompt3 (x)
        - GoT phase 1: count_prompt_cot or count_prompt_sentence (gotx)
        - GoT phase 2: tot_improve_prompt or sentence_improve_prompt (gotx)
        Asserts num_branches == 1 ('Branching should be done via multiple requests').
        """

    def aggregation_prompt(self, state_dicts: List[Dict], **kwargs) -> str:
        """
        Formats got_aggregate_prompt with two frequency dictionaries.
        Handles 0 or 1 input states by substituting empty dicts '{}' as needed.
        Asserts at most 2 states.
        """

    def improve_prompt(self, current: str, aggr1: str, aggr2: str, **kwargs) -> str:
        """
        Formats got_improve_aggregate_prompt showing:
        - Dictionary 1 (aggr1): first input to aggregation
        - Dictionary 2 (aggr2): second input to aggregation
        - Incorrectly Combined Dictionary (current): the erroneous merge
        Used by ValidateAndImprove operations in GoT4/GoT8/GoTx.
        """

    def validation_prompt(self, **kwargs) -> str:
        """Not implemented (returns None). Validation is programmatic via valid_aggregation."""

    def score_prompt(self, state_dicts: List[Dict], **kwargs) -> str:
        """Not implemented (returns None). Scoring is programmatic via num_errors."""

Instantiation

# From examples/keyword_counting/keyword_counting.py, line 1417
executor = controller.Controller(
    lm,
    operations_graph,
    KeywordCountingPrompter(),
    KeywordCountingParser(),
    {
        "original": data[1],
        "ground_truth": data[2],
        "current": "",
        "phase": 0,
        "method": method.__name__,
    },
)

I/O Contract

Input

Parameter Type Description
num_branches int Must be 1 (branching via multiple requests)
original str Full input text passage with country names
current str Intermediate frequency dictionary as JSON string; empty initially
method str One of: "io", "cot", "tot", "tot2", "got4", "got8", "gotx"
**kwargs Dict Must include phase (int) for GoT methods; may include sub_text (str)

Output

All methods return a str containing the formatted prompt. The prompt instructs the LLM to output a JSON frequency dictionary.

State Dictionary Keys

Key Type Description
original str Full input text passage
ground_truth str Expected frequency list (for scoring)
current str JSON frequency dictionary string
phase int 0 (split), 1 (count per-part), 2 (refine)
method str Reasoning approach identifier
part str Paragraph/sentence identifier, e.g. "Paragraph 1" or "Sentence 3"
sub_text str The text fragment (paragraph or sentence) assigned to this state
aggr1 str First input dictionary before aggregation (set by parser)
aggr2 str Second input dictionary before aggregation (set by parser)

Usage Examples

GoT4 Split Prompt

prompter = KeywordCountingPrompter()
prompt = prompter.generate_prompt(
    num_branches=1,
    original="Alexandra boarded the first flight...",
    current="",
    method="got4",
    phase=0,
)
# Returns got_split_prompt asking LLM to split text into 4 paragraphs as JSON

Aggregation Prompt

prompter = KeywordCountingPrompter()
prompt = prompter.aggregation_prompt([
    {"current": '{"Canada": 1, "Mexico": 1}'},
    {"current": '{"Peru": 2, "Brazil": 1}'},
])
# Returns got_aggregate_prompt asking to merge the two dictionaries

Improve (Validate Aggregation) Prompt

prompter = KeywordCountingPrompter()
prompt = prompter.improve_prompt(
    current='{"Canada": 1, "Mexico": 1, "Peru": 2, "Brazil": 2}',
    aggr1='{"Canada": 1, "Mexico": 1}',
    aggr2='{"Peru": 2, "Brazil": 1}',
)
# Returns got_improve_aggregate_prompt showing the incorrect merge and asking to fix it

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment