Class OpenAIChatModel.Builder
- Enclosing class:
- OpenAIChatModel
The built model internally wraps the configuration so that calls without explicit options use the builder-provided values.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionSets the API key for OpenAI authentication.Sets a custom base URL for OpenAI API.build()Builds the OpenAIChatModel instance.contextWindowSize(int contextWindowSize) endpointPath(String endpointPath) Sets a custom endpoint path for the API request.formatter(io.agentscope.core.formatter.Formatter<OpenAIMessage, OpenAIResponse, OpenAIRequest> formatter) Sets the message formatter to use.generateOptions(io.agentscope.core.model.GenerateOptions options) Sets the default generation options.httpTransport(io.agentscope.core.model.transport.HttpTransport httpTransport) Sets the HTTP transport to use.Sets the model name to use.nativeStructuredOutput(boolean nativeStructuredOutput) Sets whether this model supports native structured output viaresponse_format.nativeStructuredOutputWithTools(boolean nativeStructuredOutputWithTools) Sets whether this model correctly handles native structured output (response_format) alongside tool calling.proxy(io.agentscope.core.model.transport.ProxyConfig proxyConfig) Sets the proxy configuration for HTTP traffic.stream(boolean stream) Sets whether streaming should be enabled.
-
Constructor Details
-
Builder
public Builder()
-
-
Method Details
-
apiKey
Sets the API key for OpenAI authentication.- Parameters:
apiKey- the API key- Returns:
- this builder instance
-
modelName
Sets the model name to use.- Parameters:
modelName- the model name (e.g., "gpt-4", "gpt-3.5-turbo")- Returns:
- this builder instance
-
stream
Sets whether streaming should be enabled.- Parameters:
stream- true to enable streaming, false for non-streaming- Returns:
- this builder instance
-
generateOptions
Sets the default generation options.- Parameters:
options- the default options to use (null for defaults)- Returns:
- this builder instance
-
baseUrl
Sets a custom base URL for OpenAI API.- Parameters:
baseUrl- the base URL (null for default)- Returns:
- this builder instance
-
endpointPath
Sets a custom endpoint path for the API request.This allows customization for OpenAI-compatible APIs that use different endpoint paths than the standard OpenAI API (e.g., "/v4/chat/completions", "/api/v1/llm/chat", etc.). When null, the default endpoint path will be used.
- Parameters:
endpointPath- the endpoint path (e.g., "/v1/chat/completions")- Returns:
- this builder instance
-
formatter
public OpenAIChatModel.Builder formatter(io.agentscope.core.formatter.Formatter<OpenAIMessage, OpenAIResponse, OpenAIRequest> formatter) Sets the message formatter to use.Use provider-specific formatters for different providers:
OpenAIChatFormatter- Standard OpenAI GPT modelsDeepSeekFormatter- DeepSeek Chat modelsGLMFormatter- Zhipu GLM modelsMiniMaxFormatter- MiniMax modelsKimiFormatter- Kimi (Moonshot AI) models
- Parameters:
formatter- the formatter (null for default OpenAI formatter)- Returns:
- this builder instance
-
httpTransport
public OpenAIChatModel.Builder httpTransport(io.agentscope.core.model.transport.HttpTransport httpTransport) Sets the HTTP transport to use.Note on proxy configuration: Because
httpTransportis a fully-constructed HTTP client instance, its configuration (including proxy) cannot be modified after creation. If you need to configure a proxy, either:- Use
proxy(ProxyConfig)without callinghttpTransport(), for simple proxy-only customization. - Configure the proxy directly within the transport's
HttpTransportConfigwhen building the transport instance.
If both
httpTransport()andproxy()are called,httpTransport()takes full precedence andproxy()is ignored (a warning is logged at build time).- Parameters:
httpTransport- the HTTP transport (null for default from factory)- Returns:
- this builder instance
- Use
-
proxy
Sets the proxy configuration for HTTP traffic.Interaction with
httpTransport(HttpTransport): BecausehttpTransportis a fully-constructed HTTP client instance, its configuration cannot be modified after creation. The final transport is determined as follows:- If only
proxy()is called: the defaultOkHttpTransportis used with the specified proxy configuration applied. - If only
httpTransport()is called: the provided transport is used as-is (proxy must be configured within the transport's ownHttpTransportConfig). - If both are called:
httpTransport()takes full precedence. Theproxy()setting is ignored and a warning is logged. To configure proxy with a custom transport, set it within the transport'sHttpTransportConfigdirectly. - If neither is called: the default transport from
HttpTransportFactoryis used (no proxy).
- Parameters:
proxyConfig- the proxy configuration (seeProxyConfig)- Returns:
- this builder instance
- If only
-
contextWindowSize
-
nativeStructuredOutput
Sets whether this model supports native structured output viaresponse_format.Defaults to
true, which is correct for OpenAI's own models. Set tofalsefor OpenAI-compatible providers that do not reliably return schema-constrained JSON throughresponse_format; the agent will use thegenerate_responsefallback tool instead.- Parameters:
nativeStructuredOutput- false to disable native structured output- Returns:
- this builder instance
-
nativeStructuredOutputWithTools
public OpenAIChatModel.Builder nativeStructuredOutputWithTools(boolean nativeStructuredOutputWithTools) Sets whether this model correctly handles native structured output (response_format) alongside tool calling.Defaults to
true, which is correct for OpenAI's own models (GPT-4o, etc.). Set tofalsefor OpenAI-compatible providers (Kimi, Deepseek, etc.) that prioritiseresponse_formatover tool invocations, causing the model to skip tool calling when both are present.- Parameters:
nativeStructuredOutputWithTools- false to use fallback when tools are present- Returns:
- this builder instance
-
build
Builds the OpenAIChatModel instance.- Returns:
- configured OpenAIChatModel instance
- Throws:
IllegalArgumentException- if modelName is not set
-