Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Last half of Simple Java examples port #237

Merged
merged 4 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.xebia.functional.xef.java.auto;

import java.util.concurrent.ExecutionException;

public class Movies {

public static class Movie {
public String title;
public String genre;
public String director;

@Override
public String toString() {
return "The movie " + title + " is a " +
genre + " film directed by " + director + ".";
}
}

public static void main(String[] args) throws ExecutionException, InterruptedException {
try (AIScope scope = new AIScope()) {
scope.prompt("Please provide a movie title, genre and director for the Inception movie", Movie.class)
.thenAccept(movie -> System.out.println(movie))
.get();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.xebia.functional.xef.java.auto;

import java.util.concurrent.ExecutionException;

public class Persons {

public static class Person {
public String name;
public int age;

@Override
public String toString() {
return "Hello " + name + ", you are " + age + " years old.";
}
}

public static void main(String[] args) throws ExecutionException, InterruptedException {
try (AIScope scope = new AIScope()) {
scope.prompt("What is your name and age?", Person.class)
.thenAccept(person -> System.out.println(person))
.get();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.xebia.functional.xef.java.auto;

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

import static java.util.stream.Collectors.toList;

public class Planets {
static class Planet {
public String name;
public double distanceFromSun;
public List<Moon> moons;
}

static class Moon {
public String name;
public double distanceFromPlanetInKm;
}

public static void main(String[] args) throws ExecutionException, InterruptedException {
try (AIScope scope = new AIScope()) {
CompletableFuture<Planet> earth = scope.prompt("Information about Earth and its moon.", Planet.class);
CompletableFuture<Planet> mars = scope.prompt("Information about Mars and its moons.", Planet.class);

System.out.println("Celestial bodies information:\n\n" + planetInfo(earth.get()) + "\n\n" + planetInfo(mars.get()));
}
}

private static String planetInfo(Planet planet){
List<String> moonList = planet.moons.stream().map(it -> " - " + it.name + ": " + it.distanceFromPlanetInKm + " km away from " + planet.name).toList();

return String.format("%s is %s million km away from the Sun.\n" +
"It has the following moons: \n" +
"%s", planet.name, planet.distanceFromSun, String.join("\n", moonList));
}
}


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

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class Poems {
public static class Poem {
public String title;
public String content;
}

public static void main(String[] args) throws ExecutionException, InterruptedException {
try (AIScope scope = new AIScope()) {
CompletableFuture<Poem> poem1 = scope.prompt("A short poem about the beauty of nature.", Poem.class);
CompletableFuture<Poem> poem2 = scope.prompt("A short poem about the power of technology.", Poem.class);
CompletableFuture<Poem> poem3 = scope.prompt("A short poem about the wisdom of artificial intelligence.", Poem.class);

String combinedPoems = String.format("%s\n\n%s\n\n%s", poem1.get().content, poem2.get().content, poem3.get().content);
String newPoemPrompt = "Write a new poem that combines ideas from the following themes: the beauty " +
"of nature, the power of technology, and the wisdom of artificial intelligence. Here are some " +
"examples of poems on these themes: " + combinedPoems;

scope.prompt(newPoemPrompt, Poem.class).
thenAccept(poem -> System.out.printf("New Poem:\n\n" + poem.content))
.get();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.xebia.functional.xef.java.auto;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class Populations {

static class Population {
public int population;
public String description;
}

static class Image {
public String description;
public String url;
}

public static void main(String[] args) throws ExecutionException, InterruptedException {
try (AIScope scope = new AIScope()) {
CompletableFuture<Population> cadiz = scope.prompt("What is the population of Cádiz, Spain.", Population.class);
CompletableFuture<Population> seattle = scope.prompt("What is the population of Seattle, WA.", Population.class);
CompletableFuture<Image> img = scope.prompt("A hybrid city of Cádiz, Spain and Seattle, US.", Image.class);
System.out.println(img.get());
System.out.println("The population of Cádiz is " + cadiz.get().population + " and the population of Seattle is " + seattle.get().population);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.xebia.functional.xef.java.auto;

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

public class Recipes {

static class Recipe {
public String name;
public List<String> ingredients;
}

public static void main(String[] args) throws ExecutionException, InterruptedException {
try (AIScope scope = new AIScope()) {
CompletableFuture<Recipe> recipe = scope.prompt("Recipe for chocolate chip cookies.", Recipe.class);
System.out.println("The recipe for " + recipe.get().name + " is " + recipe.get().ingredients );
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.xebia.functional.xef.java.auto;

import java.util.concurrent.ExecutionException;

public class TopAttractions {

static class TopAttraction {
public City city;
public String attractionName;
public String description;
public Weather weather = new Weather();
}

static class City {
public String name;
public String country;
}

static class Weather {
public City city;
public double temperature;
public String description;
}

public static void main(String[] args) throws ExecutionException, InterruptedException {
try (AIScope scope = new AIScope()) {
scope.prompt("Top attraction in Cádiz, Spain.", TopAttraction.class)
.thenAccept(attraction -> System.out.println(
"The top attraction in " + attraction.city.name + " is " + attraction.attractionName + "." +
"Here's a brief description: " + attraction.description + "." +
"The weather in " + attraction.city.name + " is " + attraction.weather.temperature + " degrees Celsius and " + attraction.weather.description + "."
)
).get();
}
}

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

import java.util.concurrent.ExecutionException;

public class TouristAttractions {

static class TouristAttraction {
public String name;
public String location;
public String history;
}

public static void main(String[] args) throws ExecutionException, InterruptedException {
try (AIScope scope = new AIScope()) {
scope.prompt("Statue of Liberty location and history.", TouristAttraction.class)
.thenAccept(statueOfLiberty -> System.out.println(
statueOfLiberty.name + "is located in " + statueOfLiberty.location +
" and has the following history: " + statueOfLiberty.history
)
).get();
}
}
}