def save_as_tsv(
    *,
    embeddings, metadata, header,
    out_dir):
  """Save the embeddings and their metadata to TSV for embedding projector.

  Args:
    embeddings: Numpy 2d Array of embeddings
    metadata: List of metadata, where each row can have one or more string keys
    header: The header row of metadata, should match fields in metadata
    out_dir: Output directory to write
  """
  if embeddings.shape[0] != len(metadata):
    raise ValueError(
        f'Incompatible shapes: {embeddings.shape[0]} and {len(metadata)}')

  logging.info('Building TSV Formatted Output')
  emb_lines: List[Text] = []
  meta_lines: List[Text] = [header]
  for idx in range(embeddings.shape[0]):
    vec = embeddings[idx]
    meta = metadata[idx]
    emb_lines.append('\t'.join([str(v) for v in vec.tolist()]))
    meta_lines.append('\t'.join(meta))

  emb_content = '\n'.join(emb_lines)
  meta_content = '\n'.join(meta_lines)
  logging.info('Writing TSV embeddings and metadata to: %s', out_dir)
  with util.safe_open(os.path.join(out_dir, 'embeddings.tsv'), 'w') as f:
    f.write(emb_content)

  with util.safe_open(os.path.join(out_dir, 'metadata.tsv'), 'w') as f:
    f.write(meta_content)
Save the embeddings and their metadata to TSV for embedding projector.

  Args:
    embeddings: Numpy 2d Array of embeddings
    metadata: List of metadata, where each row can have one or more string keys
    header: The header row of metadata, should match fields in metadata
    out_dir: Output directory to write
  
language/serene/export_embeddings.py