1212import com .sap .ai .sdk .grounding .model .Chunk ;
1313import com .sap .ai .sdk .grounding .model .Collection ;
1414import com .sap .ai .sdk .grounding .model .CollectionRequest ;
15+ import com .sap .ai .sdk .grounding .model .CollectionsListResponse ;
1516import com .sap .ai .sdk .grounding .model .DataRepository ;
1617import com .sap .ai .sdk .grounding .model .DataRepositoryType ;
1718import com .sap .ai .sdk .grounding .model .DocumentCreateRequest ;
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 ;
2830import java .util .List ;
2931import java .util .Locale ;
@@ -49,6 +51,7 @@ class GroundingController {
4951 private static final RetrievalApi CLIENT_RETRIEVAL = new GroundingClient ().retrieval ();
5052 private static final VectorApi CLIENT_VECTOR = new GroundingClient ().vector ();
5153 private static final String RESOURCE_GROUP = "ai-sdk-java-e2e" ;
54+ private static final String COLLECTION_TITLE = "ai-sdk-java-e2e-test" ;
5255
5356 /** Retrieve (up to 10) grounding pipeline entities. */
5457 @ GetMapping ("/pipelines/list" )
@@ -136,7 +139,8 @@ Object getDocumentsByCollectionId(
136139 String createCollection (
137140 @ Nullable @ RequestParam (value = "format" , required = false ) final String format ) {
138141 final var embeddingConfig = EmbeddingConfig .create ().modelName (TEXT_EMBEDDING_ADA_002 .name ());
139- final var request = CollectionRequest .create ().embeddingConfig (embeddingConfig ).title ("e2e" );
142+ final var request =
143+ CollectionRequest .create ().embeddingConfig (embeddingConfig ).title (COLLECTION_TITLE );
140144 final var documents = CLIENT_VECTOR .createCollection (RESOURCE_GROUP , request );
141145 final Map <String , List <String >> headers = documents .getHeaders ();
142146
@@ -212,4 +216,29 @@ Object getDocumentChunksById(
212216 final var ids = document .getChunks ().stream ().map (TextOnlyBaseChunk ::getContent ).toList ();
213217 return "The following document ids are available: %s." .formatted (ids );
214218 }
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 java .util .ArrayList <>(List .of ());
228+
229+ for (final var collection : collectionsList ) {
230+ if (COLLECTION_TITLE .equals (collection .getTitle ())) {
231+ final var deletion = this .deleteDocuments (collection .getId (), "json" );
232+ if (deletion instanceof OpenApiResponse ) {
233+ final var status = ((OpenApiResponse ) deletion ).getStatusCode ();
234+ deletions .add (deletion );
235+ statusCode = Math .max (status , statusCode );
236+ }
237+ }
238+ }
239+ if ("json" .equals (format )) {
240+ return deletions ;
241+ }
242+ return statusCode >= 400 ? "Failed to delete collections" : "Deletion successful" ;
243+ }
215244}
0 commit comments