Class SpringAIDifyService
java.lang.Object
com.bytedesk.ai.service.BaseSpringAIService
com.bytedesk.ai.springai.providers.dify.SpringAIDifyService
- All Implemented Interfaces:
SpringAIService
SpringAI Dify Service - 基于 Dify API 的SpringAI服务实现
继承BaseSpringAIService,提供统一的AI服务接口
接口文档地址:https://docs.dify.ai/api-reference/chat/send-chat-message
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate DifyChatServiceprivate LlmProviderRestServiceprivate org.springframework.web.client.RestTemplateprivate TokenUsageHelperFields inherited from class com.bytedesk.ai.service.BaseSpringAIService
applicationEventPublisher, chunkElasticService, chunkVectorService, faqElasticService, faqVectorService, knowledgeBaseSearchHelper, messagePersistCache, messagePersistenceHelper, messageRestService, messageSendService, promptHelper, robotMessageCache, robotRestService, sseMessageHelper, textElasticService, textVectorService, threadRestService, uidUtils, webpageElasticService, webpageVectorService -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate StringextractMessageContent(String response) 从响应中提取消息内容private StringgetConversationId(RobotProtobuf robot, MessageProtobuf messageProtobuf) 获取或生成会话IDprivate String[]根据机器人配置获取动态的Dify配置信息protected voidprocessPromptSse(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) protected StringprocessPromptSync(String message, RobotProtobuf robot) protected voidprocessPromptWebsocket(org.springframework.ai.chat.prompt.Prompt prompt, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply) private voidprocessStreamingResponseImproved(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格式的流式响应 - 改进版本private voidrecordTokenUsage(RobotProtobuf robot, int inputLength, int outputLength) 记录token使用情况private StringsendChatMessageDirect(String baseUrl, String apiKey, String query, String conversationId, String userId, Map<String, Object> inputs, String responseMode) 直接调用Dify API发送聊天消息private voidsendChatMessageStreamingImproved(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的流式响应 - 改进版本private StringsendChatMessageWithDynamicConfig(String query, String conversationId, String userId, Map<String, Object> inputs, String responseMode, RobotLlm llm) 使用动态配置发送聊天消息Methods inherited from class com.bytedesk.ai.service.BaseSpringAIService
processSyncRequest, sendSseMessage, sendSyncMessage, sendWebsocketMessage
-
Field Details
-
tokenUsageHelper
-
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
根据机器人配置获取动态的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- 对话IDuserId- 用户标识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
从响应中提取消息内容 -
processPromptWebsocket
protected void processPromptWebsocket(org.springframework.ai.chat.prompt.Prompt prompt, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply) - Specified by:
processPromptWebsocketin classBaseSpringAIService
-
processPromptSync
- Specified by:
processPromptSyncin classBaseSpringAIService
-
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:
processPromptSsein classBaseSpringAIService
-
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
获取或生成会话ID- Parameters:
robot- 机器人信息messageProtobuf- 消息信息- Returns:
- 会话ID
-
recordTokenUsage
记录token使用情况- Parameters:
robot- 机器人信息inputLength- 输入长度outputLength- 输出长度
-