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=None, description=None, **kwargs)
| 445 | |
| 446 | @utils.positional_deprecated |
| 447 | def fewshot_context(self, doc, num_fewshot, rnd=None, description=None, **kwargs): |
| 448 | """Returns a fewshot context string that is made up of a prepended description |
| 449 | (if provided), the `num_fewshot` number of examples, and an appended prompt example. |
| 450 | |
| 451 | :param doc: str |
| 452 | The document as returned from training_docs, validation_docs, or test_docs. |
| 453 | :param num_fewshot: int |
| 454 | The number of fewshot examples to provide in the returned context string. |
| 455 | :param rnd: random.Random |
| 456 | The pseudo-random number generator used to randomly sample examples. |
| 457 | WARNING: This is currently a required arg although it's optionalized with a default `None`. |
| 458 | :param description: str |
| 459 | The task's description that will be prepended to the fewshot examples. |
| 460 | :returns: str |
| 461 | The fewshot context. |
| 462 | """ |
| 463 | if rnd is None: |
| 464 | if self.fewshot_rnd is not None: |
| 465 | rnd = self.fewshot_rnd |
| 466 | else: |
| 467 | raise ValueError( |
| 468 | "A `random.Random` generator argument must be provided to `rnd`" |
| 469 | ) |
| 470 | |
| 471 | description = description or "" |
| 472 | |
| 473 | if num_fewshot == 0: |
| 474 | labeled_examples = "" |
| 475 | else: |
| 476 | # for sets with no training docs, draw from other set *but ensure no overlap with current doc* |
| 477 | if self.has_training_docs(): |
| 478 | fewshotex = self.fewshot_examples(k=num_fewshot, rnd=rnd) |
| 479 | else: |
| 480 | if self._fewshot_docs is None: |
| 481 | self._fewshot_docs = list( |
| 482 | self.validation_docs() |
| 483 | if self.has_validation_docs() |
| 484 | else self.test_docs() |
| 485 | ) |
| 486 | |
| 487 | fewshotex = rnd.sample(self._fewshot_docs, num_fewshot + 1) |
| 488 | |
| 489 | # get rid of the doc that's the one we're evaluating, if it's in the fewshot |
| 490 | fewshotex = [x for x in fewshotex if x != doc][:num_fewshot] |
| 491 | |
| 492 | labeled_examples = ( |
| 493 | "\n\n".join( |
| 494 | [ |
| 495 | self.doc_to_text(doc) + self.doc_to_target(doc) |
| 496 | for doc in fewshotex |
| 497 | ] |
| 498 | ) |
| 499 | + "\n\n" |
| 500 | ) |
| 501 | |
| 502 | example = self.doc_to_text(doc) |
| 503 | return description + labeled_examples + example |
| 504 |