Skip to content

Commit 6c58cb2

Browse files
authored
Split jdk8 jdk21 (#277)
* Init jdk21 * Add Executors.newVirtualThreadPerTaskExecutor() with error * Add jdk8 examples * Refactor jdk8 examples * Refactor jdk21 examples * Remove code commented
1 parent 7b74452 commit 6c58cb2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+1396
-98
lines changed

examples/java/build.gradle.kts

+8-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ dependencies {
1111
implementation(projects.xefGpt4all)
1212
}
1313

14-
tasks.withType<Test>().configureEach {
15-
useJUnit()
14+
val ENABLE_PREVIEW = "--enable-preview"
15+
16+
tasks.withType<JavaCompile> {
17+
options.compilerArgs.add(ENABLE_PREVIEW)
18+
}
19+
tasks.test {
20+
useJUnitPlatform()
21+
jvmArgs(ENABLE_PREVIEW)
1622
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.xebia.functional.xef.java.auto.jdk21;
2+
3+
import com.xebia.functional.xef.java.auto.AIScope;
4+
import com.xebia.functional.xef.java.auto.ExecutionContext;
5+
import java.util.concurrent.ExecutionException;
6+
import java.util.concurrent.Executors;
7+
8+
public class ASCIIArt {
9+
public String art;
10+
11+
public static void main(String[] args) throws ExecutionException, InterruptedException {
12+
try (AIScope scope = new AIScope(new ExecutionContext(Executors.newVirtualThreadPerTaskExecutor()))) {
13+
scope.prompt("ASCII art of a cat dancing", ASCIIArt.class)
14+
.thenAccept(art -> System.out.println(art.art))
15+
.get();
16+
}
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.xebia.functional.xef.java.auto.jdk21;
2+
3+
import com.xebia.functional.xef.java.auto.AIScope;
4+
import com.xebia.functional.xef.java.auto.ExecutionContext;
5+
import java.util.concurrent.CompletableFuture;
6+
import java.util.concurrent.ExecutionException;
7+
import java.util.concurrent.Executors;
8+
9+
public class Animals {
10+
11+
private final AIScope scope;
12+
13+
public Animals(AIScope scope) {
14+
this.scope = scope;
15+
}
16+
17+
public CompletableFuture<Animal> uniqueAnimal() {
18+
return scope.prompt("A unique animal species.", Animal.class);
19+
}
20+
21+
public CompletableFuture<Invention> groundbreakingInvention() {
22+
return scope.prompt("A groundbreaking invention from the 20th century.", Invention.class);
23+
}
24+
25+
public CompletableFuture<Story> story(Animal animal, Invention invention) {
26+
String storyPrompt =
27+
"Write a short story of 500 words that involves the following elements:" +
28+
"1. A unique animal species called ${animal.name} that lives in " + animal.habitat + " and has a diet of " + animal.diet + "." +
29+
"2. A groundbreaking invention from the 20th century called " + invention.name + " , invented by " + invention.inventor + " in " + invention.year + ", which serves the purpose of " + invention.purpose + ".";
30+
return scope.prompt(storyPrompt, Story.class);
31+
}
32+
33+
public record Animal(String name, String habitat, String diet){}
34+
public record Invention(String name, String inventor, int year, String purpose){}
35+
public record Story(Animal animal, Invention invention, String text){
36+
public void tell() {
37+
System.out.println("Story about " + animal.name + " and " + invention.name + ": " + text);
38+
}
39+
}
40+
41+
public static void main(String[] args) throws ExecutionException, InterruptedException {
42+
try (AIScope scope = new AIScope(new ExecutionContext(Executors.newVirtualThreadPerTaskExecutor()))) {
43+
Animals animals = new Animals(scope);
44+
animals.uniqueAnimal()
45+
.thenCompose(animal ->
46+
animals.groundbreakingInvention()
47+
.thenCompose(invention ->
48+
animals.story(animal, invention)
49+
.thenAccept(Story::tell)
50+
)).get();
51+
}
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.xebia.functional.xef.java.auto.jdk21;
2+
3+
import com.xebia.functional.xef.java.auto.AIScope;
4+
import com.xebia.functional.xef.java.auto.ExecutionContext;
5+
import java.util.concurrent.ExecutionException;
6+
import java.util.concurrent.Executors;
7+
8+
public class Book {
9+
10+
public String title;
11+
public String author;
12+
public String summary;
13+
14+
public static void main(String[] args) throws ExecutionException, InterruptedException {
15+
try (AIScope scope = new AIScope(new ExecutionContext(Executors.newVirtualThreadPerTaskExecutor()))) {
16+
scope.prompt("To Kill a Mockingbird by Harper Lee summary.", Book.class)
17+
.thenAccept(book -> System.out.println("To Kill a Mockingbird summary:\n" + book.summary))
18+
.get();
19+
}
20+
}
21+
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.xebia.functional.xef.java.auto.jdk21;
2+
3+
import com.xebia.functional.xef.java.auto.AIScope;
4+
import com.xebia.functional.xef.java.auto.ExecutionContext;
5+
import jakarta.validation.constraints.NotNull;
6+
import java.util.concurrent.CompletableFuture;
7+
import java.util.concurrent.ExecutionException;
8+
import java.util.concurrent.Executors;
9+
10+
public class Books {
11+
12+
private final AIScope scope;
13+
14+
public Books(AIScope scope) {
15+
this.scope = scope;
16+
}
17+
18+
public record Book(@NotNull String title, @NotNull String author, @NotNull int year, @NotNull String genre){}
19+
20+
public CompletableFuture<Books.Book> bookSelection(String topic) {
21+
return scope.prompt("Give me a selection of books about " + topic, Books.Book.class);
22+
}
23+
24+
public static void main(String[] args) throws ExecutionException, InterruptedException {
25+
try (AIScope scope = new AIScope(new ExecutionContext(Executors.newVirtualThreadPerTaskExecutor()))) {
26+
Books books = new Books(scope);
27+
books.bookSelection("artificial intelligence")
28+
.thenAccept(System.out::println)
29+
.get();
30+
}
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.xebia.functional.xef.java.auto.jdk21;
2+
3+
import com.xebia.functional.xef.java.auto.AIScope;
4+
import com.xebia.functional.xef.java.auto.ExecutionContext;
5+
import java.time.LocalDateTime;
6+
import java.time.format.DateTimeFormatter;
7+
import java.util.concurrent.CompletableFuture;
8+
import java.util.concurrent.ExecutionException;
9+
import java.util.concurrent.Executors;
10+
11+
public class BreakingNews {
12+
13+
static DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd/M/yyyy");
14+
static LocalDateTime now = LocalDateTime.now();
15+
16+
public record BreakingNew(String summary){}
17+
18+
private static CompletableFuture<Void> writeParagraph(AIScope scope) {
19+
var currentDate = dtf.format(now);
20+
21+
return scope.prompt("write a paragraph of about 300 words about: " + currentDate + " Covid News", BreakingNews.BreakingNew.class)
22+
.thenAccept(breakingNews -> System.out.println(currentDate + " Covid news summary:\n" + breakingNews));
23+
}
24+
25+
public static void main(String[] args) throws ExecutionException, InterruptedException {
26+
try (AIScope scope = new AIScope(new ExecutionContext(Executors.newVirtualThreadPerTaskExecutor()))) {
27+
var currentDate = dtf.format(now);
28+
scope.contextScope(scope.search(currentDate + " Covid News"), BreakingNews::writeParagraph).get();
29+
}
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.xebia.functional.xef.java.auto.jdk21;
2+
3+
import com.xebia.functional.xef.java.auto.AIScope;
4+
import com.xebia.functional.xef.java.auto.ExecutionContext;
5+
import java.util.ArrayList;
6+
import java.util.concurrent.ExecutionException;
7+
import java.util.concurrent.Executors;
8+
import java.util.stream.Collectors;
9+
10+
public class ChessAI {
11+
12+
public record ChessMove(String player, String move){}
13+
public record ChessBoard(String board){}
14+
public record GameState(Boolean ended, String winner){}
15+
16+
public static void main(String[] args) throws ExecutionException, InterruptedException {
17+
try (AIScope scope = new AIScope(new ExecutionContext(Executors.newVirtualThreadPerTaskExecutor()))) {
18+
var moves = new ArrayList<ChessMove>();
19+
var gameEnded = false;
20+
var winner = "";
21+
22+
while (!gameEnded) {
23+
var currentPlayer = ((moves.size() % 2) == 0) ? "Player 1 (White)" : "Player 2 (Black)";
24+
25+
var prompt = String.format("""
26+
|%s, it's your turn.
27+
|Previous moves: %s
28+
|Make your next move:""",
29+
currentPlayer,
30+
moves.stream().map(ChessMove::toString).collect(Collectors.joining(", ")));
31+
32+
ChessMove move = scope.prompt(prompt, ChessMove.class).get();
33+
moves.add(move);
34+
35+
// Update boardState according to move.move
36+
// ...
37+
38+
var boardPrompt = String.format("""
39+
Given the following chess moves: %s,
40+
generate a chess board on a table with appropriate emoji representations for each move and piece.
41+
Add a brief description of the move and it's implications""",
42+
moves.stream().map(it -> it.player + ":" + it.move).collect(Collectors.joining(", ")));
43+
44+
ChessBoard chessBoard= scope.prompt(boardPrompt, ChessBoard.class).get();
45+
System.out.println("Current board:\n" + chessBoard.board);
46+
47+
var gameStatePrompt = String.format("""
48+
Given the following chess moves: %s,
49+
has the game ended (win, draw, or stalemate)?""",
50+
moves.stream().map(ChessMove::toString).collect(Collectors.joining(", ")));
51+
52+
GameState gameState = scope.prompt(gameStatePrompt, GameState.class).get();
53+
54+
gameEnded = gameState.ended;
55+
winner = gameState.winner;
56+
}
57+
58+
System.out.println("Game over. Final move: " + moves.get(moves.size() - 1) + ", Winner: " + winner);
59+
}
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.xebia.functional.xef.java.auto.jdk21;
2+
3+
import com.xebia.functional.xef.java.auto.AIScope;
4+
import com.xebia.functional.xef.java.auto.ExecutionContext;
5+
import java.util.List;
6+
import java.util.concurrent.ExecutionException;
7+
import java.util.concurrent.Executors;
8+
9+
public class Colors {
10+
11+
public List<String> colors;
12+
13+
public static void main(String[] args) throws ExecutionException, InterruptedException {
14+
try (AIScope scope = new AIScope(new ExecutionContext(Executors.newVirtualThreadPerTaskExecutor()))) {
15+
scope.prompt("a selection of 10 beautiful colors that go well together", Colors.class)
16+
.thenAccept(colors -> System.out.println("Colors:\n" + colors.colors))
17+
.get();
18+
}
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.xebia.functional.xef.java.auto.jdk21;
2+
3+
import com.xebia.functional.xef.java.auto.AIScope;
4+
import com.xebia.functional.xef.java.auto.ExecutionContext;
5+
import java.util.concurrent.CompletableFuture;
6+
import java.util.concurrent.ExecutionException;
7+
import java.util.concurrent.Executors;
8+
9+
public class DivergentTasks {
10+
11+
public Long numberOfMedicalNeedlesInWorld;
12+
13+
private static CompletableFuture<Void> numberOfMedical(AIScope scope) {
14+
return scope.prompt("Provide the number of medical needles in the world", DivergentTasks.class)
15+
.thenAccept(numberOfNeedles -> System.out.println("Needles in world:\n" + numberOfNeedles.numberOfMedicalNeedlesInWorld));
16+
}
17+
18+
public static void main(String[] args) throws ExecutionException, InterruptedException {
19+
try (AIScope scope = new AIScope(new ExecutionContext(Executors.newVirtualThreadPerTaskExecutor()))) {
20+
scope.contextScope(scope.search("Estimate amount of medical needles in the world"),
21+
DivergentTasks::numberOfMedical).get();
22+
}
23+
}
24+
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.xebia.functional.xef.java.auto.jdk21;
2+
3+
import com.xebia.functional.xef.java.auto.AIScope;
4+
import com.xebia.functional.xef.java.auto.ExecutionContext;
5+
import java.util.concurrent.ExecutionException;
6+
import java.util.concurrent.Executors;
7+
8+
public class Employees {
9+
10+
public record Employee(String firstName, String lastName, Integer age, String position, Company company){}
11+
public record Address(String street, String city, String country){}
12+
public record Company(String name, Address address){}
13+
14+
public static String complexPrompt =
15+
"Provide made up information for an Employee that includes their first name, last name, age, position, and their company's name and address (street, city, and country).\n" +
16+
"Use the information provided.";
17+
18+
public static void main(String[] args) throws ExecutionException, InterruptedException {
19+
try (AIScope scope = new AIScope(new ExecutionContext(Executors.newVirtualThreadPerTaskExecutor()))) {
20+
scope.prompt(complexPrompt, Employees.Employee.class)
21+
.thenAccept(employeeData -> System.out.println(
22+
"Employee Information:\n\n" +
23+
"Name: " + employeeData.firstName + " " + employeeData.lastName + "\n" +
24+
"Age: " + employeeData.age + "\n" +
25+
"Position: " + employeeData.position + "\n" +
26+
"Company: " + employeeData.company.name + "\n" +
27+
"Address: " + employeeData.company.address.street + ", " + employeeData.company.address.city + ", " + employeeData.company.address.country + "."
28+
))
29+
.get();
30+
}
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.xebia.functional.xef.java.auto.jdk21;
2+
3+
import com.xebia.functional.xef.java.auto.AIScope;
4+
import com.xebia.functional.xef.java.auto.ExecutionContext;
5+
import java.util.concurrent.ExecutionException;
6+
import java.util.concurrent.Executors;
7+
8+
public class Fact {
9+
10+
public record FactRecord(String topic, String content){}
11+
public record Riddle(FactRecord fact1, FactRecord fact2, String riddle){}
12+
13+
public static void main(String[] args) throws ExecutionException, InterruptedException {
14+
try (AIScope scope = new AIScope(new ExecutionContext(Executors.newVirtualThreadPerTaskExecutor()))) {
15+
var fact1 = scope.prompt("A fascinating fact about you", FactRecord.class).get();
16+
var fact2 = scope.prompt("An interesting fact about me", FactRecord.class).get();
17+
18+
var riddlePrompt = ""+
19+
"Create a riddle that combines the following facts:\n\n" +
20+
21+
"Fact 1: " + fact1.content + "\n" +
22+
"Fact 2: " + fact2.content;
23+
24+
scope.prompt(riddlePrompt, Riddle.class)
25+
.thenAccept(riddle -> System.out.println("Riddle:\n\n" + riddle)).get();
26+
}
27+
}
28+
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.xebia.functional.xef.java.auto.jdk21;
2+
3+
import com.xebia.functional.xef.java.auto.AIScope;
4+
import com.xebia.functional.xef.java.auto.ExecutionContext;
5+
import java.util.List;
6+
import java.util.concurrent.ExecutionException;
7+
import java.util.concurrent.Executors;
8+
9+
public class Love {
10+
public List<String> loveList;
11+
public static void main(String[] args) throws ExecutionException, InterruptedException {
12+
try (AIScope scope = new AIScope(new ExecutionContext(Executors.newVirtualThreadPerTaskExecutor()))) {
13+
scope.prompt("tell me you like me with just emojis", Love.class)
14+
.thenAccept(love -> System.out.println(love.loveList))
15+
.get();
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)