-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"tabWidth": 4, | ||
"printWidth": 120 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>dev.langchain4j</groupId> | ||
<artifactId>image-example</artifactId> | ||
<version>0.24.0</version> | ||
|
||
<properties> | ||
<maven.compiler.source>8</maven.compiler.source> | ||
<maven.compiler.target>8</maven.compiler.target> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>dev.langchain4j</groupId> | ||
<artifactId>langchain4j-open-ai</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>dev.langchain4j</groupId> | ||
<artifactId>langchain4j-embeddings-all-minilm-l6-v2</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>dev.langchain4j</groupId> | ||
<artifactId>langchain4j</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>dev.langchain4j</groupId> | ||
<artifactId>langchain4j-open-ai</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.tinylog</groupId> | ||
<artifactId>tinylog-impl</artifactId> | ||
<version>2.6.2</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.tinylog</groupId> | ||
<artifactId>slf4j-tinylog</artifactId> | ||
<version>2.6.2</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
90 changes: 90 additions & 0 deletions
90
image-example/src/main/java/OpenAiImageGenerationExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Image> 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<TextSegment> 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<String, Object> variables = new HashMap<>(); | ||
variables.put("information", chain.execute("Who is Charlie?")); | ||
variables.put("object", "Ultra realistic Charlie on the party, cinematic lighting"); | ||
|
||
Response<Image> response = model.generate(drawPromptTemplate.apply(variables).text()); | ||
|
||
System.out.println(response.content().url()); // Enjoy your locally stored picture of Charlie on the party :) | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
image-example/src/main/resources/example-files/story-about-happy-carrot.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters