Skip to content

Commit

Permalink
Improve text classification example
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasVitale committed Jun 11, 2024
1 parent 0362449 commit 4e2ea1a
Show file tree
Hide file tree
Showing 26 changed files with 686 additions and 206 deletions.
59 changes: 0 additions & 59 deletions 00-use-cases/classification/README.md

This file was deleted.

This file was deleted.

This file was deleted.

67 changes: 67 additions & 0 deletions 00-use-cases/text-classification/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Text Classification

Text 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 pull the _mistral_ large language model.

```shell
ollama pull 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.

Each endpoint is backed by a progressively better prompt to increase the quality of the text classification task by the LLM.

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
```

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
```

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
```

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
```

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
```
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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 TextClassifier textClassifier;

ClassificationController(TextClassifier textClassifier) {
this.textClassifier = textClassifier;
}

@PostMapping("/classify/class-names")
String classifyClassNames(@RequestBody String text) {
return textClassifier.classifyClassNames(text);
}

@PostMapping("/classify/class-descriptions")
String classifyClassDescriptions(@RequestBody String input) {
return textClassifier.classifyClassDescriptions(input);
}

@PostMapping("/classify/few-shots-prompt")
String classifyFewShotsPrompt(@RequestBody String input) {
return textClassifier.classifyFewShotsPrompt(input);
}

@PostMapping("/classify/few-shots-history")
String classifyFewShotsHistory(@RequestBody String input) {
return textClassifier.classifyFewShotsHistory(input);
}

@PostMapping("/classify/structured-output")
ClassificationType classifyStructured(@RequestBody String input) {
return textClassifier.classifyStructured(input);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.thomasvitale.ai.spring;

public enum ClassificationType {
BOOK, MUSIC, UNKNOWN;
BUSINESS, SPORT, TECHNOLOGY, OTHER;
}
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 TextClassificationApplication {

public static void main(String[] args) {
SpringApplication.run(TextClassificationApplication.class, args);
}

}
Loading

0 comments on commit 4e2ea1a

Please sign in to comment.