We report the computational investigation of a series of ternary X4Y2Z and X5Y2Z2 compounds with X={Mg, Ca, Sr, Ba}, Y={P, As, Sb, Bi}, and Z={S, Se, Te}. The compositions for these materials were predicted through a search guided by machine learning, while the structures were resolved using the minima hopping crystal structure prediction method. Based on ab initio calculations, we predict that many of these compounds are thermodynamically stable. In particular, 21 of the X4Y2Z compounds crystallize in a tetragonal structure with I-42d symmetry, and exhibit band gaps in the range of 0.3 and 1.8 eV, well suited for various energy applications. We show that several candidate compounds (in particular X4Y2Te and X4Sb2Se) exhibit good photo absorption in the visible range, while others (e.g., Ba4Sb2Se) show excellent thermoelectric performance due to a high power factor and extremely low lattice thermal conductivities.