code: def triplets_from_labeled_dataset(input_examples):
      # Create triplets for a [(label, sentence), (label, sentence)...] dataset
      # by using each example as an anchor and selecting randomly a
      # positive instance with the same label and a negative instance with a different label
      triplets = []
      label2sentence = defaultdict(list)
      for inp_example in input_examples:
          label2sentence[inp_example.label].append(inp_example)
  
      for inp_example in input_examples:
          anchor = inp_example
  
          if (
              len(label2sentence[inp_example.label]) < 2
          ):  # We need at least 2 examples per label to create a triplet
              continue
  
          positive = None
          while positive is None or positive.guid == anchor.guid:
              positive = random.choice(
                  label2sentence[inp_example.label]
              )
  
          negative = None
          while negative is None or negative.label == anchor.label:
              negative = random.choice(input_examples)
  
          triplets.append(
              InputExample(
                  texts=[
                      anchor.texts[0],
                      positive.texts[0],
                      negative.texts[0],
                  ]
              )
          )
  
      return triplets
  
file_path: sentence-transformers/examples/training/other/training_batch_hard_trec.py
imports: from sentence_transformers import SentenceTransformer, LoggingHandler, losses, util
  from sentence_transformers.datasets import SentenceLabelDataset
  from torch.utils.data import DataLoader
  from sentence_transformers.readers import InputExample
  from sentence_transformers.evaluation import TripletEvaluator
  from datetime import datetime
  import logging
  import os
  import random
  from collections import defaultdict
label_desc: Use image node_id 2 for calling a global function with example usage: triplets_from_labeled_dataset(input_examples) and returns: triplets