|
@@ -152,6 +152,63 @@ public class WorkflowRunServiceImpl implements WorkflowRunService {
|
|
|
return difyResponse;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取dify响应
|
|
|
+ *
|
|
|
+ * @param inputs 输入
|
|
|
+ * @param user 用户
|
|
|
+ * @return {@link DifyResponse }
|
|
|
+ */
|
|
|
+ public DifyResponse getDifyResponse(Map<String, Object> inputs, String user, String apiKey) {
|
|
|
+ DifyResponse difyResponse = new DifyResponse();
|
|
|
+ // 接口地址
|
|
|
+ String url = baseUrl + "/v1/workflows/run";
|
|
|
+ // 替换为你的实际 API Key
|
|
|
+
|
|
|
+
|
|
|
+ // 创建一个 ObjectMapper 实例,用于处理 JSON 转换
|
|
|
+
|
|
|
+
|
|
|
+ // 构建请求体
|
|
|
+ JSONObject requestBody = new JSONObject();
|
|
|
+ requestBody.put("inputs", inputs);
|
|
|
+ requestBody.put("response_mode", "streaming");
|
|
|
+ requestBody.put("user", user);
|
|
|
+
|
|
|
+ // 发送 POST 请求
|
|
|
+ HttpResponse response = HttpRequest.post(url)
|
|
|
+ .header("Authorization", "Bearer " + apiKey)
|
|
|
+ .header("Content-Type", "application/json")
|
|
|
+ .body(requestBody.toString())
|
|
|
+ .execute();
|
|
|
+
|
|
|
+
|
|
|
+ // 处理响应
|
|
|
+ if (response.isOk()) {
|
|
|
+ // Hutool工具示例
|
|
|
+ String decodedBody = Convert.unicodeToStr(response.body());
|
|
|
+
|
|
|
+ // 使用 \n\n 分割字符串
|
|
|
+ String[] parts = decodedBody.split("\n\n");
|
|
|
+
|
|
|
+ // 取最后一条内容
|
|
|
+ String lastPart = parts[parts.length - 1];
|
|
|
+
|
|
|
+ System.out.println("请求成功,响应内容:");
|
|
|
+ System.out.println(lastPart);
|
|
|
+ // 去除"data: "前缀
|
|
|
+ String jsonStr = lastPart.substring(6);
|
|
|
+
|
|
|
+ // 转换为DifyResponse实体
|
|
|
+ difyResponse = JSON.parseObject(jsonStr, DifyResponse.class);
|
|
|
+ System.out.println(difyResponse.getData().getOutputs().getText());
|
|
|
+ } else {
|
|
|
+ System.out.println("请求失败,状态码:" + response.getStatus());
|
|
|
+ System.out.println("错误信息:" + response.body());
|
|
|
+ }
|
|
|
+ return difyResponse;
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public FileResp uploadFile(String fileurl) {
|
|
|
String extension = getFileExtension(fileurl);
|
|
@@ -172,4 +229,12 @@ public class WorkflowRunServiceImpl implements WorkflowRunService {
|
|
|
DifyResponse response = getDifyResponse(inputs, SecurityFrameworkUtils.getLoginUserId().toString());
|
|
|
return response.getData().getOutputs().getText();
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String getDifyResul(Map<String, Object> inputs, String eventType, String aiKey) {
|
|
|
+ // 创建 inputs 的 Map
|
|
|
+// inputs.put("images", list);
|
|
|
+ DifyResponse response = getDifyResponse(inputs, SecurityFrameworkUtils.getLoginUserId().toString(), aiKey);
|
|
|
+ return response.getData().getOutputs().getText();
|
|
|
+ }
|
|
|
}
|