소스 검색

优化OllamaUtil

tsurumure 5 달 전
부모
커밋
fe3780ec95

+ 1 - 1
src/main/java/com/backendsys/modules/ai/chat/service/impl/ChatServiceImpl.java

@@ -192,7 +192,7 @@ public class ChatServiceImpl implements ChatService {
                     }
 
                 } else {
-                    System.out.println("chatResult 结果为空! 请检查当前 (model: " + model + ") 是否正确");
+                    System.out.println("chatResult 结果为空!");
                 }
 
 

+ 18 - 2
src/main/java/com/backendsys/modules/sdk/deepseek/utils/OllamaUtil.java

@@ -177,15 +177,31 @@ public class OllamaUtil {
                         /*
                             ---------------------- [Chat] line ----------------------
                             {"model":"deepseek-r1:1.5b","created_at":"2025-03-18T07:37:06.483163789Z","message":{"role":"assistant","content":"\u003cthink\u003e"},"done":false}
-                            ---------------------- [Generate] line ----------------------
+
+                            ---------------------- [Generate] line ------------------
                             {"model":"deepseek-r1:1.5b","created_at":"2025-03-05T10:51:17.443189986Z","response":"\u003cthink\u003e","done":false}
                             {"model":"deepseek-r1:1.5b","created_at":"2025-03-06T11:08:30.9219611Z","response":"\n\n","done":false}
-                            -------------------------------------------------------------
+
+                            ---------------------- [Error] line ---------------------
+                            {"error":"llama runner process has terminated: error loading model: unable to allocate CUDA0 buffer"}
                          */
 
                         // 每行数据可以是一个JSON对象,根据实际情况处理
                         JSONObject resJson = JSONObject.parseObject(line);
 
+                        // -- 判断内容是否为空 (或报错) --------------------------------------
+                        if (resJson == null) return null;
+                        String errJsonMessage = resJson.getString("error");
+                        if (errJsonMessage != null) {
+                            // [SSE] 发送消息
+                            sseUtil.send(user_id, new SseResponse(SseResponseEnum.DEEPSEEK, new ChatSseMessage("REPLY", errJsonMessage, contentDuration)).toJsonStr());
+                            // [SSE] 发送消息 (完成)
+                            sseUtil.send(user_id, new SseResponse(SseResponseEnum.DEEPSEEK, new ChatSseMessage("REPLY", "[DONE][REPLY]", contentDuration)).toJsonStr());
+                            //
+                            chatResult.setContent(errJsonMessage);
+                            return chatResult;
+                        }
+
                         // --------------------------------------------------------------
                         // [Chat]
                         JSONObject resJsonMessage = resJson.getJSONObject("message");