Class SpringAIBaiduChatService
java.lang.Object
com.bytedesk.ai.springai.service.BaseSpringAIService
com.bytedesk.ai.springai.providers.baidu.SpringAIBaiduChatService
- All Implemented Interfaces:
SpringAIService
@Service
@ConditionalOnProperty(prefix="spring.ai.baidu.chat",
name="enabled",
havingValue="true",
matchIfMissing=false)
public class SpringAIBaiduChatService
extends BaseSpringAIService
-
Nested Class Summary
Nested classes/interfaces inherited from class com.bytedesk.ai.springai.service.BaseSpringAIService
BaseSpringAIService.PromptResult
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate org.springframework.ai.openai.OpenAiChatModel
Fields inherited from class com.bytedesk.ai.springai.service.BaseSpringAIService
applicationEventPublisher, chunkElasticService, chunkVectorService, faqElasticService, faqVectorService, messagePersistCache, messageRestService, messageSendService, robotMessageCache, robotRestService, textElasticService, textVectorService, threadRestService, uidUtils
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate org.springframework.ai.openai.OpenAiChatOptions
根据机器人配置创建动态的OpenAiChatOptionsprivate ChatTokenUsage
estimateBaiduTokenUsageFromResponse
(org.springframework.ai.chat.model.ChatResponse response) 估算百度API的token使用量(当无法从API获取实际token信息时使用)private ChatTokenUsage
estimateBaiduTokenUsageFromText
(String outputText) 从完整响应文本估算百度API的token使用量private long
estimateTokens
(String text) 估算文本的token数量private ChatTokenUsage
extractBaiduTokenUsage
(org.springframework.ai.chat.model.ChatResponse response) 专门为百度API提取token使用情况 由于百度API返回的usage字段是EmptyUsage对象,需要特殊处理org.springframework.ai.openai.OpenAiChatModel
protected void
processPromptSse
(org.springframework.ai.chat.prompt.Prompt prompt, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter, String fullPromptContent) protected String
processPromptSync
(String message, RobotProtobuf robot, String fullPromptContent) protected void
processPromptWebsocket
(org.springframework.ai.chat.prompt.Prompt prompt, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, String fullPromptContent) Token usage data classMethods inherited from class com.bytedesk.ai.springai.service.BaseSpringAIService
createDynamicOptions, extractFullPromptContent, extractTextFromResponse, extractTokenUsage, getTokenUnitPrice, handleSseError, isEmitterCompleted, persistMessage, persistMessage, persistMessage, persistMessage, processDirectLlmRequest, processLlmResponseWebsocket, publishAiTokenUsageEvent, recordAiTokenUsage, searchKnowledgeBase, sendMessageWebsocket, sendSseMessage, sendStreamEndMessage, sendStreamMessage, sendStreamStartMessage, sendSyncMessage, sendWebsocketMessage
-
Field Details
-
baiduChatModel
@Autowired(required=false) private org.springframework.ai.openai.OpenAiChatModel baiduChatModel
-
-
Constructor Details
-
SpringAIBaiduChatService
public SpringAIBaiduChatService()
-
-
Method Details
-
createDynamicOptions
根据机器人配置创建动态的OpenAiChatOptions- Parameters:
llm
- 机器人LLM配置- Returns:
- 根据机器人配置创建的选项
-
processPromptWebsocket
protected void processPromptWebsocket(org.springframework.ai.chat.prompt.Prompt prompt, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, String fullPromptContent) Description copied from class:BaseSpringAIService
Token usage data class- Specified by:
processPromptWebsocket
in classBaseSpringAIService
-
processPromptSync
- Specified by:
processPromptSync
in classBaseSpringAIService
-
processPromptSse
protected void processPromptSse(org.springframework.ai.chat.prompt.Prompt prompt, RobotProtobuf robot, MessageProtobuf messageProtobufQuery, MessageProtobuf messageProtobufReply, org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter, String fullPromptContent) - Specified by:
processPromptSse
in classBaseSpringAIService
-
extractBaiduTokenUsage
private ChatTokenUsage extractBaiduTokenUsage(org.springframework.ai.chat.model.ChatResponse response) 专门为百度API提取token使用情况 由于百度API返回的usage字段是EmptyUsage对象,需要特殊处理- Parameters:
response
- ChatResponse对象- Returns:
- TokenUsage对象
-
estimateBaiduTokenUsageFromResponse
private ChatTokenUsage estimateBaiduTokenUsageFromResponse(org.springframework.ai.chat.model.ChatResponse response) 估算百度API的token使用量(当无法从API获取实际token信息时使用)- Parameters:
response
- ChatResponse对象- Returns:
- 估算的TokenUsage对象
-
estimateBaiduTokenUsageFromText
从完整响应文本估算百度API的token使用量- Parameters:
outputText
- 完整的输出文本- Returns:
- 估算的TokenUsage对象
-
estimateTokens
估算文本的token数量- Parameters:
text
- 输入文本- Returns:
- 估算的token数量
-
getChatModel
public org.springframework.ai.openai.OpenAiChatModel getChatModel() -
isServiceHealthy
-