File size: 9,681 Bytes
1856027 | 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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 | # Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import os
import pytest
import nltk
from nltk.corpus import udhr
from google.cloud import aiplatform
from vertexai.preview.tokenization import (
get_tokenizer_for_model as tokenizer_preview,
)
from vertexai.tokenization._tokenizers import (
get_tokenizer_for_model as tokenizer_ga,
)
from vertexai.generative_models import (
GenerativeModel,
Part,
Tool,
)
from tests.system.aiplatform import e2e_base
from google import auth
from google.cloud.aiplatform_v1beta1.types import (
content as gapic_content_types,
tool as gapic_tool_types,
openapi,
)
from google.protobuf import struct_pb2
_MODELS = [
"gemini-1.0-pro",
"gemini-1.5-pro",
"gemini-1.5-flash",
"gemini-1.5-flash-002",
"gemini-1.5-pro-002",
]
_CORPUS = [
"udhr",
]
_CORPUS_LIB = [
udhr,
]
_VERSIONED_TOKENIZER = [tokenizer_preview, tokenizer_ga]
_MODEL_CORPUS_PARAMS = [
(get_tokenizer_for_model, model_name, corpus_name, corpus_lib)
for get_tokenizer_for_model in _VERSIONED_TOKENIZER
for model_name in _MODELS
for (corpus_name, corpus_lib) in zip(_CORPUS, _CORPUS_LIB)
]
_STRUCT = struct_pb2.Struct(
fields={
"string_key": struct_pb2.Value(string_value="value"),
}
)
_FUNCTION_CALL = gapic_tool_types.FunctionCall(name="test_function_call", args=_STRUCT)
_FUNCTION_RESPONSE = gapic_tool_types.FunctionResponse(
name="function_response",
response=_STRUCT,
)
_SCHEMA_1 = openapi.Schema(format="schema1_format", description="schema1_description")
_SCHEMA_2 = openapi.Schema(format="schema2_format", description="schema2_description")
_EXAMPLE = struct_pb2.Value(string_value="value1")
_FUNCTION_DECLARATION_1 = gapic_tool_types.FunctionDeclaration(
name="function_declaration_name",
description="function_declaration_description",
parameters=openapi.Schema(
format="schema_format",
description="schema_description",
enum=["schema_enum1", "schema_enum2"],
required=["schema_required1", "schema_required2"],
items=_SCHEMA_2,
properties={"property_key": _SCHEMA_1},
example=_EXAMPLE,
),
)
_FUNCTION_DECLARATION_2 = gapic_tool_types.FunctionDeclaration(
parameters=openapi.Schema(
nullable=True,
default=struct_pb2.Value(string_value="value1"),
min_items=0,
max_items=0,
min_properties=0,
max_properties=0,
minimum=0,
maximum=0,
min_length=0,
max_length=0,
pattern="pattern",
),
response=_SCHEMA_1,
)
STAGING_API_ENDPOINT = "STAGING_ENDPOINT"
PROD_API_ENDPOINT = "PROD_ENDPOINT"
@pytest.mark.parametrize("api_endpoint_env_name", [PROD_API_ENDPOINT])
class TestTokenization(e2e_base.TestEndToEnd):
"""System tests for tokenization."""
_temp_prefix = "temp_tokenization_test_"
@pytest.fixture(scope="function", autouse=True)
def setup_method(self, api_endpoint_env_name):
super().setup_method()
credentials, _ = auth.default(
scopes=["https://www.googleapis.com/auth/cloud-platform"]
)
if api_endpoint_env_name == STAGING_API_ENDPOINT:
api_endpoint = os.getenv(api_endpoint_env_name)
else:
api_endpoint = None
aiplatform.init(
project=e2e_base._PROJECT,
location=e2e_base._LOCATION,
credentials=credentials,
api_endpoint=api_endpoint,
)
@pytest.mark.parametrize(
"get_tokenizer_for_model, model_name, corpus_name, corpus_lib",
_MODEL_CORPUS_PARAMS,
)
def test_count_tokens_local(
self,
get_tokenizer_for_model,
model_name,
corpus_name,
corpus_lib,
api_endpoint_env_name,
):
# The Gemini 1.5 flash model requires the model version
# number suffix (001) in staging only
if api_endpoint_env_name == STAGING_API_ENDPOINT and "-00" not in model_name:
model_name = model_name + "-001"
tokenizer = get_tokenizer_for_model(model_name)
model = GenerativeModel(model_name)
nltk.download(corpus_name, quiet=True)
for id, book in enumerate(corpus_lib.fileids()):
text = corpus_lib.raw(book)
service_result = model.count_tokens(text)
local_result = tokenizer.count_tokens(text)
assert service_result.total_tokens == local_result.total_tokens
@pytest.mark.parametrize(
"get_tokenizer_for_model, model_name, corpus_name, corpus_lib",
_MODEL_CORPUS_PARAMS,
)
def test_compute_tokens(
self,
get_tokenizer_for_model,
model_name,
corpus_name,
corpus_lib,
api_endpoint_env_name,
):
# The Gemini 1.5 flash model requires the model version
# number suffix (001) in staging only
if api_endpoint_env_name == STAGING_API_ENDPOINT:
model_name = model_name + "-001"
tokenizer = get_tokenizer_for_model(model_name)
model = GenerativeModel(model_name)
nltk.download(corpus_name, quiet=True)
for id, book in enumerate(corpus_lib.fileids()):
text = corpus_lib.raw(book)
response = model.compute_tokens(text)
local_result = tokenizer.compute_tokens(text)
for local, service in zip(local_result.tokens_info, response.tokens_info):
assert local.tokens == service.tokens
assert local.token_ids == service.token_ids
@pytest.mark.parametrize(
"model_name",
_MODELS,
)
def test_count_tokens_system_instruction(self, model_name):
tokenizer = tokenizer_preview(model_name)
model = GenerativeModel(model_name, system_instruction=["You are a chatbot."])
assert (
tokenizer.count_tokens(
"hello", system_instruction=["You are a chatbot."]
).total_tokens
== model.count_tokens("hello").total_tokens
)
@pytest.mark.parametrize(
"model_name",
_MODELS,
)
def test_count_tokens_system_instruction_is_function_call(self, model_name):
part = Part._from_gapic(gapic_content_types.Part(function_call=_FUNCTION_CALL))
tokenizer = tokenizer_preview(model_name)
model = GenerativeModel(model_name, system_instruction=[part])
assert (
tokenizer.count_tokens("hello", system_instruction=[part]).total_tokens
== model.count_tokens("hello").total_tokens
)
@pytest.mark.parametrize(
"model_name",
_MODELS,
)
def test_count_tokens_system_instruction_is_function_response(self, model_name):
part = Part._from_gapic(
gapic_content_types.Part(function_response=_FUNCTION_RESPONSE)
)
tokenizer = tokenizer_preview(model_name)
model = GenerativeModel(model_name, system_instruction=[part])
assert tokenizer.count_tokens(part, system_instruction=[part]).total_tokens
assert (
tokenizer.count_tokens("hello", system_instruction=[part]).total_tokens
== model.count_tokens("hello").total_tokens
)
@pytest.mark.parametrize(
"model_name",
_MODELS,
)
def test_count_tokens_tool_is_function_declaration(self, model_name):
tokenizer = tokenizer_preview(model_name)
model = GenerativeModel(model_name)
tool1 = Tool._from_gapic(
gapic_tool_types.Tool(function_declarations=[_FUNCTION_DECLARATION_1])
)
tool2 = Tool._from_gapic(
gapic_tool_types.Tool(function_declarations=[_FUNCTION_DECLARATION_2])
)
assert tokenizer.count_tokens("hello", tools=[tool1]).total_tokens
with pytest.raises(ValueError):
tokenizer.count_tokens("hello", tools=[tool2]).total_tokens
assert (
tokenizer.count_tokens("hello", tools=[tool1]).total_tokens
== model.count_tokens("hello", tools=[tool1]).total_tokens
)
@pytest.mark.parametrize(
"model_name",
_MODELS,
)
def test_count_tokens_content_is_function_call(self, model_name):
part = Part._from_gapic(gapic_content_types.Part(function_call=_FUNCTION_CALL))
tokenizer = tokenizer_preview(model_name)
model = GenerativeModel(model_name)
assert tokenizer.count_tokens(part).total_tokens
assert (
tokenizer.count_tokens(part).total_tokens
== model.count_tokens(part).total_tokens
)
@pytest.mark.parametrize(
"model_name",
_MODELS,
)
def test_count_tokens_content_is_function_response(self, model_name):
part = Part._from_gapic(
gapic_content_types.Part(function_response=_FUNCTION_RESPONSE)
)
tokenizer = tokenizer_preview(model_name)
model = GenerativeModel(model_name)
assert tokenizer.count_tokens(part).total_tokens
assert (
tokenizer.count_tokens(part).total_tokens
== model.count_tokens(part).total_tokens
)
|