MCPcopy Index your code
hub / github.com/abetlen/llama-cpp-python / tokenize

Method tokenize

examples/server/server.py:11934–11958  ·  view source on GitHub ↗
(
        self,
        text: str,
        *,
        add_bos: bool = True,
        special: bool = True,
    )

Source from the content-addressed store, hash-verified

11932 )
11933
11934 def tokenize(
11935 self,
11936 text: str,
11937 *,
11938 add_bos: bool = True,
11939 special: bool = True,
11940 ) -> List[int]:
11941 encoded = text.encode("utf-8")
11942 capacity = max(32, len(encoded) + 32)
11943 while True:
11944 tokens = (llama_cpp.llama_token * capacity)()
11945 count = int(
11946 llama_cpp.llama_tokenize(
11947 self.vocab,
11948 encoded,
11949 len(encoded),
11950 tokens,
11951 capacity,
11952 add_bos,
11953 special,
11954 )
11955 )
11956 if count >= 0:
11957 return [int(tokens[index]) for index in range(count)]
11958 capacity = max(capacity * 2, -count)
11959
11960 def build_prompt_tokens(self, prompt: str, suffix: Optional[str]) -> List[int]:
11961 if suffix is None:

Callers 5

build_prompt_tokensMethod · 0.95
build_chat_promptMethod · 0.95
embedMethod · 0.95
build_prompt_planMethod · 0.45

Calls 1

encodeMethod · 0.45

Tested by

no test coverage detected