Class SpringAIN8nService

java.lang.Object
com.bytedesk.ai.service.BaseSpringAIService
com.bytedesk.ai.springai.providers.n8n.SpringAIN8nService
All Implemented Interfaces:
SpringAIService

@Service public class SpringAIN8nService extends BaseSpringAIService
SpringAI N8N Service - 基于 N8N API 的SpringAI服务实现 继承BaseSpringAIService,提供统一的AI服务接口
  • Field Details

    • llmProviderRestService

      @Autowired private LlmProviderRestService llmProviderRestService
    • n8nChatService

      @Autowired(required=false) private N8nChatService n8nChatService
    • restTemplate

      @Autowired private org.springframework.web.client.RestTemplate restTemplate
    • objectMapper

      @Autowired private com.fasterxml.jackson.databind.ObjectMapper objectMapper
    • tokenUsageHelper

      @Autowired private TokenUsageHelper tokenUsageHelper
  • Constructor Details

    • SpringAIN8nService

      public SpringAIN8nService()
  • Method Details

    • getDynamicN8nConfig

      private String[] getDynamicN8nConfig(RobotLlm llm)
      根据机器人配置获取动态的N8N配置信息
      Parameters:
      llm - 机器人LLM配置
      Returns:
      包含API配置的数组 [baseUrl, apiKey],如果获取失败返回null
    • sendMessageWithDynamicConfig

      private String sendMessageWithDynamicConfig(String chatId, String content, Boolean stream, RobotLlm llm)
      使用动态配置发送聊天消息
      Parameters:
      chatId - 聊天ID
      content - 消息内容
      stream - 是否流式响应
      llm - 机器人LLM配置(用于获取动态配置)
      Returns:
      API响应
    • sendMessageDirect

      private String sendMessageDirect(String baseUrl, String apiKey, String chatId, String content, Boolean stream)
      直接调用N8N API发送聊天消息
    • extractMessageContent

      private String extractMessageContent(String response)
      从响应中提取消息内容
    • extractUsage

      private Map<String,Object> extractUsage(String response)
      解析响应中的使用情况
    • processPromptWebsocket

      protected void processPromptWebsocket(org.springframework.ai.chat.prompt.Prompt prompt, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply)
      Specified by:
      processPromptWebsocket in class BaseSpringAIService
    • processPromptSync

      protected String processPromptSync(String message, RobotProtobuf robot)
      Specified by:
      processPromptSync in class BaseSpringAIService
    • processPromptSse

      protected void processPromptSse(org.springframework.ai.chat.prompt.Prompt prompt, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, List<RobotContent.SourceReference> sourceReferences, org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter)
      Specified by:
      processPromptSse in class BaseSpringAIService
    • getChatId

      private String getChatId(RobotProtobuf robot, MessageProtobuf messageProtobuf)
      获取聊天ID 可以从robot配置中获取,或者使用默认值
      Parameters:
      robot - 机器人信息
      messageProtobuf - 消息信息
      Returns:
      聊天ID
    • recordTokenUsage

      private void recordTokenUsage(RobotProtobuf robot, String response, int inputLength, int outputLength)
      记录token使用情况
      Parameters:
      robot - 机器人信息
      response - API响应
      inputLength - 输入长度
      outputLength - 输出长度