|
| 1 | +/* |
| 2 | + * Copyright 2026 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>2. Compile the java package and run the sample code. |
| 30 | + * |
| 31 | + * <p>mvn clean compile exec:java |
| 32 | + * -Dexec.mainClass="com.google.cloud.vertexai.genai.examples.AgentEngineCreate" |
| 33 | + */ |
| 34 | +package com.google.cloud.vertexai.genai.examples; |
| 35 | + |
| 36 | +import com.google.cloud.vertexai.genai.Client; |
| 37 | +import com.google.cloud.vertexai.genai.types.AgentEngineOperation; |
| 38 | +import com.google.cloud.vertexai.genai.types.CreateAgentEngineConfig; |
| 39 | +import com.google.cloud.vertexai.genai.types.DeleteAgentEngineOperation; |
| 40 | +import com.google.common.collect.ImmutableMap; |
| 41 | + |
| 42 | +/** An example of using the Java SDK to create an agent engine. */ |
| 43 | +public final class AgentEngineCreate { |
| 44 | + public static void main(String[] args) { |
| 45 | + |
| 46 | + // Instantiate the client. It reads from the environment variables `GOOGLE_CLOUD_LOCATION` and |
| 47 | + // `GOOGLE_CLOUD_PROJECT`. |
| 48 | + Client client = new Client(); |
| 49 | + |
| 50 | + CreateAgentEngineConfig config = |
| 51 | + CreateAgentEngineConfig.builder() |
| 52 | + .labels(ImmutableMap.of("test-label", "test-value")) |
| 53 | + .build(); |
| 54 | + AgentEngineOperation createOperation = client.agentEngines.privateCreate(config); |
| 55 | + |
| 56 | + System.out.println("Create operation name: " + createOperation.name()); |
| 57 | + |
| 58 | + while (!createOperation.done().filter(Boolean::booleanValue).isPresent()) { |
| 59 | + try { |
| 60 | + Thread.sleep(10000); // Sleep for 10 seconds. |
| 61 | + createOperation = |
| 62 | + client.agentEngines.privateGetAgentOperation(createOperation.name().get(), null); |
| 63 | + } catch (InterruptedException e) { |
| 64 | + Thread.currentThread().interrupt(); |
| 65 | + } |
| 66 | + } |
| 67 | + System.out.println("Create operation response: " + createOperation.response()); |
| 68 | + |
| 69 | + // Optionally, delete to clean up resources. |
| 70 | + DeleteAgentEngineOperation deleteOperation = |
| 71 | + client.agentEngines.privateDelete( |
| 72 | + createOperation.response().get().name().get(), true, null); |
| 73 | + } |
| 74 | + |
| 75 | + private AgentEngineCreate() {} |
| 76 | +} |
0 commit comments