From 374c0428c39d5e6f3f8424b669640ce4902ca8fa Mon Sep 17 00:00:00 2001 From: Thomas Vitale Date: Thu, 13 Jun 2024 23:46:39 +0200 Subject: [PATCH] Improve text classification, update Gradle, configure GraalVM --- .sdkmanrc | 2 +- 00-use-cases/text-classification/README.md | 10 ++++----- 00-use-cases/text-classification/build.gradle | 2 +- .../ai/spring/ClassificationController.java | 21 ++++++++++++------- .../ai/spring/TextClassifier.java | 6 +++--- .../src/main/resources/application.yml | 3 --- .../ai/spring/TextClassifierTests.java | 7 +++++++ buildSrc/build.gradle | 2 ++ gradle.properties | 2 +- gradle/wrapper/gradle-wrapper.properties | 2 +- gradlew | 2 +- 11 files changed, 35 insertions(+), 24 deletions(-) diff --git a/.sdkmanrc b/.sdkmanrc index fca95be..2225755 100644 --- a/.sdkmanrc +++ b/.sdkmanrc @@ -3,4 +3,4 @@ # See https://sdkman.io/usage#config # A summary is to add the following to ~/.sdkman/etc/config # sdkman_auto_env=true -java=21.0.2-tem +java=22-oracle diff --git a/00-use-cases/text-classification/README.md b/00-use-cases/text-classification/README.md index 76ba65a..82dd676 100644 --- a/00-use-cases/text-classification/README.md +++ b/00-use-cases/text-classification/README.md @@ -39,29 +39,29 @@ Each endpoint is backed by a progressively better prompt to increase the quality Class Names: ```shell -http --raw "NBA announced a new application to enhance the experience of watching basketball on Apple Vision Pro." :8080/classify/class-names +http --raw "Basketball fans can now watch the game on the brand-new NBA app for Apple Vision Pro." :8080/classify/class-names ``` Class Descriptions: ```shell -http --raw "NBA announced a new application to enhance the experience of watching basketball on Apple Vision Pro." :8080/classify/class-descriptions +http --raw "Basketball fans can now watch the game on the brand-new NBA app for Apple Vision Pro." :8080/classify/class-descriptions ``` Few Shots Prompt: ```shell -http --raw "NBA announced a new application to enhance the experience of watching basketball on Apple Vision Pro." :8080/classify/few-shots-prompt +http --raw "Basketball fans can now watch the game on the brand-new NBA app for Apple Vision Pro." :8080/classify/few-shots-prompt ``` Few Shots History: ```shell -http --raw "NBA announced a new application to enhance the experience of watching basketball on Apple Vision Pro." :8080/classify/few-shots-history +http --raw "Basketball fans can now watch the game on the brand-new NBA app for Apple Vision Pro." :8080/classify/few-shots-history ``` Structured Output: ```shell -http --raw "NBA announced a new application to enhance the experience of watching basketball on Apple Vision Pro." :8080/classify/structured-output +http --raw "Basketball fans can now watch the game on the brand-new NBA app for Apple Vision Pro." :8080/classify/structured-output ``` diff --git a/00-use-cases/text-classification/build.gradle b/00-use-cases/text-classification/build.gradle index 3ce5e6f..361a88e 100644 --- a/00-use-cases/text-classification/build.gradle +++ b/00-use-cases/text-classification/build.gradle @@ -9,7 +9,7 @@ version = '0.0.1-SNAPSHOT' java { toolchain { - languageVersion = JavaLanguageVersion.of(21) + languageVersion = JavaLanguageVersion.of(22) } } diff --git a/00-use-cases/text-classification/src/main/java/com/thomasvitale/ai/spring/ClassificationController.java b/00-use-cases/text-classification/src/main/java/com/thomasvitale/ai/spring/ClassificationController.java index 9c35ca9..5e1cb3f 100644 --- a/00-use-cases/text-classification/src/main/java/com/thomasvitale/ai/spring/ClassificationController.java +++ b/00-use-cases/text-classification/src/main/java/com/thomasvitale/ai/spring/ClassificationController.java @@ -19,23 +19,28 @@ String classifyClassNames(@RequestBody String text) { } @PostMapping("/classify/class-descriptions") - String classifyClassDescriptions(@RequestBody String input) { - return textClassifier.classifyClassDescriptions(input); + String classifyClassDescriptions(@RequestBody String text) { + return textClassifier.classifyClassDescriptions(text); } @PostMapping("/classify/few-shots-prompt") - String classifyFewShotsPrompt(@RequestBody String input) { - return textClassifier.classifyFewShotsPrompt(input); + String classifyFewShotsPrompt(@RequestBody String text) { + return textClassifier.classifyFewShotsPrompt(text); } @PostMapping("/classify/few-shots-history") - String classifyFewShotsHistory(@RequestBody String input) { - return textClassifier.classifyFewShotsHistory(input); + String classifyFewShotsHistory(@RequestBody String text) { + return textClassifier.classifyFewShotsHistory(text); } @PostMapping("/classify/structured-output") - ClassificationType classifyStructured(@RequestBody String input) { - return textClassifier.classifyStructured(input); + ClassificationType classifyStructured(@RequestBody String text) { + return textClassifier.classifyStructured(text); + } + + @PostMapping("/classify") + ClassificationType classify(@RequestBody String text) { + return textClassifier.classifyStructured(text); } } diff --git a/00-use-cases/text-classification/src/main/java/com/thomasvitale/ai/spring/TextClassifier.java b/00-use-cases/text-classification/src/main/java/com/thomasvitale/ai/spring/TextClassifier.java index 8b17e29..a1c0516 100644 --- a/00-use-cases/text-classification/src/main/java/com/thomasvitale/ai/spring/TextClassifier.java +++ b/00-use-cases/text-classification/src/main/java/com/thomasvitale/ai/spring/TextClassifier.java @@ -43,7 +43,7 @@ String classifyClassDescriptions(String text) { BUSINESS: Financial markets, economic trends, company acquisitions. SPORT: Sport events, teams, players, tournaments, and statistics. - TECHNOLOGY: Technological advancements, innovations, software product launches. + TECHNOLOGY: Technological advancements, innovations, launches of new apps. OTHER: Anything that doesn't fit into the other categories. """) .user(text) @@ -64,7 +64,7 @@ String classifyFewShotsPrompt(String text) { Text: The semifinals of the UEFA Euro 2024 will be played on Saturday. Class: SPORT - Text: Apple announced the new version of their operating system. + Text: Football fan? You can now watch the UEFA Euro games on the brand-new app for Apple Vision Pro. Class: TECHNOLOGY Text: The Ravenclaw Quidditch team won the tournament! @@ -101,7 +101,7 @@ private List getPromptWithFewShotsHistory(String text) { new AssistantMessage("BUSINESS"), new UserMessage("The semifinals of the UEFA Euro 2024 will be played on Saturday."), new AssistantMessage("SPORT"), - new UserMessage("Apple announced the new version of their operating system."), + new UserMessage("Football fan? You can now watch the UEFA Euro games on the brand-new app for Apple Vision Pro."), new AssistantMessage("TECHNOLOGY"), new UserMessage("The Ravenclaw Quidditch team won the tournament!"), new AssistantMessage("OTHER"), diff --git a/00-use-cases/text-classification/src/main/resources/application.yml b/00-use-cases/text-classification/src/main/resources/application.yml index 05b64d6..714dc9e 100644 --- a/00-use-cases/text-classification/src/main/resources/application.yml +++ b/00-use-cases/text-classification/src/main/resources/application.yml @@ -4,6 +4,3 @@ spring: chat: options: model: mistral - threads: - virtual: - enabled: true diff --git a/00-use-cases/text-classification/src/test/java/com/thomasvitale/ai/spring/TextClassifierTests.java b/00-use-cases/text-classification/src/test/java/com/thomasvitale/ai/spring/TextClassifierTests.java index a3c5976..8715870 100644 --- a/00-use-cases/text-classification/src/test/java/com/thomasvitale/ai/spring/TextClassifierTests.java +++ b/00-use-cases/text-classification/src/test/java/com/thomasvitale/ai/spring/TextClassifierTests.java @@ -28,4 +28,11 @@ void classifyTechnologyNews() { assertThat(classificationType).isEqualTo(ClassificationType.TECHNOLOGY); } + @Test + void classifyOtherNews() { + var classificationType = textClassifier.classifyStructured( + "They're taking the hobbits to Isengard! To Isengard! To Isengard!"); + assertThat(classificationType).isEqualTo(ClassificationType.OTHER); + } + } diff --git a/buildSrc/build.gradle b/buildSrc/build.gradle index 8d6a4a2..5ef54b8 100644 --- a/buildSrc/build.gradle +++ b/buildSrc/build.gradle @@ -9,9 +9,11 @@ repositories { ext { set("springBootVersion", '3.3.0') set("dependencyManagementVersion", '1.1.5') + set("graalvmVersion", '0.10.2') } dependencies { implementation "io.spring.gradle:dependency-management-plugin:${dependencyManagementVersion}" implementation "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}" + implementation "org.graalvm.buildtools:native-gradle-plugin:${graalvmVersion}" } diff --git a/gradle.properties b/gradle.properties index 9241745..c8f5543 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1 +1 @@ -springAiVersion=1.0.0-M1 \ No newline at end of file +springAiVersion=1.0.0-SNAPSHOT \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index b82aa23..a441313 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip networkTimeout=10000 validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME diff --git a/gradlew b/gradlew index 1aa94a4..b740cf1 100755 --- a/gradlew +++ b/gradlew @@ -55,7 +55,7 @@ # Darwin, MinGW, and NonStop. # # (3) This script is generated from the Groovy template -# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt # within the Gradle project. # # You can find Gradle at https://github.com/gradle/gradle/.