ChatMotor API v1.2-beta - September 20, 2024

User Guide

ChatMotor HTTP Icon

 

ChatMotor API v1.2-beta - September 20, 2024User GuideIntroduction to ChatMotor APIWhat's NewCredits and AcknowledgmentsIntroduction to ChatMotorComplete Javadoc and Code ExamplesDownload the SoftwareChoosing your EditionRequirementsInstallation DetailsExampleAndroidPurchasing the SoftwareGetting StartedCreating a ChatMotor ObjectDefining OpenAI optionsMaking a Chat Completion RequestException ManagementConsistent Request and Response HandlingUnchecked ExceptionsExtracting OpenAI ErrorsExample of Extracting OpenAI ErrorsMaking a Chat Completion Request with Streaming ResponseExplaining the MotorResponseListener interfaceAdvantages of Using a ListenerExample Implementation: ConsoleResponseListenerKeeping the Context in the ConversationAdvanced TopicsFileSupported Text File Extensions (Case-Insensitive)FileToTextConverter UtilityException: Translation APIs support HTML filesLarge Content Management: MotorLargeRequestLineIsomorphic TransformationsLine-Based File ProcessingStreaming ProcessingChunk Input SizeUsage ExampleExplanationLarge Content Management: MotorLargeRequestTextInput SizeUsage ExampleAutomatic Retry MechanismUsage ExampleTimeout SetupUsage ExampleProxy ConfigurationUsage ExampleFailover MechanismUsage ExampleAudio and Image APIsTranscriptionKey FeaturesThe MotorTranscriptionRequest classThe MotorTranscriptFormatterRequest classUsage ExampleSpeechKey FeaturesUsage ExampleImageKey FeaturesUsage ExampleVisionKey FeaturesUsage ExampleUsage Example with a local imageFunctional APIsSummarizationClassic Summary (MotorSummaryRequest)Strategic Summary (MotorStrategicSummaryRequest)TranslationLarge Files Translation (MotorLargeTranslationRequest)Other Experimental Functional APIsContent Extraction (MotorExtractContentRequest)Usage Example:Content Filtering (MotorFilterContentRequest)Usage Example:Note on Experimental APIsPro Supplemental APIsCategorization (MotorCategorizationRequest)Usage Example:Entity Extraction (MotorEntityRequest)Usage Example:MotorSentimentRequestUsage Example:Notification APIKey FeaturesNotificationChannel InterfaceMotorNotifyResponseExample Usage: Slack NotificationOther ImplementationsExtendable DesignTroubleshooting and Best PracticesCommon Issues and SolutionsBe Patient with Large RequestsAppendixReference to sample code examples

Introduction to ChatMotor API

The ChatMotor API is designed to help you easily integrate ChatGPT capabilities into your Java applications. It abstracts away the complexities of implementation so you can focus on specifying what you want to achieve, without worrying about the details of how to achieve it.

With support for extensive text processing, functional APIs for tasks like translation and summarization, and advanced features like progressive teletext display and automatic retry mechanisms, the ChatMotor API simplifies the development of intelligent, responsive applications.

What's New

Version 1.2-beta:

Version 1.1-beta:

Version 1.0-beta:

Credits and Acknowledgments

ChatMotor is built on top of the open-source Simple-OpenAI Java SDK developed by Sashir Estela and peers.

We extend our gratitude for their work, which has been fundamental to the development of ChatMotor.

Introduction to ChatMotor

ChatMotor is an advanced Java SDK designed for integrating with the ChatGPT API, providing a comprehensive and robust set of features. Built on the solid foundation provided by Simple-OpenAI, ChatMotor not only enhances several of the base SDK's capabilities but also introduces a range of unique features tailored to extend functionality and improve user experience:

Complete Javadoc and Code Examples

ChatMotor's extensive Javadoc and sample code ensure that you have all the tools necessary for seamless integration. Each class is accompanied by a complete how-to guide embedded within the Javadoc, along with ready-to-work code examples.

This detailed Javadoc ensures that developers can access all necessary information for effective integration and utilization of the SDK, even without the user guide.

Download the Software

Download the installation file from ChatMotor Download.

Choosing your Edition

