类 AbstractBaseFormatter<TReq,TResp,TParams>
java.lang.Object
io.agentscope.core.formatter.AbstractBaseFormatter<TReq,TResp,TParams>
- 类型参数:
TReq- Provider-specific request message typeTResp- Provider-specific response typeTParams- Provider-specific request parameters builder type
- 所有已实现的接口:
Formatter<TReq,TResp, TParams>
- 直接已知子类:
AnthropicBaseFormatter,DashScopeChatFormatter,DashScopeMultiAgentFormatter,GeminiChatFormatter,GeminiMultiAgentFormatter,OllamaChatFormatter,OllamaMultiAgentFormatter,OpenAIBaseFormatter
public abstract class AbstractBaseFormatter<TReq,TResp,TParams>
extends Object
implements Formatter<TReq,TResp,TParams>
Abstract base formatter providing common functionality for all formatter implementations.
This class contains shared logic across all formatters including:
- Text content extraction (with ThinkingBlock filtering)
- Media content detection
- Role label formatting
- Shared ObjectMapper instance
-
构造器概要
构造器 -
方法概要
修饰符和类型方法说明protected StringconvertToolResultToString(List<ContentBlock> output) Convert tool result output to string representation.protected StringextractTextContent(ContentBlock block) Extract text content from a single ContentBlock.protected StringextractTextContent(Msg msg) Extract text content from a message, filtering out ThinkingBlock.Format AgentScope messages to provider-specific request format.protected StringformatRoleLabel(MsgRole role) Format role label for conversation history.protected <T> TgetOptionOrDefault(GenerateOptions options, GenerateOptions defaultOptions, Function<GenerateOptions, T> getter) Get an option value from options or fall back to defaultOptions.protected booleanhasMediaContent(Msg msg) Check if a message contains multimodal content (images, audio, video).protected StringsaveBase64DataToTempFile(String mediaType, String base64Data) Save base64 data to a temporary file.protected booleanshouldBypassHistory(Msg msg) Check if a message should bypass history merging in multiagent formatters.从类继承的方法 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait从接口继承的方法 io.agentscope.core.formatter.Formatter
applyOptions, applyToolChoice, applyToolChoice, applyTools, applyTools, parseResponse
-
构造器详细资料
-
AbstractBaseFormatter
public AbstractBaseFormatter()
-
-
方法详细资料
-
format
Format AgentScope messages to provider-specific request format. -
doFormat
-
extractTextContent
Extract text content from a message, filtering out ThinkingBlock.- 参数:
msg- The message to extract text from- 返回:
- Concatenated text content (newline-separated)
-
extractTextContent
Extract text content from a single ContentBlock.- 参数:
block- The content block- 返回:
- Text content or empty string
-
hasMediaContent
Check if a message contains multimodal content (images, audio, video).- 参数:
msg- The message to check- 返回:
- true if message contains media content
-
formatRoleLabel
Format role label for conversation history.- 参数:
role- The message role- 返回:
- Formatted role label (e.g., "User", "Assistant")
-
shouldBypassHistory
Check if a message should bypass history merging in multiagent formatters. Messages with theMessageMetadataKeys.BYPASS_MULTIAGENT_HISTORY_MERGEflag set totrueshould be kept as separate messages rather than merged into the conversation history.- 参数:
msg- The message to check- 返回:
- true if message should bypass history merging
-
getOptionOrDefault
protected <T> T getOptionOrDefault(GenerateOptions options, GenerateOptions defaultOptions, Function<GenerateOptions, T> getter) Get an option value from options or fall back to defaultOptions.- 类型参数:
T- The type of the option value- 参数:
options- The primary options objectdefaultOptions- The fallback options objectgetter- Function to extract the value from GenerateOptions- 返回:
- The option value or null if not found in either options object
-
convertToolResultToString
Convert tool result output to string representation. Handles multimodal content by converting images/audio/video to textual references.- 参数:
output- The tool result output blocks (can be null or empty)- 返回:
- String representation (single items directly, multiple with "- " prefix)
-
saveBase64DataToTempFile
Save base64 data to a temporary file.- 参数:
mediaType- The MIME type (e.g., "image/png", "audio/wav")base64Data- The base64-encoded data (without prefix)- 返回:
- Absolute path to the temporary file
- 抛出:
IOException- If file creation or writing fails
-