Skip to content

Commit 669d610

Browse files
authored
fix: Fix Grounding e2e test (#376)
* Fix Grounding e2e test * Requested changes --------- Co-authored-by: Jonas Israel <[email protected]>
1 parent 8e94e2d commit 669d610

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/controllers/GroundingController.java

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
import com.sap.ai.sdk.grounding.client.RetrievalApi;
1010
import com.sap.ai.sdk.grounding.client.VectorApi;
1111
import com.sap.ai.sdk.grounding.model.BaseDocument;
12+
import com.sap.ai.sdk.grounding.model.Chunk;
1213
import com.sap.ai.sdk.grounding.model.Collection;
1314
import com.sap.ai.sdk.grounding.model.CollectionRequest;
15+
import com.sap.ai.sdk.grounding.model.CollectionsListResponse;
1416
import com.sap.ai.sdk.grounding.model.DataRepository;
1517
import com.sap.ai.sdk.grounding.model.DataRepositoryType;
1618
import com.sap.ai.sdk.grounding.model.DocumentCreateRequest;
@@ -19,12 +21,13 @@
1921
import com.sap.ai.sdk.grounding.model.EmbeddingConfig;
2022
import com.sap.ai.sdk.grounding.model.KeyValueListPair;
2123
import com.sap.ai.sdk.grounding.model.Pipeline;
22-
import com.sap.ai.sdk.grounding.model.ResultsInner1;
2324
import com.sap.ai.sdk.grounding.model.RetrievalSearchFilter;
2425
import com.sap.ai.sdk.grounding.model.RetrievalSearchInput;
2526
import com.sap.ai.sdk.grounding.model.SearchConfiguration;
2627
import com.sap.ai.sdk.grounding.model.TextOnlyBaseChunk;
28+
import com.sap.cloud.sdk.services.openapi.core.OpenApiResponse;
2729
import java.time.format.TextStyle;
30+
import java.util.ArrayList;
2831
import java.util.List;
2932
import java.util.Locale;
3033
import java.util.Map;
@@ -49,6 +52,7 @@ class GroundingController {
4952
private static final RetrievalApi CLIENT_RETRIEVAL = new GroundingClient().retrieval();
5053
private static final VectorApi CLIENT_VECTOR = new GroundingClient().vector();
5154
private static final String RESOURCE_GROUP = "ai-sdk-java-e2e";
55+
private static final String COLLECTION_TITLE = "ai-sdk-java-e2e-test";
5256

5357
/** Retrieve (up to 10) grounding pipeline entities. */
5458
@GetMapping("/pipelines/list")
@@ -95,7 +99,13 @@ Object searchInDocuments(
9599
if ("json".equals(format)) {
96100
return results;
97101
}
98-
final var messages = results.getResults().stream().map(ResultsInner1::getMessage).toList();
102+
final var messages =
103+
results.getResults().stream()
104+
.flatMap(resultsInner1 -> resultsInner1.getResults().stream())
105+
.flatMap(result -> result.getDataRepository().getDocuments().stream())
106+
.flatMap(dataRepositorySearchResult -> dataRepositorySearchResult.getChunks().stream())
107+
.map(Chunk::getContent)
108+
.toList();
99109
return "Found the following response(s): " + messages;
100110
}
101111

@@ -130,7 +140,8 @@ Object getDocumentsByCollectionId(
130140
String createCollection(
131141
@Nullable @RequestParam(value = "format", required = false) final String format) {
132142
final var embeddingConfig = EmbeddingConfig.create().modelName(TEXT_EMBEDDING_ADA_002.name());
133-
final var request = CollectionRequest.create().embeddingConfig(embeddingConfig).title("e2e");
143+
final var request =
144+
CollectionRequest.create().embeddingConfig(embeddingConfig).title(COLLECTION_TITLE);
134145
final var documents = CLIENT_VECTOR.createCollection(RESOURCE_GROUP, request);
135146
final Map<String, List<String>> headers = documents.getHeaders();
136147

@@ -162,7 +173,7 @@ Object createDocument(
162173

163174
/** Delete all items from a given grounding document collection. */
164175
@GetMapping("/vector/collection/by-id/{id}/clear")
165-
Object deleteDocuments(
176+
Object deleteCollection(
166177
@Nonnull @PathVariable("id") final UUID collectionId,
167178
@Nullable @RequestParam(value = "format", required = false) final String format) {
168179
final var dayOfWeek = now().getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.ENGLISH);
@@ -182,7 +193,6 @@ Object deleteDocuments(
182193
if (del.getStatusCode() >= 400) {
183194
final var msg = "Document {} could not be deleted, status code [{}], headers: {}";
184195
log.error(msg, documentId, del.getStatusCode(), del.getHeaders());
185-
throw new IllegalStateException("Document deletion failed for id " + documentId);
186196
}
187197
}
188198
final var response = CLIENT_VECTOR.deleteCollectionById(RESOURCE_GROUP, collectionId + "");
@@ -206,4 +216,25 @@ Object getDocumentChunksById(
206216
final var ids = document.getChunks().stream().map(TextOnlyBaseChunk::getContent).toList();
207217
return "The following document ids are available: %s.".formatted(ids);
208218
}
219+
220+
/** Delete all collections. */
221+
@GetMapping("/vector/collection/clear")
222+
Object deleteCollections(
223+
@Nullable @RequestParam(value = "format", required = false) final String format) {
224+
final var collections = this.getAllCollections("json");
225+
final var collectionsList = ((CollectionsListResponse) collections).getResources();
226+
var statusCode = 0;
227+
final var deletions = new ArrayList<>();
228+
for (final var collection : collectionsList) {
229+
if (COLLECTION_TITLE.equals(collection.getTitle())) {
230+
final var deletion = (OpenApiResponse) this.deleteCollection(collection.getId(), "json");
231+
deletions.add(deletion);
232+
statusCode = Math.max(deletion.getStatusCode(), statusCode);
233+
}
234+
}
235+
if ("json".equals(format)) {
236+
return deletions;
237+
}
238+
return statusCode >= 400 ? "Failed to delete collections" : "Deletion successful";
239+
}
209240
}

sample-code/spring-app/src/test/java/com/sap/ai/sdk/app/controllers/GroundingTest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ void testRepositoriesGetAll() {
5757
void testCreateDeleteCollection() {
5858
final var controller = new GroundingController();
5959

60+
// (0) DELETE OLD COLLECTIONS
61+
controller.deleteCollections(JSON_FORMAT);
62+
6063
// (1) CREATE COLLECTION
6164
final var collectionId = controller.createCollection(JSON_FORMAT);
6265
final var collectionUuid = UUID.fromString(collectionId);
@@ -65,7 +68,7 @@ void testCreateDeleteCollection() {
6568
// (1.1) TEST COLLECTION LOOKUP
6669
this.testCollectionsGetAll();
6770

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

89-
// (5) DELETE COLLECTION
90-
Object deletion = controller.deleteDocuments(collectionUuid, JSON_FORMAT);
92+
// (5) CLEAN UP
93+
Object deletion = controller.deleteCollection(collectionUuid, JSON_FORMAT);
9194
assertThat(deletion).isInstanceOf(OpenApiResponse.class);
9295
assertThat(((OpenApiResponse) deletion).getStatusCode()).isEqualTo(202);
9396

0 commit comments

Comments
 (0)