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 SparkServiceImpl

From Leeroopedia
Knowledge Sources
Domains AI_Chat, Spark_Integration
Last Updated 2026-10-13 00:00 GMT

Overview

Concrete service for iFlytek Spark AI chat integration, handling credit validation, content moderation, message logging, and WebSocket chat session creation for Spark V2/V3/V3.5 models.

Description

The SparkServiceImpl class implements `ISparkService` to provide Spark AI chat. The `chat()` method validates Spark is enabled, runs content moderation via Baidu, creates a message log, deducts credits via `CheckService`, builds a `SparkDeskClient` with the appropriate model endpoint (V2/V3/V3.5), constructs an `AIChatRequest` with header/payload/parameter, and delegates to `AsyncService.sparkChat()` for async WebSocket execution.

Usage

Inject this service when implementing Spark AI chat functionality. The `chat()` method is the entry point for all Spark model conversations.

Code Reference

Source Location

Signature

@Service
@Transactional(rollbackFor = E.class)
public class SparkServiceImpl implements ISparkService {

    @Resource
    CheckService checkService;
    @Resource
    BaiDuService baiDuService;
    @Resource
    IMessageLogService messageLogService;
    @Resource
    AsyncService asyncService;

    @Override
    public Long chat(ChatReq req);
}

Import

import com.intelligent.bot.service.spark.impl.SparkServiceImpl;

I/O Contract

Inputs

Name Type Required Description
req ChatReq Yes Contains type (2/3/3.5), problem, logId
req.type int Yes Spark model version (2, 3, or 3.5)
req.problem String Yes │ User's question

Outputs

Name Type Description
chat returns Long Message log ID for tracking

Usage Examples

Spark Chat Flow

// POST /spark/chat
ChatReq req = new ChatReq();
req.setType(3);  // Spark V3
req.setProblem("What is machine learning?");
req.setLogId(null);  // New conversation

Long logId = sparkService.chat(req);
// Returns log ID for tracking the async response

Internal Flow

// 1. Validate Spark is enabled
if (sysConfig.getIsOpenSpark() == 0) throw new E("星火大模型对话已关闭");

// 2. Content moderation
if (!baiDuService.textToExamine(req.getProblem()))
    throw new E("提问违反相关规定");

// 3. Build Spark client with model-specific endpoint
SparkDeskClient client = SparkDeskClient.builder()
    .host(req.getType() == 2 ? SPARK_V2_1_URL :
          req.getType() == 3 ? SPARK_V3_1_URL : SPARK_V3_5_URL)
    .appid(cacheObject.getSparkAppId())
    .apiKey(cacheObject.getSparkApiKey())
    .apiSecret(cacheObject.getSparkApiSecret())
    .build();

// 4. Delegate to async service
asyncService.sparkChat(client, aiChatRequest, logId, atomicLong);

Related Pages

Page Connections

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