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 classPrompt处理结果类 -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected org.springframework.context.ApplicationEventPublisherprotected ChunkElasticServiceprotected ChunkVectorServiceprotected FaqElasticServiceprotected FaqVectorServiceprotected MessagePersistCacheprotected MessageRestServiceprotected IMessageSendServiceprotected RobotMessageCacheprotected RobotRestServiceprotected TextElasticServiceprotected TextVectorServiceprotected ThreadRestServiceprotected UidUtils -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedprotectedBaseSpringAIService(IMessageSendService messageSendService) -
Method Summary
Modifier and TypeMethodDescriptionprivate voidcreateAndProcessPrompt(String query, String context, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter) private BaseSpringAIService.PromptResultcreateAndProcessPromptSyncWithPrompt(String query, String context, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply) protected <T> TcreateDynamicOptions(RobotLlm llm, Function<RobotLlm, T> optionBuilder) private voidexecuteFulltextSearch(String query, String kbUid, List<FaqProtobuf> searchResultList) private voidexecuteVectorSearch(String query, String kbUid, List<FaqProtobuf> searchResultList) protected StringextractFullPromptContent(List<org.springframework.ai.chat.messages.Message> messages) 提取完整的prompt内容protected StringextractTextFromResponse(Object response) protected ChatTokenUsageextractTokenUsage(Object response) Extract token usage from ChatResponse metadataprivate longextractTokenUsageFromResponse(org.springframework.ai.chat.model.ChatResponse chatResponse) Extract token usage from response object itself (for cases where metadata doesn't contain token info)protected BigDecimalgetTokenUnitPrice(String aiProvider, String aiModelType) Get token unit price based on AI provider and model typeprotected voidhandleSseError(Throwable error, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter) protected booleanisEmitterCompleted(org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter) voidpersistMessage(MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, Boolean isUnanswered) 消息持久化 用于保存用户查询和机器人回复的消息记录voidpersistMessage(MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, Boolean isUnanswered, long promptTokens, long completionTokens, long totalTokens) 持久化消息,包含token使用情况voidpersistMessage(MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, Boolean isUnanswered, long promptTokens, long completionTokens, long totalTokens, String prompt) 持久化消息,包含token使用情况和prompt内容voidpersistMessage(MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, Boolean isUnanswered, long promptTokens, long completionTokens, long totalTokens, String prompt, String aiProvider, String aiModel) 持久化消息,包含token使用情况、prompt内容和AI模型信息private voidprocessAnswerMessage(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 voidprocessDirectResponse(String query, List<FaqProtobuf> searchContentList, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter) private voidprocessLlmResponse(String query, List<FaqProtobuf> searchResultList, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter) protected voidprocessLlmResponseWebsocket(String query, List<FaqProtobuf> searchResultList, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply) protected abstract voidprocessPromptSse(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 StringprocessPromptSync(String message, RobotProtobuf robot, String fullPromptContent) protected abstract voidprocessPromptWebsocket(org.springframework.ai.chat.prompt.Prompt prompt, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, String fullPromptContent) Token usage data classvoidpublishAiTokenUsageEvent(String orgUid, String aiProvider, String aiModelType, long promptTokens, long completionTokens, boolean success, long responseTime, BigDecimal tokenUnitPrice) 发布AI Token使用事件protected voidrecordAiTokenUsage(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 voidsendMessageWebsocket(MessageTypeEnum type, String content, MessageProtobuf messageProtobufReply) voidsendSseMessage(String query, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter) 发送Sse消息protected voidsendStreamEndMessage(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 voidsendStreamMessage(MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter, String content) protected voidsendStreamStartMessage(MessageProtobuf messageProtobufReply, org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter, String initialContent) sendSyncMessage(String query, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply) 发送同步消息并返回回复内容voidsendWebsocketMessage(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:
sendWebsocketMessagein 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:
sendSseMessagein interfaceSpringAIServiceemitter- SseEmitter
-
sendSyncMessage
public String sendSyncMessage(String query, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply) Description copied from interface:SpringAIService发送同步消息并返回回复内容- Specified by:
sendSyncMessagein interfaceSpringAIService- Parameters:
query- 用户查询robot- 机器人实体messageProtobufQuery- 查询消息messageProtobufReply- 回复消息- Returns:
- 回复内容
-
persistMessage
public void persistMessage(MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, Boolean isUnanswered) Description copied from interface:SpringAIService消息持久化 用于保存用户查询和机器人回复的消息记录- Specified by:
persistMessagein 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:
processDirectLlmRequestin 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)
-