|
16 | 16 | import modelengine.fel.engine.operators.models.ChatFlowModel; |
17 | 17 | import modelengine.fel.engine.operators.prompts.Prompts; |
18 | 18 | import modelengine.fit.waterflow.domain.context.FlowSession; |
| 19 | +import modelengine.fit.waterflow.domain.context.StateContext; |
19 | 20 | import modelengine.fit.waterflow.domain.utils.SleepUtil; |
20 | 21 | import modelengine.fitframework.flowable.Choir; |
21 | 22 |
|
|
33 | 34 | * @since 2025-06-11 |
34 | 35 | */ |
35 | 36 | public class AiFlowCaseTest { |
| 37 | + @Nested |
| 38 | + class DesensitizeCase { |
| 39 | + private final ChatFlowModel model = new ChatFlowModel((prompt, chatOption) -> Choir.create(emitter -> { |
| 40 | + emitter.emit(new AiMessage("<think>")); |
| 41 | + for (int i = 0; i < 10; i++) { |
| 42 | + emitter.emit(new AiMessage(String.valueOf(i))); |
| 43 | + SleepUtil.sleep(100); |
| 44 | + } |
| 45 | + emitter.emit(new AiMessage("</think>")); |
| 46 | + for (int i = 100; i < 110; i++) { |
| 47 | + emitter.emit(new AiMessage(String.valueOf(i))); |
| 48 | + SleepUtil.sleep(100); |
| 49 | + } |
| 50 | + emitter.complete(); |
| 51 | + }), ChatOption.custom().model("modelName").stream(true).build()); |
| 52 | + |
| 53 | + private final AiProcessFlow<Tip, String> flow = AiFlows.<Tip>create() |
| 54 | + .prompt(Prompts.human("{{0}}")) |
| 55 | + .generate(model) |
| 56 | + .map(this::classic) |
| 57 | + .conditions() |
| 58 | + .when(chunk -> chunk.isThinkContent, input -> input) |
| 59 | + .others(input -> { |
| 60 | + this.log(input); |
| 61 | + return input; |
| 62 | + }) |
| 63 | + .map(this::mockDesensitize) |
| 64 | + .close(); |
| 65 | + |
| 66 | + @Test |
| 67 | + void run() { |
| 68 | + AtomicInteger counter = new AtomicInteger(0); |
| 69 | + long startTime = System.currentTimeMillis(); |
| 70 | + System.out.printf("time:%s, start.\n", startTime); |
| 71 | + ConverseLatch<String> result = flow.converse(new FlowSession(true)).doOnConsume(answer -> { |
| 72 | + System.out.printf("time:%s, chunk=%s\n", System.currentTimeMillis(), answer); |
| 73 | + counter.incrementAndGet(); |
| 74 | + }).offer(Tip.fromArray("hi")); |
| 75 | + result.await(); |
| 76 | + System.out.printf("time:%s, cost=%s\n", System.currentTimeMillis(), System.currentTimeMillis() - startTime); |
| 77 | + Assertions.assertEquals(22, counter.get()); |
| 78 | + } |
| 79 | + |
| 80 | + private Chunk classic(ChatMessage message, StateContext ctx) { |
| 81 | + if (message.text().trim().equals("<think>")) { |
| 82 | + ctx.setState("isThinking", true); |
| 83 | + return new Chunk(true, message.text()); |
| 84 | + } |
| 85 | + if (message.text().trim().equals("</think>")) { |
| 86 | + ctx.setState("isThinking", false); |
| 87 | + return new Chunk(true, message.text()); |
| 88 | + } |
| 89 | + if (Boolean.TRUE.equals(ctx.getState("isThinking"))) { |
| 90 | + return new Chunk(true, message.text()); |
| 91 | + } |
| 92 | + return new Chunk(false, message.text()); |
| 93 | + } |
| 94 | + |
| 95 | + private String mockDesensitize(Chunk chunk) { |
| 96 | + return chunk.content.replace("3", "*"); |
| 97 | + } |
| 98 | + |
| 99 | + private void log(Chunk chunk) { |
| 100 | + System.out.println("log content:" + chunk.content); |
| 101 | + } |
| 102 | + |
| 103 | + private static class Chunk { |
| 104 | + private final boolean isThinkContent; |
| 105 | + private final String content; |
| 106 | + |
| 107 | + private Chunk(boolean isThinkContent, String content) {this.isThinkContent = isThinkContent; |
| 108 | + this.content = content; |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
36 | 113 | /** |
37 | 114 | * Simulates a backpressure scenario where: |
38 | 115 | * <ol> |
|
0 commit comments