Package com.chatmotorapi.api
Class MotorLargeResponse
java.lang.Object
com.chatmotorapi.api.MotorLargeResponse
- All Implemented Interfaces:
MotorResponseFile
,MotorResponseType
public class MotorLargeResponse extends Object implements MotorResponseType, MotorResponseFile
Represents the response from executing a request that typically involves
large data transfers, adapted to handle responses as files rather than
strings. This class encapsulates all details relevant to the response,
including any errors, usage metrics, and the content of the response itself,
making it particularly suitable for requests like
MotorLargeRequestLines
,
MotorLargeRequestText
,
MotorLargeTranslationRequest
or MotorSpeechRequest
where
output can be extensive.
Instances of this class are generated as a result of executing requests
through the ChatMotor API and are crucial for managing large-scale data
outputs. The class provides access methods that allow for thorough analysis
and processing of the response.
The class includes:
- Status indicators to check the success of the request.
- Detailed error messages if applicable.
- Usage statistics related to the request.
- The actual response content provided as a
File
, making it suitable for handling large amounts of data. - Methods to get the error type and message, if the error has been raised by OpenAI.
Usage example:
MotorLargeRequestText request = // Build a large text request
MotorLargeResponse response = request.execute();
if (response.isResponseOk()) {
try (InputStream inputStream = response.getInputStream()) {
// Process the input stream as needed
} catch (IOException e) {
// Handle the exception
}
} else {
// Check if the response is an OpenAI error and get the OpenAI error details
// (type and message)
OpenAiError openAiError = response.getOpenAiError();
OpenAiErrorType errorType = openAiError.getType();
if (errorType != OpenAiErrorType.NO_OPENAI_ERROR) {
System.out.println("OpenAI has returned an error : " + openAiError);
// Take specific action depending on the error code:
if (errorType.equals(OpenAiErrorType.SERVER_ERROR)) {
// ...
} else if (errorType.equals(OpenAiErrorType.TOKENS)) {
// ..
} else {
// Etc.
}
} else {
System.out.println("An Exception was raised during the treatment: "
+ response.getThrowable());
}
}
-
Constructor Summary
Constructors Constructor Description MotorLargeResponse()
-
Method Summary
Modifier and Type Method Description InputStream
getInputStream()
Gets the response as InputStream.MotorCompletionUsage
getMotorCompletionUsage()
Gets the usage details of the response.OpenAiError
getOpenAiError()
Gets the OpenAI error associated with the response.Reader
getReader(String charsetName)
Gets the response as Reader.Reader
getReader(Charset charset)
Gets the response as Reader.long
getSize()
Gets the size of the response InputStream.Throwable
getThrowable()
Gets the throwable associated with the response.boolean
isResponseOk()
Returns whether the response was successfully processed.String
toString()
Provides a string representation of this object.
-
Constructor Details
-
MotorLargeResponse
public MotorLargeResponse()
-
-
Method Details
-
getMotorCompletionUsage
Gets the usage details of the response. Maybe null for non chat requests.- Returns:
- the usage object containing metrics associated with the request
-
isResponseOk
public boolean isResponseOk()Returns whether the response was successfully processed.- Returns:
- true if the response was processed successfully; otherwise, false
-
getSize
public long getSize()Gets the size of the response InputStream.- Returns:
- the response file.
-
getInputStream
Gets the response as InputStream.- Returns:
- the response file's input stream.
- Throws:
IOException
- if an I/O error occurs while reading the response file or if the file cannot be found.
-
getReader
Gets the response as Reader.- Parameters:
charset
- the charset to use for decoding the response file's bytes.'- Returns:
- the response file's text stream.
- Throws:
IOException
- if an I/O error occurs while reading the response file or if the file cannot be found.
-
getReader
Gets the response as Reader.- Parameters:
charsetName
- the charset name to use for decoding the response file's bytes.'- Returns:
- the response file's text stream.
- Throws:
IOException
- if an I/O error occurs while reading the response file or if the file cannot be found.
-
getThrowable
Gets the throwable associated with the response.- Returns:
- the throwable, or a MotorNoExceptionThrown instance if there is no throwable.
-
getOpenAiError
Gets the OpenAI error associated with the response.- Returns:
- the OpenAiError
-
toString
Provides a string representation of this object. The output includes the class name along with key attribute values.
-