Package com.bytedesk.ai.springai.service
Class BaseSpringAIService
java.lang.Object
com.bytedesk.ai.springai.service.BaseSpringAIService
- All Implemented Interfaces:
SpringAIService
- Direct Known Subclasses:
SpringAIBaiduService
,SpringAICustomService
,SpringAIDashscopeService
,SpringAIDeepseekService
,SpringAIGiteeService
,SpringAIOllamaService
,SpringAIOpenaiService
,SpringAISiliconFlowService
,SpringAITencentService
,SpringAIVolcengineService
,SpringAIZhipuaiService
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected FaqService
protected MessagePersistCache
protected IMessageSendService
protected RobotMessageCache
protected RobotRestService
protected SpringAIVectorStoreService
protected ThreadRestService
protected UidUtils
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
protected
BaseSpringAIService
(SpringAIVectorStoreService springAIVectorService, IMessageSendService messageSendService) -
Method Summary
Modifier and TypeMethodDescriptionbuildKbPrompt
(String systemPrompt, String query, String history, String context) private void
createAndProcessPrompt
(String query, String context, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter) 创建提示词并处理SSE消息的通用方法protected <T> T
createDynamicOptions
(RobotLlm llm, Function<RobotLlm, T> optionBuilder) 创建动态的聊天选项(通用方法,使用泛型)private void
executeFulltextSearch
(String query, String kbUid, List<String> searchContentList, List<FaqProtobuf> faqProtobufList) 执行全文搜索private void
executeVectorSearch
(String query, String kbUid, List<String> searchContentList, List<FaqProtobuf> faqProtobufList) 执行向量搜索protected abstract String
generateFaqPairs
(String prompt) generateFaqPairsAsync
(String chunk) 异步生成FAQ对void
generateFaqPairsSync
(String chunk) 同步生成FAQ对protected void
handleSseError
(Throwable error, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter) 处理SSE错误的通用方法protected boolean
isEmitterCompleted
(org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter) 检查SseEmitter是否已完成void
persistMessage
(MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply) private void
processAnswerMessage
(String answer, MessageTypeEnum type, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter) private void
processLlmResponse
(String query, List<String> searchContentList, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter) protected abstract void
processPrompt
(org.springframework.ai.chat.prompt.Prompt prompt, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply) protected abstract void
processPromptSSE
(org.springframework.ai.chat.prompt.Prompt prompt, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter) protected abstract String
processPromptSync
(String message) private void
processSearchResponse
(String query, List<FaqProtobuf> searchContentList, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter) protected void
sendMessage
(MessageTypeEnum type, String content, MessageProtobuf messageProtobufReply) 发送消息的通用方法void
sendSseMessage
(String query, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter) 发送Sse消息protected void
sendStreamEndMessage
(MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter) 发送流式结束消息protected void
sendStreamMessage
(MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter, String content) 发送流式内容消息protected void
sendStreamStartMessage
(MessageProtobuf messageProtobufReply, org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter, String initialContent) 发送流式开始消息void
sendWebsocketMessage
(String query, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply) 发送websocket消息
-
Field Details
-
springAIVectorService
-
faqService
-
messageSendService
-
uidUtils
-
robotRestService
-
threadRestService
-
messagePersistCache
-
robotMessageCache
-
-
Constructor Details
-
BaseSpringAIService
protected BaseSpringAIService() -
BaseSpringAIService
protected BaseSpringAIService(SpringAIVectorStoreService springAIVectorService, IMessageSendService messageSendService)
-
-
Method Details
-
sendWebsocketMessage
public void sendWebsocketMessage(String query, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply) Description copied from interface:SpringAIService
发送websocket消息- Specified by:
sendWebsocketMessage
in interfaceSpringAIService
- Parameters:
query
- 用户查询robot
- 机器人实体
-
sendSseMessage
public void sendSseMessage(String query, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter) Description copied from interface:SpringAIService
发送Sse消息- Specified by:
sendSseMessage
in interfaceSpringAIService
emitter
- SseEmitter
-
executeFulltextSearch
private void executeFulltextSearch(String query, String kbUid, List<String> searchContentList, List<FaqProtobuf> faqProtobufList) 执行全文搜索 -
executeVectorSearch
private void executeVectorSearch(String query, String kbUid, List<String> searchContentList, List<FaqProtobuf> faqProtobufList) 执行向量搜索 -
processLlmResponse
private void processLlmResponse(String query, List<String> searchContentList, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter) -
createAndProcessPrompt
private void createAndProcessPrompt(String query, String context, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter) 创建提示词并处理SSE消息的通用方法- Parameters:
query
- 用户查询context
- 上下文信息robot
- 机器人配置messageProtobufQuery
- 查询消息messageProtobufReply
- 回复消息emitter
- SSE发射器
-
processSearchResponse
private void processSearchResponse(String query, List<FaqProtobuf> searchContentList, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter) -
processAnswerMessage
private void processAnswerMessage(String answer, MessageTypeEnum type, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter) -
generateFaqPairsAsync
Description copied from interface:SpringAIService
异步生成FAQ对- Specified by:
generateFaqPairsAsync
in interfaceSpringAIService
- Parameters:
chunk
- 文本块- Returns:
- 生成的FAQ对
-
generateFaqPairsSync
Description copied from interface:SpringAIService
同步生成FAQ对- Specified by:
generateFaqPairsSync
in interfaceSpringAIService
- Parameters:
chunk
- 文本块
-
persistMessage
public void persistMessage(MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply) - Specified by:
persistMessage
in interfaceSpringAIService
-
buildKbPrompt
-
processPrompt
protected abstract void processPrompt(org.springframework.ai.chat.prompt.Prompt prompt, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply) -
processPromptSync
-
processPromptSSE
protected abstract void processPromptSSE(org.springframework.ai.chat.prompt.Prompt prompt, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter) -
generateFaqPairs
-
sendMessage
protected void sendMessage(MessageTypeEnum type, String content, MessageProtobuf messageProtobufReply) 发送消息的通用方法- Parameters:
type
- 消息类型content
- 消息内容messageProtobufReply
- 回复消息对象
-
isEmitterCompleted
protected boolean isEmitterCompleted(org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter) 检查SseEmitter是否已完成- Parameters:
emitter
- SSE发射器- Returns:
- 如果emitter已完成或关闭返回true,否则返回false
-
handleSseError
protected void handleSseError(Throwable error, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter) 处理SSE错误的通用方法- Parameters:
error
- 发生的错误messageProtobufQuery
- 查询消息messageProtobufReply
- 回复消息emitter
- SSE发射器
-
sendStreamStartMessage
protected void sendStreamStartMessage(MessageProtobuf messageProtobufReply, org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter, String initialContent) 发送流式开始消息- Parameters:
messageProtobufReply
- 回复消息对象emitter
- SSE发射器initialContent
- 初始内容,通常为"正在思考中..."
-
sendStreamMessage
protected void sendStreamMessage(MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter, String content) 发送流式内容消息- Parameters:
messageProtobufQuery
- 查询消息对象messageProtobufReply
- 回复消息对象emitter
- SSE发射器content
- 消息内容
-
sendStreamEndMessage
protected void sendStreamEndMessage(MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter) 发送流式结束消息- Parameters:
messageProtobufQuery
- 查询消息对象messageProtobufReply
- 回复消息对象emitter
- SSE发射器
-
createDynamicOptions
创建动态的聊天选项(通用方法,使用泛型)- Type Parameters:
T
- 选项类型参数- Parameters:
llm
- 机器人LLM配置optionBuilder
- 选项构建器函数接口- Returns:
- 根据机器人配置创建的选项
-