embedWithEmbedContent function

Future<List<Embedding>> embedWithEmbedContent(
  1. GenerativeLanguageBaseClient service, {
  2. required String embedderName,
  3. required List<DocumentData> docs,
  4. GoogleGenAiEmbedderOptions? options,
  5. bool sendTaskTypeAndTitle = true,
})

Embeds each of docs with the :embedContent method: one request per document, with its text and media as parts (the googleAI embedders, and gemini-embedding-2 and newer on vertexAI, as in JS).

sendTaskTypeAndTitle is false where the model has no such fields (vertexAI's gemini-embedding-2; the caller warns about them).

Implementation

Future<List<Embedding>> embedWithEmbedContent(
  GenerativeLanguageBaseClient service, {
  required String embedderName,
  required List<DocumentData> docs,
  GoogleGenAiEmbedderOptions? options,
  bool sendTaskTypeAndTitle = true,
}) {
  // Built first, so malformed media fails before any request is sent.
  final contents = [
    for (final (index, doc) in docs.indexed)
      gcl.Content(role: 'user', parts: _embedParts(index, doc)),
  ];
  return Future.wait(
    contents.map((content) async {
      final res = await service.embedContent(
        gcl.EmbedContentRequest(
          content: content,
          outputDimensionality: options?.outputDimensionality,
          taskType: sendTaskTypeAndTitle ? options?.taskType : null,
          title: sendTaskTypeAndTitle ? options?.title : null,
        ),
        model: 'models/$embedderName',
      );
      return Embedding(embedding: res.embedding?.values ?? []);
    }),
  );
}