99import com .sap .ai .sdk .grounding .client .RetrievalApi ;
1010import com .sap .ai .sdk .grounding .client .VectorApi ;
1111import com .sap .ai .sdk .grounding .model .BaseDocument ;
12+ import com .sap .ai .sdk .grounding .model .Chunk ;
1213import com .sap .ai .sdk .grounding .model .Collection ;
1314import com .sap .ai .sdk .grounding .model .CollectionRequest ;
15+ import com .sap .ai .sdk .grounding .model .CollectionsListResponse ;
1416import com .sap .ai .sdk .grounding .model .DataRepository ;
1517import com .sap .ai .sdk .grounding .model .DataRepositoryType ;
1618import com .sap .ai .sdk .grounding .model .DocumentCreateRequest ;
1921import com .sap .ai .sdk .grounding .model .EmbeddingConfig ;
2022import com .sap .ai .sdk .grounding .model .KeyValueListPair ;
2123import com .sap .ai .sdk .grounding .model .Pipeline ;
22- import com .sap .ai .sdk .grounding .model .ResultsInner1 ;
2324import com .sap .ai .sdk .grounding .model .RetrievalSearchFilter ;
2425import com .sap .ai .sdk .grounding .model .RetrievalSearchInput ;
2526import com .sap .ai .sdk .grounding .model .SearchConfiguration ;
2627import com .sap .ai .sdk .grounding .model .TextOnlyBaseChunk ;
28+ import com .sap .cloud .sdk .services .openapi .core .OpenApiResponse ;
2729import java .time .format .TextStyle ;
30+ import java .util .ArrayList ;
2831import java .util .List ;
2932import java .util .Locale ;
3033import 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}
0 commit comments