Skip to content

Commit 7d4f711

Browse files
authored
Adding an example to show how to get the token count (GoogleCloudPlatform#8944)
* Revert "Use best practice in multimodal prompts to put the image first, the string prompt second" This reverts commit 594a259. * Add token count example in Java * Put back the switch in image/prompt order * Put back the switch in image/prompt order * Add missing license header * Fix indentation * Let's please checkstyle * Reduce scioe if region tags * Correct indentation to please checkstyle
1 parent 7f83458 commit 7d4f711

3 files changed

Lines changed: 63 additions & 2 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2023 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+
* http://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+
17+
package vertexai.gemini;
18+
19+
// [START aiplatform_gemini_token_count]
20+
import com.google.cloud.vertexai.VertexAI;
21+
import com.google.cloud.vertexai.api.CountTokensResponse;
22+
import com.google.cloud.vertexai.generativeai.preview.GenerativeModel;
23+
import java.io.IOException;
24+
// [END aiplatform_gemini_token_count]
25+
26+
public class GetTokenCount {
27+
public static void main(String[] args) throws IOException {
28+
// TODO(developer): Replace these variables before running the sample.
29+
String projectId = "your-google-cloud-project-id";
30+
String location = "us-central1";
31+
String modelName = "gemini-pro-vision";
32+
33+
String textPrompt = "How many tokens are there in this prompt?";
34+
getTokenCount(projectId, location, modelName, textPrompt);
35+
}
36+
37+
// [START aiplatform_gemini_token_count]
38+
public static int getTokenCount(String projectId, String location, String modelName,
39+
String textPrompt)
40+
throws IOException {
41+
try (VertexAI vertexAI = new VertexAI(projectId, location)) {
42+
GenerativeModel model = new GenerativeModel(modelName, vertexAI);
43+
CountTokensResponse response = model.countTokens(textPrompt);
44+
45+
int tokenCount = response.getTotalTokens();
46+
System.out.println("There are " + tokenCount + " tokens in the prompt.");
47+
48+
return tokenCount;
49+
}
50+
}
51+
// [END aiplatform_gemini_token_count]
52+
}

vertexai/snippets/src/main/java/vertexai/gemini/Quickstart.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ public static String quickstart(String projectId, String location, String modelN
4646

4747
GenerativeModel model = new GenerativeModel(modelName, vertexAI);
4848
GenerateContentResponse response = model.generateContent(ContentMaker.fromMultiModalData(
49-
"What's in this photo",
50-
PartMaker.fromMimeTypeAndData("image/jpg", imageUri)
49+
PartMaker.fromMimeTypeAndData("image/jpg", imageUri),
50+
"What's in this photo"
5151
));
5252

5353
return response.toString();

vertexai/snippets/src/test/java/vertexai/gemini/SnippetsIT.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,13 @@ public void testSafetySettings() throws Exception {
183183
assertThat(output).isNotEmpty();
184184
assertThat(output).contains("reasons?");
185185
}
186+
187+
@Test
188+
public void testTokenCount() throws Exception {
189+
String textPrompt = "How many tokens are there in this prompt?";
190+
191+
int tokenCount = GetTokenCount.getTokenCount(PROJECT_ID, LOCATION, GEMINI_PRO_VISION,
192+
textPrompt);
193+
assertThat(tokenCount).isGreaterThan(6);
194+
}
186195
}

0 commit comments

Comments
 (0)