-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
117 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package tiktoken | ||
|
||
import ( | ||
_ "embed" | ||
"encoding/base64" | ||
"encoding/json" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
//go:embed resource/claude.json | ||
var claude string | ||
|
||
type claudeJSON struct { | ||
ExplicitNVocab int `json:"explicit_n_vocab"` | ||
PatStr string `json:"pat_str"` | ||
BPERanks string `json:"bpe_ranks"` | ||
SpecialTokens map[string]uint `json:"special_tokens"` | ||
} | ||
|
||
// NewClaude creates a new Codec instance for the claude tokenization scheme. | ||
// It loads the mergeable ranks from the embedded claude resource. | ||
// The function returns a pointer to the Codec or an error if any. | ||
func NewClaude() (*Codec, error) { | ||
c := claudeJSON{} | ||
if err := json.Unmarshal([]byte(claude), &c); err != nil { | ||
return nil, err | ||
} | ||
|
||
parts := strings.SplitN(c.BPERanks, " ", 3) | ||
|
||
offset, err := strconv.Atoi(parts[1]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
tokens := strings.Split(parts[2], " ") | ||
|
||
mergeableRanks := make(map[string]uint, len(tokens)) | ||
|
||
for i, token := range tokens { | ||
t, bErr := base64.StdEncoding.DecodeString(token) | ||
if bErr != nil { | ||
return nil, bErr | ||
} | ||
|
||
mergeableRanks[string(t)] = uint(i * offset) | ||
} | ||
|
||
return &Codec{ | ||
Name: "claude", | ||
ExplicitNVocab: c.ExplicitNVocab, | ||
PatStr: c.PatStr, | ||
MergeableRanks: mergeableRanks, | ||
SpecialTokens: c.SpecialTokens, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package tiktoken | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestClaude(t *testing.T) { | ||
claude, err := NewClaude() | ||
require.NoError(t, err) | ||
|
||
encoding, err := NewEncoding(claude) | ||
require.NoError(t, err) | ||
|
||
t.Run("small text", func(t *testing.T) { | ||
idx, _ := encoding.EncodeOrdinary("hello world!") | ||
require.Equal(t, 3, len(idx)) | ||
}) | ||
|
||
t.Run("small text", func(t *testing.T) { | ||
idx, _ := encoding.EncodeOrdinary("hello world!") | ||
require.Equal(t, 3, len(idx)) | ||
}) | ||
|
||
t.Run("text normalising", func(t *testing.T) { | ||
idx, _ := encoding.EncodeOrdinary("™") | ||
assert.Equal(t, 1, len(idx)) | ||
|
||
idx, _ = encoding.EncodeOrdinary("ϰ") | ||
assert.Equal(t, 1, len(idx)) | ||
}) | ||
|
||
t.Run("allows special tokens", func(t *testing.T) { | ||
idx, _, err := encoding.Encode("<EOT>", AllSpecial, nil) | ||
require.NoError(t, err) | ||
require.Equal(t, 1, len(idx)) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.