diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 00000000..e9e3a01c --- /dev/null +++ b/.prettierrc @@ -0,0 +1,4 @@ +{ + "tabWidth": 4, + "printWidth": 120 +} diff --git a/image-example/pom.xml b/image-example/pom.xml new file mode 100644 index 00000000..d502f0cc --- /dev/null +++ b/image-example/pom.xml @@ -0,0 +1,51 @@ + + + 4.0.0 + + dev.langchain4j + image-example + 0.24.0 + + + 8 + 8 + UTF-8 + + + + + dev.langchain4j + langchain4j-open-ai + ${project.version} + + + + dev.langchain4j + langchain4j-embeddings-all-minilm-l6-v2 + ${project.version} + + + dev.langchain4j + langchain4j + ${project.version} + + + dev.langchain4j + langchain4j-open-ai + ${project.version} + + + org.tinylog + tinylog-impl + 2.6.2 + + + org.tinylog + slf4j-tinylog + 2.6.2 + + + + \ No newline at end of file diff --git a/image-example/src/main/java/OpenAiImageGenerationExample.java b/image-example/src/main/java/OpenAiImageGenerationExample.java new file mode 100644 index 00000000..a80f3b2a --- /dev/null +++ b/image-example/src/main/java/OpenAiImageGenerationExample.java @@ -0,0 +1,90 @@ +import static dev.ai4j.openai4j.image.ImageModel.DALL_E_QUALITY_HD; +import static dev.langchain4j.data.document.FileSystemDocumentLoader.loadDocument; + +import dev.langchain4j.chain.ConversationalRetrievalChain; +import dev.langchain4j.data.document.Document; +import dev.langchain4j.data.document.splitter.DocumentSplitters; +import dev.langchain4j.data.image.Image; +import dev.langchain4j.data.segment.TextSegment; +import dev.langchain4j.model.embedding.AllMiniLmL6V2EmbeddingModel; +import dev.langchain4j.model.embedding.EmbeddingModel; +import dev.langchain4j.model.input.PromptTemplate; +import dev.langchain4j.model.openai.OpenAiChatModel; +import dev.langchain4j.model.openai.OpenAiImageModel; +import dev.langchain4j.model.output.Response; +import dev.langchain4j.retriever.EmbeddingStoreRetriever; +import dev.langchain4j.store.embedding.EmbeddingStore; +import dev.langchain4j.store.embedding.EmbeddingStoreIngestor; +import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore; +import java.net.URISyntaxException; +import java.nio.file.Paths; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; + +public class OpenAiImageGenerationExample { + + static class Simple_Prompt { + + public static void main(String[] args) { + OpenAiImageModel model = OpenAiImageModel.builder().apiKey(System.getenv("OPENAI_API_KEY")).build(); + + Response response = model.generate("Donald Duck in New York, cartoon style"); + + System.out.println(response.content().url()); // Donald Duck is here :) + } + } + + static class Draw_Story_From_My_Document { + + public static void main(String[] args) throws URISyntaxException { + OpenAiImageModel model = OpenAiImageModel + .builder() + .apiKey(System.getenv("OPENAI_API_KEY")) + .quality(DALL_E_QUALITY_HD) + .logRequests(true) + .logResponses(true) + .withPersisting() + .build(); + + EmbeddingModel embeddingModel = new AllMiniLmL6V2EmbeddingModel(); + EmbeddingStore embeddingStore = new InMemoryEmbeddingStore<>(); + + EmbeddingStoreIngestor ingestor = EmbeddingStoreIngestor + .builder() + .documentSplitter(DocumentSplitters.recursive(1000, 0)) + .embeddingModel(embeddingModel) + .embeddingStore(embeddingStore) + .build(); + + Document document = loadDocument( + Paths.get( + Objects + .requireNonNull( + OpenAiImageGenerationExample.class.getResource("example-files/story-about-happy-carrot.txt") + ) + .toURI() + ) + ); + ingestor.ingest(document); + + ConversationalRetrievalChain chain = ConversationalRetrievalChain + .builder() + .chatLanguageModel(OpenAiChatModel.builder().apiKey(System.getenv("OPENAI_API_KEY")).build()) + .retriever(EmbeddingStoreRetriever.from(embeddingStore, embeddingModel)) + .build(); + + PromptTemplate drawPromptTemplate = PromptTemplate.from( + "Draw {{object}}. Base the picture on following information:\n\n{{information}}" + ); + + Map variables = new HashMap<>(); + variables.put("information", chain.execute("Who is Charlie?")); + variables.put("object", "Ultra realistic Charlie on the party, cinematic lighting"); + + Response response = model.generate(drawPromptTemplate.apply(variables).text()); + + System.out.println(response.content().url()); // Enjoy your locally stored picture of Charlie on the party :) + } + } +} diff --git a/image-example/src/main/resources/example-files/story-about-happy-carrot.txt b/image-example/src/main/resources/example-files/story-about-happy-carrot.txt new file mode 100644 index 00000000..66ae976d --- /dev/null +++ b/image-example/src/main/resources/example-files/story-about-happy-carrot.txt @@ -0,0 +1,28 @@ +Once upon a time in the town of VeggieVille, there lived a cheerful carrot named Charlie. +Charlie was a radiant carrot, always beaming with joy and positivity. +His vibrant orange skin and lush green top were a sight to behold, but it was his infectious laughter and warm personality that really set him apart. + +Charlie had a diverse group of friends, each a vegetable with their own unique characteristics. +There was Bella the blushing beetroot, always ready with a riddle or two; Timmy the timid tomato, a gentle soul with a heart of gold; and Percy the prankster potato, whose jokes always brought a smile to everyone's faces. +Despite their differences, they shared a close bond, their friendship as robust as their natural goodness. + +Their lives were filled with delightful adventures, from playing hide-and-seek amidst the leafy lettuce to swimming in the dewy droplets that pooled on the cabbage leaves. +Their favorite place, though, was the sunlit corner of the vegetable patch, where they would bask in the warmth of the sun, share stories, and have hearty laughs. + +One day, a bunch of pesky caterpillars invaded VeggieVille. +The vegetables were terrified, fearing they would be nibbled to nothingness. +But Charlie, with his usual sunny disposition, had an idea. +He proposed they host a grand feast for the caterpillars, with the juiciest leaves from the outskirts of the town. +Charlie's optimism was contagious, and his friends eagerly joined in to prepare the feast. + +When the caterpillars arrived, they were pleasantly surprised. +They enjoyed the feast and were so impressed with the vegetables' hospitality that they promised not to trouble VeggieVille again. +In return, they agreed to help pollinate the flowers, contributing to a more lush and vibrant VeggieVille. + +Charlie's idea had saved the day, but he humbly attributed the success to their teamwork and friendship. +They celebrated their victory with a grand party, filled with laughter, dance, and merry games. +That night, under the twinkling stars, they made a pact to always stand by each other, come what may. + +From then on, the story of the happy carrot and his friends spread far and wide, a tale of friendship, unity, and positivity. +Charlie, Bella, Timmy, and Percy continued to live their joyful lives, their laughter echoing through VeggieVille. +And so, the tale of the happy carrot and his friends serves as a reminder that no matter the challenge, with optimism, teamwork, and a bit of creativity, anything is possible. \ No newline at end of file diff --git a/pom.xml b/pom.xml index febecd6a..d06fc688 100644 --- a/pom.xml +++ b/pom.xml @@ -22,6 +22,7 @@ spring-boot-example vespa-example weaviate-example + image-example \ No newline at end of file