-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokens.go
38 lines (35 loc) · 851 Bytes
/
tokens.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
package main
import (
"context"
"fmt"
"github.com/newhook/whoishiring/queries"
"github.com/pkoukk/tiktoken-go"
tiktoken_loader "github.com/pkoukk/tiktoken-go-loader"
)
func PrintTokens(ctx context.Context, q *queries.Queries) error {
encoding := "cl100k_base"
tiktoken.SetBpeLoader(tiktoken_loader.NewOfflineLoader())
tke, err := tiktoken.GetEncoding(encoding)
if err != nil {
return err
}
posts, err := q.GetItemsWithTitle(ctx, whoIsHiring)
if err != nil {
return err
}
total := 0
for _, post := range posts[:MaxWindow] {
children, err := q.GetItemsForParent(ctx, post.ID)
if err != nil {
return err
}
for _, c := range children {
// encode
token := tke.Encode(c.Text, nil, nil)
total += len(token)
//fmt.Println(c.ID, "has", len(token), "tokens")
}
}
fmt.Println("total tokens", total)
return nil
}