How plexpt/chatgpt-java works
Project Type and Purpose
• This is an open-source Java SDK (library) for interacting with OpenAI’s ChatGPT REST and streaming APIs.
• It provides two primary client entry points: synchronous (ChatGPT) and streaming (ChatGPTStream).
File Structure
• Top-level:
• pom.xml (Maven coordinates, dependency management)
• src/main/java/com/plexpt/chatgpt – core packages
• .github/workflows – CI (publish to Maven Central)
• Key packages under com.plexpt.chatgpt:
• api – HTTP client wrapper (Api.java)
• entity – request/response models for chat, images, audio, billing
• exception – custom SDK exception (ChatException)
• listener – streaming handlers (ConsoleStreamListener, SseStreamListener)
• util – support classes (Proxy construction, JSON via fastjson, Token counting, SSE helper, Date formatting, Context holder)
• Tests live under src/test/java/com/plexpt/chatgpt to verify streaming and function-call workflows.
Extract Insights from the README
• Features: chat, streaming, function calls, embeddings, image generation, audio transcription, billing usage, token counting, multi-key rotation, proxy support, context management.
• Usage: builder pattern for ChatGPT/ChatGPTStream, injecting apiKey(s), proxy, timeout, apiHost.
• Two execution modes:
• chatCompletion (single HTTP POST)
• streamChatCompletion (SSE style streaming with listeners)
• Integration example: Spring’s SseEmitter via SseStreamListener.
Core Components & Interactions
a. Main Components
1. SDK Client Layer
– ChatGPT (synchronous client)
– ChatGPTStream (streaming client)
– ConsoleChatGPT (demo CLI)
2. API Layer
– Api.java: constructs and executes HTTP requests (POST to OpenAI endpoints)
3. Domain Models
– entity package: ChatCompletionRequest, ChatCompletionResponse, Message, AudioResponse, ImageResponse, UsageResponse, etc.
4. Utilities
– Proxy builder (Proxys.java)
– JSON mapping (JSON, JSONObject in fastjson wrapper)
– Token counter (TokensUtil)
– Context manager (ChatContextHolder)
– SSE helper (SseHelper)
5. Streaming Listeners
– AbstractStreamListener (base)
– ConsoleStreamListener (prints to console)
– SseStreamListener (bridges to Spring SseEmitter)
6. Exception Handling
– ChatException for wrapping HTTP or parsing errors
7. External Systems
– OpenAI REST API (api.openai.com or reverse proxy)
b. Relationships & Data Flow
• Client Layer (ChatGPT or ChatGPTStream) is configured via Builder (apiKey(s), proxy, host, timeout).
• On invocation (chatCompletion or streamChatCompletion), client delegates to Api.java.
• Api.java serializes domain model → HTTP request → sends to OpenAI endpoint.
• Response is deserialized into entity objects (BaseResponse subclasses).
• In streaming mode, Api.java opens an SSE connection and pushes chunks into a StreamListener.
• StreamListener callbacks propagate data back to application code (console, SSE emitter, or custom listener).
• Utilities provide cross-cutting support: JSON parsing, date formatting, token counting, context storage, and proxy handling.
• Errors bubble up as ChatException.
c. Architectural Patterns & Principles
• Builder Pattern for client configuration.
• Layered architecture: Client API → HTTP layer → Domain entities → Util/Listener.
• Observer pattern for streaming (listeners subscribe to events).
• Separation of concerns:
• Domain models separate from HTTP transport.
• Utilities isolated from core client logic.
• Extensibility: Users can implement their own StreamListener or extend entity classes if needed.
d. Key Technologies & Frameworks
• Java 8+
• Fastjson (via shaded util/fastjson packages)
• Java HTTP client (HttpURLConnection or HttpClient inside Api.java)
• Spring SseEmitter (illustrated via SseStreamListener)
• Maven (dependency management & publishing)