|
| 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 | + * https://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 | +/** |
| 18 | + * Usage: |
| 19 | + * |
| 20 | + * <p>1a. If you are using Vertex AI, setup ADC to get credentials: |
| 21 | + * https://cloud.google.com/docs/authentication/provide-credentials-adc#google-idp |
| 22 | + * |
| 23 | + * <p>Then set Project, Location, and USE_VERTEXAI flag as environment variables: |
| 24 | + * |
| 25 | + * <p>export GOOGLE_CLOUD_PROJECT=YOUR_PROJECT |
| 26 | + * |
| 27 | + * <p>export GOOGLE_CLOUD_LOCATION=YOUR_LOCATION |
| 28 | + * |
| 29 | + * <p>export GOOGLE_GENAI_USE_VERTEXAI=true |
| 30 | + * |
| 31 | + * <p>1b. If you are using Gemini Developer API, set an API key environment variable. You can find a |
| 32 | + * list of available API keys here: https://aistudio.google.com/app/apikey |
| 33 | + * |
| 34 | + * <p>export GOOGLE_API_KEY=YOUR_API_KEY |
| 35 | + * |
| 36 | + * <p>2. Compile the java package and run the sample code. |
| 37 | + * |
| 38 | + * <p>mvn clean compile |
| 39 | + * |
| 40 | + * <p>mvn exec:java -Dexec.mainClass="com.google.genai.examples.GenerateContentWithClientOptions" |
| 41 | + * -Dexec.args="YOUR_MODEL_ID" |
| 42 | + */ |
| 43 | +package com.google.genai.examples; |
| 44 | + |
| 45 | +import com.google.genai.Client; |
| 46 | +import com.google.genai.types.ClientOptions; |
| 47 | +import com.google.genai.types.GenerateContentResponse; |
| 48 | +import com.google.genai.types.ProxyOptions; |
| 49 | +import com.google.genai.types.ProxyType; |
| 50 | + |
| 51 | +/** An example of setting client options in a GenerateContent request. */ |
| 52 | +public final class GenerateContentWithClientOptions { |
| 53 | + public static void main(String[] args) { |
| 54 | + final String modelId; |
| 55 | + if (args.length != 0) { |
| 56 | + modelId = args[0]; |
| 57 | + } else { |
| 58 | + modelId = Constants.GEMINI_MODEL_NAME; |
| 59 | + } |
| 60 | + |
| 61 | + // Set the client options when creating the client. This applies to all requests made through |
| 62 | + // this client. |
| 63 | + ClientOptions clientOptions = |
| 64 | + ClientOptions.builder() |
| 65 | + .proxyOptions(ProxyOptions.builder().type(ProxyType.Known.DIRECT)) |
| 66 | + .maxConnections(10) |
| 67 | + .maxConnectionsPerHost(5) |
| 68 | + .build(); |
| 69 | + |
| 70 | + Client client = Client.builder().clientOptions(clientOptions).build(); |
| 71 | + |
| 72 | + if (client.vertexAI()) { |
| 73 | + System.out.println("Using Vertex AI"); |
| 74 | + } else { |
| 75 | + System.out.println("Using Gemini Developer API"); |
| 76 | + } |
| 77 | + |
| 78 | + GenerateContentResponse response = |
| 79 | + client.models.generateContent(modelId, "Tell me the history of LLM in 100 words", null); |
| 80 | + |
| 81 | + System.out.println("Response: " + response.text()); |
| 82 | + } |
| 83 | + |
| 84 | + private GenerateContentWithClientOptions() {} |
| 85 | +} |
0 commit comments