Class SpringAIDifyService

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

@Service public class SpringAIDifyService extends BaseSpringAIService
SpringAI Dify Service - 基于 Dify API 的SpringAI服务实现 继承BaseSpringAIService,提供统一的AI服务接口 接口文档地址:https://docs.dify.ai/api-reference/chat/send-chat-message
  • Field Details

    • tokenUsageHelper

      @Autowired private TokenUsageHelper tokenUsageHelper
    • llmProviderRestService

      @Autowired private LlmProviderRestService llmProviderRestService
    • defaultDifyChatService

      @Autowired(required=false) @Qualifier("defaultDifyChatService") private DifyChatService defaultDifyChatService
    • restTemplate

      @Autowired private org.springframework.web.client.RestTemplate restTemplate
  • Constructor Details

    • SpringAIDifyService

      public SpringAIDifyService()
  • Method Details

    • getDynamicDifyConfig

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

      private String sendChatMessageWithDynamicConfig(String query, String conversationId, String userId, Map<String,Object> inputs, String responseMode, RobotLlm llm)
      使用动态配置发送聊天消息
      Parameters:
      query - 用户查询
      conversationId - 对话ID
      userId - 用户标识
      inputs - 输入变量
      responseMode - 响应模式
      llm - 机器人LLM配置(用于获取动态配置)
      Returns:
      API响应
    • sendChatMessageDirect

      private String sendChatMessageDirect(String baseUrl, String apiKey, String query, String conversationId, String userId, Map<String,Object> inputs, String responseMode)
      直接调用Dify API发送聊天消息
    • extractMessageContent

      private String extractMessageContent(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
    • sendChatMessageStreamingImproved

      private void sendChatMessageStreamingImproved(String baseUrl, String apiKey, String query, String conversationId, String userId, org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, boolean[] success, int[] tokenUsage, List<RobotContent.SourceReference> sourceReferences)
      处理Dify的流式响应 - 改进版本
    • processStreamingResponseImproved

      private void processStreamingResponseImproved(String streamingResponse, org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter, RobotProtobuf robot, int inputLength, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, boolean[] success, int[] tokenUsage, List<RobotContent.SourceReference> sourceReferences)
      处理SSE格式的流式响应 - 改进版本
    • getConversationId

      private String getConversationId(RobotProtobuf robot, MessageProtobuf messageProtobuf)
      获取或生成会话ID
      Parameters:
      robot - 机器人信息
      messageProtobuf - 消息信息
      Returns:
      会话ID
    • recordTokenUsage

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