|
17 | 17 |
|
18 | 18 | # pylint: disable=protected-access, g-multiple-import
|
19 | 19 |
|
| 20 | +import pytest |
| 21 | + |
| 22 | + |
20 | 23 | from google.cloud import aiplatform
|
21 | 24 | from google.cloud.aiplatform.compat.types import (
|
22 | 25 | job_state as gca_job_state,
|
@@ -54,6 +57,22 @@ def test_text_generation(self):
|
54 | 57 | stop_sequences=["# %%"],
|
55 | 58 | ).text
|
56 | 59 |
|
| 60 | + @pytest.mark.asyncio |
| 61 | + async def test_text_generation_model_predict_async(self): |
| 62 | + aiplatform.init(project=e2e_base._PROJECT, location=e2e_base._LOCATION) |
| 63 | + |
| 64 | + model = TextGenerationModel.from_pretrained("google/text-bison@001") |
| 65 | + |
| 66 | + response = await model.predict_async( |
| 67 | + "What is the best recipe for banana bread? Recipe:", |
| 68 | + max_output_tokens=128, |
| 69 | + temperature=0.0, |
| 70 | + top_p=1.0, |
| 71 | + top_k=5, |
| 72 | + stop_sequences=["# %%"], |
| 73 | + ) |
| 74 | + assert response.text |
| 75 | + |
57 | 76 | def test_text_generation_streaming(self):
|
58 | 77 | aiplatform.init(project=e2e_base._PROJECT, location=e2e_base._LOCATION)
|
59 | 78 |
|
@@ -107,6 +126,46 @@ def test_chat_on_chat_model(self):
|
107 | 126 | assert chat.message_history[2].content == message2
|
108 | 127 | assert chat.message_history[3].author == chat.MODEL_AUTHOR
|
109 | 128 |
|
| 129 | + @pytest.mark.asyncio |
| 130 | + async def test_chat_model_async(self): |
| 131 | + aiplatform.init(project=e2e_base._PROJECT, location=e2e_base._LOCATION) |
| 132 | + |
| 133 | + chat_model = ChatModel.from_pretrained("google/chat-bison@001") |
| 134 | + chat = chat_model.start_chat( |
| 135 | + context="My name is Ned. You are my personal assistant. My favorite movies are Lord of the Rings and Hobbit.", |
| 136 | + examples=[ |
| 137 | + InputOutputTextPair( |
| 138 | + input_text="Who do you work for?", |
| 139 | + output_text="I work for Ned.", |
| 140 | + ), |
| 141 | + InputOutputTextPair( |
| 142 | + input_text="What do I like?", |
| 143 | + output_text="Ned likes watching movies.", |
| 144 | + ), |
| 145 | + ], |
| 146 | + temperature=0.0, |
| 147 | + stop_sequences=["# %%"], |
| 148 | + ) |
| 149 | + |
| 150 | + message1 = "Are my favorite movies based on a book series?" |
| 151 | + response1 = await chat.send_message_async(message1) |
| 152 | + assert response1.text |
| 153 | + assert len(chat.message_history) == 2 |
| 154 | + assert chat.message_history[0].author == chat.USER_AUTHOR |
| 155 | + assert chat.message_history[0].content == message1 |
| 156 | + assert chat.message_history[1].author == chat.MODEL_AUTHOR |
| 157 | + |
| 158 | + message2 = "When were these books published?" |
| 159 | + response2 = await chat.send_message_async( |
| 160 | + message2, |
| 161 | + temperature=0.1, |
| 162 | + ) |
| 163 | + assert response2.text |
| 164 | + assert len(chat.message_history) == 4 |
| 165 | + assert chat.message_history[2].author == chat.USER_AUTHOR |
| 166 | + assert chat.message_history[2].content == message2 |
| 167 | + assert chat.message_history[3].author == chat.MODEL_AUTHOR |
| 168 | + |
110 | 169 | def test_chat_model_send_message_streaming(self):
|
111 | 170 | aiplatform.init(project=e2e_base._PROJECT, location=e2e_base._LOCATION)
|
112 | 171 |
|
@@ -161,6 +220,23 @@ def test_text_embedding(self):
|
161 | 220 | assert embeddings[1].statistics.token_count > 1000
|
162 | 221 | assert embeddings[1].statistics.truncated
|
163 | 222 |
|
| 223 | + @pytest.mark.asyncio |
| 224 | + async def test_text_embedding_async(self): |
| 225 | + aiplatform.init(project=e2e_base._PROJECT, location=e2e_base._LOCATION) |
| 226 | + |
| 227 | + model = TextEmbeddingModel.from_pretrained("google/textembedding-gecko@001") |
| 228 | + # One short text, one llong text (to check truncation) |
| 229 | + texts = ["What is life?", "What is life?" * 1000] |
| 230 | + embeddings = await model.get_embeddings_async(texts) |
| 231 | + assert len(embeddings) == 2 |
| 232 | + assert len(embeddings[0].values) == 768 |
| 233 | + assert embeddings[0].statistics.token_count > 0 |
| 234 | + assert not embeddings[0].statistics.truncated |
| 235 | + |
| 236 | + assert len(embeddings[1].values) == 768 |
| 237 | + assert embeddings[1].statistics.token_count > 1000 |
| 238 | + assert embeddings[1].statistics.truncated |
| 239 | + |
164 | 240 | def test_tuning(self, shared_state):
|
165 | 241 | """Test tuning, listing and loading models."""
|
166 | 242 | aiplatform.init(project=e2e_base._PROJECT, location=e2e_base._LOCATION)
|
|
0 commit comments