Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
Expand Up @@ -9,8 +9,10 @@
import com.sap.ai.sdk.grounding.client.RetrievalApi;
import com.sap.ai.sdk.grounding.client.VectorApi;
import com.sap.ai.sdk.grounding.model.BaseDocument;
import com.sap.ai.sdk.grounding.model.Chunk;
import com.sap.ai.sdk.grounding.model.Collection;
import com.sap.ai.sdk.grounding.model.CollectionRequest;
import com.sap.ai.sdk.grounding.model.CollectionsListResponse;
import com.sap.ai.sdk.grounding.model.DataRepository;
import com.sap.ai.sdk.grounding.model.DataRepositoryType;
import com.sap.ai.sdk.grounding.model.DocumentCreateRequest;
Expand All @@ -19,11 +21,11 @@
import com.sap.ai.sdk.grounding.model.EmbeddingConfig;
import com.sap.ai.sdk.grounding.model.KeyValueListPair;
import com.sap.ai.sdk.grounding.model.Pipeline;
import com.sap.ai.sdk.grounding.model.ResultsInner1;
import com.sap.ai.sdk.grounding.model.RetrievalSearchFilter;
import com.sap.ai.sdk.grounding.model.RetrievalSearchInput;
import com.sap.ai.sdk.grounding.model.SearchConfiguration;
import com.sap.ai.sdk.grounding.model.TextOnlyBaseChunk;
import com.sap.cloud.sdk.services.openapi.core.OpenApiResponse;
import java.time.format.TextStyle;
import java.util.List;
import java.util.Locale;
Expand All @@ -49,6 +51,7 @@ class GroundingController {
private static final RetrievalApi CLIENT_RETRIEVAL = new GroundingClient().retrieval();
private static final VectorApi CLIENT_VECTOR = new GroundingClient().vector();
private static final String RESOURCE_GROUP = "ai-sdk-java-e2e";
private static final String COLLECTION_TITLE = "ai-sdk-java-e2e-test";

/** Retrieve (up to 10) grounding pipeline entities. */
@GetMapping("/pipelines/list")
Expand Down Expand Up @@ -95,7 +98,13 @@ Object searchInDocuments(
if ("json".equals(format)) {
return results;
}
final var messages = results.getResults().stream().map(ResultsInner1::getMessage).toList();
final var messages =
results.getResults().stream()
.flatMap(resultsInner1 -> resultsInner1.getResults().stream())
.flatMap(result -> result.getDataRepository().getDocuments().stream())
.flatMap(dataRepositorySearchResult -> dataRepositorySearchResult.getChunks().stream())
.map(Chunk::getContent)
.toList();
return "Found the following response(s): " + messages;
}

Expand Down Expand Up @@ -130,7 +139,8 @@ Object getDocumentsByCollectionId(
String createCollection(
@Nullable @RequestParam(value = "format", required = false) final String format) {
final var embeddingConfig = EmbeddingConfig.create().modelName(TEXT_EMBEDDING_ADA_002.name());
final var request = CollectionRequest.create().embeddingConfig(embeddingConfig).title("e2e");
final var request =
CollectionRequest.create().embeddingConfig(embeddingConfig).title(COLLECTION_TITLE);
final var documents = CLIENT_VECTOR.createCollection(RESOURCE_GROUP, request);
final Map<String, List<String>> headers = documents.getHeaders();

Expand Down Expand Up @@ -206,4 +216,29 @@ Object getDocumentChunksById(
final var ids = document.getChunks().stream().map(TextOnlyBaseChunk::getContent).toList();
return "The following document ids are available: %s.".formatted(ids);
}

/** Delete all collections. */
@GetMapping("/vector/collection/clear")
Object deleteCollections(
@Nullable @RequestParam(value = "format", required = false) final String format) {
final var collections = this.getAllCollections("json");
final var collectionsList = ((CollectionsListResponse) collections).getResources();
var statusCode = 0;
final var deletions = new java.util.ArrayList<>(List.of());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Minor)

Suggested change
final var deletions = new java.util.ArrayList<>(List.of());
final var deletions = new java.util.ArrayList<>();

You could also use an import statement for that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What exactly do you mean with using an import statement?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean it's uncommon to use some basic collection class with fully-qualified name:

new java.util.ArrayList<>();

Because using FQN here implies that there might be a naming conflict with some custom class that's named ArrayList which would very likely result in confusions and mixups: It has a code smell.

Normally, devs would just put an import statement upfront.

import java.util.ArrayList;

new ArrayList<>();

Copy link
Member Author

@Jonas-Isr Jonas-Isr Mar 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, sorry... I was thinking you ment something else (ich stand auf dem Schlauch) 😅


for (final var collection : collectionsList) {
if (COLLECTION_TITLE.equals(collection.getTitle())) {
final var deletion = this.deleteDocuments(collection.getId(), "json");
if (deletion instanceof OpenApiResponse) {
final var status = ((OpenApiResponse) deletion).getStatusCode();
deletions.add(deletion);
statusCode = Math.max(status, statusCode);
}
}
}
if ("json".equals(format)) {
return deletions;
}
return statusCode >= 400 ? "Failed to delete collections" : "Deletion successful";
Comment on lines 226 to 238
Copy link
Contributor

@newtork newtork Mar 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like your loop! For comparison, this is what it would've looked with Stream API
Suggested change
var statusCode = 0;
final var deletions = new java.util.ArrayList<>(List.of());
for (final var collection : collectionsList) {
if (COLLECTION_TITLE.equals(collection.getTitle())) {
final var deletion = this.deleteDocuments(collection.getId(), "json");
if (deletion instanceof OpenApiResponse) {
final var status = ((OpenApiResponse) deletion).getStatusCode();
deletions.add(deletion);
statusCode = Math.max(status, statusCode);
}
}
}
if ("json".equals(format)) {
return deletions;
}
return statusCode >= 400 ? "Failed to delete collections" : "Deletion successful";
final var deletions = new java.util.ArrayList<>();
var statusCode =
collectionsList.stream()
.filter(c -> COLLECTION_TITLE.equals(c.getTitle()))
.map(collection -> this.deleteDocuments(collection.getId(), "json"))
.peek(deletions::add)
.mapToInt(deletion -> ((OpenApiResponse) deletion).getStatusCode())
.max();
if ("json".equals(format)) {
return deletions;
}
return statusCode.orElse(0) >= 400 ? "Failed to delete collections" : "Deletion successful";
However you could still reduce the code a little by dropping the instanceof check
Suggested change
var statusCode = 0;
final var deletions = new java.util.ArrayList<>(List.of());
for (final var collection : collectionsList) {
if (COLLECTION_TITLE.equals(collection.getTitle())) {
final var deletion = this.deleteDocuments(collection.getId(), "json");
if (deletion instanceof OpenApiResponse) {
final var status = ((OpenApiResponse) deletion).getStatusCode();
deletions.add(deletion);
statusCode = Math.max(status, statusCode);
}
}
}
if ("json".equals(format)) {
return deletions;
}
return statusCode >= 400 ? "Failed to delete collections" : "Deletion successful";
var statusCode = 0;
final var deletions = new java.util.ArrayList<>(List.of());
for (final var collection : collectionsList) {
if (COLLECTION_TITLE.equals(collection.getTitle())) {
final var deletion = (OpenApiResponse) this.deleteDocuments(collection.getId(), "json");
deletions.add(deletion);
statusCode = Math.max(deletion.getStatusCode(), statusCode);
}
}
if ("json".equals(format)) {
return deletions;
}
return statusCode >= 400 ? "Failed to delete collections" : "Deletion successful";

(Minor)

  • For consistency, please remove the throw statement in deleteDocuments, the rest of the controller is not throwing (explicitly).
  • Would you mind renaming deleteDocuments to deleteCollection (singular)´? To me it looks like it would tie in better with deleteCollections (plural).

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ void testRepositoriesGetAll() {
void testCreateDeleteCollection() {
final var controller = new GroundingController();

// (0) DELETE OLD COLLECTIONS
controller.deleteCollections(JSON_FORMAT);

// (1) CREATE COLLECTION
final var collectionId = controller.createCollection(JSON_FORMAT);
final var collectionUuid = UUID.fromString(collectionId);
Expand All @@ -65,7 +68,7 @@ void testCreateDeleteCollection() {
// (1.1) TEST COLLECTION LOOKUP
this.testCollectionsGetAll();

// (2) SANITY CHECK: NO DOCUMENTS
// (2) SANITY CHECK: NO DOCUMENTS IN COLLECTION
final var documentsEmpty = controller.getDocumentsByCollectionId(collectionUuid, JSON_FORMAT);
assertThat(documentsEmpty).isInstanceOf(Documents.class);
assertThat(((Documents) documentsEmpty).getCount()).isEqualTo(0);
Expand All @@ -86,7 +89,7 @@ void testCreateDeleteCollection() {
final var dayOfWeek = now().getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.ENGLISH);
this.assertDocumentSearchResult((RetievalSearchResults) search, dayOfWeek);

// (5) DELETE COLLECTION
// (5) CLEAN UP
Object deletion = controller.deleteDocuments(collectionUuid, JSON_FORMAT);
assertThat(deletion).isInstanceOf(OpenApiResponse.class);
assertThat(((OpenApiResponse) deletion).getStatusCode()).isEqualTo(202);
Expand Down