MCPcopy Create free account
hub / github.com/abetlen/llama-cpp-python / LlamaSamplingContext

Class LlamaSamplingContext

llama_cpp/_internals.py:597–641  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

595
596@dataclass
597class 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
644class CustomSampler:

Callers 1

cpMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…