Skip to content

Commit

Permalink
feat(vectorsearch): add create/list/delete index samples
Browse files Browse the repository at this point in the history
  • Loading branch information
ericgribkoff committed Dec 16, 2024
1 parent 9b219c0 commit 1431d6a
Show file tree
Hide file tree
Showing 9 changed files with 632 additions and 0 deletions.
21 changes: 21 additions & 0 deletions aiplatform/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@
<packaging>jar</packaging>
<name>Google Cloud Vertex AI Snippets</name>
<url>https://github.com/GoogleCloudPlatform/java-docs-samples/tree/main/aiplatform</url>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>15</source>
<target>15</target>
</configuration>
</plugin>
</plugins>
</build>

<!--
The parent pom defines common style checks and testing strategies for our samples.
Expand Down Expand Up @@ -68,6 +80,10 @@
<version>1.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.api.grpc</groupId>
<artifactId>grpc-google-cloud-aiplatform-v1</artifactId>
</dependency>
<dependency>
<groupId>com.google.api.grpc</groupId>
<artifactId>proto-google-cloud-aiplatform-v1beta1</artifactId>
Expand All @@ -89,6 +105,11 @@
<version>1.7.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-testing</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright 2024 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 aiplatform.vectorsearch;

// [START aiplatform_vector_search_create_index_sample]

import com.google.cloud.aiplatform.v1.CreateIndexRequest;
import com.google.cloud.aiplatform.v1.Index;
import com.google.cloud.aiplatform.v1.Index.IndexUpdateMethod;
import com.google.cloud.aiplatform.v1.IndexServiceClient;
import com.google.cloud.aiplatform.v1.IndexServiceSettings;
import com.google.cloud.aiplatform.v1.LocationName;
import com.google.protobuf.Value;
import com.google.protobuf.util.JsonFormat;

public class CreateIndexSample {

public static void main(String[] args) throws Exception {
// TODO(developer): Replace these variables before running the sample.
String project = "YOUR_PROJECT_ID";
String location = "YOUR_LOCATION";
String displayName = "YOUR_INDEX_DISPLAY_NAME";
String contentsDeltaUri = "gs://YOUR_BUCKET/";
String metadataJson = """
{
"contentsDeltaUri": "%s",
"config": {
"dimensions": 100,
"approximateNeighborsCount": 150,
"distanceMeasureType": "DOT_PRODUCT_DISTANCE",
"shardSize": "SHARD_SIZE_MEDIUM",
"algorithm_config": {
"treeAhConfig": {
"leafNodeEmbeddingCount": 5000,
"fractionLeafNodesToSearch": 0.03
}
}
}
}
""".formatted(contentsDeltaUri);

try (IndexServiceClient indexServiceClient = IndexServiceClient.create(
IndexServiceSettings.newBuilder().setEndpoint(location + "-aiplatform.googleapis.com:443")
.build())) {
createIndexSample(project, location, displayName, metadataJson, indexServiceClient);
}
}

static Index createIndexSample(String project, String location, String displayName,
String metadataJson, IndexServiceClient indexServiceClient) throws Exception {
Value.Builder metadataBuilder = Value.newBuilder();
JsonFormat.parser().merge(metadataJson, metadataBuilder);
CreateIndexRequest request = CreateIndexRequest.newBuilder()
.setParent(LocationName.of(project, location).toString()).setIndex(
Index.newBuilder().setDisplayName(displayName).setMetadata(metadataBuilder)
.setIndexUpdateMethod(IndexUpdateMethod.BATCH_UPDATE)).build();
Index response = indexServiceClient.createIndexAsync(request).get();
return response;
}
}

// [END aiplatform_vector_search_create_index_sample]
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright 2024 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 aiplatform.vectorsearch;

// [START aiplatform_vector_search_create_streaming_index_sample]

