Package com.bytedesk.ai.springai.service
Class BaseSpringAIService
java.lang.Object
com.bytedesk.ai.springai.service.BaseSpringAIService
- All Implemented Interfaces:
SpringAIService
- Direct Known Subclasses:
SpringAIBaiduChatService
,SpringAIBaiduService
,SpringAICozeService
,SpringAICustomChatService
,SpringAICustomService
,SpringAIDashscopeChatService
,SpringAIDashscopeService
,SpringAIDeepseekChatService
,SpringAIDeepseekService
,SpringAIDifyService
,SpringAIGiteeChatService
,SpringAIGiteeService
,SpringAIMaxkbService
,SpringAIMinimaxChatService
,SpringAIMinimaxService
,SpringAIN8nService
,SpringAIOllamaChatService
,SpringAIOllamaService
,SpringAIOpenaiChatService
,SpringAIOpenaiService
,SpringAIOpenrouterChatService
,SpringAIOpenrouterService
,SpringAIRagflowService
,SpringAISiliconFlowChatService
,SpringAISiliconFlowService
,SpringAITencentChatService
,SpringAITencentService
,SpringAIVolcengineChatService
,SpringAIVolcengineService
,SpringAIZhipuaiChatService
,SpringAIZhipuaiService
,ZhipuaiService
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static class
Prompt处理结果类 -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected org.springframework.context.ApplicationEventPublisher
protected ChunkElasticService
protected ChunkVectorService
protected FaqElasticService
protected FaqVectorService
protected MessagePersistCache
protected MessageRestService
protected IMessageSendService
protected RobotMessageCache
protected RobotRestService
protected TextElasticService
protected TextVectorService
protected ThreadRestService
protected UidUtils
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
protected
BaseSpringAIService
(IMessageSendService messageSendService) -
Method Summary
Modifier and TypeMethodDescriptionprivate void
createAndProcessPrompt
(String query, String context, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter) private BaseSpringAIService.PromptResult
createAndProcessPromptSyncWithPrompt
(String query, String context, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply) protected <T> T
createDynamicOptions
(RobotLlm llm, Function<RobotLlm, T> optionBuilder) private void
executeFulltextSearch
(String query, String kbUid, List<FaqProtobuf> searchResultList) private void
executeVectorSearch
(String query, String kbUid, List<FaqProtobuf> searchResultList) protected String
extractFullPromptContent
(List<org.springframework.ai.chat.messages.Message> messages) 提取完整的prompt内容protected String
extractTextFromResponse
(Object response) protected ChatTokenUsage
extractTokenUsage
(Object response) Extract token usage from ChatResponse metadataprivate long
extractTokenUsageFromResponse
(org.springframework.ai.chat.model.ChatResponse chatResponse) Extract token usage from response object itself (for cases where metadata doesn't contain token info)protected BigDecimal
getTokenUnitPrice
(String aiProvider, String aiModelType) Get token unit price based on AI provider and model typeprotected void
handleSseError
(Throwable error, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter) protected boolean
isEmitterCompleted
(org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter) void
persistMessage
(MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, Boolean isUnanswered) 消息持久化 用于保存用户查询和机器人回复的消息记录void
persistMessage
(MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, Boolean isUnanswered, long promptTokens, long completionTokens, long totalTokens) 持久化消息,包含token使用情况void
persistMessage
(MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, Boolean isUnanswered, long promptTokens, long completionTokens, long totalTokens, String prompt) 持久化消息,包含token使用情况和prompt内容void
persistMessage
(MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, Boolean isUnanswered, long promptTokens, long completionTokens, long totalTokens, String prompt, String aiProvider, String aiModel) 持久化消息,包含token使用情况、prompt内容和AI模型信息private void
processAnswerMessage
(String answer, MessageTypeEnum type, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, Boolean isUnanswered, org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter) processDirectLlmRequest
(String query, RobotProtobuf robot) 直接处理LLM请求并同步返回结果 主要用于RobotAgentService中的processLlmRequest方法private void
processDirectResponse
(String query, List<FaqProtobuf> searchContentList, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter) private void
processLlmResponse
(String query, List<FaqProtobuf> searchResultList, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter) protected void
processLlmResponseWebsocket
(String query, List<FaqProtobuf> searchResultList, 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, String fullPromptContent) protected abstract String
processPromptSync
(String message, RobotProtobuf robot, String fullPromptContent) protected abstract void
processPromptWebsocket
(org.springframework.ai.chat.prompt.Prompt prompt, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, String fullPromptContent) Token usage data classvoid
publishAiTokenUsageEvent
(String orgUid, String aiProvider, String aiModelType, long promptTokens, long completionTokens, boolean success, long responseTime, BigDecimal tokenUnitPrice) 发布AI Token使用事件protected void
recordAiTokenUsage
(RobotProtobuf robot, String aiProvider, String aiModelType, long promptTokens, long completionTokens, boolean success, long responseTime) Record AI token usage statistics by publishing an eventprotected List<FaqProtobuf>
searchKnowledgeBase
(String query, RobotProtobuf robot) protected void
sendMessageWebsocket
(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, long promptTokens, long completionTokens, long totalTokens, String prompt, String aiProvider, String aiModel) 发送流结束消息,包含token使用情况、prompt内容和AI模型信息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) sendSyncMessage
(String query, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply) 发送同步消息并返回回复内容void
sendWebsocketMessage
(String query, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply) 发送websocket消息
-
Field Details
-
faqElasticService
-
faqVectorService
-
textElasticService
-
textVectorService
-
chunkElasticService
-
chunkVectorService
-
messageSendService
-
uidUtils
-
robotRestService
-
threadRestService
-
messagePersistCache
-
robotMessageCache
-
messageRestService
-
applicationEventPublisher
@Autowired protected org.springframework.context.ApplicationEventPublisher applicationEventPublisher
-
-
Constructor Details
-
BaseSpringAIService
protected BaseSpringAIService() -
BaseSpringAIService
-
-
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
-
sendSyncMessage
public String sendSyncMessage(String query, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply) Description copied from interface:SpringAIService
发送同步消息并返回回复内容- Specified by:
sendSyncMessage
in interfaceSpringAIService
- Parameters:
query
- 用户查询robot
- 机器人实体messageProtobufQuery
- 查询消息messageProtobufReply
- 回复消息- Returns:
- 回复内容
-
persistMessage
public void persistMessage(MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, Boolean isUnanswered) Description copied from interface:SpringAIService
消息持久化 用于保存用户查询和机器人回复的消息记录- Specified by:
persistMessage
in interfaceSpringAIService
-
persistMessage
public void persistMessage(MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, Boolean isUnanswered, long promptTokens, long completionTokens, long totalTokens) 持久化消息,包含token使用情况- Parameters:
messageProtobufQuery
- 查询消息messageProtobufReply
- 回复消息isUnanswered
- 是否未回答promptTokens
- prompt token数量completionTokens
- completion token数量totalTokens
- 总token数量
-
persistMessage
public void persistMessage(MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, Boolean isUnanswered, long promptTokens, long completionTokens, long totalTokens, String prompt) 持久化消息,包含token使用情况和prompt内容- Parameters:
messageProtobufQuery
- 查询消息messageProtobufReply
- 回复消息isUnanswered
- 是否未回答promptTokens
- prompt token数量completionTokens
- completion token数量totalTokens
- 总token数量prompt
- 传入到大模型的完整prompt内容
-
persistMessage
public void persistMessage(MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, Boolean isUnanswered, long promptTokens, long completionTokens, long totalTokens, String prompt, String aiProvider, String aiModel) 持久化消息,包含token使用情况、prompt内容和AI模型信息- Parameters:
messageProtobufQuery
- 查询消息messageProtobufReply
- 回复消息isUnanswered
- 是否未回答promptTokens
- prompt token数量completionTokens
- completion token数量totalTokens
- 总token数量prompt
- 传入到大模型的完整prompt内容aiProvider
- 大模型提供商aiModel
- 大模型名称
-
processDirectLlmRequest
Description copied from interface:SpringAIService
直接处理LLM请求并同步返回结果 主要用于RobotAgentService中的processLlmRequest方法- Specified by:
processDirectLlmRequest
in interfaceSpringAIService
- Parameters:
query
- 用户查询robot
- 机器人配置- Returns:
- 大模型生成的回复内容
-
searchKnowledgeBase
-
executeFulltextSearch
-
executeVectorSearch
-
processLlmResponse
private void processLlmResponse(String query, List<FaqProtobuf> searchResultList, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter) -
processLlmResponseWebsocket
protected void processLlmResponseWebsocket(String query, List<FaqProtobuf> searchResultList, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply) -
createAndProcessPrompt
private void createAndProcessPrompt(String query, String context, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter) -
createAndProcessPromptSyncWithPrompt
private BaseSpringAIService.PromptResult createAndProcessPromptSyncWithPrompt(String query, String context, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply) -
processDirectResponse
private void processDirectResponse(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, Boolean isUnanswered, org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter) -
sendMessageWebsocket
protected void sendMessageWebsocket(MessageTypeEnum type, String content, MessageProtobuf messageProtobufReply) -
handleSseError
protected void handleSseError(Throwable error, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter) -
sendStreamStartMessage
protected void sendStreamStartMessage(MessageProtobuf messageProtobufReply, org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter, String initialContent) -
sendStreamMessage
protected void sendStreamMessage(MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter, String content) -
sendStreamEndMessage
protected void sendStreamEndMessage(MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter, long promptTokens, long completionTokens, long totalTokens, String prompt, String aiProvider, String aiModel) 发送流结束消息,包含token使用情况、prompt内容和AI模型信息- Parameters:
messageProtobufQuery
- 查询消息messageProtobufReply
- 回复消息emitter
- SSE发射器promptTokens
- prompt token数量completionTokens
- completion token数量totalTokens
- 总token数量prompt
- 传入到大模型的完整prompt内容aiProvider
- 大模型提供商aiModel
- 大模型名称
-
extractTextFromResponse
-
isEmitterCompleted
protected boolean isEmitterCompleted(org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter) -
createDynamicOptions
-
recordAiTokenUsage
protected void recordAiTokenUsage(RobotProtobuf robot, String aiProvider, String aiModelType, long promptTokens, long completionTokens, boolean success, long responseTime) Record AI token usage statistics by publishing an event- Parameters:
robot
- Robot configurationaiProvider
- AI provider nameaiModelType
- AI model typepromptTokens
- Number of prompt tokens consumedcompletionTokens
- Number of completion tokens consumedsuccess
- Whether the request was successfulresponseTime
- Response time in milliseconds
-
getTokenUnitPrice
Get token unit price based on AI provider and model type- Parameters:
aiProvider
- AI provideraiModelType
- AI model type- Returns:
- Token unit price in USD
-
extractTokenUsage
Extract token usage from ChatResponse metadata- Parameters:
response
- ChatResponse from AI service- Returns:
- TokenUsage object containing prompt and completion tokens
-
extractTokenUsageFromResponse
private long extractTokenUsageFromResponse(org.springframework.ai.chat.model.ChatResponse chatResponse) Extract token usage from response object itself (for cases where metadata doesn't contain token info)- Parameters:
chatResponse
- ChatResponse object- Returns:
- total token count if found, 0 otherwise
-
extractFullPromptContent
protected String extractFullPromptContent(List<org.springframework.ai.chat.messages.Message> messages) 提取完整的prompt内容- Parameters:
messages
- 消息列表- Returns:
- 格式化的完整prompt内容
-
publishAiTokenUsageEvent
public void publishAiTokenUsageEvent(String orgUid, String aiProvider, String aiModelType, long promptTokens, long completionTokens, boolean success, long responseTime, BigDecimal tokenUnitPrice) 发布AI Token使用事件- Parameters:
orgUid
- 组织UIDaiProvider
- AI提供商aiModelType
- AI模型类型promptTokens
- Prompt token数量completionTokens
- Completion token数量success
- 请求是否成功responseTime
- 响应时间(毫秒)tokenUnitPrice
- Token单价
-
processPromptWebsocket
protected abstract void processPromptWebsocket(org.springframework.ai.chat.prompt.Prompt prompt, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, String fullPromptContent) Token usage data class -
processPromptSync
protected abstract String processPromptSync(String message, RobotProtobuf robot, String fullPromptContent) -
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, String fullPromptContent)
-