-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_tokenization.go
53 lines (44 loc) · 1.03 KB
/
simple_tokenization.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"fmt"
"log"
bpe "github.com/edit4i/gh-bpe-openai-go"
)
func main() {
// Initialize the tokenizer with CL100k model
tokenizer, err := bpe.NewCL100kTokenizer()
if err != nil {
log.Fatal(err)
}
// Example texts to tokenize
texts := []string{
"Hello, this is a test of the OpenAI tokenizer!",
"Hello 👋 World 🌍", // Unicode example
"", // Empty string
}
for _, text := range texts {
fmt.Printf("\nProcessing text: %q\n", text)
// Count tokens
count, err := tokenizer.Count(text)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Token count: %d\n", count)
// Encode the text
tokens, err := tokenizer.Encode(text)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Tokens: %v\n", tokens)
// Decode back to text
decoded, err := tokenizer.Decode(tokens)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Decoded: %q\n", decoded)
// Verify round-trip
if decoded != text {
fmt.Printf("Warning: Round-trip encoding/decoding produced different result!\n")
}
}
}