Package org.machanism.machai.ai.openAI
Class OpenAIProvider
java.lang.Object
org.machanism.machai.ai.openAI.OpenAIProvider
- All Implemented Interfaces:
GenAIProvider
The `OpenAIProvider` class integrates seamlessly with the OpenAI API, serving
as a concrete implementation of the `GenAIProvider` interface.
This provider enables a wide range of generative AI capabilities, including:
- Sending prompts and receiving responses from OpenAI Chat models. - Managing
files for use in various OpenAI workflows. - Performing advanced large
language model (LLM) requests, such as text generation, summarization, and
question answering. - Creating and utilizing vector embeddings for tasks like
semantic search and similarity analysis.
By abstracting the complexities of direct API interaction, `OpenAIProvider`
allows developers to leverage OpenAI’s powerful models efficiently within
their applications. It supports both synchronous and asynchronous operations,
and can be easily extended or configured to accommodate different use cases
and model parameters.
This class provides capabilities to send prompts, manage files, perform LLM requests, and create embeddings using OpenAI Chat models.
Usage example:
GenAIProvider provider = GenAIProviderManager.getProvider("OpenAI:gpt-5.1");
Thread safety: This implementation is NOT thread-safe.
- Since:
- 0.0.2
- Author:
- Viktor Tovstyi
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidUploads a file to OpenAI and adds its reference as input.voidAdds input from a file by URL.voidAdds a tool to the current session, providing its function handler.voidclear()Clears all input items for the next request.Requests embeddings for the given text using the current embedding model.voidSet the file used for logging model inputs.voidinstructions(String instructions) Sets instructions for the model to use in the session.voidInitializes the chat model and OpenAI client.perform()Performs a request using current inputs and returns the response string.voidAdds a text prompt to the current conversation.voidpromptBundle(ResourceBundle promptBundle) Set the ResourceBundle for prompts.voidpromptFile(File file, String bundleMessageName) Adds a prompt from a file, formatting the file content according to a ResourceBundle message.voidsetWorkingDir(File workingDir)
-
Constructor Details
-
OpenAIProvider
public OpenAIProvider()
-
-
Method Details
-
prompt
Adds a text prompt to the current conversation.- Specified by:
promptin interfaceGenAIProvider- Parameters:
text- the prompt string
-
promptFile
Adds a prompt from a file, formatting the file content according to a ResourceBundle message.- Specified by:
promptFilein interfaceGenAIProvider- Parameters:
file- the file containing prompt databundleMessageName- key for message in ResourceBundle, or null for raw file content- Throws:
IOException- if reading the file fails
-
addFile
Uploads a file to OpenAI and adds its reference as input.- Specified by:
addFilein interfaceGenAIProvider- Parameters:
file- local file to upload- Throws:
IOException- if reading file failsFileNotFoundException- if file is missing
-
addFile
Adds input from a file by URL.- Specified by:
addFilein interfaceGenAIProvider- Parameters:
fileUrl- the URL of the input file- Throws:
IOException- on network errorFileNotFoundException- if the file cannot be found
-
perform
Performs a request using current inputs and returns the response string.- Specified by:
performin interfaceGenAIProvider- Returns:
- the model response
-
embedding
Requests embeddings for the given text using the current embedding model.- Specified by:
embeddingin interfaceGenAIProvider- Parameters:
text- input to embed- Returns:
- embedding as a list of
floatvalues
-
clear
public void clear()Clears all input items for the next request.- Specified by:
clearin interfaceGenAIProvider
-
addTool
public void addTool(String name, String description, Function<Object[], Object> function, String... paramsDesc) Adds a tool to the current session, providing its function handler.- Specified by:
addToolin interfaceGenAIProvider- Parameters:
name- tool function namedescription- tool descriptionfunction- handler callback for tool executionparamsDesc- array of parameter descriptions in format name:type:required:description
-
instructions
Sets instructions for the model to use in the session.- Specified by:
instructionsin interfaceGenAIProvider- Parameters:
instructions- text for model instructions
-
promptBundle
Set the ResourceBundle for prompts.- Parameters:
promptBundle- ResourceBundle instance
-
inputsLog
Set the file used for logging model inputs.- Specified by:
inputsLogin interfaceGenAIProvider- Parameters:
inputsLog- file for input logging
-
model
Initializes the chat model and OpenAI client. Requires OPENAI_API_KEY environment variable to be set.- Specified by:
modelin interfaceGenAIProvider- Parameters:
chatModelName- the model name (e.g., "gpt-4")- Throws:
RuntimeException- if API key is missing
-
setWorkingDir
- Specified by:
setWorkingDirin interfaceGenAIProvider
-