async function getEmbeddingsPaLM()

in firestore-semantic-search/functions/src/common/palm_embeddings.ts [90:117]


async function getEmbeddingsPaLM(text: string | string[]): Promise<number[][]> {
  if (!client && (typeof text !== 'string' || text.length !== 0)) {
    await initializePaLMClient();
  }

  if (typeof text === 'string') text = [text];

  // chunk into batches of 5 (the current limit of the PaLM API)
  const batchSize = 5;
  const batches = [];
  for (let i = 0; i < text.length; i += batchSize) {
    batches.push(text.slice(i, i + batchSize));
  }

  const t0 = performance.now();
  const embeddingBatches = await Promise.all(
    batches.map(async batch => {
      return embedBatchPaLM(batch);
    })
  );
  const embeddings = embeddingBatches.flat();

  const duration = performance.now() - t0;
  console.log(`Processed embeddings. This took ${duration}ms`);
  // const embeddings = await client.embedText(text.length ? text : [text]);

  return embeddings;
}