-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcompact_integration_test.go
More file actions
136 lines (115 loc) · 3.86 KB
/
Copy pathcompact_integration_test.go
File metadata and controls
136 lines (115 loc) · 3.86 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
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
package gobed
import (
"strings"
"testing"
)
func TestCompactIntegration(t *testing.T) {
model, err := LoadSimpleInt8Model512()
if err != nil {
t.Skipf("model not available: %v", err)
}
text := "Machine learning is a subset of artificial intelligence. " +
"It enables computers to learn from data without explicit programming. " +
"Deep learning uses neural networks with many layers. " +
"These networks excel at image recognition and natural language processing. " +
"The field has grown rapidly since 2012 when AlexNet won ImageNet. " +
"Transfer learning allows reusing pretrained models on new tasks. " +
"Transformers revolutionized NLP with attention mechanisms. " +
"GPT and BERT are examples of transformer-based models. " +
"Reinforcement learning trains agents through reward signals. " +
"Computer vision applies deep learning to visual data."
sentences := SentenceSplit(text)
if len(sentences) != 10 {
t.Fatalf("expected 10 sentences, got %d", len(sentences))
}
totalTokens := 0
for _, s := range sentences {
totalTokens += EstimateTokens(s)
}
t.Logf("total estimated tokens: %d", totalTokens)
// compact to ~half
budget := totalTokens / 2
result, err := Compact(model, text, budget, "deep learning and neural networks")
if err != nil {
t.Fatalf("compact failed: %v", err)
}
resultTokens := EstimateTokens(result)
t.Logf("compacted to %d tokens (budget %d): %s", resultTokens, budget, result)
if resultTokens > budget+5 { // small tolerance
t.Errorf("result %d tokens exceeds budget %d", resultTokens, budget)
}
if len(result) == 0 {
t.Error("result is empty")
}
// verify it contains relevant content about deep learning
lower := strings.ToLower(result)
if !strings.Contains(lower, "deep learning") && !strings.Contains(lower, "neural") {
t.Logf("warning: compacted text may not contain most relevant sentences for query")
}
}
func TestCompactIntegration_NoQuery(t *testing.T) {
model, err := LoadSimpleInt8Model512()
if err != nil {
t.Skipf("model not available: %v", err)
}
text := "Apples are red. Bananas are yellow. Oranges are orange. Grapes are purple. Strawberries are red."
result, err := Compact(model, text, 10, "")
if err != nil {
t.Fatalf("compact failed: %v", err)
}
t.Logf("compacted (no query): %s", result)
if len(result) == 0 {
t.Error("result is empty")
}
}
func TestCompactIntegration_AlreadyFits(t *testing.T) {
model, err := LoadSimpleInt8Model512()
if err != nil {
t.Skipf("model not available: %v", err)
}
text := "Short text."
result, err := Compact(model, text, 10000, "anything")
if err != nil {
t.Fatalf("compact failed: %v", err)
}
if result != "Short text." {
t.Errorf("expected passthrough, got %q", result)
}
}
func TestCompactIntegration_VeryTightBudget(t *testing.T) {
model, err := LoadSimpleInt8Model512()
if err != nil {
t.Skipf("model not available: %v", err)
}
text := "First sentence is here. Second sentence follows. Third one too. Fourth is last."
result, err := Compact(model, text, 5, "first")
if err != nil {
t.Fatalf("compact failed: %v", err)
}
t.Logf("tight budget result: %q", result)
if len(result) == 0 {
t.Error("should return at least something")
}
}
func BenchmarkCompact(b *testing.B) {
model, err := LoadSimpleInt8Model512()
if err != nil {
b.Skipf("model not available: %v", err)
}
text := strings.Repeat("Machine learning enables computers to learn. Deep learning uses neural networks. "+
"Transformers use attention. Models can be pretrained. Transfer learning is powerful. ", 20)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := Compact(model, text, 50, "neural networks")
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkSentenceSplit(b *testing.B) {
text := strings.Repeat("This is a sentence. Another one follows! Is this a question? Yes; it is. ", 100)
b.ResetTimer()
for i := 0; i < b.N; i++ {
SentenceSplit(text)
}
}