Package com.chatmotorapi.api
Class ChatMotor
java.lang.Object
com.chatmotorapi.api.ChatMotor
public class ChatMotor extends Object
The ChatMotor class encapsulates configuration for interacting with the OpenAI API.
It includes configurations such as the API key, HTTP client, and organization ID.
The API key can be set manually during configuration, or alternatively, it will be automatically retrieved from the environment variable MOTOR_API_KEY. If this value is provided via an environment variable, there is no need to manually set it in the builder.
The builder pattern is used to provide a flexible way of constructing an instance with multiple configuration options, allowing seamless integration and ease of setup in various deployment environments. Usage:
The API key can be set manually during configuration, or alternatively, it will be automatically retrieved from the environment variable MOTOR_API_KEY. If this value is provided via an environment variable, there is no need to manually set it in the builder.
The builder pattern is used to provide a flexible way of constructing an instance with multiple configuration options, allowing seamless integration and ease of setup in various deployment environments. Usage:
String key = "[key value];
String organisationId = "AcmeCorp"; // if you want to use a specific organization
HttpClient httpClient = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(200))
ChatMotor chatMotor = ChatMotor.builder()
.chatMotorHome("/path/to/chatmotor-api-1.1-beta)) // Optional if CHATMOTOR_HOME env var is set
.apiKey(key) // Optional if MOTOR_API_KEY env var is set
.httpClient(httpsClient) // Optional
.organizationId(organisationId) // Optional
.maxRetries(3) // Sets the maximum number of retries for the API request. Optional
.retryInterval(Duration.ofMillis(3000)) // Sets the retry interval between API request retries. Optional
.nThreads(20) // Sets the number of threads for processing. Optional
.maxInputSize(ChunkSize.ofKilobytes(440)) // Sets the max input size for non-isomorphic operations. Optional
.maxChunkInputSize(ChunkSize.ofKilobytes(10)) // Sets the max input size for isomorphic operations. Optional
.build();
This approach ensures flexibility and facilitates easier management of API credentials,
making it highly suitable for environments where configurations are dynamically managed through environment variables.-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ChatMotor.Builder
The Builder class for the ChatMotor class. -
Field Summary
Fields Modifier and Type Field Description static Duration
DEFAULT_INTERVAL_RETRY
Default retry interval between API request retries: 2 seconds.static ChunkSize
DEFAULT_MAX_CHUNK_INPUT_SIZE
The default maximum input size for isomorphic operations.static ChunkSize
DEFAULT_MAX_INPUT_SIZE
The default maximum input size for non-isomorphic operations.static int
DEFAULT_MAX_RETRIES
Default maximum number of retries for API requests: no retries. -
Method Summary
Modifier and Type Method Description static ChatMotor.Builder
builder()
Static method to initiate building a ChatMotor instance.Charset
charset()
Gets the charset to use for reading and writing text fileString
chatMotorHome()
Gets the ChatMotor home/installation directory.boolean
failoverApiKeyEnabled()
Says whether failover API key is enabled for the ChatMotor.boolean
failoverApiKeyIsUsed()
Says is the failover API key is used.FailoverNotifier
failoverNotifier()
Gets the failover notifier for the ChatMotor.HttpClient
httpClient()
Gets the HTTP client for the ChatMotor.ChunkSize
maxChunkInputSize()
Gets the maximum input size for isomorphic operations.ChunkSize
maxInputSize()
Gets the maximum input size for non-isomorphic operations.int
maxRetries()
Gets the maximum number of retries for the API requests.int
nThreads()
Gets the number of threads for the ChatMotor for processing large content.String
organizationId()
Gets the organization ID for the ChatMotor.String
projectId()
Gets the project ID for the ChatMotor.Duration
retryInterval()
Gets the retry interval for the API requests.Duration
timeout()
Gets the timeout for the API requests.ChatMotor.Builder
toBuilder()
Creates a new Builder initialized with the current values of this ChatMotor instance.String
toString()
Provides a string representation of this object.
-
Field Details
-
DEFAULT_MAX_RETRIES
public static final int DEFAULT_MAX_RETRIESDefault maximum number of retries for API requests: no retries. This value specifies the number of times an API request is retried before failing permanently.- See Also:
- Constant Field Values
-
DEFAULT_INTERVAL_RETRY
Default retry interval between API request retries: 2 seconds. This is the default time to wait before attempting a retry after a failed request. -
DEFAULT_MAX_INPUT_SIZE
The default maximum input size for non-isomorphic operations. This value is set to 440 kilobytes, based on the context window size minus the output size. This size is suitable for tasks like summarization where the input size needs to be large. -
DEFAULT_MAX_CHUNK_INPUT_SIZE
The default maximum input size for isomorphic operations. This value is set to 10 kilobytes to avoid data loss during the transformation, ensuring the output size does not exceed the model's maximum token limit. This size is suitable for tasks like translation or CSV line processing.
-
-
Method Details
-
builder
Static method to initiate building a ChatMotor instance.- Returns:
- a new Builder instance
-
chatMotorHome
Gets the ChatMotor home/installation directory.- Returns:
- the ChatMotor home/installation directory
-
failoverApiKeyEnabled
public boolean failoverApiKeyEnabled()Says whether failover API key is enabled for the ChatMotor.- Returns:
- true if failover API key is enabled, false otherwise.
-
failoverApiKeyIsUsed
public boolean failoverApiKeyIsUsed()Says is the failover API key is used.- Returns:
- true if the failover API key must be used.
-
httpClient
Gets the HTTP client for the ChatMotor.- Returns:
- the HTTP client
-
organizationId
Gets the organization ID for the ChatMotor.- Returns:
- the organization ID
-
projectId
Gets the project ID for the ChatMotor.- Returns:
- the project ID
-
maxRetries
public int maxRetries()Gets the maximum number of retries for the API requests.- Returns:
- the maximum number of retries for the API requests
-
retryInterval
Gets the retry interval for the API requests.- Returns:
- the retry interval for the API requests
-
charset
Gets the charset to use for reading and writing text file- Returns:
- the charset use for reading and writing text file
-
timeout
Gets the timeout for the API requests.- Returns:
- the timeout for the API requests
-
nThreads
public int nThreads()Gets the number of threads for the ChatMotor for processing large content.- Returns:
- the number of threads for the ChatMotor for processing large content.
-
maxInputSize
Gets the maximum input size for non-isomorphic operations.- Returns:
- the maximum input size for non-isomorphic operations
-
maxChunkInputSize
Gets the maximum input size for isomorphic operations.- Returns:
- the maximum input size for isomorphic operations
-
failoverNotifier
Gets the failover notifier for the ChatMotor.- Returns:
- the failover notifier for the ChatMotor.
-
toBuilder
Creates a new Builder initialized with the current values of this ChatMotor instance.This method allows users to create a modified copy of the current immutable ChatMotor instance. By using the builder, users can change specific values while retaining the rest of the existing values.
Usage Example:
ChatMotor original = new ChatMotor.Builder() .maxRetries(2) .build(); ChatMotor modified = original.toBuilder() .maxRetries(4) .build(); System.out.println("original: " + original.toString()); System.out.println("modified: " + modified.toString());
- Returns:
- a new Builder initialized with the current values of this instance.
-
toString
Provides a string representation of this object. The output includes the class name along with key attribute values.
-