import com.google.cloud.aiplatform.v1.CreateIndexRequest;
import com.google.cloud.aiplatform.v1.Index;
import com.google.cloud.aiplatform.v1.Index.IndexUpdateMethod;
import com.google.cloud.aiplatform.v1.IndexServiceClient;
import com.google.cloud.aiplatform.v1.IndexServiceSettings;
import com.google.cloud.aiplatform.v1.LocationName;
import com.google.protobuf.Value;
import com.google.protobuf.util.JsonFormat;

public class CreateStreamingIndexSample {

public static void main(String[] args) throws Exception {
// TODO(developer): Replace these variables before running the sample.
String project = "YOUR_PROJECT_ID";
String location = "YOUR_LOCATION";
String displayName = "YOUR_INDEX_DISPLAY_NAME";
String contentsDeltaUri = "gs://YOUR_BUCKET/";
String metadataJson = """
{
"contentsDeltaUri": "%s",
"config": {
"dimensions": 100,
"approximateNeighborsCount": 150,
"distanceMeasureType": "DOT_PRODUCT_DISTANCE",
"shardSize": "SHARD_SIZE_MEDIUM",
"algorithm_config": {
"treeAhConfig": {
"leafNodeEmbeddingCount": 5000,
"fractionLeafNodesToSearch": 0.03
}
}
}
}
""".formatted(contentsDeltaUri);

try (IndexServiceClient indexServiceClient = IndexServiceClient.create(
IndexServiceSettings.newBuilder().setEndpoint(location + "-aiplatform.googleapis.com:443")
.build())) {
createStreamingIndexSample(project, location, displayName, metadataJson, indexServiceClient);
}
}

static Index createStreamingIndexSample(String project, String location, String displayName,
String metadataJson, IndexServiceClient indexServiceClient) throws Exception {
Value.Builder metadataBuilder = Value.newBuilder();
JsonFormat.parser().merge(metadataJson, metadataBuilder);
CreateIndexRequest request =
CreateIndexRequest.newBuilder()
.setParent(LocationName.of(project, location).toString())
.setIndex(Index.newBuilder()
.setDisplayName(displayName)
.setMetadata(
metadataBuilder)
.setIndexUpdateMethod(IndexUpdateMethod.STREAM_UPDATE))
.build();
Index response = indexServiceClient.createIndexAsync(request).get();
return response;
}
}

// [END aiplatform_vector_search_create_streaming_index_sample]
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2024 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 aiplatform.vectorsearch;

// [START aiplatform_vector_search_delete_index_sample]

import com.google.cloud.aiplatform.v1.IndexName;
import com.google.cloud.aiplatform.v1.IndexServiceClient;
import com.google.cloud.aiplatform.v1.IndexServiceSettings;

public class DeleteIndexSample {

public static void main(String[] args) throws Exception {
// TODO(developer): Replace these variables before running the sample.
String project = "YOUR_PROJECT_ID";
String location = "YOUR_LOCATION";
String indexId = "YOUR_INDEX_ID";

try (IndexServiceClient indexServiceClient = IndexServiceClient.create(
IndexServiceSettings.newBuilder().setEndpoint(location + "-aiplatform.googleapis.com:443")
.build())) {
deleteIndexSample(project, location, indexId, indexServiceClient);
}
}

static void deleteIndexSample(String project, String location, String indexId,
IndexServiceClient indexServiceClient) throws Exception {
String indexName = IndexName.of(project, location, indexId).toString();
indexServiceClient.deleteIndexAsync(indexName).get();
}
}

// [END aiplatform_vector_search_delete_index_sample]
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2024 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 aiplatform.vectorsearch;

// [START aiplatform_vector_search_list_indexes_sample]

import com.google.cloud.aiplatform.v1.Index;
import com.google.cloud.aiplatform.v1.IndexServiceClient;
import com.google.cloud.aiplatform.v1.IndexServiceClient.ListIndexesPagedResponse;
import com.google.cloud.aiplatform.v1.IndexServiceSettings;
import com.google.cloud.aiplatform.v1.LocationName;

