-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
71 lines (54 loc) · 2.31 KB
/
example_test.go
File metadata and controls
71 lines (54 loc) · 2.31 KB
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
package tok_test
import (
"fmt"
"github.com/GrayCodeAI/tok"
)
func ExampleCompress() {
text := `This is a very long piece of text that contains redundant information.
The purpose of this text is to demonstrate how tok can compress it.
We repeat the same ideas multiple times to inflate the token count.
The key insight is that tok uses entropy-based filtering to remove low-information tokens.
This allows us to reduce the token count while preserving the core meaning.`
compressed, stats := tok.Compress(text)
fmt.Printf("Original tokens: %d\n", stats.OriginalTokens)
fmt.Printf("Final tokens: %d\n", stats.FinalTokens)
fmt.Printf("Tokens saved: %d\n", stats.TokensSaved)
fmt.Printf("Reduction: %.1f%%\n", stats.ReductionPercent)
fmt.Printf("Compressed text length: %d\n", len(compressed))
}
func ExampleEstimateTokens() {
text := "Hello, how are you today?"
// Fast heuristic estimate
count := tok.EstimateTokens(text)
fmt.Printf("Estimated tokens: %d\n", count)
}
func ExampleNewCompressor() {
// Create a reusable compressor for multiple calls
c := tok.NewCompressor(
tok.WithBudget(1000),
)
text1 := "First document to compress..."
text2 := "Second document to compress..."
_, stats1 := c.Compress(text1)
_, stats2 := c.Compress(text2)
fmt.Printf("Doc 1 saved: %d tokens\n", stats1.TokensSaved)
fmt.Printf("Doc 2 saved: %d tokens\n", stats2.TokensSaved)
}
func ExampleCompress_withBudget() {
text := `Long document with many details that we want to fit within a token budget.
We can use the budget option to limit the output to a specific number of tokens.
This is useful when working with context windows that have strict limits.`
compressed, stats := tok.Compress(text, tok.WithBudget(50))
fmt.Printf("Final tokens: %d (budget: 50)\n", stats.FinalTokens)
fmt.Printf("Compressed text: %s\n", compressed[:50]+"...")
}
func ExampleCompress_withQuery() {
text := `The application uses a REST API with JSON payloads.
Database migrations are handled by the migration package.
The frontend is built with React and TypeScript.
Authentication uses JWT tokens with refresh token rotation.
The CI/CD pipeline runs on GitHub Actions.`
// Query-aware compression preserves content relevant to the query
_, stats := tok.Compress(text, tok.WithQuery("authentication"))
fmt.Printf("Tokens saved: %d\n", stats.TokensSaved)
}