forked from pkoukk/tiktoken-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoding.go
169 lines (154 loc) · 4.81 KB
/
encoding.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package tiktoken
import (
"errors"
)
const ENDOFTEXT string = "<|endoftext|>"
const FIM_PREFIX string = "<|fim_prefix|>"
const FIM_MIDDLE string = "<|fim_middle|>"
const FIM_SUFFIX string = "<|fim_suffix|>"
const ENDOFPROMPT string = "<|endofprompt|>"
var MODEL_TO_ENCODING = map[string]string{
// chat
"gpt-4": "cl100k_base",
"gpt-3.5-turbo": "cl100k_base",
// text
"text-davinci-003": "p50k_base",
"text-davinci-002": "p50k_base",
"text-davinci-001": "r50k_base",
"text-curie-001": "r50k_base",
"text-babbage-001": "r50k_base",
"text-ada-001": "r50k_base",
"davinci": "r50k_base",
"curie": "r50k_base",
"babbage": "r50k_base",
"ada": "r50k_base",
// code
"code-davinci-002": "p50k_base",
"code-davinci-001": "p50k_base",
"code-cushman-002": "p50k_base",
"code-cushman-001": "p50k_base",
"davinci-codex": "p50k_base",
"cushman-codex": "p50k_base",
// edit
"text-davinci-edit-001": "p50k_edit",
"code-davinci-edit-001": "p50k_edit",
// embeddings
"text-embedding-ada-002": "cl100k_base",
// old embeddings
"text-similarity-davinci-001": "r50k_base",
"text-similarity-curie-001": "r50k_base",
"text-similarity-babbage-001": "r50k_base",
"text-similarity-ada-001": "r50k_base",
"text-search-davinci-doc-001": "r50k_base",
"text-search-curie-doc-001": "r50k_base",
"text-search-babbage-doc-001": "r50k_base",
"text-search-ada-doc-001": "r50k_base",
"code-search-babbage-code-001": "r50k_base",
"code-search-ada-code-001": "r50k_base",
// open source
"gpt2": "gpt2",
}
var MODEL_PREFIX_TO_ENCODING = map[string]string{
// chat
"gpt-4-": "cl100k_base", // e.g., gpt-4-0314, etc., plus gpt-4-32k
"gpt-3.5-turbo-": "cl100k_base", // e.g, gpt-3.5-turbo-0301, -0401, etc.
}
type Encoding struct {
Name string
PatStr string
MergeableRanks map[string]int
SpecialTokens map[string]int
ExplicitNVocab int
}
func getEncoding(encodingName string) (*Encoding, error) {
encoding, ok := ENCODING_MAP[encodingName]
if !ok {
initEncoding, err := initEncoding(encodingName)
if err != nil {
return nil, err
}
encoding = initEncoding
ENCODING_MAP[encodingName] = encoding
}
return encoding, nil
}
func initEncoding(encodingName string) (*Encoding, error) {
switch encodingName {
case "cl100k_base":
return cl100k_base()
case "p50k_base":
return p50k_base()
case "r50k_base":
return r50k_base()
case "p50k_edit":
return p50k_edit()
default:
return nil, errors.New("Unknown encoding: " + encodingName)
}
}
func cl100k_base() (*Encoding, error) {
ranks, err := loadTiktokenBpe("https://openaipublic.blob.core.windows.net/encodings/cl100k_base.tiktoken")
if err != nil {
return nil, err
}
special_tokens := map[string]int{
ENDOFTEXT: 100257,
FIM_PREFIX: 100258,
FIM_MIDDLE: 100259,
FIM_SUFFIX: 100260,
ENDOFPROMPT: 100276,
}
return &Encoding{
Name: "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+`,
MergeableRanks: ranks,
SpecialTokens: special_tokens,
}, nil
}
func p50k_edit() (*Encoding, error) {
ranks, err := loadTiktokenBpe("https://openaipublic.blob.core.windows.net/encodings/p50k_base.tiktoken")
if err != nil {
return nil, err
}
special_tokens := map[string]int{ENDOFTEXT: 50256, FIM_PREFIX: 50281, FIM_MIDDLE: 50282, FIM_SUFFIX: 50283}
return &Encoding{
Name: "p50k_edit",
PatStr: `'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+`,
MergeableRanks: ranks,
SpecialTokens: special_tokens,
}, nil
}
func p50k_base() (*Encoding, error) {
ranks, err := loadTiktokenBpe("https://openaipublic.blob.core.windows.net/encodings/p50k_base.tiktoken")
if err != nil {
return nil, err
}
special_tokens := map[string]int{ENDOFTEXT: 50256}
// ExplicitNVocab := 50281
// max_tokens := int(math.Max(float64(len(special_tokens)), float64(len(ranks))))
// if len(special_tokens)+len(ranks) != max_tokens {
// return nil, errors.New("special_tokens and ranks must be disjoint")
// }
return &Encoding{
Name: "p50k_base",
PatStr: `'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+`,
MergeableRanks: ranks,
SpecialTokens: special_tokens,
ExplicitNVocab: 50281,
}, nil
}
func r50k_base() (*Encoding, error) {
ranks, err := loadTiktokenBpe("https://openaipublic.blob.core.windows.net/encodings/r50k_base.tiktoken")
if err != nil {
return nil, err
}
special_tokens := map[string]int{ENDOFTEXT: 50256}
return &Encoding{
Name: "r50k_base",
MergeableRanks: ranks,
PatStr: `'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+`,
SpecialTokens: special_tokens,
ExplicitNVocab: 50257,
}, nil
}
var ENCODING_MAP = map[string]*Encoding{}