-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vectorsearch): add create/list/delete index samples
- Loading branch information
1 parent
9b219c0
commit 1431d6a
Showing
9 changed files
with
632 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
aiplatform/src/main/java/aiplatform/vectorsearch/CreateIndexSample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
81 changes: 81 additions & 0 deletions
81
aiplatform/src/main/java/aiplatform/vectorsearch/CreateStreamingIndexSample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
47 changes: 47 additions & 0 deletions
47
aiplatform/src/main/java/aiplatform/vectorsearch/DeleteIndexSample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
49 changes: 49 additions & 0 deletions
49
aiplatform/src/main/java/aiplatform/vectorsearch/ListIndexesSample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
91 changes: 91 additions & 0 deletions
91
aiplatform/src/test/java/aiplatform/vectorsearch/CreateIndexSampleTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
Oops, something went wrong.