Class OpenAIProvider
java.lang.Object
org.machanism.machai.ai.provider.AbstractAIProvider
org.machanism.machai.ai.provider.openai.OpenAIProvider
- All Implemented Interfaces:
Genai
OpenAI-backed
</ul>
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
<p>Configuration values are read from theConfigurator passed to AbstractAIProvider.init(Configurator).
</p><ul>
chatModel (required): model identifier passed to the OpenAI
Responses API, for example gpt-4.1 or gpt-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 of 0 leaves the limit
unset.embedding.model (optional): embedding model identifier used by
embedding(String, long).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.authorization and similarly numbered groups (optional):
registers additional MCP server tools.-
Field Summary
FieldsModifier and TypeFieldDescription(package private) final List<com.openai.models.responses.ResponseInputItem> Accumulated request input items for the current conversation.(package private) static org.slf4j.LoggerLogger instance for this provider.static final StringConfiguration/environment key used by OpenAI-compatible clients to provide the API key.static final String(package private) final Map<com.openai.models.responses.Tool, ToolFunction> Maps tools to handler functions.Fields inherited from class org.machanism.machai.ai.provider.AbstractAIProvider
chatModel, config, embeddingModel, instructions, lastUsage, LOG_SECTION_SEPARATOR, MAX_OUTPUT_TOKENS, maxOutputTokens, maxToolCalls, MCP_PROP_NAME_PREFIX, timeoutSec, workingDirFields inherited from interface org.machanism.machai.ai.provider.Genai
LINE_SEPARATOR, LOG_INPUTS_PROP_NAME, PARAGRAPH_SEPARATOR, PASSWORD_PROP_NAME, SERVERID_PROP_NAME, USERNAME_PROP_NAME -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds one or more MCP server tools from configuration.voidaddTool(String name, String description, ToolFunction function, String... paramsDesc) Registers a function tool for the current provider instance.voidAdds the built-in OpenAI web search tool when configured.protected voidcaptureUsage(Optional<com.openai.models.responses.ResponseUsage> usage) Extracts token usage from the response and stores it asAbstractAIProvider.usage().voidclear()Clears all accumulated inputs for the next request.Requests an embedding vector for the given input text.protected com.openai.client.OpenAIClientReturns the underlying OpenAI client.protected voidlogInputsSpec(Writer streamWriter) 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
getTimeout, init, inputsLog, instructions, isRequiredParameter, logInputs, normalize, safelyInvokeTool, setTimeout, setWorkingDir, usage
-
Field Details
-
logger
static org.slf4j.Logger loggerLogger instance for this provider. -
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
- See Also:
-
toolMap
Maps tools to handler functions. -
inputs
Accumulated request input items for the current conversation.
-
-
Constructor Details
-
OpenAIProvider
public OpenAIProvider()
-
-
Method Details
-
addWebSearch
public void addWebSearch()Adds the built-in OpenAI web search tool when configured.- Specified by:
addWebSearchin classAbstractAIProvider
-
addMcpServer
public void addMcpServer()Adds one or more MCP server tools from configuration.- Specified by:
addMcpServerin classAbstractAIProvider
-
addTool
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.- 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.- 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.
- Returns:
- the final model response text, or
nullif no text was produced
-
embedding
Requests an embedding vector for the given input text.- Parameters:
text- input to embeddimensions- number of dimensions requested from the embedding model- Returns:
- embedding as a list of
doublevalues, ornullwhentextisnull
-
clear
public void clear()Clears all accumulated inputs for the next request. -
getClient
protected com.openai.client.OpenAIClient getClient()Returns the underlying OpenAI client.- Returns:
- OpenAI client
-
captureUsage
Extracts token usage from the response and stores it asAbstractAIProvider.usage().- Parameters:
usage- optional usage information from the OpenAI response
-
logInputsSpec
- Specified by:
logInputsSpecin classAbstractAIProvider- Throws:
IOException
-