Class SpringAIFastGptService

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

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

    • tokenUsageHelper

      @Autowired private TokenUsageHelper tokenUsageHelper
    • llmProviderRestService

      @Autowired private LlmProviderRestService llmProviderRestService
    • restTemplate

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

      @Autowired private com.fasterxml.jackson.databind.ObjectMapper objectMapper
  • Constructor Details

    • SpringAIFastGptService

      public SpringAIFastGptService()
  • Method Details

    • getDynamicFastGptConfig

      private String[] getDynamicFastGptConfig(RobotLlm llm)
      根据机器人配置获取动态的FastGPT配置信息
      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)
      直接调用FastGPT 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 - 输出长度