Commit ad68abba authored by 林洋洋's avatar 林洋洋

添加调用工具 接口

parent 86989a4b
...@@ -8,6 +8,7 @@ import com.ask.common.core.R; ...@@ -8,6 +8,7 @@ import com.ask.common.core.R;
import com.ask.service.ChatConversationService; import com.ask.service.ChatConversationService;
import com.ask.service.impl.ChatService; import com.ask.service.impl.ChatService;
import com.ask.service.impl.RagPromptService; import com.ask.service.impl.RagPromptService;
import com.ask.tools.ExcelTools;
import com.ask.utils.FluxUtils; import com.ask.utils.FluxUtils;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.Parameter;
...@@ -25,6 +26,8 @@ import org.springframework.ai.chat.model.ChatResponse; ...@@ -25,6 +26,8 @@ import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt; import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.deepseek.DeepSeekAssistantMessage; import org.springframework.ai.deepseek.DeepSeekAssistantMessage;
import org.springframework.ai.document.Document; import org.springframework.ai.document.Document;
import org.springframework.ai.model.tool.ToolCallingChatOptions;
import org.springframework.ai.model.tool.ToolExecutionResult;
import org.springframework.ai.openai.OpenAiChatModel; import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.ai.rag.advisor.RetrievalAugmentationAdvisor; import org.springframework.ai.rag.advisor.RetrievalAugmentationAdvisor;
import org.springframework.ai.rag.retrieval.search.VectorStoreDocumentRetriever; import org.springframework.ai.rag.retrieval.search.VectorStoreDocumentRetriever;
...@@ -68,6 +71,8 @@ public class ChatController { ...@@ -68,6 +71,8 @@ public class ChatController {
private final ChatMemory chatMemory; private final ChatMemory chatMemory;
private final OpenAiChatModel openAiChatModel; private final OpenAiChatModel openAiChatModel;
private final ExcelTools excelTools;
/** /**
* 获取会话ID * 获取会话ID
* *
...@@ -104,9 +109,6 @@ public class ChatController { ...@@ -104,9 +109,6 @@ public class ChatController {
Message userMessage = new UserMessage("问题:" + message + "\n回答要求:请使用markdown格式输出"); Message userMessage = new UserMessage("问题:" + message + "\n回答要求:请使用markdown格式输出");
Prompt prompt = new Prompt(List.of(systemMessage, userMessage)); Prompt prompt = new Prompt(List.of(systemMessage, userMessage));
AtomicBoolean reasoningStarted = new AtomicBoolean(false);
AtomicBoolean answerStarted = new AtomicBoolean(false);
return FluxUtils.wrapDeepSeekStream(deepseekChatClient.prompt(prompt) return FluxUtils.wrapDeepSeekStream(deepseekChatClient.prompt(prompt)
.advisors(messageChatMemoryAdvisor) .advisors(messageChatMemoryAdvisor)
.advisors(a -> a.param(ChatMemory.CONVERSATION_ID, conversationId)) .advisors(a -> a.param(ChatMemory.CONVERSATION_ID, conversationId))
...@@ -114,6 +116,7 @@ public class ChatController { ...@@ -114,6 +116,7 @@ public class ChatController {
.chatResponse()); .chatResponse());
} }
/** /**
* 知识库对话 * 知识库对话
* <p> * <p>
...@@ -145,9 +148,9 @@ public class ChatController { ...@@ -145,9 +148,9 @@ public class ChatController {
return FluxUtils.wrapDeepSeekStream(deepseekChatClient.prompt() return FluxUtils.wrapDeepSeekStream(deepseekChatClient.prompt()
.user(userPrompt) .user(userPrompt)
.system("你是一个智能助手,基于以下上下文和历史对话回答问题,请用简洁的语言回答问题,并确保答案准确,要求" + .system("你是一个智能助手,基于以下上下文和历史对话回答问题,请用简洁的语言回答问题,并确保答案准确,要求" +
"1.以 Markdown 格式输出" ) "1.以 Markdown 格式输出")
.stream() .stream()
.chatResponse(),contentBuilder) .chatResponse(), contentBuilder)
.concatWith(Flux.just(reference)) .concatWith(Flux.just(reference))
.doOnComplete(() -> { .doOnComplete(() -> {
// 流结束时获取完整内容 // 流结束时获取完整内容
...@@ -156,4 +159,36 @@ public class ChatController { ...@@ -156,4 +159,36 @@ public class ChatController {
chatService.saveHistoryMemory(conversationId, new AssistantMessage(fullResponse)); chatService.saveHistoryMemory(conversationId, new AssistantMessage(fullResponse));
}); });
} }
@Operation(summary = "智能数据报表对话", description = "智能数据报表对话")
@GetMapping(value = "/chat/report")
public String reportChat(@RequestParam String message,
@RequestParam String conversationId) {
Message systemMessage = new SystemMessage("你是一个AI问答助手,请用回答用户问题,使用相关工具");
Message userMessage = new UserMessage("问题:" + message + "\n回答要求:请使用markdown格式输出");
Prompt prompt = new Prompt(List.of(systemMessage, userMessage));
// ChatResponse chatResponse = openAiChatClient.prompt(prompt)
// .advisors(messageChatMemoryAdvisor)
// .advisors(a -> a.param(ChatMemory.CONVERSATION_ID, conversationId))
// .tools(new ExcelTools())
// // 关闭内部自动执行
// .options(
// ToolCallingChatOptions.builder()
// .internalToolExecutionEnabled(false) // ← 关键
// .build()
// )
// .call().chatResponse();
return openAiChatClient.prompt(prompt)
.advisors(messageChatMemoryAdvisor)
.advisors(a -> a.param(ChatMemory.CONVERSATION_ID, conversationId))
.tools(new ExcelTools())
.call()
.content();
}
} }
\ No newline at end of file
package com.ask.tools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
@Component
@Slf4j
public class ExcelTools {
@Tool(description = "Get the current date and time in the user's timezone")
String getCurrentDateTime() {
log.info("调用getCurrentDateTime");
return LocalDateTime.now().atZone(LocaleContextHolder.getTimeZone().toZoneId()).toString();
}
}
...@@ -25,12 +25,12 @@ public class FluxUtils { ...@@ -25,12 +25,12 @@ public class FluxUtils {
(DeepSeekAssistantMessage) resp.getResult().getOutput(); (DeepSeekAssistantMessage) resp.getResult().getOutput();
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
if (reasoningStarted.compareAndSet(false, true)) {
sb.append("<think>");
}
// 推理阶段:第一次出现推理内容时输出 <think> // 推理阶段:第一次出现推理内容时输出 <think>
if (StringUtils.isNotBlank(msg.getReasoningContent())) { if (StringUtils.isNotBlank(msg.getReasoningContent())) {
if (reasoningStarted.compareAndSet(false, true)) {
sb.append("<think>");
}
sb.append(msg.getReasoningContent()); sb.append(msg.getReasoningContent());
} }
...@@ -58,12 +58,11 @@ public class FluxUtils { ...@@ -58,12 +58,11 @@ public class FluxUtils {
(DeepSeekAssistantMessage) resp.getResult().getOutput(); (DeepSeekAssistantMessage) resp.getResult().getOutput();
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
if (reasoningStarted.compareAndSet(false, true)) {
sb.append("<think>");
}
// 推理阶段:第一次出现推理内容时输出 <think> // 推理阶段:第一次出现推理内容时输出 <think>
if (StringUtils.isNotBlank(msg.getReasoningContent())) { if (StringUtils.isNotBlank(msg.getReasoningContent())) {
if (reasoningStarted.compareAndSet(false, true)) {
sb.append("<think>");
}
sb.append(msg.getReasoningContent()); sb.append(msg.getReasoningContent());
} }
......
...@@ -37,7 +37,7 @@ spring: ...@@ -37,7 +37,7 @@ spring:
api-key: sk-ae96ff281ff644c992843c64a711a950 api-key: sk-ae96ff281ff644c992843c64a711a950
chat: chat:
options: options:
model: deepseek-r1 model: qwen-plus
embedding: embedding:
base-url: https://dashscope.aliyuncs.com/compatible-mode base-url: https://dashscope.aliyuncs.com/compatible-mode
api-key: sk-ae96ff281ff644c992843c64a711a950 api-key: sk-ae96ff281ff644c992843c64a711a950
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment