code: def generation_metric(
      hypos, refs, metric: Optional[str] = "sentence_bleu"
  ):
      r"""Some basic metric function for generation. However, many generation tasks
      has their own evaluation bash scripts.
  
      Args:
          hypos (:obj:`str`) : the generated sentence.
          refs (:obj:`list(str)`) : the referenced (ground-truth) sentence.
          metric (:obj:`str`, `optional`) : the type of metric option
  
      Returns:
          score (float): evaluate score
      """
      if metric == "sentence_bleu":
          # a simple criterion to visualize the performance, not rigorous.
          import nltk
  
          try:
              nltk_path = str(nltk.data.find("tokenizers/punkt"))
              logger.info(f"using nltk from: {nltk_path}")
          except LookupError:
              nltk.download("punkt")
  
          from nltk.translate.bleu_score import sentence_bleu
          from nltk.tokenize import word_tokenize
          from nltk.translate.bleu_score import SmoothingFunction
  
          smoothie = (
              SmoothingFunction().method4
          )  # a function for smooth
          scores = []
  
          for ref, hypo in zip(refs, hypos):
              tokenized_rs = []
              ref = ref.split("\n")
              for r in ref:
                  tokenized_rs.append(word_tokenize(r))
              hypo = word_tokenize(hypo)
              try:
                  sc = sentence_bleu(
                      tokenized_rs, hypo, smoothing_function=smoothie
                  )
              except ValueError:  # TODO ZeroDivisionError
                  logger.warning(
                      "math domain error in bleu, set to 0.0. generated sentence: {}".format(
                          hypo
                      )
                  )
                  sc = 0.0
              scores.append(sc)
          score = sum(scores) / len(scores)
          return score
      else:
          raise ValueError(
              "'{}' is not a valid metric type.".format(metric)
          )
  
file_path: OpenPrompt/openprompt/utils/metrics.py
imports: from sklearn.metrics import f1_score, precision_score, recall_score, accuracy_score
  from typing import *
  from openprompt.utils.logging import logger
label_desc: Use image node_id 6 for calling a global function with example usage: generation_metric(hypos, refs, metric) and returns: score