Skip to content

Commit ecc013e

Browse files
committed
refactor samples
1 parent d126f11 commit ecc013e

File tree

3 files changed

+33
-41
lines changed

3 files changed

+33
-41
lines changed

genai/pom.xml

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
</parent>
2121

2222
<properties>
23-
<maven.compiler.target>1.8</maven.compiler.target>
24-
<maven.compiler.source>1.8</maven.compiler.source>
23+
<maven.compiler.target>11</maven.compiler.target>
24+
<maven.compiler.source>11</maven.compiler.source>
2525
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2626
</properties>
2727

@@ -32,7 +32,7 @@
3232
<groupId>com.google.cloud</groupId>
3333
<scope>import</scope>
3434
<type>pom</type>
35-
<version>26.32.0</version>
35+
<version>26.33.0</version>
3636
</dependency>
3737
</dependencies>
3838
</dependencyManagement>
@@ -55,11 +55,5 @@
5555
<version>1.4.0</version>
5656
<scope>test</scope>
5757
</dependency>
58-
<dependency>
59-
<groupId>org.junit.jupiter</groupId>
60-
<artifactId>junit-jupiter</artifactId>
61-
<version>RELEASE</version>
62-
<scope>test</scope>
63-
</dependency>
6458
</dependencies>
6559
</project>

genai/src/main/java/genai/text_generation/TextgenWithTxt.java renamed to genai/src/main/java/genai/text_generation/TextGeneration.java

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,26 @@
2222
import java.io.IOException;
2323
import org.apache.http.HttpException;
2424

25-
public class TxtgenWithTxt {
25+
public class TextGeneration {
2626
public static void main(String[] args) throws IOException, HttpException {
27-
Client client = new Client();
27+
// TODO(Developer): Replace the below variables before running the sample.
28+
String modelId = "gemini-2.0-flash-001";
29+
String prompt = "How does AI work?";
30+
generateContent(modelId, prompt);
31+
}
2832

29-
GenerateContentResponse response = client.models.generateContent(
30-
"gemini-2.0-flash-001",
31-
"How does AI work?",
32-
null
33-
);
34-
System.out.println(response.text());
35-
// Example response:
36-
// Okay, let's break down how AI works. It's a broad field, so I'll focus on the ...
37-
//
38-
// Here's a simplified overview:
39-
// ...
33+
public static String generateContent(String modelId, String prompt) throws HttpException, IOException {
34+
// Initialize client that will be used to send requests. This client only needs to be created
35+
// once, and can be reused for multiple requests.
36+
try (Client client = new Client()) {
37+
GenerateContentResponse response = client.models.generateContent(modelId, prompt, null);
38+
System.out.println(response.text());
39+
// Example response:
40+
// Okay, let's break down how AI works. It's a broad field, so I'll focus on the ...
41+
// Here's a simplified overview:
42+
// ...
43+
return response.text();
44+
}
4045
}
4146
}
4247
// [END googlegenaisdk_textgen_with_txt]

genai/src/test/java/genai/text_generation/TextGenerationIT.java

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,17 @@
2121
import static com.google.common.truth.Truth.assertThat;
2222
import static com.google.common.truth.Truth.assertWithMessage;
2323

24-
import java.io.ByteArrayOutputStream;
2524
import java.io.IOException;
26-
import java.io.PrintStream;
2725
import org.apache.http.HttpException;
28-
import org.junit.Before;
26+
import org.junit.BeforeClass;
2927
import org.junit.Test;
3028
import org.junit.runner.RunWith;
3129
import org.junit.runners.JUnit4;
3230

3331
@RunWith(JUnit4.class)
3432
public class TextGenerationIT {
3533

36-
private ByteArrayOutputStream bout;
37-
private PrintStream originalOut;
38-
34+
private String MODEL_ID="gemini-2.0-flash-001";
3935

4036
// Check if the required environment variables are set.
4137
public static void requireEnvVar(String envVarName) {
@@ -44,21 +40,18 @@ public static void requireEnvVar(String envVarName) {
4440
.isNotEmpty();
4541
}
4642

47-
@Before
48-
public void setUp() {
49-
originalOut = System.out;
50-
bout = new ByteArrayOutputStream();
51-
System.setOut(new PrintStream(bout));
43+
@BeforeClass
44+
public static void setUp() {
45+
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
46+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
47+
requireEnvVar("GOOGLE_CLOUD_LOCATION");
48+
requireEnvVar("GOOGLE_GENAI_USE_VERTEXAI");
5249
}
5350

54-
public void tearDown() {
55-
System.setOut(originalOut);
56-
}
57-
5851
@Test
59-
public void testTextgenWithTxt() throws IOException, HttpException {
60-
TxtgenWithTxt.main(new String[]{});
61-
tearDown();
62-
assertThat(bout.toString()).isNotEmpty();
52+
public void testTextGeneration() throws IOException, HttpException {
53+
String prompt = "How does AI work?";
54+
String response = TextGeneration.generateContent(MODEL_ID, prompt);
55+
assertThat(response).isNotEmpty();
6356
}
6457
}

0 commit comments

Comments
 (0)