Skip to content

Commit 8711d53

Browse files
committed
Simplify the code.
1 parent 5a06fc8 commit 8711d53

1 file changed

Lines changed: 28 additions & 37 deletions

File tree

src/main/java/custom/application/v1/smalltalk.java

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -401,27 +401,23 @@ private String chatGPT(String sessionId, String message, String image) throws Ap
401401
Builder apiResponse = (Builder) ApplicationManager.call("openai", context);
402402
assert apiResponse != null;
403403
Builders builders;
404-
if (apiResponse.get("choices") != null) {
405-
builders = (Builders) apiResponse.get("choices");
406-
407-
if (builders.get(0).size() > 0) {
408-
Builder choice = builders.get(0);
409-
410-
if (choice.get("message") != null) {
411-
String choiceText = ((Builder) choice.get("message")).get("content").toString();
412-
this.setVariable("previous_user_message", message);
413-
this.setVariable("previous_system_message", choiceText);
414-
415-
if (choiceText.contains(IMAGES_GENERATIONS)) {
416-
return this.imageProcessorStability(ImageProcessorType.GENERATIONS, null, sessionId + ":" + message);
417-
} else if (choiceText.contains(IMAGES_EDITS)) {
418-
return this.imageProcessorStability(ImageProcessorType.EDITS, image, sessionId + ":" + message);
419-
} else if (choiceText.contains(IMAGES_VARIATIONS)) {
420-
return this.imageProcessor(ImageProcessorType.VARIATIONS, image, sessionId + ":" + message);
421-
}
422-
423-
return choiceText;
404+
if ((builders = (Builders) apiResponse.get("choices")) != null && builders.get(0).size() > 0) {
405+
Builder choice = builders.get(0);
406+
407+
if (choice.get("message") != null) {
408+
String choiceText = ((Builder) choice.get("message")).get("content").toString();
409+
this.setVariable("previous_user_message", message);
410+
this.setVariable("previous_system_message", choiceText);
411+
412+
if (choiceText.contains(IMAGES_GENERATIONS)) {
413+
return this.imageProcessorStability(ImageProcessorType.GENERATIONS, null, sessionId + ":" + message);
414+
} else if (choiceText.contains(IMAGES_EDITS)) {
415+
return this.imageProcessorStability(ImageProcessorType.EDITS, image, sessionId + ":" + message);
416+
} else if (choiceText.contains(IMAGES_VARIATIONS)) {
417+
return this.imageProcessor(ImageProcessorType.VARIATIONS, image, sessionId + ":" + message);
424418
}
419+
420+
return choiceText;
425421
}
426422
} else if (apiResponse.get("error") != null) {
427423
Builder error = (Builder) apiResponse.get("error");
@@ -474,24 +470,19 @@ private String chat(String sessionId, String message, String image) throws Appli
474470
Builder apiResponse = (Builder) ApplicationManager.call("openai", context);
475471
assert apiResponse != null;
476472
Builders builders;
477-
if (apiResponse.get("choices") != null) {
478-
builders = (Builders) apiResponse.get("choices");
479-
480-
if (builders.get(0).size() > 0) {
481-
Builder choice = builders.get(0);
482-
483-
if (choice.get("text") != null) {
484-
String choiceText = choice.get("text").toString();
485-
if (choiceText.contains(IMAGES_GENERATIONS)) {
486-
return this.imageProcessorStability(ImageProcessorType.GENERATIONS, null, sessionId + ":" + message);
487-
} else if (choiceText.contains(IMAGES_EDITS)) {
488-
return this.imageProcessorStability(ImageProcessorType.EDITS, image, sessionId + ":" + message);
489-
} else if (choiceText.contains(IMAGES_VARIATIONS)) {
490-
return this.imageProcessor(ImageProcessorType.VARIATIONS, image, sessionId + ":" + message);
491-
}
492-
493-
return choiceText;
473+
if ((builders = (Builders) apiResponse.get("choices")) != null && builders.get(0).size() > 0) {
474+
Builder choice = builders.get(0);
475+
if (choice.get("text") != null) {
476+
String choiceText = choice.get("text").toString();
477+
if (choiceText.contains(IMAGES_GENERATIONS)) {
478+
return this.imageProcessorStability(ImageProcessorType.GENERATIONS, null, sessionId + ":" + message);
479+
} else if (choiceText.contains(IMAGES_EDITS)) {
480+
return this.imageProcessorStability(ImageProcessorType.EDITS, image, sessionId + ":" + message);
481+
} else if (choiceText.contains(IMAGES_VARIATIONS)) {
482+
return this.imageProcessor(ImageProcessorType.VARIATIONS, image, sessionId + ":" + message);
494483
}
484+
485+
return choiceText;
495486
}
496487
} else if (apiResponse.get("error") != null) {
497488
Builder error = (Builder) apiResponse.get("error");

0 commit comments

Comments
 (0)