How node states and generation work
mainIn TreeQuest, a node is associated with a user-definable state. This state can be a simple type like str or a complex dataclass containing LLM responses and metadata.
To integrate with an LLM, your generation function should follow this pattern:
@dataclasses.dataclass
class State:
llm_answer: str
score: float
def generate(parent_state: State | None) -> tuple[State, float]:
if parent_state is None:
state = initial_generation()
else:
state = refine_answer(parent_state.llm_answer, parent_state.score)
return state, state.scoreTreeQuest supports multiple action types by passing a dictionary of generation functions to the step method. This allows you to represent different LLM models or different prompting strategies as distinct actions in the search tree.
import dataclasses
import treequest as tq
@dataclasses.dataclass
class State:
llm_answer: str
score: float
def generate(parent_state: State | None) -> tuple[State, float]:
# ... logic to call LLM ...
return state, state.score
algo = tq.ABMCTSM()
search_tree = algo.init_tree()
search_tree = algo.step(search_tree, {'Action Label': generate})