When downloading, you have atomically access to Pro Edition for 30 days, then ChatMotor will reverse to Free Edition

FeatureFree EditionPro Edition
Integration with OpenAI APIYesYes
HTTP Proxy SupportYes, without authenticationYes, with authentication
Failover OpenAI API KeyNoYes
Email SupportBest EffortPriority Support
Base Functional APIs
(Translation, Summary)
Limited (Up to 50,000 characters text files and 250KB HTML files)Unlimited
Advanced Functional APIs
(Categorization, Entity Recognition, Sentiment Analysis)
NoYes
Post-Processing Notification API
(SMS, Slack, Email)
NoYes
Audio Files TranscriptionUp to 1 hour durationUnlimited
Text to AudioUp to 50,000 charactersUnlimited
Token Usage VisibilityBasic (Total tokens only)Detailed (Prompt, Completion, and Total tokens)
Access to All Functional APIs for 30-Day TrialYesN/A
Upgrades and New VersionsNoYes, with annual maintenance fee

Requirements

ChatMotor API requires Java 11 or higher and is compatible with Windows, macOS, Linux, and Android.

Transcribing files in formats other than MP3 requires FFMPEG to be installed on Linux and macOS.

On Android, only MP3 file transcription is supported.

Installation Details

  1. Extract the downloaded chatmotor-api-1.2-beta.zip archive to a directory, such as /path/to/software.

  2. Set the CHATMOTOR_HOME environment variable to the installation directory:

  3. Add the CHATMOTOR_HOME/lib directory to your development environment's CLASSPATH.

    When deploying in production, include all JAR files from the /lib directory in the CLASSPATH.

    • For J2EE Web servers: Copy the contents of the CHATMOTOR_HOME/lib directory to the server's lib directory while preserving the installation files.

Example

If the zip file is extracted to /home/user1, the directory structure will be /home/user1/chatmotor-api-1.2-beta. Set CHATMOTOR_HOME in your bash settings file as follows:

Android

Since Android does not support environment variables, you will need to explicitly set the ChatMotor home directory using the ChatMotor.setChatMotorHome(String chatMotorHome) method:

Add the following three lines to your AndroidManifest.xml:

Purchasing the Software

If you decide to purchase the software after the 30-day trial, visit our Pricing Page for detailed information and purchasing options.

After purchasing, you will receive a license file via email. If you have previously used a trial version, there is no need to reinstall the software. Simply place the license file in the following directory:

Where CHATMOTOR_HOME is either the environment variable CHATMOTOR_HOME or the value defined by the ChatMotor.setChatMotorHome(String chatMotorHome) method.

Getting Started

Creating a ChatMotor Object

Here is the initial step required to use the services: you must create a ChatMotor instance and pass to it the license file path and the OpenAI key:

In this example, replace "your_openai_api_key" with your actual API key. This code initializes the ChatMotorClient with the API key, enabling you to make authenticated requests.

Alternatively, you may set the OpenAI keey using the environment variables CHATMOTOR_API_KEY:

For example, this could be your settings:

Defining OpenAI options

The AI options (temperature, max tokens, etc.) are optionally defined with the MotorAiOptions class:

See the MotorAiOptions class Javadoc all options and the corresponding default values.

Making a Chat Completion Request

After creating the ChatMotor instance, and optionally a MotorAiOptions instance he you can start making requests to various endpoints provided by the ChatMotor SDK.

Here's an example of how to call the Chat Completion service to ask a question and wait for a complete answer. The response is printed to the console. This is a simple request, functioning exactly as it does when using ChatGPT in your browser. It uses 2 classes: MotorRequest for the request creation, and MotorResponse to manage the response.

The OpenAI model in use is defined in

Exception Management

To make the developer's life easier, the MotorRequest and all other API requests provided by the ChatMotor SDK are designed to never throw exceptions. Instead, they use a status-checking mechanism that simplifies error handling and improves code readability:

  1. No Need for Try-Catch Blocks:

    • Developers do not need to wrap their API calls in try-catch blocks, resulting in cleaner and shorter code.

  2. Consistent Error Checking:

    • By using a consistent method to check for errors, developers can easily manage error handling across different parts of their application.

