Class SpringAIRagflowService
java.lang.Object
com.bytedesk.ai.service.BaseSpringAIService
com.bytedesk.ai.springai.providers.ragflow.SpringAIRagflowService
- All Implemented Interfaces:
SpringAIService
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 Summary
FieldsModifier and TypeFieldDescriptionprivate LlmProviderRestServiceprivate com.fasterxml.jackson.databind.ObjectMapperprivate RagflowChatServiceprivate 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 StringbuildRagflowApiUrl(String baseUrl, String ragflowId, String ragflowType) 根据ragflowType构建对应的API URLprivate StringextractMessageContent(String response) 从响应中提取消息内容extractUsage(String response) 解析响应中的使用情况private LlmProviderEntitygetLlmProvider(RobotLlm llm) 获取LLM Provider实体private String[]getRagflowConfigAndId(RobotProtobuf robot, MessageProtobuf messageProtobuf) 综合获取RAGFlow配置信息和聊天IDparseStreamResponse(String streamResponse) 解析RAGFlow流式响应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 voidrecordTokenUsage(RobotProtobuf robot, String response, int inputLength, int outputLength) 记录token使用情况private StringsendMessageDirect(String baseUrl, String apiKey, String ragflowId, String ragflowType, String content, Boolean stream) 直接调用RAGFlow OpenAI兼容API发送聊天消息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客户端Methods inherited from class com.bytedesk.ai.service.BaseSpringAIService
processSyncRequest, sendSseMessage, sendSyncMessage, sendWebsocketMessage
-
Field Details
-
llmProviderRestService
-
ragflowChatService
-
restTemplate
@Autowired private org.springframework.web.client.RestTemplate restTemplate -
objectMapper
@Autowired private com.fasterxml.jackson.databind.ObjectMapper objectMapper -
tokenUsageHelper
-
-
Constructor Details
-
SpringAIRagflowService
public SpringAIRagflowService()
-
-
Method Details
-
getLlmProvider
获取LLM Provider实体- Parameters:
llm- 机器人LLM配置- Returns:
- LlmProviderEntity,如果获取失败返回null
-
getRagflowConfigAndId
综合获取RAGFlow配置信息和聊天ID- Parameters:
robot- 机器人信息messageProtobuf- 消息信息- Returns:
- 包含配置信息的数组 [baseUrl, apiKey, ragflowId, ragflowType],如果获取失败返回null
-
buildRagflowApiUrl
根据ragflowType构建对应的API URL- Parameters:
baseUrl- 基础URLragflowId- chat ID 或 agent IDragflowType- "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基础URLapiKey- API密钥ragflowId- chat ID 或 agent IDragflowType- "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
从响应中提取消息内容 -
parseStreamResponse
解析RAGFlow流式响应- Parameters:
streamResponse- 流式响应字符串- Returns:
- 包含内容和使用情况的Map,key为"content"和"usage"
-
extractUsage
解析响应中的使用情况 -
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
-
recordTokenUsage
private void recordTokenUsage(RobotProtobuf robot, String response, int inputLength, int outputLength) 记录token使用情况- Parameters:
robot- 机器人信息response- API响应inputLength- 输入长度outputLength- 输出长度
-