diff --git a/service-ai/service-ai-chat/pom.xml b/service-ai/service-ai-chat/pom.xml index 3e96a43..e2cb1e5 100644 --- a/service-ai/service-ai-chat/pom.xml +++ b/service-ai/service-ai-chat/pom.xml @@ -16,11 +16,6 @@ com.lanyuanxiaoyao service-ai-core - - org.springframework.ai - spring-ai-starter-model-openai - test - org.springframework.ai spring-ai-starter-model-deepseek diff --git a/service-ai/service-ai-chat/src/main/java/com/lanyuanxiaoyao/service/ai/chat/controller/ChatController.java b/service-ai/service-ai-chat/src/main/java/com/lanyuanxiaoyao/service/ai/chat/controller/ChatController.java index d910542..369a644 100644 --- a/service-ai/service-ai-chat/src/main/java/com/lanyuanxiaoyao/service/ai/chat/controller/ChatController.java +++ b/service-ai/service-ai-chat/src/main/java/com/lanyuanxiaoyao/service/ai/chat/controller/ChatController.java @@ -3,9 +3,10 @@ package com.lanyuanxiaoyao.service.ai.chat.controller; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import com.lanyuanxiaoyao.service.ai.chat.entity.MessageVO; -import com.lanyuanxiaoyao.service.ai.chat.tools.DatetimeTools; import com.lanyuanxiaoyao.service.forest.service.KnowledgeService; import java.io.IOException; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; import java.util.Optional; import org.eclipse.collections.api.list.ImmutableList; import org.slf4j.Logger; @@ -35,6 +36,7 @@ import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; @RequestMapping("chat") public class ChatController { private static final Logger logger = LoggerFactory.getLogger(ChatController.class); + private final static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); private static final String ROLE_ASSISTANT = "assistant"; private static final String ROLE_USER = "user"; @@ -48,26 +50,29 @@ public class ChatController { } private ChatClient.ChatClientRequestSpec buildRequest(Long knowledgeId, ImmutableList messages) { - var systemPromptBuilder = new StringBuilder(); - systemPromptBuilder.append(""" - 你是一名专业的AI运维助手,负责“Hudi数据同步服务平台”的运维工作; - 你将会友好地帮助用户解答关于该平台运维工作的问题,你会尽可能通过各种方式获取知识和数据来解答; - 对于无法通过已有知识回答的问题,你会提示用户你无法解答该问题,而不是虚构不存在的数据或答案; - 对于与该平台无关的问题,你会委婉地拒绝用户,并提示无法回答; - 你将始终在中文语境下进行对话。 - """); + var builder = StrUtil.builder() + .append(StrUtil.format(""" + 你是一名专业的AI运维助手,负责“Hudi数据同步服务平台”的运维工作; + 你将会友好地帮助用户解答关于该平台运维工作的问题,你会尽可能通过各种方式获取知识和数据来解答; + 对于无法通过已有知识回答的问题,你会提示用户你无法解答该问题,而不是虚构不存在的数据或答案; + 对于与该平台无关的问题,你会委婉地拒绝用户,并提示无法回答; + 你将始终在中文语境下进行对话。 + 当前时间为:{} + + """, LocalDateTime.now().format(formatter))); if (ObjectUtil.isNotNull(knowledgeId)) { var vo = messages.select(message -> StrUtil.equals(message.getRole(), "user")).getLastOptional().orElseThrow(); - var documents = knowledgeService.query(knowledgeId, vo.getContent()); - logger.info("Knowledge id:{}, content:{}", knowledgeId, vo.getContent()); + var documents = knowledgeService.query(knowledgeId, vo.getContent(), 0.5); if (ObjectUtil.isNotEmpty(documents)) { - systemPromptBuilder.append("以下是与用户问题有关的外部知识,优先利用该知识回答用户的提问:\n"); - systemPromptBuilder.append(documents.makeString("\n")); + builder.append(StrUtil.format(""" + 以下是与用户问题有关的外部知识,优先利用该知识回答用户的提问: + {} + + """, documents.makeString("\n"))); } } return chatClient.prompt() - .system(systemPromptBuilder.toString()) - .tools(new DatetimeTools()) + .system(builder.toString()) .messages( messages .collect(message -> StrUtil.equals(message.getRole(), ROLE_ASSISTANT) diff --git a/service-ai/service-ai-chat/src/main/java/com/lanyuanxiaoyao/service/ai/chat/tools/DatetimeTools.java b/service-ai/service-ai-chat/src/main/java/com/lanyuanxiaoyao/service/ai/chat/tools/DatetimeTools.java deleted file mode 100644 index 5745b09..0000000 --- a/service-ai/service-ai-chat/src/main/java/com/lanyuanxiaoyao/service/ai/chat/tools/DatetimeTools.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.lanyuanxiaoyao.service.ai.chat.tools; - -import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; -import org.springframework.ai.tool.annotation.Tool; - -/** - * @author lanyuanxiaoyao - * @version 20250516 - */ -public class DatetimeTools { - private final static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); - - @Tool(description = "获取当前日期和时间") - public String getCurrentDateTime() { - return LocalDateTime.now().format(formatter); - } -} diff --git a/service-ai/service-ai-chat/src/test/java/com/lanyuanxiaoyao/service/ai/chat/TestChat.java b/service-ai/service-ai-chat/src/test/java/com/lanyuanxiaoyao/service/ai/chat/TestChat.java deleted file mode 100644 index 9939241..0000000 --- a/service-ai/service-ai-chat/src/test/java/com/lanyuanxiaoyao/service/ai/chat/TestChat.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.lanyuanxiaoyao.service.ai.chat; - -import org.springframework.ai.chat.client.ChatClient; -import org.springframework.ai.openai.OpenAiChatModel; -import org.springframework.ai.openai.OpenAiChatOptions; -import org.springframework.ai.openai.api.OpenAiApi; - -/** - * @author lanyuanxiaoyao - * @version 20250514 - */ -public class TestChat { - public static void main(String[] args) { - ChatClient client = ChatClient.builder( - OpenAiChatModel.builder() - .openAiApi( - OpenAiApi.builder() - .baseUrl("http://132.121.206.65:10086") - .apiKey("*XMySqV%>hR&v>>g*NwCs3tpQ5FVMFEF2VHVTjhR&v>>g*NwCs3tpQ5FVMFEF2VHVTj query(@Query("id") Long id, @Body String text); @Post(value = "/query", contentType = "plain/text") - ImmutableList query(@Query("id") Long id, @Query("limit") Integer limit, @Query("threshold") Double threshold, @Body String text); + ImmutableList query(@Query("id") Long id, @Body String text, @Query("threshold") Double threshold); + + @Post(value = "/query", contentType = "plain/text") + ImmutableList query(@Query("id") Long id, @Body String text, @Query("limit") Integer limit, @Query("threshold") Double threshold); }