|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved. |
| 3 | + * This file is a part of the ModelEngine Project. |
| 4 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 5 | + *--------------------------------------------------------------------------------------------*/ |
| 6 | + |
| 7 | +package modelengine.fel.engine; |
| 8 | + |
| 9 | +import modelengine.fel.core.chat.ChatMessage; |
| 10 | +import modelengine.fel.core.chat.ChatOption; |
| 11 | +import modelengine.fel.core.chat.support.AiMessage; |
| 12 | +import modelengine.fel.core.util.Tip; |
| 13 | +import modelengine.fel.engine.flows.AiFlows; |
| 14 | +import modelengine.fel.engine.flows.AiProcessFlow; |
| 15 | +import modelengine.fel.engine.flows.ConverseLatch; |
| 16 | +import modelengine.fel.engine.operators.models.ChatFlowModel; |
| 17 | +import modelengine.fel.engine.operators.prompts.Prompts; |
| 18 | +import modelengine.fit.waterflow.domain.context.FlowSession; |
| 19 | +import modelengine.fit.waterflow.domain.context.StateContext; |
| 20 | +import modelengine.fit.waterflow.domain.utils.SleepUtil; |
| 21 | +import modelengine.fitframework.flowable.Choir; |
| 22 | + |
| 23 | +import org.junit.jupiter.api.Assertions; |
| 24 | +import org.junit.jupiter.api.DisplayName; |
| 25 | +import org.junit.jupiter.api.Nested; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | + |
| 28 | +import java.util.concurrent.atomic.AtomicInteger; |
| 29 | + |
| 30 | +/** |
| 31 | + * Test cases demonstrating different flow control scenarios in AI processing pipelines. |
| 32 | + * Contains nested test classes for specific flow control mechanisms. |
| 33 | + * |
| 34 | + * @author 宋永坦 |
| 35 | + * @since 2025-06-11 |
| 36 | + */ |
| 37 | +public class AiFlowCaseTest { |
| 38 | + private static final int SPEED = 1; |
| 39 | + |
| 40 | + @Nested |
| 41 | + class DesensitizeCase { |
| 42 | + private final ChatFlowModel model = new ChatFlowModel((prompt, chatOption) -> Choir.create(emitter -> { |
| 43 | + emitter.emit(new AiMessage("<think>")); |
| 44 | + int takeTime = 10 * SPEED; |
| 45 | + SleepUtil.sleep(takeTime); |
| 46 | + for (int i = 0; i < 48; i++) { |
| 47 | + emitter.emit(new AiMessage(String.valueOf(i))); |
| 48 | + SleepUtil.sleep(takeTime); |
| 49 | + } |
| 50 | + emitter.emit(new AiMessage("</think>")); |
| 51 | + SleepUtil.sleep(takeTime); |
| 52 | + for (int i = 100; i < 150; i++) { |
| 53 | + emitter.emit(new AiMessage(String.valueOf(i))); |
| 54 | + SleepUtil.sleep(takeTime); |
| 55 | + } |
| 56 | + emitter.complete(); |
| 57 | + }), ChatOption.custom().model("modelName").stream(true).build()); |
| 58 | + |
| 59 | + private final AiProcessFlow<Tip, String> flow = AiFlows.<Tip>create() |
| 60 | + .prompt(Prompts.human("{{0}}")) |
| 61 | + .generate(model) |
| 62 | + .map(this::classic) |
| 63 | + .conditions() |
| 64 | + .when(chunk -> chunk.isThinkContent, input -> input) |
| 65 | + .others(input -> { |
| 66 | + this.log(input); |
| 67 | + return input; |
| 68 | + }) |
| 69 | + .map(this::mockDesensitize1) |
| 70 | + .map(this::mockDesensitize2) |
| 71 | + .close(); |
| 72 | + |
| 73 | + @Test |
| 74 | + @DisplayName("DesensitizeCase") |
| 75 | + void run() { |
| 76 | + AtomicInteger counter = new AtomicInteger(0); |
| 77 | + long startTime = System.currentTimeMillis(); |
| 78 | + System.out.printf("time:%s, start.\n", startTime); |
| 79 | + ConverseLatch<String> result = flow.converse(new FlowSession(true)).doOnConsume(answer -> { |
| 80 | + System.out.printf("time:%s, chunk=%s\n", System.currentTimeMillis(), answer); |
| 81 | + counter.incrementAndGet(); |
| 82 | + }).offer(Tip.fromArray("hi")); |
| 83 | + result.await(); |
| 84 | + System.out.printf("time:%s, cost=%s\n", System.currentTimeMillis(), System.currentTimeMillis() - startTime); |
| 85 | + Assertions.assertEquals(100, counter.get()); |
| 86 | + } |
| 87 | + |
| 88 | + private Chunk classic(ChatMessage message, StateContext ctx) { |
| 89 | + if (message.text().trim().equals("<think>")) { |
| 90 | + ctx.setState("isThinking", true); |
| 91 | + return new Chunk(true, message.text()); |
| 92 | + } |
| 93 | + if (message.text().trim().equals("</think>")) { |
| 94 | + ctx.setState("isThinking", false); |
| 95 | + return new Chunk(true, message.text()); |
| 96 | + } |
| 97 | + if (Boolean.TRUE.equals(ctx.getState("isThinking"))) { |
| 98 | + return new Chunk(true, message.text()); |
| 99 | + } |
| 100 | + return new Chunk(false, message.text()); |
| 101 | + } |
| 102 | + |
| 103 | + private String mockDesensitize1(Chunk chunk) { |
| 104 | + SleepUtil.sleep(10 * SPEED); |
| 105 | + return chunk.content.replace("3", "*"); |
| 106 | + } |
| 107 | + |
| 108 | + private String mockDesensitize2(String chunk) { |
| 109 | + SleepUtil.sleep(10 * SPEED); |
| 110 | + return chunk.replace("4", "*"); |
| 111 | + } |
| 112 | + |
| 113 | + private void log(Chunk chunk) { |
| 114 | + System.out.println("log content:" + chunk.content); |
| 115 | + } |
| 116 | + |
| 117 | + private static class Chunk { |
| 118 | + private final boolean isThinkContent; |
| 119 | + private final String content; |
| 120 | + |
| 121 | + private Chunk(boolean isThinkContent, String content) {this.isThinkContent = isThinkContent; |
| 122 | + this.content = content; |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Simulates a backpressure scenario where: |
| 129 | + * <ol> |
| 130 | + * <li>The LLM generates data faster than the TTS can process it.</li> |
| 131 | + * <li>TTS processing is constrained to a single thread.</li> |
| 132 | + * <li>TTS processing speed is artificially slowed.</li> |
| 133 | + * </ol> |
| 134 | + */ |
| 135 | + @Nested |
| 136 | + class BackPressureCase { |
| 137 | + private final ChatFlowModel model = new ChatFlowModel((prompt, chatOption) -> Choir.create(emitter -> { |
| 138 | + for (int i = 0; i < 100; i++) { |
| 139 | + emitter.emit(new AiMessage(String.valueOf(i))); |
| 140 | + SleepUtil.sleep(5 * SPEED); |
| 141 | + } |
| 142 | + emitter.complete(); |
| 143 | + System.out.printf("time:%s, generate completed.\n", System.currentTimeMillis()); |
| 144 | + }), ChatOption.custom().model("modelName").stream(true).build()); |
| 145 | + |
| 146 | + private final AiProcessFlow<Tip, String> flow = AiFlows.<Tip>create() |
| 147 | + .prompt(Prompts.human("{{0}}")) |
| 148 | + .generate(model) |
| 149 | + .map(this::mockDesensitize).concurrency(1) // Limit processing to 1 concurrent thread |
| 150 | + .map(this::mockTTS).concurrency(1) // Limit processing to 1 concurrent thread |
| 151 | + .close(); |
| 152 | + |
| 153 | + @Test |
| 154 | + @DisplayName("BackPressureCase") |
| 155 | + void run() { |
| 156 | + AtomicInteger counter = new AtomicInteger(0); |
| 157 | + long startTime = System.currentTimeMillis(); |
| 158 | + System.out.printf("time:%s, start.\n", startTime); |
| 159 | + ConverseLatch<String> result = flow.converse(new FlowSession(false)).doOnConsume(answer -> { |
| 160 | + System.out.printf("time:%s, chunk=%s\n", System.currentTimeMillis(), answer); |
| 161 | + counter.incrementAndGet(); |
| 162 | + }).offer(Tip.fromArray("hi")); |
| 163 | + result.await(); |
| 164 | + System.out.printf("time:%s, cost=%s\n", System.currentTimeMillis(), System.currentTimeMillis() - startTime); |
| 165 | + Assertions.assertEquals(100, counter.get()); |
| 166 | + } |
| 167 | + |
| 168 | + private String mockDesensitize(ChatMessage chunk) { |
| 169 | + // Simulate time-consuming operation with a delay. |
| 170 | + SleepUtil.sleep(10 * SPEED); |
| 171 | + return chunk.text().replace("3", "*"); |
| 172 | + } |
| 173 | + |
| 174 | + private String mockTTS(String chunk) { |
| 175 | + // Simulate time-consuming operation with a delay. |
| 176 | + SleepUtil.sleep(10 * SPEED); |
| 177 | + return chunk; |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * Demonstrates concurrent processing with balanced throughput where: |
| 183 | + * <ol> |
| 184 | + * <li>LLM generates data at moderate pace.</li> |
| 185 | + * <li>Downstream processing runs with 3 concurrent threads.</li> |
| 186 | + * <li>Processing speed is slightly slower than generation (3 : 1).</li> |
| 187 | + * </ol> |
| 188 | + */ |
| 189 | + @Nested |
| 190 | + class ConcurrencyCase { |
| 191 | + private final ChatFlowModel model = new ChatFlowModel((prompt, chatOption) -> Choir.create(emitter -> { |
| 192 | + for (int i = 0; i < 100; i++) { |
| 193 | + emitter.emit(new AiMessage(String.valueOf(i))); |
| 194 | + SleepUtil.sleep(10 * SPEED); |
| 195 | + } |
| 196 | + emitter.complete(); |
| 197 | + }), ChatOption.custom().model("modelName").stream(true).build()); |
| 198 | + |
| 199 | + private final AiProcessFlow<Tip, String> flow = AiFlows.<Tip>create() |
| 200 | + .prompt(Prompts.human("{{0}}")) |
| 201 | + .generate(model) |
| 202 | + .map(this::mockDesensitize).concurrency(3) // Set processing to 3 concurrent thread |
| 203 | + .close(); |
| 204 | + |
| 205 | + @Test |
| 206 | + @DisplayName("ConcurrencyCase") |
| 207 | + void run() { |
| 208 | + AtomicInteger counter = new AtomicInteger(0); |
| 209 | + long startTime = System.currentTimeMillis(); |
| 210 | + System.out.printf("time:%s, start.\n", startTime); |
| 211 | + ConverseLatch<String> result = flow.converse(new FlowSession(false)).doOnConsume(answer -> { |
| 212 | + System.out.printf("time:%s, chunk=%s\n", System.currentTimeMillis(), answer); |
| 213 | + counter.incrementAndGet(); |
| 214 | + }).offer(Tip.fromArray("hi")); |
| 215 | + result.await(); |
| 216 | + System.out.printf("time:%s, cost=%s\n", System.currentTimeMillis(), System.currentTimeMillis() - startTime); |
| 217 | + Assertions.assertEquals(100, counter.get()); |
| 218 | + } |
| 219 | + |
| 220 | + private String mockDesensitize(ChatMessage chunk) { |
| 221 | + // Simulate slower processing at 1/3 speed of LLM generation. |
| 222 | + SleepUtil.sleep(30 * SPEED); |
| 223 | + return chunk.text().replace("3", "*"); |
| 224 | + } |
| 225 | + } |
| 226 | +} |
0 commit comments