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

Class Token

examples/server/server.py:3723–3803  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3721
3722@dataclass
3723class Token:
3724 token: int
3725 text_bytes: bytes
3726 token_logprob: Optional[float]
3727 top_logprobs: Optional[Dict[str, float]]
3728
3729 @classmethod
3730 def from_token(
3731 cls,
3732 *,
3733 model: Model,
3734 prev_tokens: Sequence[int],
3735 prev_text_bytes: Optional[Union[bytes, bytearray]] = None,
3736 token: int,
3737 ) -> "Token":
3738 return cls(
3739 token=token,
3740 text_bytes=(
3741 model.token_bytes_with_prev_bytes(prev_tokens, prev_text_bytes, token)
3742 if prev_text_bytes is not None
3743 else model.token_bytes_with_prev(prev_tokens, token)
3744 ),
3745 token_logprob=None,
3746 top_logprobs=None,
3747 )
3748
3749 @classmethod
3750 def from_logits(
3751 cls,
3752 *,
3753 model: Model,
3754 formatter: OpenAIFormatter,
3755 prev_tokens: Sequence[int],
3756 prev_text_bytes: Optional[Union[bytes, bytearray]] = None,
3757 token: int,
3758 logits: np.ndarray,
3759 logprobs_count: Optional[int],
3760 need_token_logprob: bool = False,
3761 ) -> "Token":
3762 text_bytes = (
3763 model.token_bytes_with_prev_bytes(prev_tokens, prev_text_bytes, token)
3764 if prev_text_bytes is not None
3765 else model.token_bytes_with_prev(prev_tokens, token)
3766 )
3767 if not model.store_logits:
3768 return cls(
3769 token=token,
3770 text_bytes=text_bytes,
3771 token_logprob=None,
3772 top_logprobs=None,
3773 )
3774 if logprobs_count is None and not need_token_logprob:
3775 return cls(
3776 token=token,
3777 text_bytes=text_bytes,
3778 token_logprob=None,
3779 top_logprobs=None,
3780 )

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…