function convertToTextGeneratorResponse()

in firestore-palm-gen-text/functions/src/generator.ts [228:262]


function convertToTextGeneratorResponse(
  prediction: VertexPrediction | GenerativePrediction
): TextGeneratorResponse {
  // if it's generative language
  if ('candidates' in prediction) {
    const {candidates, filters, safetyFeedback} = prediction;
    const blocked = !!filters && filters.length > 0;
    const safetyMetadata = {
      blocked,
      safetyFeedback,
    };
    if (!candidates.length && !blocked) {
      throw new Error('No candidates returned from the Generative API.');
    }
    return {
      candidates: candidates.map(candidate => candidate.output),
      safetyMetadata,
    };
  } else {
    // provider will be vertex
    const {content, safetyAttributes} = prediction;
    const blocked = !!safetyAttributes && !!safetyAttributes.blocked;
    const safetyMetadata = {
      blocked,
      safetyAttributes,
    };
    if (!content && !blocked) {
      throw new Error('No content returned from the Vertex PaLM API.');
    }
    return {
      candidates: blocked ? [] : [content!],
      safetyMetadata,
    };
  }
}