From 166c28608c4f61005b55593a04cef0785fb72a11 Mon Sep 17 00:00:00 2001 From: xuxueli <931591021@qq.com> Date: Sat, 29 Nov 2025 21:00:45 +0800 Subject: [PATCH] update doc --- .../xxl/job/executor/test/dify/DifyTest.java | 51 +++++++++++++++++++ .../job/executor/test/ollama/OllamaTest.java | 39 +++++++++++++- 2 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 xxl-job-executor-samples/xxl-job-executor-sample-springboot-ai/src/test/java/com/xxl/job/executor/test/dify/DifyTest.java diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-springboot-ai/src/test/java/com/xxl/job/executor/test/dify/DifyTest.java b/xxl-job-executor-samples/xxl-job-executor-sample-springboot-ai/src/test/java/com/xxl/job/executor/test/dify/DifyTest.java new file mode 100644 index 00000000..5ca98124 --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-springboot-ai/src/test/java/com/xxl/job/executor/test/dify/DifyTest.java @@ -0,0 +1,51 @@ +package com.xxl.job.executor.test.dify; + +import com.xxl.job.core.executor.impl.XxlJobSpringExecutor; +import io.github.imfangs.dify.client.DifyClientFactory; +import io.github.imfangs.dify.client.DifyWorkflowClient; +import io.github.imfangs.dify.client.enums.ResponseMode; +import io.github.imfangs.dify.client.model.workflow.WorkflowRunRequest; +import io.github.imfangs.dify.client.model.workflow.WorkflowRunResponse; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.bean.override.mockito.MockitoBean; + +import java.util.Map; + +@SpringBootTest +public class DifyTest { + private static final Logger logger = LoggerFactory.getLogger(DifyTest.class); + + // ignore + @MockitoBean + private XxlJobSpringExecutor xxlJobSpringExecutor; + + @Test + public void test() throws Exception { + + String baseUrl = "https://xx.ai"; + String apiKey = "xx"; + String user = "zhangsan"; + Map inputs = Map.of( + "input", "请写一个java程序,实现一个方法,输入一个字符串,返回字符串的长度。" + ); + + // dify request + WorkflowRunRequest request = WorkflowRunRequest.builder() + .inputs(inputs) + .responseMode(ResponseMode.BLOCKING) + .user(user) + .build(); + + // dify invoke + DifyWorkflowClient workflowClient = DifyClientFactory.createWorkflowClient(baseUrl, apiKey); + WorkflowRunResponse response = workflowClient.runWorkflow(request); + + // response + logger.info("input: " + inputs); + logger.info("output: " + response.getData().getOutputs()); + } + +} diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-springboot-ai/src/test/java/com/xxl/job/executor/test/ollama/OllamaTest.java b/xxl-job-executor-samples/xxl-job-executor-sample-springboot-ai/src/test/java/com/xxl/job/executor/test/ollama/OllamaTest.java index 31e14bca..140c5567 100644 --- a/xxl-job-executor-samples/xxl-job-executor-sample-springboot-ai/src/test/java/com/xxl/job/executor/test/ollama/OllamaTest.java +++ b/xxl-job-executor-samples/xxl-job-executor-sample-springboot-ai/src/test/java/com/xxl/job/executor/test/ollama/OllamaTest.java @@ -13,6 +13,9 @@ import org.springframework.ai.ollama.OllamaChatModel; import org.springframework.ai.ollama.api.OllamaChatOptions; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.bean.override.mockito.MockitoBean; +import reactor.core.publisher.Flux; + +import java.util.concurrent.TimeUnit; @SpringBootTest public class OllamaTest { @@ -27,7 +30,7 @@ public class OllamaTest { private OllamaChatModel ollamaChatModel; @Test - public void test() { + public void chatTest() { String model = "qwen3:0.6b"; String prompt = "背景说明:你是一个研发工程师,擅长解决技术类问题。"; @@ -53,5 +56,39 @@ public class OllamaTest { logger.info("response: {}", response); } + @Test + public void chatStreamTest() throws InterruptedException { + + String model = "qwen3:0.6b"; + String prompt = "背景说明:你是一个研发工程师,擅长解决技术类问题。"; + String input = "请写一个java程序,实现一个方法,输入一个字符串,返回字符串的长度。"; + + + // build chat-client + ChatClient ollamaChatClient = ChatClient + .builder(ollamaChatModel) + .defaultAdvisors(MessageChatMemoryAdvisor.builder(MessageWindowChatMemory.builder().build()).build()) + .defaultAdvisors(SimpleLoggerAdvisor.builder().build()) + .defaultOptions(OllamaChatOptions.builder().model(model).build()) + .build(); + + // call ollama + logger.info("input: {}", input); + Flux flux = ollamaChatClient + .prompt(prompt) + .user(input) + .stream() + .content(); + + flux.subscribe( + data -> System.out.println("Received: " + data), // onNext 处理 + error -> System.err.println("Error: " + error), // onError 处理 + () -> System.out.println("Completed") // onComplete 处理 + ); + + TimeUnit.SECONDS.sleep(10); + + } + }