-
Notifications
You must be signed in to change notification settings - Fork 2
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
[FEAT]: Add AMR enrichment to 'Enrich metadata with AI assistant' #5019
Merged
YohannParis
merged 8 commits into
main
from
4856-feat-add-amr-enrichment-to-enrich-metadata-with-ai-assistant
Oct 2, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
746905a
[FEAT]: Add AMR enrichment to 'Enrich metadata with AI assistant'
dvince2 361a2fd
chore: lint and format client codebase
github-actions[bot] d3c2b98
Merge branch 'main' into 4856-feat-add-amr-enrichment-to-enrich-metad…
dvince2 a261dea
Update packages/server/src/main/java/software/uncharted/terarium/hmis…
dvince2 c47ba24
Merging main
dvince2 c0fc0d7
Merge branch 'main' into 4856-feat-add-amr-enrichment-to-enrich-metad…
dvince2 ca2617b
Merge branch 'main' into 4856-feat-add-amr-enrichment-to-enrich-metad…
dvince2 6b81e0a
Merge branch 'main' into 4856-feat-add-amr-enrichment-to-enrich-metad…
dvince2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,7 @@ | |
import software.uncharted.terarium.hmiserver.models.dataservice.document.DocumentAsset; | ||
import software.uncharted.terarium.hmiserver.models.dataservice.document.ExtractedDocumentPage; | ||
import software.uncharted.terarium.hmiserver.models.dataservice.model.Model; | ||
import software.uncharted.terarium.hmiserver.models.task.CompoundTask; | ||
import software.uncharted.terarium.hmiserver.models.task.TaskRequest; | ||
import software.uncharted.terarium.hmiserver.models.task.TaskRequest.TaskType; | ||
import software.uncharted.terarium.hmiserver.models.task.TaskResponse; | ||
|
@@ -144,55 +145,30 @@ public ResponseEntity<TaskResponse> createModelCardTask( | |
throw new ResponseStatusException(HttpStatus.NOT_FOUND, messages.get("model.not-found")); | ||
} | ||
|
||
final ModelCardResponseHandler.Input input = new ModelCardResponseHandler.Input(); | ||
input.setAmr(model.get().serializeWithoutTerariumFields(null, new String[] { "gollmCard" })); | ||
|
||
// Grab the document | ||
final DocumentAsset document; | ||
if (documentId != null) { | ||
final Optional<DocumentAsset> documentOpt = documentAssetService.getAsset(documentId, permission); | ||
if (documentOpt.isEmpty()) { | ||
log.warn(String.format("Document %s not found", documentId)); | ||
throw new ResponseStatusException(HttpStatus.NOT_FOUND, messages.get("document.not-found")); | ||
} | ||
|
||
document = documentOpt.get(); | ||
|
||
// make sure there is text in the document | ||
if (document.getText() == null || document.getText().isEmpty()) { | ||
log.warn(String.format("Document %s has no text to send", documentId)); | ||
throw new ResponseStatusException(HttpStatus.NOT_FOUND, messages.get("document.extraction.not-done")); | ||
} | ||
|
||
// check for input length | ||
if (document.getText().length() > ModelCardResponseHandler.MAX_TEXT_SIZE) { | ||
log.warn(String.format("Document %s text too long for GoLLM model card task", documentId)); | ||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, messages.get("document.text-length-exceeded")); | ||
} | ||
|
||
input.setResearchPaper(document.getText()); | ||
throw new ResponseStatusException(HttpStatus.NOT_FOUND, messages.get("document.not-found")); | ||
} | ||
|
||
// Create the task | ||
final TaskRequest req = new TaskRequest(); | ||
req.setType(TaskType.GOLLM); | ||
req.setScript(ModelCardResponseHandler.NAME); | ||
req.setUserId(currentUserService.get().getId()); | ||
final Optional<DocumentAsset> documentOpt = documentAssetService.getAsset(documentId, permission); | ||
if (documentOpt.isEmpty()) { | ||
log.warn(String.format("Document %s not found", documentId)); | ||
throw new ResponseStatusException(HttpStatus.NOT_FOUND, messages.get("document.not-found")); | ||
} | ||
|
||
try { | ||
req.setInput(objectMapper.writeValueAsBytes(input)); | ||
} catch (final Exception e) { | ||
log.error("Unable to serialize input", e); | ||
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, messages.get("generic.io-error.write")); | ||
// make sure there is text in the document | ||
if (documentOpt.get().getText() == null || documentOpt.get().getText().isEmpty()) { | ||
log.warn(String.format("Document %s has no text to send", documentId)); | ||
throw new ResponseStatusException(HttpStatus.NOT_FOUND, messages.get("document.extraction.not-done")); | ||
} | ||
|
||
req.setProjectId(projectId); | ||
// check for input length | ||
if (documentOpt.get().getText().length() > ModelCardResponseHandler.MAX_TEXT_SIZE) { | ||
log.warn(String.format("Document %s text too long for GoLLM model card task", documentId)); | ||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, messages.get("document.text-length-exceeded")); | ||
} | ||
|
||
final ModelCardResponseHandler.Properties props = new ModelCardResponseHandler.Properties(); | ||
props.setProjectId(projectId); | ||
props.setDocumentId(documentId); | ||
props.setModelId(modelId); | ||
req.setAdditionalProperties(props); | ||
final TaskRequest req = getModelCardTask(documentOpt.get(), model.get(), projectId); | ||
|
||
final TaskResponse resp; | ||
try { | ||
|
@@ -746,6 +722,101 @@ public ResponseEntity<TaskResponse> createGenerateResponseTask( | |
return ResponseEntity.ok().body(resp); | ||
} | ||
|
||
/** | ||
* This endpoint will dispatch a few GoLLM tasks to enrich model metadata, | ||
* including enriching the AMR and creating the model card | ||
* @param modelId UUID of the model to enrich | ||
* @param documentId UUID of the document to use for enrichment | ||
* @param mode TaskMode to run the task in (is this ASYNC?) | ||
* @param projectId UUID of the project to associate the task with for permissions | ||
* @param overwrite boolean to determine if the model should be overwritten | ||
* @return TaskResponse with the task ID | ||
*/ | ||
@GetMapping("/enrich-model-metadata") | ||
@Secured(Roles.USER) | ||
@Operation(summary = "Dispatch multiple GoLLM tasks to enrich model metadata") | ||
@ApiResponses( | ||
value = { | ||
@ApiResponse( | ||
responseCode = "200", | ||
description = "Dispatched successfully", | ||
content = @Content( | ||
mediaType = "application/json", | ||
schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = TaskResponse.class) | ||
) | ||
), | ||
@ApiResponse( | ||
responseCode = "404", | ||
description = "The provided model or document arguments are not found", | ||
content = @Content | ||
), | ||
@ApiResponse(responseCode = "500", description = "There was an issue dispatching the request", content = @Content) | ||
} | ||
) | ||
public ResponseEntity<TaskResponse> createEnrichModelMetadataTask( | ||
@RequestParam(name = "model-id", required = true) final UUID modelId, | ||
@RequestParam(name = "document-id", required = false) final UUID documentId, | ||
@RequestParam(name = "mode", required = false, defaultValue = "ASYNC") final TaskMode mode, | ||
@RequestParam(name = "project-id", required = false) final UUID projectId, | ||
@RequestParam(name = "overwrite", required = false, defaultValue = "false") final boolean overwrite | ||
) { | ||
final Schema.Permission permission = projectService.checkPermissionCanRead( | ||
currentUserService.get().getId(), | ||
projectId | ||
); | ||
|
||
// Grab the document | ||
final Optional<DocumentAsset> document = documentAssetService.getAsset(documentId, permission); | ||
|
||
// make sure there is text in the document. We don't need a document but if we do have one it can't be empty | ||
if (document.isPresent() && (document.get().getText() == null || document.get().getText().isEmpty())) { | ||
log.warn(String.format("Document %s has no extracted text", documentId)); | ||
throw new ResponseStatusException(HttpStatus.NOT_FOUND, messages.get("document.extraction.not-done")); | ||
} | ||
|
||
// Grab the model | ||
final Optional<Model> model = modelService.getAsset(modelId, permission); | ||
if (model.isEmpty()) { | ||
log.warn(String.format("Model %s not found", modelId)); | ||
throw new ResponseStatusException(HttpStatus.NOT_FOUND, messages.get("model.not-found")); | ||
} | ||
|
||
final TaskRequest req; | ||
|
||
if (document.isPresent()) { | ||
dvince2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
final TaskRequest enrichAmrRequest = getEnrichAMRTaskRequest( | ||
document.orElse(null), | ||
model.get(), | ||
projectId, | ||
overwrite | ||
); | ||
final TaskRequest modelCardRequest = getModelCardTask(document.orElse(null), model.get(), projectId); | ||
|
||
req = new CompoundTask(enrichAmrRequest, modelCardRequest); | ||
} else { | ||
req = getModelCardTask(null, model.get(), projectId); | ||
} | ||
|
||
final TaskResponse resp; | ||
try { | ||
resp = taskService.runTask(mode, req); | ||
} catch (final JsonProcessingException e) { | ||
log.error("Unable to serialize input", e); | ||
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, messages.get("task.gollm.json-processing")); | ||
} catch (final TimeoutException e) { | ||
log.warn("Timeout while waiting for task response", e); | ||
throw new ResponseStatusException(HttpStatus.SERVICE_UNAVAILABLE, messages.get("task.gollm.timeout")); | ||
} catch (final InterruptedException e) { | ||
log.warn("Interrupted while waiting for task response", e); | ||
throw new ResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY, messages.get("task.gollm.interrupted")); | ||
} catch (final ExecutionException e) { | ||
log.error("Error while waiting for task response", e); | ||
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, messages.get("task.gollm.execution-failure")); | ||
} | ||
|
||
return ResponseEntity.ok().body(resp); | ||
} | ||
|
||
@GetMapping("/enrich-amr") | ||
@Secured(Roles.USER) | ||
@Operation(summary = "Dispatch a `GoLLM Enrich AMR` task") | ||
|
@@ -799,41 +870,11 @@ public ResponseEntity<TaskResponse> createEnrichAMRTask( | |
throw new ResponseStatusException(HttpStatus.NOT_FOUND, messages.get("model.not-found")); | ||
} | ||
|
||
final EnrichAmrResponseHandler.Input input = new EnrichAmrResponseHandler.Input(); | ||
input.setResearchPaper(document.get().getText()); | ||
// stripping the metadata from the model before its sent since it can cause | ||
// gollm to fail with massive inputs | ||
model.get().setMetadata(null); | ||
|
||
try { | ||
final String amr = objectMapper.writeValueAsString(model.get()); | ||
input.setAmr(amr); | ||
} catch (final JsonProcessingException e) { | ||
log.error("Unable to serialize model card", e); | ||
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, messages.get("task.gollm.json-processing")); | ||
} | ||
|
||
// Create the task | ||
final TaskRequest req = new TaskRequest(); | ||
req.setType(TaskType.GOLLM); | ||
req.setScript(EnrichAmrResponseHandler.NAME); | ||
req.setUserId(currentUserService.get().getId()); | ||
|
||
try { | ||
req.setInput(objectMapper.writeValueAsBytes(input)); | ||
} catch (final Exception e) { | ||
log.error("Unable to serialize input", e); | ||
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, messages.get("generic.io-error.write")); | ||
} | ||
|
||
req.setProjectId(projectId); | ||
|
||
final EnrichAmrResponseHandler.Properties props = new EnrichAmrResponseHandler.Properties(); | ||
props.setProjectId(projectId); | ||
props.setDocumentId(documentId); | ||
props.setModelId(modelId); | ||
props.setOverwrite(overwrite); | ||
req.setAdditionalProperties(props); | ||
TaskRequest req = getEnrichAMRTaskRequest(document.get(), model.get(), projectId, overwrite); | ||
|
||
final TaskResponse resp; | ||
try { | ||
|
@@ -1071,4 +1112,71 @@ public ResponseEntity<Void> cancelTask(@PathVariable("task-id") final UUID taskI | |
taskService.cancelTask(taskId); | ||
return ResponseEntity.ok().build(); | ||
} | ||
|
||
private TaskRequest getModelCardTask(DocumentAsset document, Model model, UUID projectId) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should |
||
final ModelCardResponseHandler.Input input = new ModelCardResponseHandler.Input(); | ||
input.setAmr(model.serializeWithoutTerariumFields(null, new String[] { "gollmCard" })); | ||
|
||
if (document != null) input.setResearchPaper(document.getText()); | ||
|
||
// Create the task | ||
final TaskRequest req = new TaskRequest(); | ||
req.setType(TaskType.GOLLM); | ||
req.setScript(ModelCardResponseHandler.NAME); | ||
req.setUserId(currentUserService.get().getId()); | ||
|
||
try { | ||
req.setInput(objectMapper.writeValueAsBytes(input)); | ||
} catch (final Exception e) { | ||
log.error("Unable to serialize input", e); | ||
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, messages.get("generic.io-error.write")); | ||
} | ||
|
||
req.setProjectId(projectId); | ||
|
||
final ModelCardResponseHandler.Properties props = new ModelCardResponseHandler.Properties(); | ||
props.setProjectId(projectId); | ||
if (document != null) props.setDocumentId(document.getId()); | ||
props.setModelId(model.getId()); | ||
req.setAdditionalProperties(props); | ||
|
||
return req; | ||
} | ||
|
||
private TaskRequest getEnrichAMRTaskRequest(DocumentAsset document, Model model, UUID projectId, Boolean overwrite) { | ||
final EnrichAmrResponseHandler.Input input = new EnrichAmrResponseHandler.Input(); | ||
if (document != null) input.setResearchPaper(document.getText()); | ||
|
||
try { | ||
final String amr = objectMapper.writeValueAsString(model); | ||
input.setAmr(amr); | ||
} catch (final JsonProcessingException e) { | ||
log.error("Unable to serialize model card", e); | ||
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, messages.get("task.gollm.json-processing")); | ||
} | ||
|
||
// Create the task | ||
final TaskRequest req = new TaskRequest(); | ||
req.setType(TaskType.GOLLM); | ||
req.setScript(EnrichAmrResponseHandler.NAME); | ||
req.setUserId(currentUserService.get().getId()); | ||
|
||
try { | ||
req.setInput(objectMapper.writeValueAsBytes(input)); | ||
} catch (final Exception e) { | ||
log.error("Unable to serialize input", e); | ||
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, messages.get("generic.io-error.write")); | ||
} | ||
|
||
req.setProjectId(projectId); | ||
|
||
final EnrichAmrResponseHandler.Properties props = new EnrichAmrResponseHandler.Properties(); | ||
props.setProjectId(projectId); | ||
if (document != null) props.setDocumentId(document.getId()); | ||
props.setModelId(model.getId()); | ||
props.setOverwrite(overwrite); | ||
req.setAdditionalProperties(props); | ||
|
||
return req; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
.../server/src/main/java/software/uncharted/terarium/hmiserver/models/task/CompoundTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package software.uncharted.terarium.hmiserver.models.task; | ||
|
||
import java.util.List; | ||
import lombok.Data; | ||
|
||
/** | ||
* Represents a compound task that consists of a primary task and one or more secondary tasks. | ||
*/ | ||
@Data | ||
public class CompoundTask extends TaskRequest { | ||
|
||
/** | ||
* Constructs a CompoundTask with a primary task and optional secondary tasks. | ||
* | ||
* @param primaryTask the primary task | ||
* @param secondaryTasks the secondary tasks | ||
*/ | ||
public CompoundTask(TaskRequest primaryTask, TaskRequest... secondaryTasks) { | ||
this.primaryTask = primaryTask; | ||
this.secondaryTasks = List.of(secondaryTasks); | ||
} | ||
|
||
/** The primary task of the compound task. */ | ||
private TaskRequest primaryTask; | ||
|
||
/** The list of secondary tasks of the compound task. */ | ||
private List<TaskRequest> secondaryTasks; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 think we can remove this code. Since, instead of creating connection for each task, we can subscribe/unsubscribe on global sse event channel whenever needed using
subscribe
inClientEventService.ts
oruseClientEvent.ts
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.
And I believe the endpoint for subscribing for each individual task event got removed in the server side at some point so this code won't work if I remember correctly.