Implementation:A616567126 GPT WEB JAVA MoonshotAiService
| Knowledge Sources | |
|---|---|
| Domains | AI_Chat, File_Processing |
| Last Updated | 2026-10-13 00:00 GMT |
Overview
Concrete service for Moonshot/Kimi AI integration providing file upload, file content extraction, token estimation, and streaming chat completion via the Moonshot API.
Description
The MoonshotAiService class integrates with the Moonshot AI API (api.moonshot.cn) for file-based AI chat. It provides methods for: uploading files, listing/deleting files, extracting file content, estimating token counts, and streaming chat completions. The `chat()` method uses OkHttp to make a streaming POST request, parsing SSE-style `data:` lines and forwarding content chunks via `SendMessageUtil` to the user's SSE/WebSocket connection.
= Usage
Inject this service when you need to upload files to Moonshot AI and have the AI analyze their content, or when performing streaming chat with the Kimi model.
Code Reference
Source Location
- Repository: A616567126_GPT_WEB_JAVA
- File: src/main/java/com/intelligent/bot/service/kimi/MoonshotAiService.java
- Lines: 1-157
Signature
@Service
public class MoonshotAiService extends EventSourceListener {
public String uploadFile(@NonNull File file, String secretKey);
public String getFileContent(@NonNull String fileId, String secretKey);
public static String getModelList(String secretKey);
public static String getFileList(String secretKey);
public static String deleteFile(@NonNull String fileId, String secretKey);
public static String estimateTokenCount(@NonNull String model, @NonNull List<Message> messages, String secretKey);
@Async
public void chat(@NonNull String model, @NonNull List<Message> messages,
String secretKey, Long logId, Long userId);
}
Import
import com.intelligent.bot.service.kimi.MoonshotAiService;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| file | File | Yes (upload) | File to upload |
| fileId | String | Yes (content/delete) | Moonshot file ID |
| model | String | Yes (chat) | Model name (e.g., "moonshot-v1-8k") |
| messages | List<Message> | Yes (chat) │ Chat messages | |
| secretKey | String | Yes | Moonshot API key |
| logId | Long | Yes (chat) | Message log ID |
| userId | Long | Yes (chat) | User ID for SSE delivery |
Outputs
| Name | Type | Description |
|---|---|---|
| uploadFile | String | JSON response with file ID |
| getFileContent | String | File content text |
| chat | void | Streams content via SendMessageUtil |
Usage Examples
Upload and Analyze File
// Upload file to Moonshot
String result = moonshotAiService.uploadFile(file, secretKey);
String fileId = JSONObject.parseObject(result).getString("id");
// Get file content
String content = moonshotAiService.getFileContent(fileId, secretKey);
// Chat with file content
List<Message> messages = List.of(
Message.ofSystem(content),
Message.ofUser("Summarize this document")
);
moonshotAiService.chat("moonshot-v1-8k", messages, secretKey, logId, userId);