|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +# Copyright 2023 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | + |
| 18 | +# pylint: disable=protected-access, g-multiple-import |
| 19 | + |
| 20 | +from google.cloud import aiplatform |
| 21 | +from tests.system.aiplatform import e2e_base |
| 22 | +from vertexai.preview.language_models import ( |
| 23 | + ChatModel, |
| 24 | + InputOutputTextPair, |
| 25 | + TextGenerationModel, |
| 26 | + TextEmbeddingModel, |
| 27 | +) |
| 28 | + |
| 29 | + |
| 30 | +class TestLanguageModels(e2e_base.TestEndToEnd): |
| 31 | + """System tests for language models.""" |
| 32 | + |
| 33 | + _temp_prefix = "temp_language_models_test_" |
| 34 | + |
| 35 | + def test_text_generation(self): |
| 36 | + aiplatform.init(project=e2e_base._PROJECT, location=e2e_base._LOCATION) |
| 37 | + |
| 38 | + model = TextGenerationModel.from_pretrained("google/text-bison@001") |
| 39 | + |
| 40 | + assert model.predict( |
| 41 | + "What is the best recipe for banana bread? Recipe:", |
| 42 | + max_output_tokens=128, |
| 43 | + temperature=0, |
| 44 | + top_p=1, |
| 45 | + top_k=5, |
| 46 | + ).text |
| 47 | + |
| 48 | + def test_chat_on_chat_model(self): |
| 49 | + aiplatform.init(project=e2e_base._PROJECT, location=e2e_base._LOCATION) |
| 50 | + |
| 51 | + chat_model = ChatModel.from_pretrained("google/chat-bison@001") |
| 52 | + chat = chat_model.start_chat( |
| 53 | + context="My name is Ned. You are my personal assistant. My favorite movies are Lord of the Rings and Hobbit.", |
| 54 | + examples=[ |
| 55 | + InputOutputTextPair( |
| 56 | + input_text="Who do you work for?", |
| 57 | + output_text="I work for Ned.", |
| 58 | + ), |
| 59 | + InputOutputTextPair( |
| 60 | + input_text="What do I like?", |
| 61 | + output_text="Ned likes watching movies.", |
| 62 | + ), |
| 63 | + ], |
| 64 | + temperature=0.0, |
| 65 | + ) |
| 66 | + |
| 67 | + assert chat.send_message("Are my favorite movies based on a book series?").text |
| 68 | + assert len(chat._history) == 1 |
| 69 | + assert chat.send_message( |
| 70 | + "When where these books published?", |
| 71 | + temperature=0.1, |
| 72 | + ).text |
| 73 | + assert len(chat._history) == 2 |
| 74 | + |
| 75 | + def test_text_embedding(self): |
| 76 | + aiplatform.init(project=e2e_base._PROJECT, location=e2e_base._LOCATION) |
| 77 | + |
| 78 | + model = TextEmbeddingModel.from_pretrained("google/textembedding-gecko@001") |
| 79 | + embeddings = model.get_embeddings(["What is life?"]) |
| 80 | + assert embeddings |
| 81 | + for embedding in embeddings: |
| 82 | + vector = embedding.values |
| 83 | + assert len(vector) == 768 |
0 commit comments