@@ -0,0 +1,16 @@
|
||||
package com.xxl.job.executor;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* @author xuxueli 2018-10-28 00:38:13
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class XxlJobAIExecutorApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(XxlJobAIExecutorApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package com.xxl.job.executor.config;
|
||||
|
||||
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* xxl-job config
|
||||
*
|
||||
* @author xuxueli 2017-04-28
|
||||
*/
|
||||
@Configuration
|
||||
public class XxlJobConfig {
|
||||
private static final Logger logger = LoggerFactory.getLogger(XxlJobConfig.class);
|
||||
|
||||
@Value("${xxl.job.admin.addresses}")
|
||||
private String adminAddresses;
|
||||
|
||||
@Value("${xxl.job.admin.accessToken}")
|
||||
private String accessToken;
|
||||
|
||||
@Value("${xxl.job.admin.timeout}")
|
||||
private int timeout;
|
||||
|
||||
@Value("${xxl.job.executor.enabled}")
|
||||
private Boolean enabled;
|
||||
|
||||
@Value("${xxl.job.executor.appname}")
|
||||
private String appname;
|
||||
|
||||
@Value("${xxl.job.executor.address}")
|
||||
private String address;
|
||||
|
||||
@Value("${xxl.job.executor.ip}")
|
||||
private String ip;
|
||||
|
||||
@Value("${xxl.job.executor.port}")
|
||||
private int port;
|
||||
|
||||
@Value("${xxl.job.executor.logpath}")
|
||||
private String logPath;
|
||||
|
||||
@Value("${xxl.job.executor.logretentiondays}")
|
||||
private int logRetentionDays;
|
||||
|
||||
@Value("${xxl.job.executor.excludedpackage}")
|
||||
private String excludedPackage;
|
||||
|
||||
|
||||
@Bean
|
||||
public XxlJobSpringExecutor xxlJobExecutor() {
|
||||
logger.info(">>>>>>>>>>> xxl-job config init.");
|
||||
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
|
||||
xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
|
||||
xxlJobSpringExecutor.setAccessToken(accessToken);
|
||||
xxlJobSpringExecutor.setTimeout(timeout);
|
||||
xxlJobSpringExecutor.setEnabled(enabled);
|
||||
xxlJobSpringExecutor.setAppname(appname);
|
||||
xxlJobSpringExecutor.setAddress(address);
|
||||
xxlJobSpringExecutor.setIp(ip);
|
||||
xxlJobSpringExecutor.setPort(port);
|
||||
xxlJobSpringExecutor.setLogPath(logPath);
|
||||
xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
|
||||
xxlJobSpringExecutor.setExcludedPackage(excludedPackage);
|
||||
|
||||
return xxlJobSpringExecutor;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,202 @@
|
||||
package com.xxl.job.executor.controller;//package com.xxl.job.executor.mvc.controller;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.imfangs.dify.client.DifyClientFactory;
|
||||
import io.github.imfangs.dify.client.DifyWorkflowClient;
|
||||
import io.github.imfangs.dify.client.callback.WorkflowStreamCallback;
|
||||
import io.github.imfangs.dify.client.enums.ResponseMode;
|
||||
import io.github.imfangs.dify.client.event.*;
|
||||
import io.github.imfangs.dify.client.model.workflow.WorkflowRunRequest;
|
||||
import io.github.imfangs.dify.client.model.workflow.WorkflowRunResponse;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.ai.chat.client.ChatClient;
|
||||
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
|
||||
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
|
||||
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
|
||||
import org.springframework.ai.ollama.OllamaChatModel;
|
||||
import org.springframework.ai.ollama.api.OllamaChatOptions;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.FluxSink;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
@Controller
|
||||
@EnableAutoConfiguration
|
||||
public class IndexController {
|
||||
private static Logger logger = LoggerFactory.getLogger(IndexController.class);
|
||||
|
||||
|
||||
@RequestMapping("/")
|
||||
@ResponseBody
|
||||
String index() {
|
||||
return "xxl job ai executor running.";
|
||||
}
|
||||
|
||||
|
||||
// --------------------------------- ollama chat ---------------------------------
|
||||
|
||||
@Resource
|
||||
private OllamaChatModel ollamaChatModel;
|
||||
private String prompt = "你好,你是一个研发工程师,擅长解决技术类问题。";
|
||||
private String modle = "qwen3:0.6b";
|
||||
|
||||
/**
|
||||
* ChatClient 简单调用
|
||||
*/
|
||||
@GetMapping("/chat/simple")
|
||||
@ResponseBody
|
||||
public String simpleChat(@RequestParam(value = "input", required = false, defaultValue = "介绍你自己") String input) {
|
||||
|
||||
// build chat-client
|
||||
ChatClient ollamaChatClient = ChatClient
|
||||
.builder(ollamaChatModel)
|
||||
.defaultAdvisors(MessageChatMemoryAdvisor.builder(MessageWindowChatMemory.builder().build()).build()) // add memory
|
||||
.defaultAdvisors(SimpleLoggerAdvisor.builder().build()) // add logger
|
||||
.defaultOptions(OllamaChatOptions.builder().model(modle).build()) // assign model
|
||||
.build();
|
||||
|
||||
// call ollama
|
||||
String response = ollamaChatClient
|
||||
.prompt(prompt)
|
||||
.user(input)
|
||||
.call()
|
||||
.content();
|
||||
|
||||
logger.info("result: " + response);
|
||||
return response;
|
||||
}
|
||||
|
||||
/**
|
||||
* ChatClient 流式调用
|
||||
*/
|
||||
@GetMapping("/chat/stream")
|
||||
public Flux<String> streamChat(HttpServletResponse response, @RequestParam(value = "input", required = false, defaultValue = "介绍你自己") String input) {
|
||||
response.setCharacterEncoding("UTF-8");
|
||||
|
||||
// build chat-client
|
||||
ChatClient ollamaChatClient = ChatClient
|
||||
.builder(ollamaChatModel)
|
||||
.defaultAdvisors(MessageChatMemoryAdvisor.builder(MessageWindowChatMemory.builder().build()).build())
|
||||
.defaultAdvisors(SimpleLoggerAdvisor.builder().build())
|
||||
.defaultOptions(OllamaChatOptions.builder().model(modle).build())
|
||||
.build();
|
||||
|
||||
// call ollama
|
||||
return ollamaChatClient
|
||||
.prompt(prompt)
|
||||
.user(input)
|
||||
.stream()
|
||||
.content();
|
||||
}
|
||||
|
||||
|
||||
// --------------------------------- dify workflow ---------------------------------
|
||||
|
||||
// dify config sample
|
||||
private final String apiKey = "app-46gHBiqUb5jqAHl9TDWwnRZ8";
|
||||
private final String baseUrl = "http://localhost/v1";
|
||||
|
||||
@GetMapping("/dify/simple")
|
||||
@ResponseBody
|
||||
public String difySimple(@RequestParam(required = false, value = "input") String input) throws Exception {
|
||||
|
||||
Map<String, Object> inputs = new HashMap<>();
|
||||
inputs.put("input", input);
|
||||
|
||||
// request
|
||||
WorkflowRunRequest request = WorkflowRunRequest.builder()
|
||||
.inputs(inputs)
|
||||
.responseMode(ResponseMode.BLOCKING)
|
||||
.user("user-123")
|
||||
.build();
|
||||
|
||||
// invoke
|
||||
DifyWorkflowClient workflowClient = DifyClientFactory.createWorkflowClient(baseUrl, apiKey);
|
||||
WorkflowRunResponse response = workflowClient.runWorkflow(request);
|
||||
|
||||
// response
|
||||
return write2Json(response.getData().getOutputs());
|
||||
}
|
||||
|
||||
private String write2Json(Object obj) {
|
||||
if (obj == null) {
|
||||
return "null";
|
||||
}
|
||||
try {
|
||||
return new ObjectMapper().writeValueAsString(obj);
|
||||
} catch (JsonProcessingException e) {
|
||||
return obj.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping( "/dify/stream")
|
||||
public Flux<String> difyStream(@RequestParam(required = false, value = "input") String input) {
|
||||
|
||||
Map<String, Object> inputs = new HashMap<>();
|
||||
inputs.put("input", input);
|
||||
|
||||
// request
|
||||
WorkflowRunRequest request = WorkflowRunRequest.builder()
|
||||
.inputs(inputs)
|
||||
.responseMode(ResponseMode.STREAMING)
|
||||
.user("user-123")
|
||||
.build();
|
||||
|
||||
// invoke
|
||||
DifyWorkflowClient workflowClient = DifyClientFactory.createWorkflowClient(baseUrl, apiKey);
|
||||
return Flux.create(new Consumer<FluxSink<String>>() {
|
||||
@Override
|
||||
public void accept(FluxSink<String> sink) {
|
||||
try {
|
||||
workflowClient.runWorkflowStream(request, new WorkflowStreamCallback() {
|
||||
@Override
|
||||
public void onWorkflowStarted(WorkflowStartedEvent event) {
|
||||
sink.next("工作流开始: " + write2Json(event.getData()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNodeStarted(NodeStartedEvent event) {
|
||||
sink.next("节点开始: " + write2Json(event.getData()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNodeFinished(NodeFinishedEvent event) {
|
||||
sink.next("节点完成: " + write2Json(event.getData().getOutputs()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onWorkflowFinished(WorkflowFinishedEvent event) {
|
||||
sink.next("工作流完成: " + write2Json(event.getData().getOutputs()));
|
||||
sink.complete();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(ErrorEvent event) {
|
||||
sink.error(new RuntimeException(event.getMessage()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onException(Throwable throwable) {
|
||||
sink.error(throwable);
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,255 @@
|
||||
package com.xxl.job.executor.jobhandler;
|
||||
|
||||
import com.xxl.job.core.context.XxlJobHelper;
|
||||
import com.xxl.job.core.handler.annotation.XxlJob;
|
||||
import com.xxl.tool.json.GsonTool;
|
||||
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 jakarta.annotation.Resource;
|
||||
import org.springframework.ai.chat.client.ChatClient;
|
||||
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
|
||||
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
|
||||
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
|
||||
import org.springframework.ai.ollama.OllamaChatModel;
|
||||
import org.springframework.ai.ollama.api.OllamaChatOptions;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* AI 任务开发示例
|
||||
*
|
||||
* @author xuxueli 2025-04-06
|
||||
*/
|
||||
@Component
|
||||
public class AIXxlJob {
|
||||
|
||||
// --------------------------------- ollama chat ---------------------------------
|
||||
|
||||
@Resource
|
||||
private OllamaChatModel ollamaChatModel;
|
||||
|
||||
/**
|
||||
* 1、ollama Chat任务
|
||||
*
|
||||
* 参数示例:格式见 OllamaParam
|
||||
* <pre>
|
||||
* {
|
||||
* "input": "{输入信息,必填信息}",
|
||||
* "prompt": "{模型prompt,可选信息}"
|
||||
* }
|
||||
* </pre>
|
||||
*/
|
||||
@XxlJob("ollamaJobHandler")
|
||||
public void ollamaJobHandler() {
|
||||
|
||||
// param
|
||||
String param = XxlJobHelper.getJobParam();
|
||||
if (param==null || param.trim().isEmpty()) {
|
||||
XxlJobHelper.log("param is empty.");
|
||||
|
||||
XxlJobHelper.handleFail();
|
||||
return;
|
||||
}
|
||||
|
||||
// ollama param
|
||||
OllamaParam ollamaParam = null;
|
||||
try {
|
||||
ollamaParam = GsonTool.fromJson(param, OllamaParam.class);
|
||||
if (ollamaParam.getPrompt()==null || ollamaParam.getPrompt().isBlank()) {
|
||||
ollamaParam.setPrompt("你是一个研发工程师,擅长解决技术类问题。");
|
||||
}
|
||||
if (ollamaParam.getInput() == null || ollamaParam.getInput().isBlank()) {
|
||||
XxlJobHelper.log("input is empty.");
|
||||
|
||||
XxlJobHelper.handleFail();
|
||||
return;
|
||||
}
|
||||
if (ollamaParam.getModel()==null || ollamaParam.getModel().isBlank()) {
|
||||
ollamaParam.setModel("qwen3:0.6b");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
XxlJobHelper.log(new RuntimeException("OllamaParam parse error", e));
|
||||
XxlJobHelper.handleFail();
|
||||
return;
|
||||
}
|
||||
|
||||
// input
|
||||
XxlJobHelper.log("<br><br><b>【Input】: " + ollamaParam.getInput()+ "</b><br><br>");
|
||||
|
||||
// build chat-client
|
||||
ChatClient ollamaChatClient = ChatClient
|
||||
.builder(ollamaChatModel)
|
||||
.defaultAdvisors(MessageChatMemoryAdvisor.builder(MessageWindowChatMemory.builder().build()).build())
|
||||
.defaultAdvisors(SimpleLoggerAdvisor.builder().build())
|
||||
.defaultOptions(OllamaChatOptions.builder().model(ollamaParam.getModel()).build())
|
||||
.build();
|
||||
|
||||
// call ollama
|
||||
String response = ollamaChatClient
|
||||
.prompt(ollamaParam.getPrompt())
|
||||
.user(ollamaParam.getInput())
|
||||
.call()
|
||||
.content();
|
||||
|
||||
XxlJobHelper.log("<br><br><b>【Output】: " + response + "</b><br><br>");
|
||||
}
|
||||
|
||||
private static class OllamaParam{
|
||||
private String input;
|
||||
private String prompt;
|
||||
private String model;
|
||||
|
||||
public String getInput() {
|
||||
return input;
|
||||
}
|
||||
|
||||
public void setInput(String input) {
|
||||
this.input = input;
|
||||
}
|
||||
|
||||
public String getPrompt() {
|
||||
return prompt;
|
||||
}
|
||||
|
||||
public void setPrompt(String prompt) {
|
||||
this.prompt = prompt;
|
||||
}
|
||||
|
||||
public String getModel() {
|
||||
return model;
|
||||
}
|
||||
|
||||
public void setModel(String model) {
|
||||
this.model = model;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// --------------------------------- dify workflow ---------------------------------
|
||||
|
||||
/**
|
||||
* 2、dify Workflow任务
|
||||
*
|
||||
* 参数示例:格式见 DifyParam
|
||||
* <pre>
|
||||
* {
|
||||
* "inputs":{ // inputs 为dify工作流任务参数;参数不固定,结合各自 workflow 自行定义。
|
||||
* "input":"{用户输入信息}" // 该参数为示例变量,需要 workflow 的“开始”节点 自定义参数 “input”,可自行调整或删除。
|
||||
* },
|
||||
* "user": "{用户标识,选填}"
|
||||
* }
|
||||
* </pre>
|
||||
*/
|
||||
@XxlJob("difyWorkflowJobHandler")
|
||||
public void difyWorkflowJobHandler() throws Exception {
|
||||
|
||||
// param
|
||||
String param = XxlJobHelper.getJobParam();
|
||||
if (param==null || param.trim().isEmpty()) {
|
||||
XxlJobHelper.log("param is empty.");
|
||||
XxlJobHelper.handleFail();
|
||||
return;
|
||||
}
|
||||
|
||||
// param parse
|
||||
DifyParam difyParam;
|
||||
try {
|
||||
difyParam =GsonTool.fromJson(param, DifyParam.class);
|
||||
if (difyParam.getInputs() == null) {
|
||||
difyParam.setInputs(new HashMap<>());
|
||||
}
|
||||
if (difyParam.getUser() == null) {
|
||||
difyParam.setUser("xxl-job");
|
||||
}
|
||||
if (difyParam.getBaseUrl()==null || difyParam.getApiKey()==null) {
|
||||
XxlJobHelper.log("baseUrl or apiKey invalid.");
|
||||
XxlJobHelper.handleFail();
|
||||
return;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
XxlJobHelper.log(new RuntimeException("DifyParam parse error", e));
|
||||
XxlJobHelper.handleFail();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// dify param
|
||||
XxlJobHelper.log("<br><br><b>【inputs】: " + difyParam.getInputs() + "</b><br><br>");
|
||||
|
||||
// dify request
|
||||
WorkflowRunRequest request = WorkflowRunRequest.builder()
|
||||
.inputs(difyParam.getInputs())
|
||||
.responseMode(ResponseMode.BLOCKING)
|
||||
.user(difyParam.getUser())
|
||||
.build();
|
||||
|
||||
// dify invoke
|
||||
DifyWorkflowClient workflowClient = DifyClientFactory.createWorkflowClient(difyParam.getBaseUrl(), difyParam.getApiKey());
|
||||
WorkflowRunResponse response = workflowClient.runWorkflow(request);
|
||||
|
||||
// response
|
||||
XxlJobHelper.log("<br><br><b>【Output】: " + response.getData().getOutputs()+ "</b><br><br>");
|
||||
}
|
||||
|
||||
private static class DifyParam{
|
||||
|
||||
/**
|
||||
* dify input, 允许传入 Dify App 定义的各变量值
|
||||
*/
|
||||
private Map<String, Object> inputs;
|
||||
|
||||
/**
|
||||
* dify user
|
||||
*/
|
||||
private String user;
|
||||
|
||||
/**
|
||||
* dify baseUrl
|
||||
*/
|
||||
private String baseUrl;
|
||||
|
||||
/**
|
||||
* dify apiKey
|
||||
*/
|
||||
private String apiKey;
|
||||
|
||||
public Map<String, Object> getInputs() {
|
||||
return inputs;
|
||||
}
|
||||
|
||||
public void setInputs(Map<String, Object> inputs) {
|
||||
this.inputs = inputs;
|
||||
}
|
||||
|
||||
public String getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public void setUser(String user) {
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
public String getBaseUrl() {
|
||||
return baseUrl;
|
||||
}
|
||||
|
||||
public void setBaseUrl(String baseUrl) {
|
||||
this.baseUrl = baseUrl;
|
||||
}
|
||||
|
||||
public String getApiKey() {
|
||||
return apiKey;
|
||||
}
|
||||
|
||||
public void setApiKey(String apiKey) {
|
||||
this.apiKey = apiKey;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
# web port
|
||||
server.port=8082
|
||||
# no web
|
||||
#spring.main.web-environment=false
|
||||
|
||||
server.servlet.encoding.force=true
|
||||
server.servlet.encoding.charset=UTF-8
|
||||
|
||||
# log config
|
||||
logging.config=classpath:logback.xml
|
||||
|
||||
|
||||
### xxl-job admin address list, such as "http://address" or "http://address01,http://address02"
|
||||
xxl.job.admin.addresses=http://127.0.0.1:8080/xxl-job-admin
|
||||
### xxl-job access token
|
||||
xxl.job.admin.accessToken=default_token
|
||||
### xxl-job timeout by second, default 3s
|
||||
xxl.job.admin.timeout=3
|
||||
|
||||
### xxl-job executor enable, default true
|
||||
xxl.job.executor.enabled=true
|
||||
### xxl-job executor appname
|
||||
xxl.job.executor.appname=xxl-job-executor-sample-ai
|
||||
### xxl-job executor registry-address: default use address to registry , otherwise use ip:port if address is null
|
||||
xxl.job.executor.address=
|
||||
### xxl-job executor server-info
|
||||
xxl.job.executor.ip=
|
||||
xxl.job.executor.port=9997
|
||||
### xxl-job executor log-path
|
||||
xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler
|
||||
### xxl-job executor log-retention-days
|
||||
xxl.job.executor.logretentiondays=30
|
||||
### xxl-job executor excluded package, will skip scan job. such as "org.package01" or "org.package01,org.package02"
|
||||
xxl.job.executor.excludedpackage=
|
||||
|
||||
|
||||
### ollama
|
||||
spring.ai.model.chat=ollama
|
||||
### ollama url
|
||||
spring.ai.ollama.base-url=http://localhost:11434
|
||||
@@ -0,0 +1,27 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration debug="false" scan="true" scanPeriod="1 seconds">
|
||||
|
||||
<contextName>logback</contextName>
|
||||
<property name="log.path" value="${LOG_HOME:-/data/applogs}/xxl-job/xxl-job-executor-sample-springboot-ai.log" />
|
||||
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<file>${log.path}</file>
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<fileNamePattern>${log.path}.%d{yyyy-MM-dd}.log</fileNamePattern>
|
||||
</rollingPolicy>
|
||||
<encoder>
|
||||
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="info">
|
||||
<appender-ref ref="console"/>
|
||||
<!--<appender-ref ref="file"/>-->
|
||||
</root>
|
||||
|
||||
</configuration>
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.xxl.job.executor.test;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
public class BaseTests {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
System.out.println(11);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<String, Object> 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());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package com.xxl.job.executor.test.ollama;
|
||||
|
||||
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.ai.chat.client.ChatClient;
|
||||
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
|
||||
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
|
||||
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
|
||||
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 {
|
||||
private static final Logger logger = LoggerFactory.getLogger(OllamaTest.class);
|
||||
|
||||
// ignore
|
||||
@MockitoBean
|
||||
private XxlJobSpringExecutor xxlJobSpringExecutor;
|
||||
|
||||
|
||||
@Resource
|
||||
private OllamaChatModel ollamaChatModel;
|
||||
|
||||
@Test
|
||||
public void chatTest() {
|
||||
|
||||
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
|
||||
String response = ollamaChatClient
|
||||
.prompt(prompt)
|
||||
.user(input)
|
||||
.call()
|
||||
.content();
|
||||
|
||||
logger.info("input: {}", input);
|
||||
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<String> 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);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user