22
33Welcome to the [ ** Replicate AI** ] ( https://replicate.com/ ) Integration project! 🎉
44
5- This project provides a streamlined Java 21-based framework to effortlessly interact with and create predictions for ** Replicate** 's
6- powerful AI models. Built with ** Spring Boot** , it enables quick and modular access to various AI services, making integration a breeze for
5+ This project provides a streamlined Java 21-based framework to effortlessly interact with and create predictions for *
6+ * Replicate** 's
7+ powerful AI models. Built with ** Spring Boot** , it enables quick and modular access to various AI services, making
8+ integration a breeze for
79developers.
810
9- The project allows downloading of ** Replicate AI** models' JSONSchemas for requests and responses through the official API, and dynamically
11+ The project allows downloading of ** Replicate AI** models' JSONSchemas for requests and responses through the official
12+ API, and dynamically
1013creates a Maven module to enable developers to interact with the AI models using strongly typed Java code.
1114
12- Furthermore, it supports ** Spring Boot AutoConfiguration** , allowing efficient and powerful integration with Spring Boot applications to
15+ Furthermore, it supports ** Spring Boot AutoConfiguration** , allowing efficient and powerful integration with Spring Boot
16+ applications to
1317enable easy access to ** Replicate** 's AI offerings—just add your API key!
1418
1519## 🚀 Quick Start
1620
17- The quickest way to get off the ground is depending on ` spring-boot-starter-replicate ` . The module, provided an API key to Replicate,
21+ The quickest way to get off the ground is depending on ` spring-boot-starter-replicate ` . The module, provided an API key
22+ to Replicate,
1823handles all configuration of the underlying modules to ensure a smooth start.
1924
2025``` xml
@@ -37,28 +42,22 @@ replicate:
3742
3843## Replicate Models
3944
40- By default, this project supports a number of common AI models for text completion using large language models and text-to-image models for
45+ By default, this project supports a number of common AI models for text completion using large language models and
46+ text-to-image models for
4147image generation. Specifically, the following models are currently supported out of the box:
4248
43- ### Meta Llama 3
44-
45- * [` meta-llama-3-8b-instruct`](https://replicate.com/meta/meta-llama-3-8b-instruct)
46- * [`meta-llama-3-70b-instruct`](https://replicate.com/meta/meta-llama-3-70b-instruct)
47- * [`meta-llama-3.1-405b-instruct`](https://replicate.com/meta/meta-llama-3.1-405b-instruct)
48-
49- For the Llama 3 family of models, tokenization for text completion is supported by `spring-boot-starter-replicate`, making it easy to
50- create and maintain stateful conversations.
51-
5249### Black Forest Labs Flux
5350
54- * [`flux-dev`](https://replicate.com/black-forest-labs/flux-dev)
55- * [`flux-schnell`](https://replicate.com/black-forest-labs/flux-schnell)
56- * [`flux-pro`](https://replicate.com/black-forest-labs/flux-pro)
51+ * [` flux-2-dev`](https://replicate.com/black-forest-labs/flux-2-dev)
52+ * [`flux-2-flex`](https://replicate.com/black-forest-labs/flux-2-flex)
53+ * [`flux-2-pro`](https://replicate.com/black-forest-labs/flux-2-pro)
54+ * [`flux-2-max`](https://replicate.com/black-forest-labs/flux-2-max)
5755
5856---
5957
6058> [!TIP]
61- > It is possible to build this project to support different AI models that are not included by default for simplicity reasons.
59+ > It is possible to build this project to support different AI models that are not included by default for simplicity
60+ > reasons.
6261> Please see the section describing the `replicate-tools` module.
6362
6463# # 📂 Module Overview
@@ -67,18 +66,22 @@ Below is a more focused description regarding each discrete module of this proje
6766
6867# ## `replicate-client`
6968
70- Core module for handling API requests and responses from **Replicate**. Use this if you only want to create predictions with Java code in
69+ Core module for handling API requests and responses from **Replicate**. Use this if you only want to create predictions
70+ with Java code in
7171the simplest possible manner.
7272
7373# ## `replicate-models`
7474
75- Auto-generated models from **Replicate**’s JSONSchemas, providing strongly typed Java interfaces for AI models. The models supported are
75+ Auto-generated models from **Replicate**’s JSONSchemas, providing strongly typed Java interfaces for AI models. The
76+ models supported are
7677generated using `replicate-tools`. From this repository, the following packages are supported :
7778
78- * [`default`](https://github.com/MrGraversen/replicate-java/packages/2297525?version=0.0.8-default): Default models as described above.
79- * [`llama3`](https://github.com/MrGraversen/replicate-java/packages/2297525?version=0.0.8-llama3): A specialised module consisting only of
80- the Llama 3 family of AI models.
81- * [`flux`](https://github.com/MrGraversen/replicate-java/packages/2297525?version=0.0.8-flux): A specialised module consisting only of the
79+ * [`default`](https://github.com/MrGraversen/replicate-java/packages/2297525?version=0.0.8-default): Default models as
80+ described above.
81+ * [`openai`](https://github.com/MrGraversen/replicate-java/packages/2297525?version=0.0.8-openai): A specialized module
82+ consisting only of OpenAI models.
83+ * [`flux`](https://github.com/MrGraversen/replicate-java/packages/2297525?version=0.0.8-flux): A specialized module
84+ consisting only of the
8285 Flux family of AI models.
8386
8487# ## `replicate-tools`
@@ -111,100 +114,48 @@ mvn compile
111114` ` `
112115
113116> [!TIP]
114- > If you decide to use AI models that are more complex than simple text completion or text-to-image generators, note that you may depend on
115- > just `replicate-client` and `replicate-models` with your custom set of models, to enable easy, strongly typed access to the Replicate API.
117+ > If you decide to use AI models that are more complex than simple text completion or text-to-image generators, note
118+ > that you may depend on
119+ > just `replicate-client` and `replicate-models` with your custom set of models, to enable easy, strongly typed access
120+ > to the Replicate API.
116121
117122# ## `spring-boot-starter-replicate`
118123
119- Auto-configures **Replicate** integration, provided an API token. Through this, you will have access to more advanced encapsulations to
124+ Auto-configures **Replicate** integration, provided an API token. Through this, you will have access to more advanced
125+ encapsulations to
120126create images and manage conversations.
121127
122128# # 📈 Examples
123129
124- # ## Example 1 - Conversations with Llama 3
130+ # ## Example – Image generation with Flux 2 Pro
125131
126132` ` ` java
127133
128134@Slf4j
135+ @Component
129136@RequiredArgsConstructor
130- public class LlamaConversationExample {
131- private final ConversationFacade conversationFacade;
132-
133- /**
134- * Simple example of exchanging one message with meta-llama-3-70b-instruct
135- */
136- public void runExampleOne() {
137- final var conversationOptions = new ConversationOptions(
138- 1.25 // temperature
139- );
140-
141- final var createConversation = new CreateConversation(
142- "You are a friendly and witty assistant! Respond in one short sentence only.", // systemMessage
143- Llama3Models.LLAMA_3_70B_INSTRUCT // meta-llama-3-70b-instruct
144- );
145-
146- final var conversation = conversationFacade.create(createConversation, conversationOptions);
147-
148- conversationFacade.chat(conversation.getId(), TextMessage.user("Introduce yourself to me 😊")).whenComplete(logConversation());
149- // => I'm LLaMA, your go-to sidekick for banter, advice, and getting stuff done, with a healthy dose of sarcasm and humor! 😉
150- }
137+ public class Flux2Example {
138+ private final ReplicateFacade replicateFacade;
151139
152140 /**
153- * Example of exchanging a "deeper" conversation with meta-llama-3-70b-instruct
141+ * Create one image using flux2-dev
154142 */
155- public void runExampleTwo() {
156- final var conversationOptions = new ConversationOptions(
157- 0.75 // temperature
158- );
159-
160- final var createConversation = new CreateConversation(
161- "You are a calculator app. Respond only with the result of the math query.", // systemMessage
162- Llama3Models.LLAMA_3_70B_INSTRUCT // meta-llama-3-70b-instruct
163- );
143+ public void runExample() {
144+ final var createImagePrediction = CreateImagePrediction2.builder()
145+ .prompt("A photo of a happy sheep grazing on a beautiful summer day")
146+ .aspectRatio(AspectRatios.RATIO_5_BY_4)
147+ .build();
164148
165- final var conversation = conversationFacade.create(createConversation, conversationOptions);
166-
167- conversationFacade.chat(conversation.getId(), TextMessage.user("What is 2 + 2?")) // => 4
168- .thenCompose(conversationFacade.chat(TextMessage.user("What is 3 * 3?"))) // => 9
169- .thenCompose(conversationFacade.chat(TextMessage.user("What is seven minus two?"))) // => 5
170- .whenComplete(logConversation());
171- }
172-
173- private BiConsumer<Conversation, Throwable> logConversation() {
174- return (conversation, throwable) -> {
175- if (throwable == null) {
176- log.info("{}", conversation.getConversation());
177- } else {
178- log.error(throwable.getMessage(), throwable);
179- }
180- };
149+ replicateFacade.createPrediction(FluxModels.FLUX_2_PRO, createImagePrediction);
181150 }
182- }
183- ` ` `
184-
185- # ## Example 2 - Image generation with Flux
186-
187- ` ` ` java
188-
189- @Slf4j
190- @RequiredArgsConstructor
191- public class FluxExample {
192- private final ReplicateFacade replicateFacade;
193-
194- /**
195- * Create one image using flux-dev
196- */
197- public void runExampleOne() {
198- final var createImagePrediction = new CreateImagePrediction(
199- TextToImagePrompt.portrait("A photo of a sheep on a grassy field on a beautiful summer day"),
200- 1, // outputs
201- 25, // inferenceSteps
202- null // seed (random)
203- );
204151
205- replicateFacade.createPrediction(
206- FluxModels.FLUX_DEV,
207- createImagePrediction
152+ @EventListener(value = PredictionUpdatedEvent.class, condition = "#event.getStatus().name() == 'SUCCEEDED'")
153+ public void onPredictionUpdated(PredictionUpdatedEvent event) {
154+ log.info(
155+ "Completed prediction '{}' with model '{}', view it here: {}",
156+ event.getId(),
157+ event.getModel(),
158+ ReplicateUrl.fromPrediction(event.getId())
208159 );
209160 }
210161}
0 commit comments