MCPcopy Create free account
hub / github.com/OpenSparseLLMs/MoM / fewshot_context

Method fewshot_context

lm-eval-harness/lm_eval/api/task.py:555–613  ·  view source on GitHub ↗

Returns a fewshot context string that is made up of a prepended description (if provided), the `num_fewshot` number of examples, and an appended prompt example. :param doc: str The document as returned from training_docs, validation_docs, or test_docs. :param num

(
        self,
        doc,
        num_fewshot,
        rnd=random.Random(1234),
        description=None,
    )

Source from the content-addressed store, hash-verified

553
554 @utils.positional_deprecated
555 def fewshot_context(
556 self,
557 doc,
558 num_fewshot,
559 rnd=random.Random(1234),
560 description=None,
561 ):
562 """Returns a fewshot context string that is made up of a prepended description
563 (if provided), the `num_fewshot` number of examples, and an appended prompt example.
564
565 :param doc: str
566 The document as returned from training_docs, validation_docs, or test_docs.
567 :param num_fewshot: int
568 The number of fewshot examples to provide in the returned context string.
569 :param rnd: random.Random
570 The pseudo-random number generator used to randomly sample examples.
571 WARNING: This is currently a required arg although it's optionalized with a default `None`.
572 :param description: str
573 The task's description that will be prepended to the fewshot examples.
574 :returns: str
575 The fewshot context.
576 """
577 assert (
578 rnd is not None
579 ), "A `random.Random` generator argument must be provided to `rnd`"
580
581 description = description if description else ""
582
583 if num_fewshot == 0:
584 labeled_examples = ""
585 else:
586 # for sets with no training docs, draw from other set *but ensure no overlap with current doc*
587 if self.has_training_docs():
588 fewshotex = self.fewshot_examples(k=num_fewshot, rnd=rnd)
589 else:
590 if self._fewshot_docs is None:
591 self._fewshot_docs = list(
592 self.validation_docs()
593 if self.has_validation_docs()
594 else self.test_docs()
595 )
596
597 fewshotex = rnd.sample(self._fewshot_docs, num_fewshot + 1)
598
599 # get rid of the doc that's the one we're evaluating, if it's in the fewshot
600 fewshotex = [x for x in fewshotex if x != doc][:num_fewshot]
601
602 labeled_examples = (
603 "\n\n".join(
604 [
605 self.doc_to_text(doc) + self.doc_to_target(doc)
606 for doc in fewshotex
607 ]
608 )
609 + "\n\n"
610 )
611
612 example = self.doc_to_text(doc)

Callers 2

truncate_contextMethod · 0.95
build_all_requestsMethod · 0.95

Calls 8

has_training_docsMethod · 0.95
fewshot_examplesMethod · 0.95
validation_docsMethod · 0.95
has_validation_docsMethod · 0.95
test_docsMethod · 0.95
doc_to_textMethod · 0.95
doc_to_targetMethod · 0.95
sampleMethod · 0.45

Tested by

no test coverage detected