|
@@ -5,6 +5,7 @@ import cn.hutool.core.util.IdUtil;
|
|
|
import cn.hutool.http.HttpRequest;
|
|
|
import cn.hutool.http.HttpResponse;
|
|
|
import cn.hutool.json.JSONObject;
|
|
|
+import cn.iocoder.yudao.framework.ai.core.model.wenduoduo.api.DifyApi;
|
|
|
import cn.iocoder.yudao.framework.dict.core.DictFrameworkUtils;
|
|
|
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
|
|
|
import cn.iocoder.yudao.module.infra.service.aipromptmanagement.utils.DifyFilesUtils;
|
|
@@ -14,6 +15,7 @@ import com.alibaba.fastjson.JSON;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
+import reactor.core.publisher.Flux;
|
|
|
|
|
|
import java.io.File;
|
|
|
import java.util.Map;
|
|
@@ -29,12 +31,12 @@ public class WorkflowRunServiceImpl implements WorkflowRunService {
|
|
|
|
|
|
@Value("${dify.base-url}")
|
|
|
private String baseUrl;
|
|
|
-
|
|
|
@Value("${dify.ocr-key}")
|
|
|
private String ocrKey;
|
|
|
@Value("${dify.file-path}")
|
|
|
private String filePath;
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* 从 URL 中提取文件扩展名
|
|
|
*
|
|
@@ -247,8 +249,19 @@ public class WorkflowRunServiceImpl implements WorkflowRunService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public String getDifyMessage(Map<String, Object> inputs, String eventType, String aiKey, Long userId) {
|
|
|
- return "";
|
|
|
+ public String getDifyMessage(Map<String, Object> inputs, String user, String apiKey, String query,
|
|
|
+ String conversationId) {
|
|
|
+ DifyResponse response = getDifyMessageResponse(inputs, user, apiKey, query, conversationId);
|
|
|
+ return response.getData().getOutputs().getText();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Flux<StreamResponse> getDifyMessageStreaming(Map<String, Object> inputs, String user, String apiKey,
|
|
|
+ String query,
|
|
|
+ String conversationId) {
|
|
|
+ DifyApi difyApi = new DifyApi(baseUrl);
|
|
|
+ Flux<StreamResponse> rs = difyApi.chatMessages(apiKey, inputs, user, query, conversationId);
|
|
|
+ return rs;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -258,7 +271,8 @@ public class WorkflowRunServiceImpl implements WorkflowRunService {
|
|
|
* @param user 用户
|
|
|
* @return {@link DifyResponse }
|
|
|
*/
|
|
|
- public DifyResponse getDifyMessageResponse(Map<String, Object> inputs, String user, String apiKey) {
|
|
|
+ public DifyResponse getDifyMessageResponse(Map<String, Object> inputs, String user, String apiKey, String query,
|
|
|
+ String conversationId) {
|
|
|
DifyResponse difyResponse = new DifyResponse();
|
|
|
// 接口地址
|
|
|
String url = baseUrl + "/v1/chat-messages";
|
|
@@ -271,10 +285,10 @@ public class WorkflowRunServiceImpl implements WorkflowRunService {
|
|
|
// 构建请求体
|
|
|
JSONObject requestBody = new JSONObject();
|
|
|
requestBody.put("inputs", inputs);
|
|
|
- requestBody.put("response_mode", "streaming");
|
|
|
+ requestBody.put("response_mode", "blocking");
|
|
|
requestBody.put("user", user);
|
|
|
-// requestBody.put("conversation_id", );
|
|
|
- requestBody.put("query", user);
|
|
|
+ requestBody.put("conversation_id", conversationId);
|
|
|
+ requestBody.put("query", query);
|
|
|
|
|
|
// 发送 POST 请求
|
|
|
HttpResponse response = HttpRequest.post(url)
|