Consistent Request and Response Handling

All our requests and responses are designed to be consistent and never throw exceptions.

Developers can simply check the response status using isResponseOk() to determine if the request was successful and use getThrowable() to retrieve any exceptions. This approach ensures unified and predictable error management, leading to more reliable and maintainable code.

Unchecked Exceptions

To provide immediate understanding of certain errors, we have a few unchecked exceptions that can be accessed via MotorResponse.getThrowable(). These exceptions help developers quickly identify specific issues without the need for extensive debugging:

  1. MotorExecutionException:

    • Indicates that an error occurred during the execution of the request. This exception helps pinpoint issues that arise during the processing phase.

  2. MotorNotImplementedException:

    • Indicates that a requested feature or function is not implemented. This exception is useful for identifying missing functionality and ensuring that developers are aware of unimplemented features.

  3. MotorTimeoutException:

    • Indicates that the request timed out. This exception helps developers understand that the operation took too long and was aborted due to a timeout.

By using these unchecked exceptions, developers can quickly understand and address common errors, improving the development experience and reducing the time spent on troubleshooting.

Extracting OpenAI Errors

Handling OpenAI errors effectively is crucial for robust application development. ChatMotor provides mechanisms to extract and manage OpenAI errors efficiently.

Example of Extracting OpenAI Errors

When executing a request, you can check if the response contains an OpenAI error and extract the error details as follows:

See the OpenAiError Javadoc.

Making a Chat Completion Request with Streaming Response

The executeAsStream method allows you to benefit from ChatGPT's streaming capabilities, enabling the response to be processed and displayed in real-time. This method is particularly useful when you want to provide immediate feedback to the user as the response is being generated, rather than waiting for the entire response to be completed.

Explaining the MotorResponseListener interface

The MotorResponseListener interface allows you to handle responses from the ChatMotor service in real-time as they are being streamed. Implementing this interface enables you to process each part of the response immediately, which is useful for providing instant feedback to users.

Advantages of Using a Listener

  1. Avoids Async Methods: The listener mechanism eliminates the need for complex asynchronous methods, simplifying the code.

  2. Immediate Processing: Each part of the response is processed as soon as it is received, providing real-time interaction and feedback.

Using the MotorResponseListener, developers can easily handle streaming responses, improving user experience with real-time updates and reducing the complexity of asynchronous programming.

Example Implementation: ConsoleResponseListener

Here's an example implementation of the MotorResponseListener that prints the response to the console as it is received:

We provide also other standard ready to use implementation : QueueResponseListener, MotorFileResponseListener and SwingResponseListener which are described in their Javadoc.

Keeping the Context in the Conversation

By adding UserMessages to the list in a loop and executing the motorRequest.execute() method, you can maintain the context of the conversation, just as you would in a browser session. Here's an example demonstrating how to do this in the context of building a Java method:

This will display the following flow in the console.

Advanced Topics

File

The ChatMotor APIs are designed to handle input text files, defined by their extensions (txt, text, csv, or their uppercase equivalents). This means that the .filepath(String filepath) method of all APIs (except MotorLargeTranslationRequest see below) accepts only text files. This restriction is due to the nature of OpenAI APIs, which take text content as input and return text content without formatting options.

Supported Text File Extensions (Case-Insensitive)

FileToTextConverter Utility

For other formatted files, we provide a utility called FileToTextConverter that allows extracting text from the following file types based on their (case-insensitive) extensions and dumping content in a text file:

.html

.csv

.doc

.docx

.pdf

.rtf

Example:

Exception: Translation APIs support HTML files

The MotorLargeTranslationRequest and MotorTranslationRequest APIs also accept HTML input. These APIs will return the same formatted HTML file with its content translated.

Large Content Management: MotorLargeRequestLine

The MotorLargeRequestLine class facilitates creating and executing large-sized requests for the ChatMotor API. It uses a builder pattern for flexible and reliable configuration, handling large input by submitting consecutive standard-sized requests and aggregating responses into a final comprehensive result.

Isomorphic Transformations

This class is designed for isomorphic tasks like translation and CSV manipulation, where each chunk of user prompts can be processed independently and then combined. It is ideal for scenarios requiring the transmission of huge data volumes efficiently.

