Skip to content

Commit

Permalink
Examples simple (#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zevleg authored Jul 12, 2023
1 parent 4e94b80 commit 851537b
Show file tree
Hide file tree
Showing 7 changed files with 239 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.xebia.functional.xef.java.auto;

import java.util.concurrent.ExecutionException;

public class Book {

public String title;
public String author;
public String summary;

public static void main(String[] args) throws ExecutionException, InterruptedException {
try (AIScope scope = new AIScope()) {
scope.prompt("To Kill a Mockingbird by Harper Lee summary.", Book.class)
.thenAccept(book -> System.out.println("To Kill a Mockingbird summary:\n" + book.summary))
.get();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.xebia.functional.xef.java.auto;

import java.util.ArrayList;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;

public class ChessAI {

private static class ChessMove {
public String player;
public String move;

@Override
public String toString() {
return "ChessMove{" +
"player='" + player + '\'' +
", move='" + move + '\'' +
'}';
}
}

private static class ChessBoard {
public String board;
}

private static class GameState {
public Boolean ended;
public String winner;
}

public static void main(String[] args) throws ExecutionException, InterruptedException {
try (AIScope scope = new AIScope()) {
var moves = new ArrayList<ChessMove>();
var gameEnded = false;
var winner = "";

while (!gameEnded) {
var currentPlayer = ((moves.size() % 2) == 0) ? "Player 1 (White)" : "Player 2 (Black)";

var prompt = String.format("""
|%s, it's your turn.
|Previous moves: %s
|Make your next move:""",
currentPlayer,
moves.stream().map(ChessMove::toString).collect(Collectors.joining(", ")));

ChessMove move = scope.prompt(prompt, ChessMove.class).get();
moves.add(move);

// Update boardState according to move.move
// ...

var boardPrompt = String.format("""
Given the following chess moves: %s,
generate a chess board on a table with appropriate emoji representations for each move and piece.
Add a brief description of the move and it's implications""",
moves.stream().map(it -> it.player + ":" + it.move).collect(Collectors.joining(", ")));

ChessBoard chessBoard= scope.prompt(boardPrompt, ChessBoard.class).get();
System.out.println("Current board:\n" + chessBoard.board);

var gameStatePrompt = String.format("""
Given the following chess moves: %s,
has the game ended (win, draw, or stalemate)?""",
moves.stream().map(ChessMove::toString).collect(Collectors.joining(", ")));

GameState gameState = scope.prompt(gameStatePrompt, GameState.class).get();

gameEnded = gameState.ended;
winner = gameState.winner;
}

System.out.println("Game over. Final move: " + moves.get(moves.size() - 1) + ", Winner: " + winner);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.xebia.functional.xef.java.auto;

import java.util.List;
import java.util.concurrent.ExecutionException;

public class Colors {

public List<String> colors;

public static void main(String[] args) throws ExecutionException, InterruptedException {
try (AIScope scope = new AIScope()) {
scope.prompt("a selection of 10 beautiful colors that go well together", Colors.class)
.thenAccept(colors -> System.out.println("Colors:\n" + colors.colors))
.get();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.xebia.functional.xef.java.auto;

import java.util.concurrent.ExecutionException;

public class Employee {

public String firstName;
public String lastName;
public Integer age;
public String position;
public Company company;

private static class Address {
public String street;
public String city;
public String country;
}

private static class Company {
public String name;
public Address address;
}

public static String complexPrompt =
"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" +
"Use the information provided.";

public static void main(String[] args) throws ExecutionException, InterruptedException {
try (AIScope scope = new AIScope()) {
scope.prompt(complexPrompt, Employee.class)
.thenAccept(employeeData -> System.out.println(
"Employee Information:\n\n" +
"Name: " + employeeData.firstName + " " + employeeData.lastName + "\n" +
"Age: " + employeeData.age + "\n" +
"Position: " + employeeData.position + "\n" +
"Company: " + employeeData.company.name + "\n" +
"Address: " + employeeData.company.address.street + ", " + employeeData.company.address.city + ", " + employeeData.company.address.country + "."
))
.get();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.xebia.functional.xef.java.auto;

import java.util.concurrent.ExecutionException;

public class Fact {

private static class FactClass {
public String topic;
public String content;

@Override
public String toString() {
return "FactClass{" +
"topic='" + topic + '\'' +
", content='" + content + '\'' +
'}';
}
}

private static class Riddle {
public FactClass fact1;
public FactClass fact2;
public String riddle;

@Override
public String toString() {
return "Riddle{" +
"fact1=" + fact1 +
", fact2=" + fact2 +
", riddle='" + riddle + '\'' +
'}';
}
}



public static void main(String[] args) throws ExecutionException, InterruptedException {
try (AIScope scope = new AIScope()) {
var fact1 = scope.prompt("A fascinating fact about you", FactClass.class).get();
var fact2 = scope.prompt("An interesting fact about me", FactClass.class).get();

String riddlePrompt = ""+
"Create a riddle that combines the following facts:\n\n" +

"Fact 1: " + fact1.content + "\n" +
"Fact 2: " + fact2.content;

scope.prompt(riddlePrompt, Riddle.class)
.thenAccept(riddle -> System.out.println("Riddle:\n\n" + riddle)).get();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.xebia.functional.xef.java.auto;

import java.util.List;
import java.util.concurrent.ExecutionException;

public class Love {
public List<String> loveList;
public static void main(String[] args) throws ExecutionException, InterruptedException {
try (AIScope scope = new AIScope()) {
scope.prompt("tell me you like me with just emojis", Love.class)
.thenAccept(love -> System.out.println(love.loveList))
.get();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.xebia.functional.xef.java.auto;

import java.util.List;
import java.util.concurrent.ExecutionException;

public class MeaningOfLife {
public List<String> mainTheories;

public static void main(String[] args) throws ExecutionException, InterruptedException {
try (AIScope scope = new AIScope()) {
scope.prompt("What are the main theories about the meaning of life", MeaningOfLife.class)
.thenAccept(meaningOfLife ->
System.out.println("There are several theories about the meaning of life:\n" + meaningOfLife.mainTheories))
.get();
}
}
}

0 comments on commit 851537b

Please sign in to comment.