Implementation:A616567126 GPT WEB JAVA ChatGPTStream
| Knowledge Sources | |
|---|---|
| Domains | NLP, LLM, SSE_Streaming |
| Last Updated | 2026-10-13 00:00 GMT |
Overview
Concrete tool for streaming OpenAI Chat Completions responses via OkHttp's EventSource, provided by the OkHttp SSE library.
Description
The ChatGPTStream class is a builder-pattern client that wraps OkHttp to open Server-Sent Events connections to the OpenAI Chat Completions API. It configures the OkHttpClient with timeout, proxy, and API host, then creates an EventSource that streams token-by-token responses to a provided EventSourceListener.
Two overloads of streamChatCompletion exist: one accepts a pre-built completion object, the other accepts a message list with model type and constructs the appropriate ChatCompletion or ChatImgCompletion object internally.
Usage
Import this class when you need to:
- Stream GPT chat completions from the OpenAI API
- Support multiple GPT models (3.5, 4, Vision) via a unified interface
- Use a reverse proxy or custom API host for OpenAI API access
Code Reference
Source Location
- Repository: GPT-WEB-JAVA
- File: src/main/java/com/intelligent/bot/service/gpt/ChatGPTStream.java
Signature
@Data @Builder @NoArgsConstructor @AllArgsConstructor
public class ChatGPTStream {
private String apiKey;
private OkHttpClient okHttpClient;
@Builder.Default private long timeout = 90;
@Builder.Default private Proxy proxy = Proxy.NO_PROXY;
@Builder.Default private String apiHost = "https://api.openai.com/";
public ChatGPTStream init();
// Generic streaming (accepts any completion object)
public void streamChatCompletion(Object chatCompletion,
EventSourceListener eventSourceListener);
// Model-aware streaming (constructs ChatCompletion/ChatImgCompletion)
public void streamChatCompletion(List<Message> messages,
EventSourceListener eventSourceListener,
Integer type, GptStreamReq req,
List<ImgMessage> imgMessages);
}
Import
import com.intelligent.bot.service.gpt.ChatGPTStream;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| apiKey | String | Yes | OpenAI API key |
| timeout | long | No | Connection timeout in seconds (default 90, set to 600 by caller) |
| proxy | Proxy | No | HTTP proxy for domestic access (default NO_PROXY) |
| apiHost | String | No | API base URL (supports reverse proxy, default api.openai.com) |
| messages | List<Message> | Yes | Chat message history |
| type | Integer | Yes | Model type: 3=GPT-3.5, 4=GPT-4, 5=Vision |
| imgMessages | List<ImgMessage> | No | Image messages for Vision |
| eventSourceListener | EventSourceListener | Yes | SSE event handler |
Outputs
| Name | Type | Description |
|---|---|---|
| (async) | SSE events | Token-by-token deltas delivered to listener via onEvent/onFailure |
Usage Examples
Basic Streaming
ChatGPTStream stream = ChatGPTStream.builder()
.timeout(600)
.apiKey("sk-...")
.proxy(proxy)
.apiHost("https://api.openai.com/")
.build()
.init();
ConsoleStreamListener listener = ConsoleStreamListener.builder()
.userId(userId)
.asyncService(asyncService)
.logId(logId)
.build();
listener.setOnComplate(msg -> asyncService.endOfAnswer(logId, msg.toString()));
stream.streamChatCompletion(messages, listener, 3, req, Collections.emptyList());
Vision Streaming
// Type 5 triggers ChatImgCompletion with image_url messages
List<ImgMessage> imgMessages = new ArrayList<>();
imgMessages.add(ImgMessage.of(imageMessages));
stream.streamChatCompletion(null, listener, 5, req, imgMessages);
Related Pages
Implements Principle
- Principle:A616567126_GPT_WEB_JAVA_SSE_Streaming
- Principle:A616567126_GPT_WEB_JAVA_OpenAI_Chat_Completions
Requires Environment
- Environment:A616567126_GPT_WEB_JAVA_Spring_Boot_Runtime
- Environment:A616567126_GPT_WEB_JAVA_OkHttp_SSE_Client