Line-Based File Processing

The input file for MotorLargeRequestLine must contain lines of text, as it processes the content line by line. This approach ensures smooth handling of large files without exceeding token limits.

Streaming Processing

MotorLargeRequestLine processes large content using streaming, allowing it to handle very large-sized files efficiently. By processing the file line by line, it submits consecutive requests and aggregates the results, ensuring that even enormous data volumes are managed effectively without hitting token limits.

Chunk Input Size

It is optionally possible to set the chunk input size with MotorLargeRequestLine.inputChunkSizeBytes(int inputChunkSizeBytes). This is not recommended.

Since we are doing isomorphic transformations, the chunk input size must be less than 4096 tokens, corresponding to the OpenAI maximum response size. The default value for inputChunkSizeBytes is retrieved from the current ChatMotor instance using chatMotor.maxChunkInputSize(), which ensures a conservative size to prevent data loss.

inputChunkSizeBytes cannot exceed ChatMotor.maxChunkInputSize().

This default size avoids data loss during transformation by ensuring the output size does not exceed the model's maximum token limit.

Usage Example

Here's how you can use MotorLargeRequestLine to process a large CSV file:

Explanation

This approach ensures efficient handling of large content, making it ideal for scenarios requiring the processing of extensive data volumes.

Large Content Management: MotorLargeRequestText

The MotorLargeRequestText class facilitates the creation and execution of large-sized requests for the ChatMotor API using a builder pattern for flexible and reliable configuration. It is ideal for scenarios requiring the transmission of large volumes of unstructured text, such as books and reports. This class handles large input by splitting it into smaller chunks, submitting them consecutively to the ChatMotor, and aggregating individual responses into a final comprehensive result.

Input Size

This example processes a large, unstructured text by splitting it into complete sentences and applying the same query to each portion. The input file will be divided into multiple chunks submitted to the ChatGPT API, meaning context will be lost between each request and response due to the context size restrictions of the ChatMotor API.

GPT-4 Turbo, for example, has a context window of 128k tokens and a maximum response size of 4096 tokens. Each response is dedicated to a specific portion of the text because we cannot produce a response that exceeds 4096 tokens.

You can define a chunk size using the MotorLargeRequestText.inputChunkSizeBytes method. This is not recommended.

The default value for inputChunkSizeBytes is retrieved from the current ChatMotor instance using chatMotor.maxInputSize(), which is set to 440KB. This size is suitable for GPT-4 models.

inputChunkSizeBytes cannot exceed the value defined by ChatMotor.maxInputSize().

Usage Example

Check the input text at: proust-chapter-1.txt Check the response produced at: proust-chapter-1-response.txt

Automatic Retry Mechanism

ChatMotor ensures robust and reliable communication by handling HTTP requests automatically and offering a retry functionality with capping and delay management. This reduces the likelihood of errors disrupting operations.

Usage Example

Timeout Setup

ChatMotor allows you to set a timeout for API requests to ensure efficient resource management and prevent indefinite blocking, maintaining the responsiveness of your application.

Usage Example

Proxy Configuration

ChatMotor allows you to configure a proxy for API requests, enabling you to manage network traffic effectively and ensure secure and controlled communication.

Usage Example

Failover Mechanism

If your account exceeds its quota or hits the rate limit, ChatMotor ensures continuity with a failover mechanism. A secondary OpenAI key linked to a secondary account will be used automatically if the primary account fails.

Usage Example

Audio and Image APIs

Transcription

ChatMotor handles the creation and execution of transcription requests using the OpenAI Whisper model. The MotorTranscriptionRequest class is designed to transcribe audio files into text with unlimited size, bypassing the 25MB OpenAI Whisper limitation.

Large files are chunked to reduce memory consumption. The default chunk size is 4MB, but this can be overridden

Key Features

The MotorTranscriptionRequest class

The MotorTranscriptFormatterRequest class

The MotorTranscriptFormatterRequest class allows you to format a transcription file using line breaks and paragraphs. It ensures that the transcribed text is readable and well-structured.

Usage Example

Speech

