| 595 | |
| 596 | @dataclass |
| 597 | class LlamaSamplingContext: |
| 598 | params: LlamaSamplingParams = field(default_factory=LlamaSamplingParams) |
| 599 | mirostat_mu: ctypes.c_float = field(default_factory=ctypes.c_float) |
| 600 | grammar: Optional[LlamaGrammar] = None |
| 601 | # NOTE: Missing parsed_grammar |
| 602 | prev: list[int] = field(default_factory=list) |
| 603 | cur: list[llama_cpp.llama_token_data] = field(default_factory=list) |
| 604 | |
| 605 | def reset(self): |
| 606 | self.prev = [] |
| 607 | self.cur = [] |
| 608 | if self.grammar is not None: |
| 609 | self.grammar.reset() |
| 610 | |
| 611 | def cp(self): |
| 612 | return LlamaSamplingContext( |
| 613 | params=self.params, |
| 614 | mirostat_mu=self.mirostat_mu, |
| 615 | grammar=self.grammar, |
| 616 | prev=self.prev.copy(), |
| 617 | cur=self.cur.copy(), |
| 618 | ) |
| 619 | |
| 620 | def last(self) -> Optional[int]: |
| 621 | if len(self.prev) > 0: |
| 622 | return self.prev[-1] |
| 623 | else: |
| 624 | return None |
| 625 | |
| 626 | def prev_str(self, ctx_main: LlamaContext, n: int) -> str: |
| 627 | return ctx_main.model.detokenize(self.prev[-n:]).decode("utf-8") |
| 628 | |
| 629 | def sample( |
| 630 | self, |
| 631 | ctx_main: LlamaContext, |
| 632 | idx: int = 0, |
| 633 | logits_array: Optional[npt.NDArray[np.single]] = None, |
| 634 | ): |
| 635 | # This method is deprecated in favor of using LlamaSampler directly |
| 636 | raise NotImplementedError( |
| 637 | "LlamaSamplingContext.sample is deprecated, use LlamaSampler instead" |
| 638 | ) |
| 639 | |
| 640 | def accept(self, ctx_main: LlamaContext, id: int, apply_grammar: bool): |
| 641 | self.prev.append(id) |
| 642 | |
| 643 | |
| 644 | class CustomSampler: |
no outgoing calls
no test coverage detected
searching dependent graphs…