def dump_neighbors(self,
                     examples: Iterable[Example],
                     outfile: str,
                     neighbor_filter: NeighborFilter,
                     max_neighbors: int = 100) -> None:
    """Dumps the neighbors to a JSONL file.

    Args:
      examples: A stream of examples
      outfile: JSONL filename.
      neighbor_filter: A NeighborFilter.
      max_neighbors: Maximum number of neighbors per example (int).
    """
    with tf.io.gfile.GFile(outfile, "w") as fout:
      count = 0
      for result in self.retrieve_all(examples):
        if count % self._log_every == 0:
          logging.info("Processing %d", count)
        input_ex = result.example
        neighbor_hashed_ids = []
        neighbor_distances = []
        filter_state = None
        for neighbor_id, neighbor_distance in zip(result.neighbor_ids,
                                                  result.neighbor_distances):
          neighbor_ex = self.get_index_entry(neighbor_id)
          is_ok = neighbor_filter.check_neighbor(input_ex, neighbor_ex,
                                                 filter_state)
          if is_ok:
            filter_state = neighbor_filter.accept_neighbor(
                neighbor_ex, filter_state)
            neighbor_hashed_ids.append(neighbor_ex["hashed_id"])
            neighbor_distances.append(round(neighbor_distance, 3))
          if len(neighbor_hashed_ids) >= max_neighbors:
            break
        output = dict(
            input_ex,
            exemplars={
                "hashed_ids": neighbor_hashed_ids,
                "distances": neighbor_distances,
            })
        print(json.dumps(output), file=fout)
        count += 1
    logging.info("Wrote %d entries to %s", count, outfile)
Dumps the neighbors to a JSONL file.

    Args:
      examples: A stream of examples
      outfile: JSONL filename.
      neighbor_filter: A NeighborFilter.
      max_neighbors: Maximum number of neighbors per example (int).
    
language/casper/retrieve/query_retrievers.py