配置提示詞

在使用 FLUX.1-dev 進(jìn)行繪畫時(shí),建議使用英文提示詞以取得更好的效果,例如:

Prompt: a young artist sitting in front of a canvas, focused on painting, surrounded by colorful paints and brushes, sunlight streaming through the window onto him.

人物肖像生成

API調(diào)用

FLUX.1-dev 還提供 API 調(diào)用功能,方便開(kāi)發(fā)者將其集成到應(yīng)用中。API 文檔可以在這里 找到。

import requests

url = "https://api.siliconflow.cn/v1/black-forest-labs/FLUX.1-schnell/text-to-image"

payload = {
    "prompt": "an island near sea, with seagulls, moon shining over the sea, light house, boats in the background, fish flying over the sea",
    "image_size": "1024x1024",
    "batch_size": 1,
    "num_inference_steps": 20,
    "guidance_scale": 7.5
}
headers = {
    "accept": "application/json",
    "content-type": "application/json",
    "Authorization": "Bearer your_api_key"
}

response = requests.post(url, json=payload, headers=headers)

print(response.text)

使用Flux和Mono創(chuàng)建響應(yīng)式流

創(chuàng)建簡(jiǎn)單的Flux和Mono

在 Java 中,使用 Flux 和 Mono 可以輕松創(chuàng)建響應(yīng)式流。使用 Flux.just()Mono.just() 方法,可以從簡(jiǎn)單的數(shù)據(jù)集合中創(chuàng)建流。

Flux.just(1, 2, 3, 4).subscribe(System.out::println);
Mono.just("Hello").subscribe(System.out::println);

從數(shù)組創(chuàng)建Flux

Flux.fromArray() 方法可以從數(shù)組中創(chuàng)建 Flux 流,適用于已知大小的數(shù)據(jù)集。

Flux.fromArray(new Integer[]{1, 2, 3, 4}).subscribe(System.out::println);

Flux與Mono的轉(zhuǎn)換

Flux 和 Mono 可以相互轉(zhuǎn)換。使用 collectList() 可以將 Flux 轉(zhuǎn)換為 Mono,反之亦然。

Flux flux = Flux.just(1, 2, 3);
Mono<List> mono = flux.collectList();
mono.subscribe(System.out::println);

從Iterable對(duì)象中創(chuàng)建Flux流

使用fromIterable

通過(guò) Flux.fromIterable() 方法,可以從任何實(shí)現(xiàn)了 Iterable 接口的對(duì)象中創(chuàng)建 Flux 流。這種方法非常靈活,適用于多種數(shù)據(jù)源。

List list = Arrays.asList(1, 2, 3, 4);
Flux.fromIterable(list).subscribe(System.out::println);

動(dòng)態(tài)添加元素

當(dāng)使用 Iterable 對(duì)象創(chuàng)建 Flux 后,可以動(dòng)態(tài)添加元素到 Iterable 中,F(xiàn)lux 將自動(dòng)包含這些新元素。

ArrayList list = new ArrayList(Arrays.asList(1, 2, 3));
Flux flux = Flux.fromIterable(list);
list.add(4);
flux.subscribe(System.out::println);

處理空集合

即使 Iterable 對(duì)象為空,Flux.fromIterable() 也能創(chuàng)建一個(gè)空的 Flux 而不會(huì)拋出異常。

List emptyList = Collections.emptyList();
Flux.fromIterable(emptyList).subscribe(System.out::println);

通過(guò)Stream和Range生成Flux流

使用fromStream

通過(guò) Java 的 Stream API,可以將數(shù)據(jù)流轉(zhuǎn)換為 Flux 流。Flux.fromStream() 是一個(gè)簡(jiǎn)單而強(qiáng)大的工具。

Flux.fromStream(Stream.of(1, 2, 3, 4)).subscribe(System.out::println);

使用range

Flux.range() 方法可以生成一個(gè)整數(shù)序列的 Flux 流,適用于需要生成特定范圍數(shù)字的場(chǎng)景。

Flux.range(0, 10).subscribe(System.out::println);

結(jié)合Stream和Range

可以將 Stream 和 Range 結(jié)合起來(lái)使用,進(jìn)一步擴(kuò)展 Flux 的功能。

Stream stream = Stream.iterate(0, n -> n + 1).limit(10);
Flux.fromStream(stream).subscribe(System.out::println);

處理Flux和Mono中的異常

創(chuàng)建包含異常的流

在開(kāi)發(fā)過(guò)程中,處理異常是不可或缺的一部分。可以直接創(chuàng)建一個(gè)包含異常的 Flux 或 Mono。

Flux.error(new RuntimeException("Error occurred")).subscribe(System.out::println, System.err::println);
Mono.error(new RuntimeException("Error occurred")).subscribe(System.out::println, System.err::println);

異常捕獲

通過(guò) doOnError() 方法,可以在異常發(fā)生時(shí)執(zhí)行特定的操作,比如記錄日志。

Mono.just("start")
    .concatWith(Mono.error(new RuntimeException("Exception")))
    .doOnError(error -> System.out.println("Caught: " + error))
    .subscribe(System.out::println);

使用onErrorResume

當(dāng)異常發(fā)生時(shí),可以通過(guò) onErrorResume() 方法生成一個(gè)新的流,作為替代方案。

