Skip to content

Commit 0eacdf6

Browse files
copybara-service[bot]Zhenyi Qi
andauthored
chore: [vertexai]Add integration test for GenerativeModel (#10627)
PiperOrigin-RevId: 619553290 Co-authored-by: Zhenyi Qi <zhenyiqi@google.com>
1 parent 64e2ed8 commit 0eacdf6

1 file changed

Lines changed: 92 additions & 0 deletions

File tree

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.cloud.vertexai.it;
17+
18+
import static com.google.common.truth.Truth.assertThat;
19+
20+
import com.google.cloud.vertexai.VertexAI;
21+
import com.google.cloud.vertexai.api.CountTokensResponse;
22+
import com.google.cloud.vertexai.api.GenerateContentResponse;
23+
import com.google.cloud.vertexai.generativeai.GenerativeModel;
24+
import com.google.cloud.vertexai.generativeai.ResponseStream;
25+
import java.io.IOException;
26+
import java.util.logging.Logger;
27+
import org.junit.After;
28+
import org.junit.Before;
29+
import org.junit.Test;
30+
import org.junit.runner.RunWith;
31+
import org.junit.runners.JUnit4;
32+
33+
@RunWith(JUnit4.class)
34+
public class ITGenerativeModelIntegrationTest {
35+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
36+
private static final String MODEL_NAME_TEXT = "gemini-pro";
37+
private static final String LOCATION = "us-central1";
38+
private static final String TEXT = "What do you think about Google Pixel?";
39+
private static final Logger logger =
40+
Logger.getLogger(ITGenerativeModelIntegrationTest.class.getName());
41+
private VertexAI vertexAi;
42+
private GenerativeModel model;
43+
44+
@Before
45+
public void setUp() throws IOException {
46+
vertexAi = new VertexAI(PROJECT_ID, LOCATION);
47+
model = new GenerativeModel(MODEL_NAME_TEXT, vertexAi);
48+
}
49+
50+
@After
51+
public void tearDown() throws IOException {
52+
vertexAi.close();
53+
}
54+
55+
@Test
56+
public void generateContentStreamWithPlainText() throws IOException {
57+
logger.info(String.format("Generating response for question: %s", TEXT));
58+
59+
ResponseStream<GenerateContentResponse> stream = model.generateContentStream(TEXT);
60+
61+
logger.info("Print response: ");
62+
// GenAI output is flaky so we always print out the response.
63+
// For the same reason, we don't do assertions much.
64+
for (GenerateContentResponse resp : stream) {
65+
logger.info(String.format("Print response:\n%s", resp));
66+
assertThat(resp.getCandidatesList()).isNotEmpty();
67+
}
68+
}
69+
70+
@Test
71+
public void generateContentWithPlainText() throws IOException {
72+
logger.info(String.format("Generating response for question: %s", TEXT));
73+
74+
GenerateContentResponse response = model.generateContent(TEXT);
75+
76+
logger.info(String.format("Print response:\n%s", response));
77+
// GenAI output is flaky so we always print out the response.
78+
// For the same reason, we don't do assertions much.
79+
assertThat(response.getCandidatesList()).isNotEmpty();
80+
}
81+
82+
@Test
83+
public void countTokensWithPlainText() throws IOException {
84+
logger.info(String.format("Counting tokens for: %s", TEXT));
85+
86+
CountTokensResponse tokens = model.countTokens(TEXT);
87+
// GenAI output is flaky so we always print out the response.
88+
// For the same reason, we don't do assertions much.
89+
logger.info(String.format("Print number of tokens:\n%s", tokens));
90+
assertThat(tokens.getTotalTokens()).isGreaterThan(0);
91+
}
92+
}

0 commit comments

Comments
 (0)