From a5a9ac7c2a9fd6026b0d87e1b0eff763eb4512d0 Mon Sep 17 00:00:00 2001
From: admin <cgf12138@163.com>
Date: 星期二, 10 六月 2025 18:25:20 +0800
Subject: [PATCH] 0610
---
aiflowy-modules/aiflowy-module-ai/src/main/java/tech/aiflowy/ai/config/DifyStreamClient.java | 52 +++++++++++++++++++++++++++++++++++++++++++---------
1 files changed, 43 insertions(+), 9 deletions(-)
diff --git a/aiflowy-modules/aiflowy-module-ai/src/main/java/tech/aiflowy/ai/config/DifyStreamClient.java b/aiflowy-modules/aiflowy-module-ai/src/main/java/tech/aiflowy/ai/config/DifyStreamClient.java
index 1226036..f489f9b 100644
--- a/aiflowy-modules/aiflowy-module-ai/src/main/java/tech/aiflowy/ai/config/DifyStreamClient.java
+++ b/aiflowy-modules/aiflowy-module-ai/src/main/java/tech/aiflowy/ai/config/DifyStreamClient.java
@@ -19,6 +19,8 @@
import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.TimeUnit;
+
public class DifyStreamClient {
private final OkHttpClient client;
private final String apiUrl;
@@ -32,7 +34,8 @@
this.apiUrl = apiUrl;
this.apiKey = apiKey;
this.gson = new GsonBuilder().setPrettyPrinting().create();
- this.client = new OkHttpClient.Builder().build();
+ this.client = new OkHttpClient.Builder().connectTimeout(60, TimeUnit.SECONDS) // 杩炴帴瓒呮椂
+ .readTimeout(120, TimeUnit.SECONDS).build();
this.aiBotMessageService = aiBotMessageService;
}
@@ -57,11 +60,11 @@
}
}
requestBody.add("inputs", inputsJson);
-
// 璁剧疆鍝嶅簲妯″紡鍜岀敤鎴稩D
requestBody.addProperty("response_mode", "streaming");
requestBody.addProperty("user", userId);
+// System.out.println(requestBody+"==============================================================================================");
// 鍒涘缓璇锋眰
RequestBody body = RequestBody.create(
gson.toJson(requestBody),
@@ -105,6 +108,7 @@
@Override
public void onResponse(Call call, Response response) {
+ StringBuffer sb = new StringBuffer();
try (ResponseBody responseBody = response.body()) {
if (!response.isSuccessful()) {
emitter.completeWithError(new IOException("API閿欒: " + response.code()));
@@ -129,6 +133,7 @@
// 鍋囪API杩斿洖鐨勬牸寮忔槸{ "output": "娑堟伅鍐呭" }
JsonObject jsonObject = gson.fromJson(data, JsonObject.class);
String title = null;
+// System.out.println(jsonObject);
if (jsonObject != null && jsonObject.has("data")) {
JsonElement dataElement = jsonObject.get("data");
if (dataElement != null && !dataElement.isJsonNull()) {
@@ -142,10 +147,36 @@
title = titleElement.getAsString();
}
}
- if (dataObject != null && dataObject.has("text")) {
+ else if (dataObject != null && dataObject.has("text")) {
JsonElement titleElement = dataObject.get("text");
if (titleElement != null && !titleElement.isJsonNull()) {
title = titleElement.getAsString();
+ }
+ }else if (dataObject != null && dataObject.has("outputs")) {
+ JsonElement titleElement = dataObject.get("outputs");
+ if (titleElement != null && !titleElement.isJsonNull()) {
+
+ AiBotMessage aiBotMessage = new AiBotMessage();
+ aiBotMessage.setBotId(botId);
+ aiBotMessage.setSessionId(sessionId);
+ aiBotMessage.setAccountId(new BigInteger(userId));
+ aiBotMessage.setRole("assistant");
+ aiBotMessage.setContent(sb.toString());
+ aiBotMessage.setCreated(new Date());
+ aiBotMessage.setIsExternalMsg(1);
+ aiBotMessageService.save(aiBotMessage);
+// dataObject = titleElement.getAsJsonObject();
+// if (dataObject != null && dataObject.has("text")) {
+// titleElement = dataObject.get("text");
+// if (titleElement != null && !titleElement.isJsonNull()) {
+// title = titleElement.getAsString();
+// }
+// }else if (dataObject != null && dataObject.has("data")) {
+// titleElement = dataObject.get("data");
+// if (titleElement != null && !titleElement.isJsonNull()) {
+// title = titleElement.getAsString();
+// }
+// }
}
}
}
@@ -155,8 +186,11 @@
AiMessage aiMessage = new AiMessage();
aiMessage.setContent(title);
System.out.println(gson.fromJson(data, JsonObject.class));
+ sb.append(aiMessage.getContent());
// 灏嗘秷鎭彂閫佺粰鍓嶇
- emitter.send(JSON.toJSONString(aiMessage));
+ if (aiMessage.getContent() != null){
+ emitter.send(JSON.toJSONString(aiMessage));
+ }
} catch (Exception e) {
// 璁板綍瑙f瀽閿欒浣嗙户缁鐞嗗悗缁暟鎹�
@@ -252,13 +286,13 @@
requestBody.addProperty("response_mode", "streaming");
requestBody.addProperty("conversation_id", "");
requestBody.addProperty("user", userId);
- requestBody.add("files", new JsonArray());
+// requestBody.add("files", new JsonArray());
// 娣诲姞鍘嗗彶瀵硅瘽淇℃伅
JsonArray historyArray = new JsonArray();
- for (AiBotMessage msg : history) {
- historyArray.add(String.valueOf(msg));
- }
- requestBody.add("history", historyArray);
+// for (AiBotMessage msg : history) {
+// historyArray.add(String.valueOf(msg));
+// }
+// requestBody.add("history", historyArray);
RequestBody body = RequestBody.create(
gson.toJson(requestBody),
--
Gitblit v1.8.0