  A wide class of problems in combinatorics, computer science and physics can
be described along the following lines. There are a large number of variables
ranging over a finite domain that interact through constraints that each bind a
few variables and either encourage or discourage certain value combinations.
Examples include the $k$-SAT problem or the Ising model. Such models naturally
induce a Gibbs measure on the set of assignments, which is characterised by its
partition function. The present paper deals with the partition function of
problems where the interactions between variables and constraints are induced
by a sparse random (hyper)graph. According to physics predictions, a generic
recipe called the "replica symmetric cavity method" yields the correct value of
the partition function if the underlying model enjoys certain properties
[Krzkala et al., PNAS 2007]. Guided by this conjecture, we prove general
sufficient conditions for the success of the cavity method. The proofs are
based on a "regularity lemma" for probability measures on sets of the form
$\Omega^n$ for a finite $\Omega$ and a large $n$ that may be of independent
interest.
