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:Iamhankai Forest of Thought CGDM Get Best Answer

From Leeroopedia
Knowledge Sources
Domains Evaluation, Reasoning
Last Updated 2026-02-14 03:00 GMT

Overview

Concrete tool for LLM-based expert answer selection in the CGDM pipeline provided by the Forest-of-Thought repository.

Description

The get_best_answer function in the CGDM module prompts a specialized mathematics expert LLM (e.g., QwQ-32B-Preview) to select the best answer from a set of candidates. It formats the question and all candidate answers into a structured prompt, generates the expert's response via the Pipeline.get_respond() interface, and returns the expert's selection. A companion get_new_answer() function generates a fresh answer when no candidate is satisfactory.

Usage

Called by the CGDM post-processing script (cgdm/cgdm.py) when processing examples flagged as need_model_judge during the first-pass majority voting. Also used inline in Monte_Carlo_Forest.get_fot_final_answer() for real-time CGDM.

Code Reference

Source Location

Signature

def get_best_answer(question, all_answer_str):
    """
    Ask expert LLM to select best answer from candidates.

    Args:
        question (str): Original math problem.
        all_answer_str (str): Formatted string of candidate answers.

    Returns:
        tuple: (expert_response: str, history: list)
    """

def get_new_answer(question):
    """
    Ask expert LLM to generate a fresh answer.

    Args:
        question (str): Original math problem.

    Returns:
        tuple: (response: str, history: list)
    """

Import

from cgdm.cgdm import get_best_answer, get_new_answer

I/O Contract

Inputs

Name Type Required Description
question str Yes Original math problem text
all_answer_str str Yes Formatted candidate answers (e.g., "Answer 1: 42\nAnswer 2: 38\n...")

Outputs

Name Type Description
expert_response str Expert LLM's response selecting or explaining the best answer
history list Updated conversation history for multi-turn follow-up

Usage Examples

from cgdm.cgdm import get_best_answer, get_new_answer

# Format candidate answers
candidates = "Answer 1: 42\nAnswer 2: 38\nAnswer 3: 42\nAnswer 4: 40"

# Ask expert to select best
response, history = get_best_answer(
    question="What is the value of 6 * 7?",
    all_answer_str=candidates
)

# If no good candidate, generate fresh
if "none" in response.lower():
    fresh, hist = get_new_answer("What is the value of 6 * 7?")

Related Pages

Implements Principle

Requires Environment

Page Connections

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