-
Notifications
You must be signed in to change notification settings - Fork 16
fix: Fix Grounding e2e test #376
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
d0301ae
e0211e2
da477bf
b499994
7e2e584
0e1293f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -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; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -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") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -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; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -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(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -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()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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<>();There was a problem hiding this comment.
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) 😅
There was a problem hiding this comment.
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
| 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
| 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
deleteDocumentstodeleteCollection(singular)´? To me it looks like it would tie in better withdeleteCollections(plural).
Uh oh!
There was an error while loading. Please reload this page.