Class SpringAIRagflowService

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

@Service public class SpringAIRagflowService extends BaseSpringAIService
SpringAI RAGFlow Service - 基于 RAGFlow API 的SpringAI服务实现 继承BaseSpringAIService,提供统一的AI服务接口 支持根据ragflowType自动选择调用chat API或agent API https://ragflow.io/docs/dev/http_api_reference 支持的API类型: - Chat API: /api/v1/chats_openai/{chat_id}/chat/completions (ragflowType = "chat") - Agent API: /api/v1/agents_openai/{agent_id}/chat/completions (ragflowType = "agent")
  • Field Details

    • llmProviderRestService

      @Autowired private LlmProviderRestService llmProviderRestService
    • ragflowChatService

      @Autowired(required=false) private RagflowChatService ragflowChatService
    • 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

    • SpringAIRagflowService

      public SpringAIRagflowService()
  • Method Details

    • getLlmProvider

      private LlmProviderEntity getLlmProvider(RobotLlm llm)
      获取LLM Provider实体
      Parameters:
      llm - 机器人LLM配置
      Returns:
      LlmProviderEntity,如果获取失败返回null
    • getRagflowConfigAndId

      private String[] getRagflowConfigAndId(RobotProtobuf robot, MessageProtobuf messageProtobuf)
      综合获取RAGFlow配置信息和聊天ID
      Parameters:
      robot - 机器人信息
      messageProtobuf - 消息信息
      Returns:
      包含配置信息的数组 [baseUrl, apiKey, ragflowId, ragflowType],如果获取失败返回null
    • buildRagflowApiUrl

      private String buildRagflowApiUrl(String baseUrl, String ragflowId, String ragflowType)
      根据ragflowType构建对应的API URL
      Parameters:
      baseUrl - 基础URL
      ragflowId - chat ID 或 agent ID
      ragflowType - "chat" 或 "agent"
      Returns:
      完整的API URL
    • sendMessageDirectStream

      private Map<String,Object> sendMessageDirectStream(String baseUrl, String apiKey, String modelType, String ragflowId, String ragflowType, String content, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter)
      处理RAGFlow流式响应并实时发送给SSE客户端
      Parameters:
      baseUrl - RAGFlow API基础URL
      apiKey - API密钥
      ragflowId - chat ID 或 agent ID
      ragflowType - "chat" 或 "agent"
      content - 消息内容
      messageProtobufQuery - 查询消息
      messageProtobufReply - 回复消息
      emitter - SSE发射器
      Returns:
      包含完整内容和使用情况的Map
    • sendMessageDirect

      private String sendMessageDirect(String baseUrl, String apiKey, String ragflowId, String ragflowType, String content, Boolean stream)
      直接调用RAGFlow OpenAI兼容API发送聊天消息
    • extractMessageContent

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

      private Map<String,Object> parseStreamResponse(String streamResponse)
      解析RAGFlow流式响应
      Parameters:
      streamResponse - 流式响应字符串
      Returns:
      包含内容和使用情况的Map,key为"content"和"usage"
    • 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
    • recordTokenUsage

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