Implementation:A616567126 GPT WEB JAVA SparkServiceImpl
| 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
- Repository: A616567126_GPT_WEB_JAVA
- File: src/main/java/com/intelligent/bot/service/spark/impl/SparkServiceImpl.java
- Lines: 1-109
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
- Principle:A616567126_GPT_WEB_JAVA_External_AI_Integration
- Principle:A616567126_GPT_WEB_JAVA_Credit_Based_Access_Control
- Principle:A616567126_GPT_WEB_JAVA_Content_Moderation
- Environment:A616567126_GPT_WEB_JAVA_Spring_Boot_Runtime
- Environment:A616567126_GPT_WEB_JAVA_MySQL_Database
- Environment:A616567126_GPT_WEB_JAVA_Redis_Cache