diff --git a/genai/pom.xml b/genai/pom.xml new file mode 100644 index 00000000000..552d0800c6f --- /dev/null +++ b/genai/pom.xml @@ -0,0 +1,58 @@ + + + 4.0.0 + com.example.genai + genai-snippets + jar + Google Gen AI SDK Snippets + https://github.com/GoogleCloudPlatform/java-docs-samples/tree/main/genai + + + + com.google.cloud.samples + shared-configuration + 1.2.0 + + + + 11 + 11 + UTF-8 + + + + + + libraries-bom + com.google.cloud + import + pom + 26.33.0 + + + + + + com.google.genai + google-genai + 0.2.0 + + + junit + junit + 4.13.2 + test + + + com.google.truth + truth + 1.4.0 + test + + + diff --git a/genai/src/main/java/genai/textgeneration/TextGeneration.java b/genai/src/main/java/genai/textgeneration/TextGeneration.java new file mode 100644 index 00000000000..499ec4f7650 --- /dev/null +++ b/genai/src/main/java/genai/textgeneration/TextGeneration.java @@ -0,0 +1,50 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package genai.textgeneration; + +// [START googlegenaisdk_textgen_with_txt] + +import com.google.genai.Client; +import com.google.genai.types.GenerateContentResponse; +import java.io.IOException; +import org.apache.http.HttpException; + +public class TextGeneration { + + public static void main(String[] args) throws IOException, HttpException { + // TODO(Developer): Replace the below variables before running the sample. + String modelId = "gemini-2.0-flash-001"; + String prompt = "How does AI work?"; + generateContent(modelId, prompt); + } + + public static String generateContent(String modelId, String prompt) + throws HttpException, IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. + try (Client client = new Client()) { + GenerateContentResponse response = client.models.generateContent(modelId, prompt, null); + System.out.println(response.text()); + // Example response: + // Okay, let's break down how AI works. It's a broad field, so I'll focus on the ... + // Here's a simplified overview: + // ... + return response.text(); + } + } +} +// [END googlegenaisdk_textgen_with_txt] diff --git a/genai/src/test/java/genai/textgeneration/TextGenerationIT.java b/genai/src/test/java/genai/textgeneration/TextGenerationIT.java new file mode 100644 index 00000000000..9e1e44d2910 --- /dev/null +++ b/genai/src/test/java/genai/textgeneration/TextGenerationIT.java @@ -0,0 +1,82 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Tests for Gen AI SDK code samples. + +package genai.textgeneration; + +import static com.google.common.truth.Truth.assertThat; +import static com.google.common.truth.Truth.assertWithMessage; + +import java.io.IOException; +import java.lang.reflect.Field; +import java.util.Collections; +import java.util.Map; + +import org.apache.http.HttpException; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class TextGenerationIT { + + private static final String MODEL_ID = "gemini-2.0-flash-001"; + + // Check if the required environment variables are set. + public static void requireEnvVar(String envVarName) { + assertWithMessage(String.format("Missing environment variable '%s' ", envVarName)) + .that(System.getenv(envVarName)) + .isNotEmpty(); + } + + // Helper function to set environment variables programmatically + public static void setEnvVariable(String key, String value) { + try { + Map env = System.getenv(); + Class cl = env.getClass(); + Field field = cl.getDeclaredField("m"); + field.setAccessible(true); + @SuppressWarnings("unchecked") + Map writableEnv = (Map) field.get(env); + if (!writableEnv.containsKey(key)) { + writableEnv.put(key, value); + } + } catch (Exception e) { + throw new IllegalStateException("Failed to set environment variable", e); + } + } + + @BeforeClass + public static void setUp() { + setEnvVariable("GOOGLE_CLOUD_LOCATION", "us-central1"); + setEnvVariable("GOOGLE_GENAI_USE_VERTEXAI", "True"); + + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + requireEnvVar("GOOGLE_CLOUD_LOCATION"); + requireEnvVar("GOOGLE_GENAI_USE_VERTEXAI"); + } + + + @Test + public void testTextGeneration() throws IOException, HttpException { + String prompt = "How does AI work?"; + String response = TextGeneration.generateContent(MODEL_ID, prompt); + assertThat(response).isNotEmpty(); + } +}