Class OpenAIProvider
- All Implemented Interfaces:
EmbeddingProvider,Genai
Genai implementation.
<p>This provider adapts the Machai provider abstraction to the OpenAI
Java SDK Responses API. It supports prompting, file inputs, tool/function
calling, optional web search and MCP tools, and embedding generation.
</p>
Configuration
Configuration values are read from the Configurator passed to
AbstractAIProvider.init(String, Configurator).
chatModel(required): model identifier passed to the OpenAI Responses API, for examplegpt-4.1orgpt-4o.OPENAI_API_KEY(required): API key used to authenticate with the OpenAI API.OPENAI_BASE_URL(optional): base URL for OpenAI-compatible endpoints. If unset, the SDK default base URL is used.GENAI_TIMEOUT(optional): request timeout in seconds. If missing,0, or negative, the SDK default timeouts are used.MAX_OUTPUT_TOKENS(optional): maximum number of output tokens. Defaults to 18000L.MAX_TOOL_CALLS(optional): maximum number of tool calls the model may issue in a single response loop. A value of0leaves the limit unset.WebSearchTool.type(optional): enables the built-in web search tool when present.WebSearchTool.city(optional): city value for the web search user location.WebSearchTool.country(optional): country value for the web search user location.WebSearchTool.region(optional): region value for the web search user location.MCP.url,MCP.label,MCP.description,MCP.authorization(optional): registers an MCP server tool.MCP_1.url,MCP_1.label,MCP_1.description,MCP_1.authorizationand similarly numbered groups (optional): registers additional MCP server tools.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringConfiguration/environment key used by OpenAI-compatible clients to provide the API key.static final StringConfiguration/environment key used to override the OpenAI-compatible base URL.Fields inherited from class org.machanism.machai.ai.provider.AbstractAIProvider
chatModel, DEFAULT_WEBSEARCH_TYPE_NAME, instructions, LINE_SEPARATOR, LOG_INPUTS_PROP_NAME, LOG_LINE_LENG, LOG_SECTION_SEPARATOR, MAX_OUTPUT_TOKENS, maxOutputTokens, maxToolCalls, MCP_PROP_NAME_PREFIX, PARAGRAPH_SEPARATOR, PASSWORD_PROP_NAME, PROJECT_DIR_PARAM_NAME, projectDir, SERVERID_PROP_NAME, timeoutSec, USERNAME_PROP_NAME -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidaddMcpServer(String name, String url, String authorization, String description) Adds one or more MCP server tools from configuration.protected voidaddTool(String name, String description, ToolFunction function, ParamDescriptor... paramsDesc) Registers a function tool for the current provider instance.protected voidaddWebSearch(String type, String city, String country, String region) Adds the built-in OpenAI web search tool when configured.protected voidcaptureUsage(Optional<com.openai.models.responses.ResponseUsage> usage) Captures and records usage statistics from an optionalResponseUsageinstance.voidclear()Clears all accumulated inputs for the next request.Requests an embedding vector for the given input text.com.openai.client.OpenAIClientReturns the underlying OpenAI client.protected voidlogInputsSpec(Writer streamWriter) Writes provider-specific input items to the supplied log writer.perform()Executes a request using the currently configured model, inputs, and tools.voidAdds a text prompt to the current request input.Methods inherited from class org.machanism.machai.ai.provider.AbstractAIProvider
addMcpServers, addPrompt, addPrompts, addTools, addWebSearch, getConfigurator, getParamValue, getProjectDir, getTimeout, init, inputsLog, instructions, isErrorHandling, logInputs, normalize, safelyInvokeTool, setErrorHandling, setProjectDir, setTimeoutMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.machanism.machai.ai.provider.EmbeddingProvider
init
-
Field Details
-
OPENAI_API_KEY
Configuration/environment key used by OpenAI-compatible clients to provide the API key.For CodeMie this value is set to the retrieved OAuth 2.0 access token.
- See Also:
-
OPENAI_BASE_URL_NAME
Configuration/environment key used to override the OpenAI-compatible base URL.- See Also:
-
-
Constructor Details
-
OpenAIProvider
public OpenAIProvider()Creates an OpenAI provider instance.
-
-
Method Details
-
addWebSearch
Adds the built-in OpenAI web search tool when configured.- Overrides:
addWebSearchin classAbstractAIProvider- Parameters:
type- provider-specific web-search tool type/versioncity- optional user citycountry- optional user countryregion- optional user region
-
addMcpServer
Adds one or more MCP server tools from configuration.- Overrides:
addMcpServerin classAbstractAIProvider- Parameters:
name- provider-visible MCP server labelurl- server endpoint URLauthorization- optional authorization token/valuedescription- optional human-readable description
-
addTool
protected void addTool(String name, String description, ToolFunction function, ParamDescriptor... paramsDesc) Registers a function tool for the current provider instance.The
paramsDescentries must follow the formatname:type:required:description. The parameter schema passed to OpenAI is a JSON Schema object of typeobject.- Specified by:
addToolin classAbstractAIProvider- Parameters:
name- tool function namedescription- tool descriptionfunction- handler callback for tool executionparamsDesc- parameter descriptors in the formatname:type:required:description
-
prompt
Adds a text prompt to the current request input.- Specified by:
promptin interfaceGenai- Overrides:
promptin classAbstractAIProvider- Parameters:
text- the prompt string
-
perform
Executes a request using the currently configured model, inputs, and tools.If the response contains tool calls, the provider executes the matching tool handlers and continues the conversation until a final text response is produced.
-
clear
public void clear()Clears all accumulated inputs for the next request.- Specified by:
clearin interfaceGenai- Overrides:
clearin classAbstractAIProvider
-
getClient
public com.openai.client.OpenAIClient getClient()Returns the underlying OpenAI client.- Returns:
- OpenAI client
-
captureUsage
Captures and records usage statistics from an optionalResponseUsageinstance.If the provided
usageis present, this method extracts input tokens, cached input tokens, and output tokens from theResponseUsageobject, constructs a newUsagerecord, and adds it to theUsageStatisticsfor the current chat model.- Parameters:
usage- anOptionalcontaining theResponseUsagedetails to record; if not present, no action is taken
-
logInputsSpec
Description copied from class:AbstractAIProviderWrites provider-specific input items to the supplied log writer.- Overrides:
logInputsSpecin classAbstractAIProvider- Parameters:
streamWriter- destination writer- Throws:
IOException- when writing fails
-
embedding
Requests an embedding vector for the given input text.- Specified by:
embeddingin interfaceEmbeddingProvider- Parameters:
text- input to embeddimensions- number of dimensions requested from the embedding model- Returns:
- embedding as a list of
doublevalues, ornullwhentextisnull
-