Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions core_bpe.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,7 @@ type CoreBPE struct {
sortedTokenBytes [][]byte
}

func NewCoreBPE(encoder map[string]int, specialTokensEncoder map[string]int, pattern string) (*CoreBPE, error) {
regex, err := regexp2.Compile(pattern, regexp2.None)
if err != nil {
return nil, fmt.Errorf("error compiling regex: %s", err)
}

func NewCoreBPE(encoder map[string]int, specialTokensEncoder map[string]int, regex *regexp2.Regexp) (*CoreBPE, error) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The regex compilation needs to be known at compile time, which means that we need to compile the regex higher up the stack, hence the reason why we pass regexp2.Regexp here instead of a string.

specialRegexStrs := make([]string, 0, len(specialTokensEncoder))
for k := range specialTokensEncoder {
specialRegexStrs = append(specialRegexStrs, regexp.QuoteMeta(k))
Expand Down
29 changes: 13 additions & 16 deletions encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package tiktoken

import (
"errors"
"strings"
"sync"

"github.com/dlclark/regexp2"
)

const ENDOFTEXT string = "<|endoftext|>"
Expand Down Expand Up @@ -82,7 +83,7 @@ func init() {

type Encoding struct {
Name string
PatStr string
Pat *regexp2.Regexp
MergeableRanks map[string]int
SpecialTokens map[string]int
ExplicitNVocab int
Expand Down Expand Up @@ -128,18 +129,10 @@ func o200k_base() (*Encoding, error) {
ENDOFTEXT: 199999,
ENDOFPROMPT: 200018,
}
pats := []string{
`[^\r\n\p{L}\p{N}]?[\p{Lu}\p{Lt}\p{Lm}\p{Lo}\p{M}]*[\p{Ll}\p{Lm}\p{Lo}\p{M}]+(?i:'s|'t|'re|'ve|'m|'ll|'d)?`,
`[^\r\n\p{L}\p{N}]?[\p{Lu}\p{Lt}\p{Lm}\p{Lo}\p{M}]+[\p{Ll}\p{Lm}\p{Lo}\p{M}]*(?i:'s|'t|'re|'ve|'m|'ll|'d)?`,
`\p{N}{1,3}`,
` ?[^\s\p{L}\p{N}]+[\r\n/]*`,
`\s*[\r\n]+`,
`\s+(?!\S)`,
`\s+`,
}
reg := regexp2.MustCompile(`[^\r\n\p{L}\p{N}]?[\p{Lu}\p{Lt}\p{Lm}\p{Lo}\p{M}]*[\p{Ll}\p{Lm}\p{Lo}\p{M}]+(?i:'s|'t|'re|'ve|'m|'ll|'d)?|[^\r\n\p{L}\p{N}]?[\p{Lu}\p{Lt}\p{Lm}\p{Lo}\p{M}]+[\p{Ll}\p{Lm}\p{Lo}\p{M}]*(?i:'s|'t|'re|'ve|'m|'ll|'d)?|\p{N}{1,3}| ?[^\s\p{L}\p{N}]+[\r\n/]*|\s*[\r\n]+|\s+(?!\S)|\s+`, 0)
return &Encoding{
Name: MODEL_O200K_BASE,
PatStr: strings.Join(pats, "|"),
Pat: reg,
MergeableRanks: ranks,
SpecialTokens: special_tokens,
}, nil
Expand All @@ -157,9 +150,10 @@ func cl100k_base() (*Encoding, error) {
FIM_SUFFIX: 100260,
ENDOFPROMPT: 100276,
}
reg := regexp2.MustCompile(`(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\r\n\p{L}\p{N}]?\p{L}+|\p{N}{1,3}| ?[^\s\p{L}\p{N}]+[\r\n]*|\s*[\r\n]+|\s+(?!\S)|\s+`, 0)
return &Encoding{
Name: MODEL_CL100K_BASE,
PatStr: `(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\r\n\p{L}\p{N}]?\p{L}+|\p{N}{1,3}| ?[^\s\p{L}\p{N}]+[\r\n]*|\s*[\r\n]+|\s+(?!\S)|\s+`,
Pat: reg,
MergeableRanks: ranks,
SpecialTokens: special_tokens,
}, nil
Expand All @@ -171,9 +165,10 @@ func p50k_edit() (*Encoding, error) {
return nil, err
}
special_tokens := map[string]int{ENDOFTEXT: 50256, FIM_PREFIX: 50281, FIM_MIDDLE: 50282, FIM_SUFFIX: 50283}
reg := regexp2.MustCompile(`'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+`, 0)
return &Encoding{
Name: MODEL_P50K_EDIT,
PatStr: `'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+`,
Pat: reg,
MergeableRanks: ranks,
SpecialTokens: special_tokens,
}, nil
Expand All @@ -193,9 +188,10 @@ func p50k_base() (*Encoding, error) {
// return nil, errors.New("special_tokens and ranks must be disjoint")
// }

reg := regexp2.MustCompile(`'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+`, 0)
return &Encoding{
Name: MODEL_P50K_BASE,
PatStr: `'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+`,
Pat: reg,
MergeableRanks: ranks,
SpecialTokens: special_tokens,
ExplicitNVocab: 50281,
Expand All @@ -208,10 +204,11 @@ func r50k_base() (*Encoding, error) {
return nil, err
}
special_tokens := map[string]int{ENDOFTEXT: 50256}
reg := regexp2.MustCompile(`'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+`, 0)
return &Encoding{
Name: MODEL_R50K_BASE,
MergeableRanks: ranks,
PatStr: `'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+`,
Pat: reg,
SpecialTokens: special_tokens,
ExplicitNVocab: 50257,
}, nil
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/pkoukk/tiktoken-go
go 1.19

require (
github.com/dlclark/regexp2 v1.10.0
github.com/dlclark/regexp2 v1.11.5-0.20240806004527-5bbbed8ea10b
github.com/google/uuid v1.3.0
github.com/stretchr/testify v1.8.2
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dlclark/regexp2 v1.10.0 h1:+/GIL799phkJqYW+3YbOd8LCcbHzT0Pbo8zl70MHsq0=
github.com/dlclark/regexp2 v1.10.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/dlclark/regexp2 v1.11.5-0.20240806004527-5bbbed8ea10b h1:AJKOdc+1fRSJ0/75Jty1npvxUUD0y7hQDg15LMAHhyU=
github.com/dlclark/regexp2 v1.11.5-0.20240806004527-5bbbed8ea10b/go.mod h1:YvCrhrh/qlds8EhFKPtJprdXn5fWBllSw1qo99dZyiQ=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
Loading