MCPcopy Create free account
hub / github.com/M4THYOU/TokenDagger / PYBIND11_MODULE

Function PYBIND11_MODULE

src/py_binding.cpp:7–52  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5// Include your tokenizer headers here
6
7PYBIND11_MODULE(_tokendagger_core, m) {
8 m.doc() = "TokenDagger low-level C++ bindings for tiktoken";
9
10 // Bind VocabItem struct
11 pybind11::class_<VocabItem>(m, "VocabItem")
12 .def(pybind11::init<>())
13 .def_readwrite("rank", &VocabItem::rank)
14 .def_readwrite("token_bytes", &VocabItem::token_bytes)
15 .def_readwrite("token_string", &VocabItem::token_string);
16
17 // Bind TiktokenError exception
18 pybind11::register_exception<tiktoken::TiktokenError>(m, "TiktokenError");
19
20 // Bind CoreBPE class
21 pybind11::class_<tiktoken::CoreBPE>(m, "CoreBPE")
22 .def(pybind11::init<const std::string&, const std::vector<VocabItem>&, const std::vector<VocabItem>&>(),
23 "Initialize CoreBPE with pattern, vocabulary, and special vocabulary",
24 pybind11::arg("pattern"), pybind11::arg("vocab"), pybind11::arg("special_vocab"))
25 .def("encode_ordinary", [](tiktoken::CoreBPE& self, const std::string& text) {
26 pybind11::gil_scoped_release release;
27 return self.encode_ordinary(text);
28 }, "Encode text using ordinary BPE tokens only",
29 pybind11::arg("text"))
30 .def("encode", [](tiktoken::CoreBPE& self, const std::string& text, const std::set<std::string>& allowed_special) {
31 // Convert std::set to emhash8::HashSet
32 pybind11::gil_scoped_release release;
33 emhash8::HashSet<std::string> allowed_set;
34 for (const auto& token : allowed_special) {
35 allowed_set.insert(token);
36 }
37 return self.encode(text, allowed_set);
38 }, "Encode text with allowed special tokens",
39 pybind11::arg("text"), pybind11::arg("allowed_special"))
40 .def("decode_bytes", [](tiktoken::CoreBPE& self, const std::vector<int>& tokens) {
41 pybind11::gil_scoped_release release;
42 return self.decode_bytes(tokens);
43 }, "Decode tokens back to bytes",
44 pybind11::arg("tokens"))
45 .def("special_tokens", &tiktoken::CoreBPE::special_tokens,
46 "Get list of special tokens")
47 .def("encode_with_special_tokens", &tiktoken::CoreBPE::encode_with_special_tokens,
48 "Encode text including special tokens",
49 pybind11::arg("text"));
50
51 // Add other functions as needed
52}

Callers

nothing calls this directly

Calls 4

encode_ordinaryMethod · 0.45
insertMethod · 0.45
encodeMethod · 0.45
decode_bytesMethod · 0.45

Tested by

no test coverage detected