Module: braid.generator

GRD Generator module for creating Guided Reasoning Diagrams using LLMs.

class braid.generator.GRDGenerator(examples: List[Dict[str, str]] | None = None, max_retries: int = 3, temperature: float = 0.3, use_dspy_predict: bool = True)[source]

Bases: object

Generator for Guided Reasoning Diagrams in Mermaid format.

DEFAULT_EXAMPLES = [{'grd': '```mermaid\nflowchart TD\n    Start[Read and understand problem] --> Extract[Extract given values]\n    Extract --> Identify[Identify what to find]\n    Identify --> Formula[Recall speed formula]\n    Formula --> Apply[Apply: divide distance by time]\n    Apply --> Calculate[Perform the division]\n    Calculate --> Verify[Verify units are correct]\n    Verify --> Answer[State the final speed]\n```', 'problem': 'If a train travels 120 km in 2 hours, what is its speed?'}, {'grd': '```mermaid\nflowchart TD\n    Start[Analyze the equation] --> Goal[Goal: isolate x]\n    Goal --> Subtract[Subtract constant from both sides]\n    Subtract --> Simplify1[Simplify the right side]\n    Simplify1 --> Divide[Divide both sides by coefficient]\n    Divide --> Simplify2[Simplify to get x value]\n    Simplify2 --> Check[Verify: substitute back]\n    Check --> Answer[State the solution]\n```', 'problem': 'Solve: 3x + 5 = 14'}, {'grd': '```mermaid\nflowchart TD\n    Start[Understand the scenario] --> Values[Identify: price and quantity]\n    Values --> Operation[Determine operation: multiplication]\n    Operation --> Calculate[Multiply price by quantity]\n    Calculate --> Answer[State total cost]\n```', 'problem': 'A store sells apples at $2 each. If John buys 5 apples, how much does he pay?'}]
__init__(examples: List[Dict[str, str]] | None = None, max_retries: int = 3, temperature: float = 0.3, use_dspy_predict: bool = True)[source]

Initialize the GRD Generator.

Parameters:
  • examples – Few-shot examples for GRD generation

  • max_retries – Maximum number of retries if generation fails

  • temperature – Temperature for LLM generation (lower = more deterministic)

  • use_dspy_predict – Whether to use DSPy’s Predict API (recommended)

generate(problem: str, problem_type: str | None = None, custom_instructions: str | None = None) Dict[str, Any][source]

Generate a GRD for a given problem.

Parameters:
  • problem – The problem to solve

  • problem_type – Optional type hint (e.g., “math”, “logic”, “reasoning”)

  • custom_instructions – Optional custom instructions for generation

Returns:

  • grd: Mermaid code string

  • raw_response: Raw LLM response

  • parsed_structure: Parsed GRDStructure object

  • valid: Whether the GRD is valid

Return type:

Dictionary containing

add_example(problem: str, grd: str)[source]

Add a custom example to the generator.

Parameters:
  • problem – Example problem

  • grd – Example GRD in Mermaid format

get_template(problem_type: str) str | None[source]

Get a template GRD for a specific problem type.

Parameters:

problem_type – Type of problem (e.g., “math”, “logic”, “reasoning”)

Returns:

Template Mermaid code or None