Class OpenAIBaseFormatter
java.lang.Object
io.agentscope.core.formatter.AbstractBaseFormatter<OpenAIMessage,OpenAIResponse,OpenAIRequest>
io.agentscope.extensions.model.openai.formatter.OpenAIBaseFormatter
- All Implemented Interfaces:
io.agentscope.core.formatter.Formatter<OpenAIMessage,OpenAIResponse, OpenAIRequest>
- Direct Known Subclasses:
OpenAIChatFormatter
public abstract class OpenAIBaseFormatter
extends io.agentscope.core.formatter.AbstractBaseFormatter<OpenAIMessage,OpenAIResponse,OpenAIRequest>
Base formatter for OpenAI Chat Completion HTTP API.
Provides common functionality for both single-agent and multi-agent formatters.
Subclasses must implement:
AbstractBaseFormatter.doFormat(List)- Convert messages to OpenAI formatapplyOptions(OpenAIRequest, GenerateOptions, GenerateOptions)- Apply generation optionsapplyTools(OpenAIRequest, List)- Apply tool schemasapplyToolChoice(OpenAIRequest, ToolChoice)- Apply tool choice configuration
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final OpenAIMessageConverterprotected final OpenAIResponseParser -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract voidapplyOptions(OpenAIRequest request, io.agentscope.core.model.GenerateOptions options, io.agentscope.core.model.GenerateOptions defaultOptions) Apply generation options to the request.abstract voidapplyToolChoice(OpenAIRequest request, io.agentscope.core.model.ToolChoice toolChoice) Apply tool choice configuration to the request.voidapplyToolChoice(OpenAIRequest request, io.agentscope.core.model.ToolChoice toolChoice, String baseUrl, String modelName) Apply tool choice with provider context.abstract voidapplyTools(OpenAIRequest request, List<io.agentscope.core.model.ToolSchema> tools) Apply tool schemas to the request.voidapplyTools(OpenAIRequest request, List<io.agentscope.core.model.ToolSchema> tools, String baseUrl, String modelName) Apply tool schemas with provider context.buildRequest(String model, List<OpenAIMessage> messages, boolean stream) Build a basic OpenAIRequest.buildRequest(String model, List<OpenAIMessage> messages, boolean stream, io.agentscope.core.model.GenerateOptions options, io.agentscope.core.model.GenerateOptions defaultOptions, List<io.agentscope.core.model.ToolSchema> tools, io.agentscope.core.model.ToolChoice toolChoice) Build a complete OpenAIRequest with full configuration.io.agentscope.core.model.ChatResponseparseResponse(OpenAIResponse response, Instant startTime) Methods inherited from class io.agentscope.core.formatter.AbstractBaseFormatter
convertToolResultToString, doFormat, doFormat, extractTextContent, extractTextContent, format, format, formatRoleLabel, getOptionOrDefault, hasMediaContent, saveBase64DataToTempFile, shouldBypassHistory
-
Field Details
-
messageConverter
-
responseParser
-
-
Constructor Details
-
OpenAIBaseFormatter
protected OpenAIBaseFormatter()
-
-
Method Details
-
parseResponse
public io.agentscope.core.model.ChatResponse parseResponse(OpenAIResponse response, Instant startTime) -
applyOptions
public abstract void applyOptions(OpenAIRequest request, io.agentscope.core.model.GenerateOptions options, io.agentscope.core.model.GenerateOptions defaultOptions) Apply generation options to the request. Subclasses implement provider-specific option handling.- Parameters:
request- OpenAI request DTOoptions- Generation options to applydefaultOptions- Default options to use if options parameter is null
-
applyTools
public abstract void applyTools(OpenAIRequest request, List<io.agentscope.core.model.ToolSchema> tools) Apply tool schemas to the request. Subclasses implement provider-specific tool handling.- Parameters:
request- OpenAI request DTOtools- List of tool schemas to apply (may be null or empty)
-
applyToolChoice
public abstract void applyToolChoice(OpenAIRequest request, io.agentscope.core.model.ToolChoice toolChoice) Apply tool choice configuration to the request. Subclasses implement provider-specific tool choice handling.- Parameters:
request- OpenAI request DTOtoolChoice- Tool choice configuration (null means auto)
-
applyTools
public void applyTools(OpenAIRequest request, List<io.agentscope.core.model.ToolSchema> tools, String baseUrl, String modelName) Apply tool schemas with provider context. Default implementation delegates toapplyTools(OpenAIRequest, List).- Parameters:
request- OpenAI request DTOtools- Tool schemas to applybaseUrl- API base URL (ignored by default)modelName- Model name (ignored by default)
-
applyToolChoice
public void applyToolChoice(OpenAIRequest request, io.agentscope.core.model.ToolChoice toolChoice, String baseUrl, String modelName) Apply tool choice with provider context. Default implementation delegates toapplyToolChoice(OpenAIRequest, ToolChoice).- Parameters:
request- OpenAI request DTOtoolChoice- Tool choice configurationbaseUrl- API base URL (ignored by default)modelName- Model name (ignored by default)
-
buildRequest
Build a basic OpenAIRequest.- Parameters:
model- Model namemessages- Formatted OpenAI messagesstream- Whether to enable streaming- Returns:
- Basic OpenAIRequest
-
buildRequest
public OpenAIRequest buildRequest(String model, List<OpenAIMessage> messages, boolean stream, io.agentscope.core.model.GenerateOptions options, io.agentscope.core.model.GenerateOptions defaultOptions, List<io.agentscope.core.model.ToolSchema> tools, io.agentscope.core.model.ToolChoice toolChoice) Build a complete OpenAIRequest with full configuration. This method is provided for convenience but usage via the standard Formatter interface (instantiating request manually and calling apply methods) is preferred in generic code.- Parameters:
model- Model namemessages- Formatted OpenAI messagesstream- Whether to enable streamingoptions- Generation optionsdefaultOptions- Default generation optionstools- Tool schemastoolChoice- Tool choice configuration- Returns:
- Complete OpenAIRequest ready for API call
-