async getEmbeddings()

in firestore-vector-search/functions/src/embeddings/client/text/vertex_ai.ts [30:74]


  async getEmbeddings(batch: string[]) {
    try {
      const instances = batch.map(text =>
        helpers.toValue({content: text})
      ) as protobuf.common.IValue[];

      const parameters = helpers.toValue({});

      const [response] = await this.client.predict({
        endpoint,
        instances,
        parameters,
      });

      if (
        !response ||
        !response.predictions ||
        response.predictions.length === 0
      )
        throw new Error('Error with embedding');

      const predictionValues = response.predictions as protobuf.common.IValue[];

      const predictions = predictionValues.map(helpers.fromValue) as {
        embeddings: {values: number[]};
      }[];

      if (
        predictions.some(
          prediction => !prediction.embeddings || !prediction.embeddings.values
        )
      ) {
        throw new Error('Error with embedding');
      }

      const embeddings = predictions.map(
        prediction => prediction.embeddings.values
      );

      return embeddings;
    } catch (error) {
      console.error('Error fetching embeddings:', error);
      throw new Error('Error with embedding, see function logs for details');
    }
  }