Class OpenAIProvider
java.lang.Object
org.machanism.machai.ai.provider.openai.OpenAIProvider
- All Implemented Interfaces:
Genai
OpenAI-backed
Genai implementation.
This provider adapts the MachAI provider abstraction to the OpenAI Java SDK Responses API. It supports prompting, file inputs, tool/function calling, and embedding generation.
Configuration
Configuration values are read from the Configurator passed to
init(Configurator).
chatModel(required): model identifier passed to the OpenAI Responses API (for example,gpt-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. Defaults to 600L seconds.MAX_OUTPUT_TOKENS(optional): maximum number of output tokens. Defaults to 18000L.MAX_TOOL_CALLS(optional): maximum number of tool calls allowed in a single response. Defaults to 200L.embedding.model(optional): embedding model identifier used byembedding(String, long). If unset, embedding generation may fail due to missing model selection.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final longDefault maximum number of tokens the model may generate.static final longDefault maximum number of tool calls allowed per response.Fields 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 TypeMethodDescriptionvoidaddTool(String name, String description, ToolFunction function, String... paramsDesc) Registers a function tool for the current provider instance.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.longReturns the configured request timeout.voidinit(Configurator config) Initializes the provider from the given configuration.voidEnables request input logging to the given file.voidinstructions(String instructions) Sets system-level instructions applied to subsequent requests.(package private) voidWrites the current request inputs toinputsLogwhen logging is enabled.perform()Executes a request using the currently configured model, inputs, and tools.voidAdds a text prompt to the current request input.voidsetTimeout(long timeout) Sets a request timeout (in seconds) for new clients created by this provider.voidsetWorkingDir(File workingDir) Sets the working directory passed to tool handlers.usage()Returns token usage metrics captured from the most recentperform()call.
-
Field Details
-
MAX_TOOL_CALLS
public static final long MAX_TOOL_CALLSDefault maximum number of tool calls allowed per response.- See Also:
-
MAX_OUTPUT_TOKENS
public static final long MAX_OUTPUT_TOKENSDefault maximum number of tokens the model may generate.- See Also:
-
-
Constructor Details
-
OpenAIProvider
public OpenAIProvider()
-
-
Method Details
-
init
Initializes the provider from the given configuration. -
prompt
Adds a text prompt to the current request input. -
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.
-
embedding
Requests an embedding vector for the given input text. -
logInputs
void logInputs()Writes the current request inputs toinputsLogwhen logging is enabled. -
clear
public void clear()Clears all accumulated inputs for the next request. -
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. -
instructions
Sets system-level instructions applied to subsequent requests.- Specified by:
instructionsin interfaceGenai- Parameters:
instructions- instruction text (may benullto clear)
-
inputsLog
Enables request input logging to the given file. -
setWorkingDir
Sets the working directory passed to tool handlers.- Specified by:
setWorkingDirin interfaceGenai- Parameters:
workingDir- working directory (may benull)
-
usage
Returns token usage metrics captured from the most recentperform()call. -
getClient
protected com.openai.client.OpenAIClient getClient()Returns the underlying OpenAI client.- Returns:
- OpenAI client
-
getTimeout
public long getTimeout()Returns the configured request timeout.- Returns:
- timeout in seconds;
0indicates the SDK default
-
setTimeout
public void setTimeout(long timeout) Sets a request timeout (in seconds) for new clients created by this provider.- Parameters:
timeout- timeout in seconds; use0to use SDK defaults
-