| 1 |
// Copyright 2024 Google LLC |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
package tokenizer |
| 15 |
|
| 16 |
import ( |
| 17 |
"fmt" |
| 18 |
"os" |
| 19 |
"path/filepath" |
| 20 |
"testing" |
| 21 |
|
| 22 |
"cloud.google.com/go/vertexai/genai" |
| 23 |
) |
| 24 |
|
| 25 |
func TestDownload(t *testing.T) { |
| 26 |
b, err := downloadModelFile(gemmaModelURL) |
| 27 |
if err != nil { |
| 28 |
t.Fatal(err) |
| 29 |
} |
| 30 |
|
| 31 |
if hashString(b) != gemmaModelHash { |
| 32 |
t.Errorf("gemma model hash doesn't match") |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
func TestLoadModelData(t *testing.T) { |
| 37 |
// Tests that loadModelData manages to load the model properly, and download |
| 38 |
// a new one as needed. |
| 39 |
checkDataAndErr := func(data []byte, err error) { |
| 40 |
t.Helper() |
| 41 |
if err != nil { |
| 42 |
t.Error(err) |
| 43 |
} |
| 44 |
gotHash := hashString(data) |
| 45 |
if gotHash != gemmaModelHash { |
| 46 |
t.Errorf("got hash=%v, want=%v", gotHash, gemmaModelHash) |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
data, err := loadModelData(gemmaModelURL, gemmaModelHash) |
| 51 |
checkDataAndErr(data, err) |
| 52 |
|
| 53 |
// The cache should exist now and have the right data, try again. |
| 54 |
data, err = loadModelData(gemmaModelURL, gemmaModelHash) |
| 55 |
checkDataAndErr(data, err) |
| 56 |
|
| 57 |
// Overwrite cache file with wrong data, and try again. |
| 58 |
cacheDir := filepath.Join(os.TempDir(), "vertexai_tokenizer_model") |
| 59 |
cachePath := filepath.Join(cacheDir, hashString([]byte(gemmaModelURL))) |
| 60 |
_ = os.MkdirAll(cacheDir, 0770) |
| 61 |
_ = os.WriteFile(cachePath, []byte{0, 1, 2, 3}, 0660) |
| 62 |
data, err = loadModelData(gemmaModelURL, gemmaModelHash) |
| 63 |
checkDataAndErr(data, err) |
| 64 |
} |
| 65 |
|
| 66 |
func TestCreateTokenizer(t *testing.T) { |
| 67 |
// Create a tokenizer successfully |
| 68 |
_, err := New("gemini-1.5-flash") |
| 69 |
if err != nil { |
| 70 |
t.Error(err) |
| 71 |
} |
| 72 |
|
| 73 |
// Create a tokenizer with an unsupported model |
| 74 |
_, err = New("gemini-0.92") |
| 75 |
if err == nil { |
| 76 |
t.Errorf("got no error, want error") |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
func TestCountTokens(t *testing.T) { |
| 81 |
var tests = []struct { |
| 82 |
parts []genai.Part |
| 83 |
wantCount int32 |
| 84 |
}{ |
| 85 |
{[]genai.Part{genai.Text("hello world")}, 2}, |
| 86 |
{[]genai.Part{genai.Text("<table><th></th></table>")}, 4}, |
| 87 |
{[]genai.Part{genai.Text("hello world"), genai.Text("<table><th></th></table>")}, 6}, |
| 88 |
} |
| 89 |
|
| 90 |
tok, err := New("gemini-1.5-flash") |
| 91 |
if err != nil { |
| 92 |
t.Error(err) |
| 93 |
} |
| 94 |
|
| 95 |
for i, tt := range tests { |
| 96 |
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { |
| 97 |
got, err := tok.CountTokens(tt.parts...) |
| 98 |
if err != nil { |
| 99 |
t.Error(err) |
| 100 |
} |
| 101 |
if got.TotalTokens != tt.wantCount { |
| 102 |
t.Errorf("got %v, want %v", got.TotalTokens, tt.wantCount) |
| 103 |
} |
| 104 |
}) |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
func TestCountTokensNonText(t *testing.T) { |
| 109 |
tok, err := New("gemini-1.5-flash") |
| 110 |
if err != nil { |
| 111 |
t.Error(err) |
| 112 |
} |
| 113 |
|
| 114 |
_, err = tok.CountTokens(genai.Text("foo"), genai.ImageData("format", []byte{0, 1})) |
| 115 |
if err == nil { |
| 116 |
t.Error("got no error, want error") |
| 117 |
} |
| 118 |
} |