diff --git a/examples/src/main/java/com/google/genai/examples/GenerateContentWithClientOptions.java b/examples/src/main/java/com/google/genai/examples/GenerateContentWithClientOptions.java new file mode 100644 index 00000000000..95e98aeae6f --- /dev/null +++ b/examples/src/main/java/com/google/genai/examples/GenerateContentWithClientOptions.java @@ -0,0 +1,85 @@ +/* + * 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 + * + * https://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. + */ + +/** + * Usage: + * + *

1a. If you are using Vertex AI, setup ADC to get credentials: + * https://cloud.google.com/docs/authentication/provide-credentials-adc#google-idp + * + *

Then set Project, Location, and USE_VERTEXAI flag as environment variables: + * + *

export GOOGLE_CLOUD_PROJECT=YOUR_PROJECT + * + *

export GOOGLE_CLOUD_LOCATION=YOUR_LOCATION + * + *

export GOOGLE_GENAI_USE_VERTEXAI=true + * + *

1b. If you are using Gemini Developer API, set an API key environment variable. You can find a + * list of available API keys here: https://aistudio.google.com/app/apikey + * + *

export GOOGLE_API_KEY=YOUR_API_KEY + * + *

2. Compile the java package and run the sample code. + * + *

mvn clean compile + * + *

mvn exec:java -Dexec.mainClass="com.google.genai.examples.GenerateContentWithClientOptions" + * -Dexec.args="YOUR_MODEL_ID" + */ +package com.google.genai.examples; + +import com.google.genai.Client; +import com.google.genai.types.ClientOptions; +import com.google.genai.types.GenerateContentResponse; +import com.google.genai.types.ProxyOptions; +import com.google.genai.types.ProxyType; + +/** An example of setting client options in a GenerateContent request. */ +public final class GenerateContentWithClientOptions { + public static void main(String[] args) { + final String modelId; + if (args.length != 0) { + modelId = args[0]; + } else { + modelId = Constants.GEMINI_MODEL_NAME; + } + + // Set the client options when creating the client. This applies to all requests made through + // this client. + ClientOptions clientOptions = + ClientOptions.builder() + .proxyOptions(ProxyOptions.builder().type(ProxyType.Known.DIRECT)) + .maxConnections(10) + .maxConnectionsPerHost(5) + .build(); + + Client client = Client.builder().clientOptions(clientOptions).build(); + + if (client.vertexAI()) { + System.out.println("Using Vertex AI"); + } else { + System.out.println("Using Gemini Developer API"); + } + + GenerateContentResponse response = + client.models.generateContent(modelId, "Tell me the history of LLM in 100 words", null); + + System.out.println("Response: " + response.text()); + } + + private GenerateContentWithClientOptions() {} +}