The MotorSpeechRequest API handles the creation and execution of text-to-speech requests to the ChatMotor using user prompts. It employs the builder pattern to ensure flexibility and enforce mandatory configuration. The model used in requests can be specified with the .aiModel setter on the MotorSpeechRequest builder. If not set, it defaults to the model specified in the MOTOR_SPEECH_MODEL environment variable or MotorDefaultsModels.MOTOR_SPEECH_MODEL.

Key Features

Usage Example

Image

The MotorImageRequest API handles the creation and execution of image-related requests for the ChatMotor API, designed to interface with OpenAI's Vision API. This class leverages the builder pattern to provide a flexible and reliable configuration mechanism, ensuring that all necessary parameters are set before execution.

Key Features

Usage Example

Vision

The MotorVisionRequest API handles the creation and execution of vision-related requests to the ChatMotor using system and user prompts. This class uses the builder pattern to ensure flexibility and enforce mandatory configuration.

Key Features

Usage Example

 

Usage Example with a local image

MotorVisionRequest also supports using a local image loaded with VisionImageUtil.

Functional APIs

The Functional APIs in ChatMotor provide advanced text processing capabilities tailored to tasks like summarization and translation. These APIs offer higher-level functionality and do not directly wrap OpenAI or underlying Sample-OpenAI APIs. Instead, they leverage these models to provide specialized features, enabling developers to handle complex text operations more effectively and efficiently.

Summarization

Classic Summary (MotorSummaryRequest)

The Classic Summary API generates a concise overview of the input text, capturing the main points while reducing the text to a shorter form.

The MotorSummaryRequest class handles the creation and execution of summarization requests. It allows configuration for the text file to be summarized and the model to be used. If the model is not specified, it defaults to the value set in the MOTOR_CHAT_MODEL environment variable or MotorDefaultsModels.MOTOR_CHAT_MODEL.

Key Features:

Usage Example:

 

Strategic Summary (MotorStrategicSummaryRequest)

The MotorStrategicSummaryRequest API offers the same key features as the Classic Summary but focuses on providing a high-level synthesis of the given text, extracting the most critical information and key insights. The output is always in HTML format, enhancing key points for better readability and understanding.

A strategic summary is designed to provide a quick understanding of the main points, trends, and actionable items within the text, making it ideal for decision-making processes and strategic planning.

Key Features:

The response will be in HTML format to guarantee readability and ease of understanding. There is no executeAsStream method implemented in this class.

Usage Example:

 

Translation

Large Files Translation (MotorLargeTranslationRequest)

