Skip to content

Commit d126f11

Browse files
committed
docs(genai): Add Initial Sample for Gen AI SDK
1 parent 76bbd41 commit d126f11

File tree

3 files changed

+171
-0
lines changed

3 files changed

+171
-0
lines changed

genai/pom.xml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>com.example.genai</groupId>
7+
<artifactId>genai-snippets</artifactId>
8+
<packaging>jar</packaging>
9+
<name>Google Gen AI SDK Snippets</name>
10+
<url>https://github.com/GoogleCloudPlatform/java-docs-samples/tree/main/genai</url>
11+
12+
<!--
13+
The parent pom defines common style checks and testing strategies for our samples.
14+
Removing or replacing it should not affect the execution of the samples in anyway.
15+
-->
16+
<parent>
17+
<groupId>com.google.cloud.samples</groupId>
18+
<artifactId>shared-configuration</artifactId>
19+
<version>1.2.0</version>
20+
</parent>
21+
22+
<properties>
23+
<maven.compiler.target>1.8</maven.compiler.target>
24+
<maven.compiler.source>1.8</maven.compiler.source>
25+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
26+
</properties>
27+
28+
<dependencyManagement>
29+
<dependencies>
30+
<dependency>
31+
<artifactId>libraries-bom</artifactId>
32+
<groupId>com.google.cloud</groupId>
33+
<scope>import</scope>
34+
<type>pom</type>
35+
<version>26.32.0</version>
36+
</dependency>
37+
</dependencies>
38+
</dependencyManagement>
39+
<!-- [START googlegenaisdk_install_with_bom] -->
40+
<dependencies>
41+
<dependency>
42+
<groupId>com.google.genai</groupId>
43+
<artifactId>google-genai</artifactId>
44+
</dependency>
45+
<!-- [END googlegenaisdk_install_with_bom] -->
46+
<dependency>
47+
<groupId>junit</groupId>
48+
<artifactId>junit</artifactId>
49+
<version>4.13.2</version>
50+
<scope>test</scope>
51+
</dependency>
52+
<dependency>
53+
<groupId>com.google.truth</groupId>
54+
<artifactId>truth</artifactId>
55+
<version>1.4.0</version>
56+
<scope>test</scope>
57+
</dependency>
58+
<dependency>
59+
<groupId>org.junit.jupiter</groupId>
60+
<artifactId>junit-jupiter</artifactId>
61+
<version>RELEASE</version>
62+
<scope>test</scope>
63+
</dependency>
64+
</dependencies>
65+
</project>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2025 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 genai.text_generation;
18+
19+
// [START googlegenaisdk_textgen_with_txt]
20+
import com.google.genai.Client;
21+
import com.google.genai.types.GenerateContentResponse;
22+
import java.io.IOException;
23+
import org.apache.http.HttpException;
24+
25+
public class TxtgenWithTxt {
26+
public static void main(String[] args) throws IOException, HttpException {
27+
Client client = new Client();
28+
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+
// ...
40+
}
41+
}
42+
// [END googlegenaisdk_textgen_with_txt]
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2025 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+
// Tests for Gen AI SDK code samples.
18+
19+
package genai.text_generation;
20+
21+
import static com.google.common.truth.Truth.assertThat;
22+
import static com.google.common.truth.Truth.assertWithMessage;
23+
24+
import java.io.ByteArrayOutputStream;
25+
import java.io.IOException;
26+
import java.io.PrintStream;
27+
import org.apache.http.HttpException;
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 TextGenerationIT {
35+
36+
private ByteArrayOutputStream bout;
37+
private PrintStream originalOut;
38+
39+
40+
// Check if the required environment variables are set.
41+
public static void requireEnvVar(String envVarName) {
42+
assertWithMessage(String.format("Missing environment variable '%s' ", envVarName))
43+
.that(System.getenv(envVarName))
44+
.isNotEmpty();
45+
}
46+
47+
@Before
48+
public void setUp() {
49+
originalOut = System.out;
50+
bout = new ByteArrayOutputStream();
51+
System.setOut(new PrintStream(bout));
52+
}
53+
54+
public void tearDown() {
55+
System.setOut(originalOut);
56+
}
57+
58+
@Test
59+
public void testTextgenWithTxt() throws IOException, HttpException {
60+
TxtgenWithTxt.main(new String[]{});
61+
tearDown();
62+
assertThat(bout.toString()).isNotEmpty();
63+
}
64+
}

0 commit comments

Comments
 (0)