Implementation:A616567126 GPT WEB JAVA GptController
| Knowledge Sources | |
|---|---|
| Domains | NLP, LLM, API_Integration |
| Last Updated | 2026-10-13 00:00 GMT |
Overview
Concrete tool for handling GPT streaming chat requests (GPT-3.5/4/Vision) and DALL-E image generation, provided by the Spring Web framework.
Description
The GptController class is the REST controller entry point for all GPT-related requests. It handles two endpoints: /gpt/chat for streaming chat completions and /gpt/official for DALL-E image generation. The chat endpoint validates the request against feature flags, performs content moderation, selects an API key, builds message history, initializes the SSE stream, and delegates to ConsoleStreamListener for real-time delivery.
Usage
Import this controller when you need to:
- Accept streaming GPT chat requests from web/mobile clients
- Support GPT-3.5, GPT-4, and GPT-4 Vision models
- Generate images via OpenAI's DALL-E API
Code Reference
Source Location
- Repository: GPT-WEB-JAVA
- File: src/main/java/com/intelligent/bot/api/gpt/GptController.java
Signature
@RestController
@RequestMapping("/gpt")
public final class GptController {
@PostMapping(value = "/chat", name = "流式对话")
public B<Long> gptChat(@Validated @RequestBody GptStreamReq req);
@PostMapping(value = "/official", name = "AI-画图")
public B<MessageLogSave> gptAlpha(@Validated @RequestBody GptAlphaReq req) throws IOException;
}
Import
import com.intelligent.bot.api.gpt.GptController;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| req (chat) | GptStreamReq | Yes | type (3/4/5), problem, logId, role, temperature, topP, fileUrl |
| req (official) | GptAlphaReq | Yes | prompt, n, size for DALL-E |
Outputs
| Name | Type | Description |
|---|---|---|
| gptChat returns | B<Long> | Wraps logId for conversation tracking; async stream initiated |
| gptAlpha returns | B<MessageLogSave> | Contains prompt, image URLs, timestamps |
Usage Examples
Streaming Chat (GPT-3.5)
// Client sends HTTP POST to /gpt/chat
// Request body:
// {"type": 3, "problem": "What is AI?", "logId": null, "temperature": 0.7, "topP": 1.0}
//
// Controller flow:
// 1. Checks SysConfig.isOpenGpt feature flag
// 2. Validates content via baiDuService.textToExamine()
// 3. Selects GPT key via InitUtil.getRandomKey(3)
// 4. Builds message history from messageLogService
// 5. Deducts credits via checkService.checkUser()
// 6. Initializes ChatGPTStream and ConsoleStreamListener
// 7. Opens SSE connection to OpenAI API
// 8. Returns logId immediately; stream delivers tokens async
GPT-4 Vision
// Request body for Vision (type=5):
// {"type": 5, "problem": "[{\"text\":\"What is in this image?\"}]", "fileUrl": "https://..."}
//
// Controller builds ImgMessage list with image_url
// and uses ChatImgCompletion instead of ChatCompletion
Related Pages
Implements Principle
- Principle:A616567126_GPT_WEB_JAVA_OpenAI_Chat_Completions
- Principle:A616567126_GPT_WEB_JAVA_SSE_Streaming
- Principle:A616567126_GPT_WEB_JAVA_Credit_Based_Access_Control
- Principle:A616567126_GPT_WEB_JAVA_Content_Moderation
Requires Environment
- Environment:A616567126_GPT_WEB_JAVA_Spring_Boot_Runtime
- Environment:A616567126_GPT_WEB_JAVA_OkHttp_SSE_Client
- Environment:A616567126_GPT_WEB_JAVA_Redis_Cache