Flux.just(1, 2, 3)
    .concatWith(Mono.error(new RuntimeException("Error")))
    .onErrorResume(e -> Flux.just(4, 5, 6))
    .subscribe(System.out::println);

合并與過(guò)濾流中的元素

合并流

使用 merge() 方法可以將多個(gè) Flux 流合并到一起,合并后的流會(huì)按時(shí)間順序輸出所有的元素。

Flux.merge(Flux.just(1, 2), Flux.just(3, 4)).subscribe(System.out::println);

過(guò)濾元素

filter() 方法用于從 Flux 流中過(guò)濾掉不符合條件的元素,只保留滿足條件的元素。

Flux.range(1, 10).filter(i -> i % 2 == 0).subscribe(System.out::println);

使用zipWith合并

zipWith() 方法可以將兩個(gè) Flux 的元素一一配對(duì),并對(duì)配對(duì)的元素進(jìn)行處理。

Flux.just(1, 2).zipWith(Flux.just("A", "B"), (num, letter) -> num + letter).subscribe(System.out::println);

控制流背壓與訂閱量管理

背壓機(jī)制

在處理響應(yīng)式流時(shí),背壓機(jī)制用于控制數(shù)據(jù)流速,以避免資源消耗過(guò)大。通過(guò) BaseSubscriber 可以自定義背壓策略。

Flux.range(1, 10).subscribe(new BaseSubscriber() {
    @Override
    protected void hookOnSubscribe(Subscription subscription) {
        request(2);
    }

    @Override
    protected void hookOnNext(Integer value) {
        System.out.println(value);
        if (value == 2) {
            cancel();
        }
    }
});

控制訂閱量

subscribe() 方法中,可以指定每次請(qǐng)求的元素?cái)?shù)量,從而控制流的消費(fèi)速率。

Flux.range(1, 100).limitRate(10).subscribe(System.out::println);

自定義Subscriber

通過(guò)實(shí)現(xiàn) Subscriber 接口,可以完全掌控?cái)?shù)據(jù)流的請(qǐng)求和消費(fèi)邏輯。

Flux.range(1, 10).subscribe(new Subscriber() {
    private Subscription subscription;
    private int count = 0;
    @Override
    public void onSubscribe(Subscription s) {
        this.subscription = s;
        subscription.request(1);
    }

    @Override
    public void onNext(Integer integer) {
        System.out.println(integer);
        if (++count >= 2) {
            subscription.request(1);
            count = 0;
        }
    }

    @Override
    public void onError(Throwable t) {
        t.printStackTrace();
    }

    @Override
    public void onComplete() {
        System.out.println("Done");
    }
});

FAQ

問(wèn):如何體驗(yàn) FLUX.1-dev AI 繪畫模型的在線功能?

問(wèn):在使用 FLUX.1-dev 進(jìn)行繪畫時(shí),提示詞應(yīng)該如何配置?

問(wèn):如何通過(guò) API 調(diào)用 FLUX.1-dev,以便集成到應(yīng)用中?

import requests

url = "https://api.siliconflow.cn/v1/black-forest-labs/FLUX.1-schnell/text-to-image"

payload = {
    "prompt": "an island near sea, with seagulls, moon shining over the sea, light house, boats in the background, fish flying over the sea",
    "image_size": "1024x1024",
    "batch_size": 1,
    "num_inference_steps": 20,
    "guidance_scale": 7.5
}
headers = {
    "accept": "application/json",
    "content-type": "application/json",
    "Authorization": "Bearer your_api_key"
}

response = requests.post(url, json=payload, headers=headers)

print(response.text)

問(wèn):如何在 Java 中使用 Flux 和 Mono 創(chuàng)建響應(yīng)式流?

Flux.just(1, 2, 3, 4).subscribe(System.out::println);
Mono.just("Hello").subscribe(System.out::println);

問(wèn):Java 開(kāi)發(fā)者如何處理 Flux 和 Mono 中的異常情況?

Mono.just("start")
    .concatWith(Mono.error(new RuntimeException("Exception")))
    .doOnError(error -> System.out.println("Caught: " + error))
    .subscribe(System.out::println);

通過(guò)這種方式,開(kāi)發(fā)者能夠更好地控制和處理流中的異常情況。

上一篇:

Stable Diffusion API Key獲取與使用指南

下一篇:

使用Stable Diffusion API進(jìn)行文生圖生成的完整指南
#你可能也喜歡這些API文章!

我們有何不同?

API服務(wù)商零注冊(cè)

多API并行試用

數(shù)據(jù)驅(qū)動(dòng)選型,提升決策效率

查看全部API→
??

熱門場(chǎng)景實(shí)測(cè),選對(duì)API

#AI文本生成大模型API

對(duì)比大模型API的內(nèi)容創(chuàng)意新穎性、情感共鳴力、商業(yè)轉(zhuǎn)化潛力

25個(gè)渠道
一鍵對(duì)比試用API 限時(shí)免費(fèi)

#AI深度推理大模型API

對(duì)比大模型API的邏輯推理準(zhǔn)確性、分析深度、可視化建議合理性

10個(gè)渠道
一鍵對(duì)比試用API 限時(shí)免費(fèi)