-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add 'Classification' example and update 'Question Answering (RAG)'
- Loading branch information
1 parent
5fef7f6
commit b1b045b
Showing
23 changed files
with
332 additions
and
23 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Classification | ||
|
||
Classification with LLMs via Ollama. | ||
|
||
## Running the application | ||
|
||
The application relies on Ollama for providing LLMs. You can either run Ollama locally on your laptop, or rely on the Testcontainers support in Spring Boot to spin up an Ollama service automatically. | ||
|
||
### Ollama as a native application | ||
|
||
First, make sure you have [Ollama](https://ollama.ai) installed on your laptop. | ||
Then, use Ollama to run the _mistral_ large language model. | ||
|
||
```shell | ||
ollama run mistral | ||
``` | ||
|
||
Finally, run the Spring Boot application. | ||
|
||
```shell | ||
./gradlew bootRun | ||
``` | ||
|
||
### Ollama as a dev service with Testcontainers | ||
|
||
The application relies on the native Testcontainers support in Spring Boot to spin up an Ollama service with a _mistral_ model at startup time. | ||
|
||
```shell | ||
./gradlew bootTestRun | ||
``` | ||
|
||
## Calling the application | ||
|
||
You can now call the application that will use Ollama and _mistral_ to classify your text. | ||
This example uses [httpie](https://httpie.io) to send HTTP requests. | ||
|
||
Simple prompt: | ||
|
||
```shell | ||
http --raw "The piano is an instrument that evokes warmth and introspection, creating a sense of intimacy and drama." :8080/classify/simple | ||
``` | ||
|
||
Structured prompt: | ||
|
||
```shell | ||
http --raw "The piano is an instrument that evokes warmth and introspection, creating a sense of intimacy and drama." :8080/classify/structured | ||
``` | ||
|
||
Few shots simple prompt: | ||
|
||
```shell | ||
http --raw "The piano is an instrument that evokes warmth and introspection, creating a sense of intimacy and drama." :8080/classify/few-shots-prompt | ||
``` | ||
|
||
Few shots with history prompt: | ||
|
||
```shell | ||
http --raw "The piano is an instrument that evokes warmth and introspection, creating a sense of intimacy and drama." :8080/classify/few-shots-history | ||
``` |
File renamed without changes.
13 changes: 13 additions & 0 deletions
13
00-use-cases/classification/src/main/java/com/thomasvitale/ai/spring/Classification.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,13 @@ | ||
package com.thomasvitale.ai.spring; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class Classification { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(Classification.class, args); | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
...ses/classification/src/main/java/com/thomasvitale/ai/spring/ClassificationController.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,36 @@ | ||
package com.thomasvitale.ai.spring; | ||
|
||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
class ClassificationController { | ||
|
||
private final ClassificationService classificationService; | ||
|
||
ClassificationController(ClassificationService classificationService) { | ||
this.classificationService = classificationService; | ||
} | ||
|
||
@PostMapping("/classify/simple") | ||
String classifySimple(@RequestBody String input) { | ||
return classificationService.classifySimple(input); | ||
} | ||
|
||
@PostMapping("/classify/structured") | ||
ClassificationType classifyStructured(@RequestBody String input) { | ||
return classificationService.classifyStructured(input); | ||
} | ||
|
||
@PostMapping("/classify/few-shots-prompt") | ||
ClassificationType classifyFewShotsPrompt(@RequestBody String input) { | ||
return classificationService.classifyFewShotsPrompt(input); | ||
} | ||
|
||
@PostMapping("/classify/few-shots-history") | ||
ClassificationType classifyFewShotsHistory(@RequestBody String input) { | ||
return classificationService.classifyFewShotsHistory(input); | ||
} | ||
|
||
} |
106 changes: 106 additions & 0 deletions
106
...-cases/classification/src/main/java/com/thomasvitale/ai/spring/ClassificationService.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,106 @@ | ||
package com.thomasvitale.ai.spring; | ||
|
||
import org.springframework.ai.chat.client.ChatClient; | ||
import org.springframework.ai.chat.messages.AssistantMessage; | ||
import org.springframework.ai.chat.messages.Message; | ||
import org.springframework.ai.chat.messages.SystemMessage; | ||
import org.springframework.ai.chat.messages.UserMessage; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
|
||
@Service | ||
class ClassificationService { | ||
|
||
private final ChatClient chatClient; | ||
|
||
ClassificationService(ChatClient.Builder chatClientBuilder) { | ||
this.chatClient = chatClientBuilder.build(); | ||
} | ||
|
||
String classifySimple(String message) { | ||
return chatClient | ||
.prompt() | ||
.user(userSpec -> userSpec.text(""" | ||
Classify the type of the provided text and return the classification type as "BOOK" OR "MUSIC". | ||
{text} | ||
""") | ||
.param("text", message)) | ||
.call() | ||
.content(); | ||
} | ||
|
||
ClassificationType classifyStructured(String message) { | ||
return chatClient | ||
.prompt() | ||
.user(userSpec -> userSpec.text(""" | ||
Classify the type of the provided text and return the classification type. | ||
--------------------- | ||
TEXT: | ||
{text} | ||
--------------------- | ||
""") | ||
.param("text", message)) | ||
.user(message) | ||
.call() | ||
.entity(ClassificationType.class); | ||
} | ||
|
||
ClassificationType classifyFewShotsPrompt(String message) { | ||
return chatClient | ||
.prompt() | ||
.user(userSpec -> userSpec.text(""" | ||
Classify the type of the provided text and return the classification type. | ||
For example: | ||
Input: The celesta is a good instrument for fantasy or mystery compositions, creating a sense of mystical and supernatural. | ||
Output: "MUSIC" | ||
Input: In "The Lord of The Rings", Tolkien wrote the famous words: "There’s some good in this world, Mr. Frodo… and it’s worth fighting for.". | ||
Output: "BOOK" | ||
Input: They're taking the hobbits to Isengard! To Isengard! To Isengard! | ||
Output: "UNKNOWN" | ||
--------------------- | ||
TEXT: | ||
{text} | ||
--------------------- | ||
""") | ||
.param("text", message)) | ||
.call() | ||
.entity(ClassificationType.class); | ||
} | ||
|
||
ClassificationType classifyFewShotsHistory(String message) { | ||
return chatClient | ||
.prompt() | ||
.messages(getPromptWithFewShotsHistory(message)) | ||
.call() | ||
.entity(ClassificationType.class); | ||
} | ||
|
||
private List<Message> getPromptWithFewShotsHistory(String message) { | ||
return List.of( | ||
new SystemMessage(""" | ||
Classify the type of the provided text and return the classification type. | ||
"""), | ||
new UserMessage(""" | ||
The celesta is a good instrument for fantasy or mystery compositions, creating a sense of mystical and supernatural. | ||
"""), | ||
new AssistantMessage("MUSIC"), | ||
new UserMessage(""" | ||
In "The Lord of The Rings", Tolkien wrote the famous words: "There’s some good in this world, Mr. Frodo… and it’s worth fighting for.". | ||
"""), | ||
new AssistantMessage("BOOK"), | ||
new UserMessage(""" | ||
They're taking the hobbits to Isengard! To Isengard! To Isengard! | ||
"""), | ||
new AssistantMessage("UNKNOWN"), | ||
new UserMessage(message) | ||
); | ||
} | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
00-use-cases/classification/src/main/java/com/thomasvitale/ai/spring/ClassificationType.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,5 @@ | ||
package com.thomasvitale.ai.spring; | ||
|
||
public enum ClassificationType { | ||
BOOK, MUSIC, UNKNOWN; | ||
} |
9 changes: 9 additions & 0 deletions
9
00-use-cases/classification/src/main/resources/application.yml
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,9 @@ | ||
spring: | ||
ai: | ||
ollama: | ||
chat: | ||
options: | ||
model: mistral | ||
threads: | ||
virtual: | ||
enabled: true |
13 changes: 13 additions & 0 deletions
13
...se-cases/classification/src/test/java/com/thomasvitale/ai/spring/ClassificationTests.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,13 @@ | ||
package com.thomasvitale.ai.spring; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
@SpringBootTest | ||
class ClassificationTests { | ||
|
||
@Test | ||
void contextLoads() { | ||
} | ||
|
||
} |
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
plugins { | ||
id 'java' | ||
id 'org.springframework.boot' | ||
id 'io.spring.dependency-management' | ||
} | ||
|
||
group = 'com.thomasvitale' | ||
version = '0.0.1-SNAPSHOT' | ||
|
||
java { | ||
toolchain { | ||
languageVersion = JavaLanguageVersion.of(21) | ||
} | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
maven { url 'https://repo.spring.io/milestone' } | ||
maven { url 'https://repo.spring.io/snapshot' } | ||
} | ||
|
||
dependencies { | ||
implementation platform("org.springframework.ai:spring-ai-bom:${springAiVersion}") | ||
|
||
implementation 'org.springframework.boot:spring-boot-starter-web' | ||
implementation 'org.springframework.ai:spring-ai-ollama-spring-boot-starter' | ||
|
||
testAndDevelopmentOnly 'org.springframework.boot:spring-boot-devtools' | ||
|
||
testImplementation 'org.springframework.boot:spring-boot-starter-test' | ||
testImplementation 'org.springframework.boot:spring-boot-starter-webflux' | ||
testImplementation 'org.springframework.boot:spring-boot-testcontainers' | ||
testImplementation 'org.springframework.ai:spring-ai-spring-boot-testcontainers' | ||
testImplementation 'org.testcontainers:junit-jupiter' | ||
testImplementation 'org.testcontainers:ollama' | ||
} | ||
|
||
tasks.named('test') { | ||
useJUnitPlatform() | ||
} |
File renamed without changes.
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
Oops, something went wrong.