Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:A616567126 GPT WEB JAVA MoonshotAiService

From Leeroopedia
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

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);

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment