Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Spcl Graph of thoughts Generate Operation

From Leeroopedia
Knowledge Sources
Domains Graph_Reasoning, Thought_Operations
Principles Principle:Spcl_Graph_of_thoughts_Thought_Generation
Source File graph_of_thoughts/operations/operations.py, Lines 391-477
Last Updated 2026-02-14

Overview

The Generate class is an operation that produces new thought states by prompting a language model with existing states from predecessor operations. If no predecessors exist, it uses the initial problem parameters (kwargs) as the base state. It is the fundamental mechanism by which the LLM generates content in the Graph of Thoughts framework.

Import

from graph_of_thoughts.operations import Generate

Class Signature

class Generate(Operation):
    operation_type = OperationType.generate

    def __init__(
        self, num_branches_prompt: int = 1, num_branches_response: int = 1
    ) -> None: ...

    def get_thoughts(self) -> List[Thought]: ...

    def _execute(
        self, lm: AbstractLanguageModel, prompter: Prompter, parser: Parser, **kwargs
    ) -> None: ...

Constructor Parameters

Parameter Type Default Description
num_branches_prompt int 1 Number of solutions requested per prompt (passed to prompter.generate_prompt)
num_branches_response int 1 Number of separate LLM calls per prompt (passed as num_responses to lm.query)

I/O Behavior

Input:

  • Predecessor thoughts (list of Thought objects from upstream operations)
  • or kwargs (initial problem parameters) if this is a root operation with no predecessors

Output:

  • New Thought objects with states parsed from LLM responses, stored in self.thoughts

Execution Flow

  1. Collect all thoughts from predecessor operations via get_previous_thoughts()
  2. If there are no previous thoughts and there are predecessor operations, return immediately (upstream produced nothing)
  3. If there are no previous thoughts and no predecessors, wrap kwargs in a Thought as the base state
  4. For each previous thought:
    1. Construct a prompt via prompter.generate_prompt(num_branches_prompt, **base_state)
    2. Query the LLM via lm.query(prompt, num_responses=num_branches_response)
    3. Extract text responses via lm.get_response_texts()
    4. Parse responses into state updates via parser.parse_generate_answer(base_state, responses)
    5. For each parsed state, merge with base state ({**base_state, **new_state}) and create a new Thought
  5. If total thoughts exceed num_branches_prompt * num_branches_response * len(previous_thoughts), log a warning

Key Implementation Details

State Merging

Each new thought's state is the union of the base state and the parsed update:

new_state = {**base_state, **new_state}
self.thoughts.append(Thought(new_state))

This ensures downstream operations have access to the full context, including fields from the original problem and any new fields produced by the LLM.

Root vs. Non-Root Behavior

if len(previous_thoughts) == 0 and len(self.predecessors) > 0:
    return  # predecessors exist but produced no thoughts -- do nothing

if len(previous_thoughts) == 0:
    previous_thoughts = [Thought(state=kwargs)]  # root node -- use kwargs

This two-check pattern distinguishes between:

  • A root Generate (no predecessors) that should bootstrap from problem parameters
  • A downstream Generate whose predecessors were exhausted by upstream filters

Overproduction Warning

The operation logs a warning if the parser extracts more thoughts than expected:

if (
    len(self.thoughts)
    > self.num_branches_prompt * self.num_branches_response * len(previous_thoughts)
    and self.num_branches_prompt > 0
):
    self.logger.warning("Generate operation %d created more thoughts than expected", self.id)

This helps detect Parser bugs where a single LLM response is incorrectly split into too many thought states.

Instance Attributes

Attribute Type Description
operation_type OperationType Always OperationType.generate
num_branches_prompt int Number of solutions per prompt
num_branches_response int Number of LLM calls per prompt
thoughts List[Thought] Generated thoughts (populated after execution)
id int Unique operation identifier (inherited from Operation)
predecessors List[Operation] Upstream operations (inherited from Operation)
successors List[Operation] Downstream operations (inherited from Operation)
executed bool Whether the operation has been executed (inherited from Operation)

Usage Example

from graph_of_thoughts.operations import Generate

# Root generate: produces 5 candidates from problem parameters
gen_root = Generate(num_branches_prompt=5, num_branches_response=1)

# Downstream generate: 1 solution per prompt, 3 separate LLM calls for diversity
gen_branch = Generate(num_branches_prompt=1, num_branches_response=3)

# Wire into a graph
gen_branch.add_predecessor(gen_root)

GitHub URL

graph_of_thoughts/operations/operations.py (Lines 391-477)

Page Connections

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