Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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:
*
* <p>1a. If you are using Vertex AI, setup ADC to get credentials:
* https://cloud.google.com/docs/authentication/provide-credentials-adc#google-idp
*
* <p>Then set Project, Location, and USE_VERTEXAI flag as environment variables:
*
* <p>export GOOGLE_CLOUD_PROJECT=YOUR_PROJECT
*
* <p>export GOOGLE_CLOUD_LOCATION=YOUR_LOCATION
*
* <p>export GOOGLE_GENAI_USE_VERTEXAI=true
*
* <p>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
*
* <p>export GOOGLE_API_KEY=YOUR_API_KEY
*
* <p>2. Compile the java package and run the sample code.
*
* <p>mvn clean compile
*
* <p>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() {}
}
Loading