Environment:A616567126 GPT WEB JAVA OkHttp SSE Client
| Knowledge Sources | |
|---|---|
| Domains | Networking, Streaming |
| Last Updated | 2026-10-13 18:00 GMT |
Overview
OkHttp 3.14.9 with okhttp-sse for Server-Sent Events streaming to OpenAI Chat Completions API with 90-second timeout and proxy support.
Description
This environment provides the HTTP client stack for GPT streaming chat in GPT-WEB-JAVA. It uses OkHttp with the okhttp-sse extension (version 3.14.9) to establish SSE (Server-Sent Events) connections to the OpenAI Chat Completions API. The client is configured with a 90-second connect/read/write timeout to accommodate long streaming responses. It supports HTTP proxy configuration for use in regions where OpenAI API requires proxy access. The API host is configurable (default `https://api.openai.com/`), allowing reverse proxy or third-party API endpoint usage.
Usage
Use this environment for GPT streaming chat and DALL-E image generation workflows. Required by the ChatGPTStream client and all GPT-related SSE streaming implementations. Also required when proxy access to OpenAI API is needed.
System Requirements
| Category | Requirement | Notes |
|---|---|---|
| OS | Any (Java-based) | Cross-platform |
| Hardware | Minimal CPU/RAM for HTTP client | Main bottleneck is network latency |
| Network | Outbound HTTPS to OpenAI API or proxy | May require proxy in China |
Dependencies
Java Packages (Maven)
- `okhttp-sse` = 3.14.9
Credentials
Credentials are stored in the `gpt_key` database table, not in config files:
- GPT API Key: Stored in `gpt_key.key` column, filtered by type (3.5 vs 4.0)
- API Host: Configured in `sys_config` table, defaults to `https://api.openai.com/`
- Proxy IP/Port: Configured in `sys_config` table (proxyIp, proxyPort), activated when `isOpenProxy=1`
Quick Install
# OkHttp SSE dependency is declared in pom.xml:
# com.squareup.okhttp3:okhttp-sse:3.14.9
# No separate installation needed — included in Maven build
Code Evidence
OkHttp client initialization with 90s timeout from `ChatGPTStream.java:62-71`:
@Builder.Default
private long timeout = 90;
public ChatGPTStream init() {
OkHttpClient.Builder client = new OkHttpClient.Builder();
client.connectTimeout(timeout, TimeUnit.SECONDS);
client.writeTimeout(timeout, TimeUnit.SECONDS);
client.readTimeout(timeout, TimeUnit.SECONDS);
if (Objects.nonNull(proxy)) {
client.proxy(proxy);
}
okHttpClient = client.build();
return this;
}
SSE event source creation from `ChatGPTStream.java:78-92`:
public void streamChatCompletion(Object chatCompletion,
EventSourceListener eventSourceListener) {
EventSource.Factory factory = EventSources.createFactory(okHttpClient);
ObjectMapper mapper = new ObjectMapper();
String requestBody = mapper.writeValueAsString(chatCompletion);
Request request = new Request.Builder()
.url(apiHost + CommonConst.CPT_CHAT_URL)
.post(RequestBody.create(MediaType.parse(ContentType.JSON.getValue()),
requestBody))
.header(Header.AUTHORIZATION.name(), "Bearer " + key)
.build();
factory.newEventSource(request, eventSourceListener);
}
Proxy configuration at startup from `InitUtil.java:67-72`:
if(sysConfig.getIsOpenProxy() == 1){
System.setProperty("http.proxyHost", sysConfig.getProxyIp());
System.setProperty("http.proxyPort", String.valueOf(sysConfig.getProxyPort()));
System.setProperty("https.proxyHost", sysConfig.getProxyIp());
System.setProperty("https.proxyPort", String.valueOf(sysConfig.getProxyPort()));
}
Common Errors
| Error Message | Cause | Solution |
|---|---|---|
| `timeout` | OpenAI API response exceeds 90 seconds | Increase timeout value in ChatGPTStream builder |
| `401 Unauthorized` | Invalid or expired GPT API key | Check `gpt_key` table for valid keys of the correct type |
| `Connection refused` | Proxy misconfigured or OpenAI API unreachable from region | Verify proxy settings in `sys_config` or use a reverse proxy |
| `No key available in cache pool` | All GPT keys disabled or removed | Add valid keys via admin panel `gpt_key` management |
Compatibility Notes
- OkHttp 4.x: Not compatible; pom.xml pins okhttp-sse 3.14.9 which depends on OkHttp 3.x.
- Proxy: Both HTTP and HTTPS proxy system properties are set when `isOpenProxy=1`; affects all HTTP clients in JVM, not just OkHttp.
- Nginx: When deploying behind Nginx, SSE requires `proxy_buffering off` to prevent buffering of streaming responses.