The MotorLargeTranslationRequest API (handles the translation of text files, whether small or extensive. It preserves the format and structure of the original content while providing high-quality translations. This API uses streaming from input to output, preventing high memory consumption and ensuring efficient handling of large inputs. Users don't need to consider the size of their files; the API automatically switches modes to handle the file efficiently.

ChatMotor will automatically handle the buffer size to ensure no loss of data, but you can always fine-tune the default value of the input buffer length.

This class ensures the format is preserved when translating an HTML file. This allows you to convert Word files to HTML, process them for translation, and then reintegrate them back into Word easily by using "Open With" on the HTML file.

Web Services for PDF or Word to HTML Conversion

To facilitate automated processing, we will offer a free web service in the next ChatMotor beta version to convert Word files to HTML and HTML files to Word. This service will leverage the latest market technology for HTML to Word conversion. We are not experts in file conversion and do not intend to become one, but we aim to provide a useful tool within the current technological limits.

Usage Example:

When setting up the MotorLargeTranslationRequest, you can choose between two implementations for handling large translation requests:

 

Other Experimental Functional APIs

As part of our continuous innovation, we are introducing experimental APIs designed to enhance text sanitization and customization. Please note that these APIs are in the experimental phase, and response times may vary.

Content Extraction (MotorExtractContentRequest)

The MotorExtractContentRequest API enables you to filter the input text to remove or alter content related to specified topics or keywords. This API enhances text sanitization and customization, making it easier to tailor content to specific needs.

Usage Example:

 

Content Filtering (MotorFilterContentRequest)

The MotorFilterContentRequest API allows you to filter the input text to remove or alter content related to specified topics or keywords. This API enhances text sanitization and customization, making it easier to ensure that your content adheres to certain standards or guidelines.

Usage Example:

Note on Experimental APIs

These APIs are currently in an experimental phase, and while they offer powerful capabilities for content extraction and filtering, their response times may not always be optimal. We are actively working to enhance their performance and reliability. Your feedback is valuable to us as we continue to improve these features.

 

Pro Supplemental APIs

ChatMotor provides advanced supplemental APIs for specialized text processing tasks. These APIs include MotorCategorizationRequest, MotorEntityRequest, and MotorSentimentRequest, each tailored for specific use cases such as categorization, entity extraction, and sentiment analysis.

Categorization (MotorCategorizationRequest)

The MotorCategorizationRequest API is designed for categorizing text into predefined or custom categories. It is useful for tasks like content tagging, spam filtering, or topic detection.

Usage Example:

See the MotorCategorizationResponse Javadoc for response details.

Entity Extraction (MotorEntityRequest)

The MotorEntityRequest API extracts named entities such as people, locations, and organizations from a text. This is useful for processing articles, reports, or emails to extract structured data.

Usage Example:

See the MotorEntityResponse Javadoc for response details.

MotorSentimentRequest

The MotorSentimentRequest API analyzes the sentiment of a given text, identifying whether the sentiment is positive, negative, or neutral. It is useful for customer feedback analysis, social media monitoring, and more.

Usage Example:

See the MotorSentimentResponse Javadoc for response details.

Notification API

The Notification API in ChatMotor allows you to notify users that their document is ready or send the document to them directly after an AI request is processed. This is especially useful for scenarios where users may be waiting for a document to be generated and need to be informed promptly. It supports multiple communication channels, including SMS (via Twilio), Slack, and Email.

The API is designed to be flexible and extendable, allowing new communication channels to be added easily, such as Notion or Airtable, in addition to the built-in implementations.

Key Features

NotificationChannel Interface

The NotificationChannel interface defines two key methods:

Both methods accept the document's file path and the execution mode (SYNC or ASYNC).

MotorNotifyResponse

The MotorNotifyResponse indicates whether the notification and/or send operation was successfully delivered. Note that this response is meaningful only in SYNC mode.

Example Usage: Slack Notification

The following example demonstrates how to use the Slack implementation to notify users and send a document.

Other Implementations

You can also use other communication channels, such as:

Extendable Design

The Notification API is designed to be flexible and extensible. You can create custom implementations for other platforms by implementing the NotificationChannel interface. This allows you to use any communication service (e.g., Notion, Airtable, or internal company systems) to notify users or send documents.

Troubleshooting and Best Practices

Common Issues and Solutions

  1. API Key Issues

    • Problem: Invalid or missing API key.

    • Solution: Ensure that your API key is correctly set in the environment variables MOTOR_API_KEY. Verify that the key has the necessary permissions.

  2. License File Issues

    • Problem: License file not found or invalid.

    • Solution: Make sure the MOTOR_LICENSE_FILE_PATH environment variable points to the correct path of your license file. Verify the contents and validity of the license file.

  3. Network Issues

    • Problem: Network connectivity problems causing request failures.

    • Solution: Verify your network connection. If using a proxy, ensure it is correctly configured in your ChatMotor setup.

  4. Performance Issues

    • Problem: high memory consumption with MotorLargeTranslationRequest API

    • Solution: Optimize your request parameters and use the streaming options for handling large files efficiently.

Be Patient with Large Requests

When using MotorLargeRequestText, MotorLargeRequestLine, or MotorLargeTranslationRequest, and MotorLargeTranscriptionRequest, it's important to be patient. These operations can take a significant amount of time, especially for very large files.

Note: Even if a translation or processing takes an hour, it is still far faster and more cost-effective than manual human processing. Understand that large text processing, line processing, and translations are intensive operations. The API handles large files by splitting them into smaller chunks and processing each chunk sequentially. This ensures accurate results but can increase the processing time.

Appendix

Reference to sample code examples

For sample code examples and detailed usage instructions, please refer to the ChatMotor Java Samples project. This repository contains comprehensive examples and guides to help you integrate and utilize the ChatMotor API in your Java applications.