MCPcopy Create free account
hub / github.com/OpenBitSys/BitDistiller / fewshot_context

Method fewshot_context

test/general/lm_eval/tasks/xnli.py:148–215  ·  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, provide_description=None, rnd=None, description=None
    )

Source from the content-addressed store, hash-verified

146
147 @utils.positional_deprecated
148 def fewshot_context(
149 self, doc, num_fewshot, provide_description=None, rnd=None, description=None
150 ):
151 """Returns a fewshot context string that is made up of a prepended description
152 (if provided), the `num_fewshot` number of examples, and an appended prompt example.
153
154 :param doc: str
155 The document as returned from training_docs, validation_docs, or test_docs.
156 :param num_fewshot: int
157 The number of fewshot examples to provide in the returned context string.
158 :param provide_description: bool
159 Not implemented, and this option is deprecated and will be removed in a future version in favor of a different description providing method
160 :param rnd: random.Random
161 The pseudo-random number generator used to randomly sample examples.
162 WARNING: This is currently a required arg although it's optionalized with a default `None`.
163 :param description: str
164 The task's description that will be prepended to the fewshot examples.
165 :returns: str
166 The fewshot context.
167 """
168 assert (
169 rnd is not None
170 ), "A `random.Random` generator argument must be provided to `rnd`"
171 assert not provide_description, (
172 "The `provide_description` arg will be removed in future versions. To prepend "
173 "a custom description to the context, supply the corresponding string via the "
174 "`description` arg."
175 )
176 if provide_description is not None:
177 # nudge people to not specify it at all
178 print(
179 "WARNING: provide_description is deprecated and will be removed in a future version in favor of description_dict"
180 )
181
182 description = description + "\n\n" if description else ""
183
184 if num_fewshot == 0:
185 labeled_examples = ""
186 else:
187 # for sets with no training docs, draw from other set *but ensure no overlap with current doc*
188 if self.has_training_docs():
189 fewshotex = self.fewshot_examples(k=num_fewshot, rnd=rnd)
190 else:
191 if self._fewshot_docs is None:
192 self._fewshot_docs = list(
193 self.validation_docs()
194 if self.has_validation_docs()
195 else self.test_docs()
196 )
197
198 fewshotex = rnd.sample(self._fewshot_docs, num_fewshot + 1)
199
200 # get rid of the doc that's the one we're evaluating, if it's in the fewshot
201 fewshotex = [x for x in fewshotex if x != doc][:num_fewshot]
202
203 labeled_examples = (
204 "\n\n".join(
205 [

Callers

nothing calls this directly

Calls 7

has_training_docsMethod · 0.95
validation_docsMethod · 0.95
has_validation_docsMethod · 0.95
test_docsMethod · 0.95
doc_to_fewshot_promptMethod · 0.95
doc_to_textMethod · 0.95
fewshot_examplesMethod · 0.45

Tested by

no test coverage detected