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

public abstract class BaseSpringAIService extends Object implements SpringAIService
  • Field Details

    • faqElasticService

      @Autowired protected FaqElasticService faqElasticService
    • faqVectorService

      @Autowired(required=false) protected FaqVectorService faqVectorService
    • textElasticService

      @Autowired protected TextElasticService textElasticService
    • textVectorService

      @Autowired(required=false) protected TextVectorService textVectorService
    • chunkElasticService

      @Autowired protected ChunkElasticService chunkElasticService
    • chunkVectorService

      @Autowired(required=false) protected ChunkVectorService chunkVectorService
    • messageSendService

      @Autowired protected IMessageSendService messageSendService
    • uidUtils

      @Autowired protected UidUtils uidUtils
    • robotRestService

      @Autowired protected RobotRestService robotRestService
    • threadRestService

      @Autowired protected ThreadRestService threadRestService
    • messagePersistCache

      @Autowired protected MessagePersistCache messagePersistCache
    • robotMessageCache

      @Autowired protected RobotMessageCache robotMessageCache
    • messageRestService

      @Autowired protected MessageRestService messageRestService
    • applicationEventPublisher

      @Autowired protected org.springframework.context.ApplicationEventPublisher applicationEventPublisher
  • Constructor Details

    • BaseSpringAIService

      protected BaseSpringAIService()
    • BaseSpringAIService

      protected BaseSpringAIService(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 interface SpringAIService
      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 interface SpringAIService
      emitter - SseEmitter
    • sendSyncMessage

      public String sendSyncMessage(String query, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply)
      Description copied from interface: SpringAIService
      发送同步消息并返回回复内容
      Specified by:
      sendSyncMessage in interface SpringAIService
      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 interface SpringAIService
    • 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

      public String processDirectLlmRequest(String query, RobotProtobuf robot)
      Description copied from interface: SpringAIService
      直接处理LLM请求并同步返回结果 主要用于RobotAgentService中的processLlmRequest方法
      Specified by:
      processDirectLlmRequest in interface SpringAIService
      Parameters:
      query - 用户查询
      robot - 机器人配置
      Returns:
      大模型生成的回复内容
    • searchKnowledgeBase

      protected List<FaqProtobuf> searchKnowledgeBase(String query, RobotProtobuf robot)
    • executeFulltextSearch

      private void executeFulltextSearch(String query, String kbUid, List<FaqProtobuf> searchResultList)
    • executeVectorSearch

      private void executeVectorSearch(String query, String kbUid, List<FaqProtobuf> searchResultList)
    • 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

      protected String extractTextFromResponse(Object response)
    • isEmitterCompleted

      protected boolean isEmitterCompleted(org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter)
    • createDynamicOptions

      protected <T> T createDynamicOptions(RobotLlm llm, Function<RobotLlm,T> optionBuilder)
    • 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 configuration
      aiProvider - AI provider name
      aiModelType - AI model type
      promptTokens - Number of prompt tokens consumed
      completionTokens - Number of completion tokens consumed
      success - Whether the request was successful
      responseTime - Response time in milliseconds
    • getTokenUnitPrice

      protected BigDecimal getTokenUnitPrice(String aiProvider, String aiModelType)
      Get token unit price based on AI provider and model type
      Parameters:
      aiProvider - AI provider
      aiModelType - AI model type
      Returns:
      Token unit price in USD
    • extractTokenUsage

      protected ChatTokenUsage extractTokenUsage(Object response)
      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 - 组织UID
      aiProvider - 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)