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

Method longest_prefix

examples/server/server.py:1001–1025  ·  view source on GitHub ↗
(
        self,
        tokens: Sequence[int],
        preferred_sequences: Optional[Any] = None,
        *,
        exact_only: bool = False,
    )

Source from the content-addressed store, hash-verified

999 return values[:target_len]
1000
1001 def longest_prefix(
1002 self,
1003 tokens: Sequence[int],
1004 preferred_sequences: Optional[Any] = None,
1005 *,
1006 exact_only: bool = False,
1007 ) -> Tuple[int, int]:
1008 node = self.root
1009 longest_sequence_id = -1
1010 longest_length = 0
1011 index = 0
1012 while index < len(tokens):
1013 child = node.children.get(tokens[index])
1014 if child is None:
1015 break
1016 match_len = self._common_prefix_len(child.label, tokens, index)
1017 if match_len < len(child.label):
1018 break
1019 node = child
1020 index += match_len
1021 candidates = node.tails if exact_only else node.sequences
1022 if candidates:
1023 longest_sequence_id = self._pick_sequence(candidates, preferred_sequences)
1024 longest_length = index
1025 return longest_sequence_id, longest_length
1026
1027
1028class SequenceHistory:

Callers 3

lookupMethod · 0.80
match_prefixMethod · 0.80

Calls 2

_common_prefix_lenMethod · 0.95
_pick_sequenceMethod · 0.95

Tested by

no test coverage detected