About the support for asynchronous REST API in the imagechat interface.
Comment From: XYWENJIE
Some cloud service providers offer REST APIs for tasks like text-to-image conversion or image-to-video conversion, similar to the functionality provided by Stability AI, where submissions are handled asynchronously, with users then querying the results through a GET method. Will future ImageClient or VideoClient libraries incorporate such functionality?
Comment From: XYWENJIE
The following is a preliminary example of an ImageClient interface implemented using an asynchronous REST API, where its functionality and design have not yet reached a polished state:
@Override
public ImageResponse call(ImagePrompt imagePrompt) {
QWenImageResponse taskImageResponse = this.retryTemplate.execute(ctx -> {
ImageOptions imageOptions = imagePrompt.getOptions();
String instructions = imagePrompt.getInstructions().get(0).getText();
QWenImageRequest imageRequest = new QWenImageRequest(new Input(instructions,null), null);
ResponseEntity<QWenImageResponse> responseEntity = this.dashCopeService.createQwenImageTask(imageRequest);
return responseEntity.getBody();
});
try {
if(taskImageResponse.output().taskStatus() == StatusStatus.PENDING) {
logger.info("Task submission successful, queuing required, sleeping for 3 seconds");
Thread.sleep(3000);
logger.info("Sleep completed, querying task results!");
}
}catch (Exception e) {
logger.error(e.getMessage(),e);
}
return this.retryTemplate.execute(ctx -> {
ResponseEntity<QWenImageResponse> responseEntity = this.dashCopeService.findImageTaskResult(taskImageResponse.output().taskId());
QWenImageResponse resultImageResponse = responseEntity.getBody();
if(resultImageResponse.output().taskStatus() != StatusStatus.SUCCEEDED) {
TaskMetrices taskMetrices = resultImageResponse.output().taskMetrices();
logger.info("Task still in progress; out of {} tasks, {} tasks have been completed. Please wait.",taskMetrices.total(),taskMetrices.succeeded());
throw new DashCopeApiException("Task is still generating, please try again later.");
}
logger.info("Query results complete, packaging data");
return convertResponse(resultImageResponse);
});
}
private ImageResponse convertResponse(QWenImageResponse qwenImageResponse) {
if(qwenImageResponse == null) {
logger.warn("No image response returned for request: {}",qwenImageResponse);
return new ImageResponse(List.of());
}
List<ImageGeneration> imageGenerationList = qwenImageResponse.output().results().stream().map(entry -> {
return new ImageGeneration(new Image(entry.url(),null));
}).toList();
return new ImageResponse(imageGenerationList);
}
Comment From: markpollack
I encountered this when looking into midjourney and decided not to tackle it at the time. However, StabilityAI does offer an API that does not require you to poll back for the completed. I guess we will tackle this when there is demand for a specific model that behaves this way.