public class ListIndexesSample {

public static void main(String[] args) throws Exception {
// TODO(developer): Replace these variables before running the sample.
String project = "YOUR_PROJECT_ID";
String location = "YOUR_LOCATION";

try (IndexServiceClient indexServiceClient = IndexServiceClient.create(
IndexServiceSettings.newBuilder().setEndpoint(location + "-aiplatform.googleapis.com:443")
.build())) {
for (Index index : listIndexesSample(project, location, indexServiceClient).iterateAll()){
System.out.println(index);
}
}
}

static ListIndexesPagedResponse listIndexesSample(String project, String location, IndexServiceClient indexServiceClient) throws Exception {
String parent = LocationName.of(project, location).toString();
return indexServiceClient.listIndexes(parent);
}
}

// [END aiplatform_vector_search_list_indexes_sample]
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright 2024 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 aiplatform.vectorsearch;

import static com.google.common.truth.Truth.assertThat;

import com.google.api.gax.grpc.GrpcTransportChannel;
import com.google.api.gax.rpc.FixedTransportChannelProvider;
import com.google.cloud.aiplatform.v1.CreateIndexRequest;
import com.google.cloud.aiplatform.v1.Index;
import com.google.cloud.aiplatform.v1.Index.IndexUpdateMethod;
import com.google.cloud.aiplatform.v1.IndexServiceClient;
import com.google.cloud.aiplatform.v1.IndexServiceGrpc.IndexServiceImplBase;
import com.google.cloud.aiplatform.v1.IndexServiceSettings;
import com.google.cloud.aiplatform.v1.LocationName;
import com.google.longrunning.Operation;
import com.google.protobuf.Any;
import com.google.protobuf.Value;
import com.google.protobuf.util.JsonFormat;
import io.grpc.ManagedChannel;
import io.grpc.inprocess.InProcessChannelBuilder;
import io.grpc.inprocess.InProcessServerBuilder;
import io.grpc.stub.StreamObserver;
import io.grpc.testing.GrpcCleanupRule;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

public class CreateIndexSampleTest {

private static final String PROJECT = "test-project";
private static final String LOCATION = "test-location";
private static final String DISPLAY_NAME = "test-display-name";
private static final String METADATA_JSON = "{'some': {'key' : 2}}";

@Rule
public final GrpcCleanupRule grpcCleanupRule = new GrpcCleanupRule();

private ManagedChannel channel;

@Before
public void setUp() throws Exception {
String serverName = InProcessServerBuilder.generateName();
grpcCleanupRule.register(InProcessServerBuilder.forName(serverName).directExecutor()
.addService(new IndexServiceImplBase() {
@Override
public void createIndex(CreateIndexRequest request,
StreamObserver<Operation> responseObserver) {
assertThat(request.getParent()).isEqualTo(
LocationName.of(PROJECT, LOCATION).toString());
responseObserver.onNext(
Operation.newBuilder().setDone(true).setResponse(Any.pack(request.getIndex()))
.build());
responseObserver.onCompleted();
}
}).build().start());

channel = grpcCleanupRule.register(
InProcessChannelBuilder.forName(serverName).directExecutor().build());
}

@Test
public void testCreateIndexSample() throws Exception {
IndexServiceClient client = IndexServiceClient.create(IndexServiceSettings.newBuilder()
.setTransportChannelProvider(FixedTransportChannelProvider.create(
GrpcTransportChannel.newBuilder().setManagedChannel(channel).build())).build());

Index response = CreateIndexSample.createIndexSample(PROJECT, LOCATION, DISPLAY_NAME,
METADATA_JSON, client);

Value.Builder metadata = Value.newBuilder();
JsonFormat.parser().merge(METADATA_JSON, metadata);
assertThat(response).isEqualTo(
Index.newBuilder().setDisplayName(DISPLAY_NAME).setMetadata(metadata)
.setIndexUpdateMethod(IndexUpdateMethod.BATCH_UPDATE).build());
}
}
Loading

0 comments on commit 1431d6a

Please sign in to comment.