feat(ai-chat): 新增豆包API + AI模型配置项支持动态切换
- 后端新增豆包(火山引擎Ark)API集成:DoubaoController、ToolDoubaoServiceImpl, 使用OkHttp3 SSE流式对话,兼容OpenAI Chat Completions格式 - 新增DoubaoConfig配置类,读取doubao.api.*配置 - 在eb_system_config表新增ai_chat_model配置项,支持doubao/coze/gemini三种模型切换 - 新增GET /api/front/doubao/ai-model-config接口供前端读取当前模型配置 - 前端ai-nutritionist.vue的sendToAI按系统配置分发到_sendViaDoubao/_sendViaCoze/_sendViaGemini - 前端models-api.js新增doubaoChatStream/doubaoChat/getAiModelConfig函数 - 附带豆包API测试脚本和数据库初始化SQL Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
package com.zbkj.front.controller;
|
||||
|
||||
import com.zbkj.common.constants.SysConfigConstants;
|
||||
import com.zbkj.common.request.doubao.DoubaoChatRequest;
|
||||
import com.zbkj.common.result.CommonResult;
|
||||
import com.zbkj.service.service.SystemConfigService;
|
||||
import com.zbkj.service.service.tool.ToolDoubaoService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 豆包(火山引擎 Ark)对话接口 + AI 模型配置
|
||||
* +----------------------------------------------------------------------
|
||||
* | Author:ScottPan
|
||||
* +----------------------------------------------------------------------
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/front/doubao")
|
||||
@Api(tags = "豆包 AI 对话")
|
||||
public class DoubaoController {
|
||||
|
||||
@Autowired
|
||||
private ToolDoubaoService toolDoubaoService;
|
||||
|
||||
@Autowired
|
||||
private SystemConfigService systemConfigService;
|
||||
|
||||
/**
|
||||
* 获取当前 AI 对话模型配置
|
||||
* 返回 { model: "doubao" | "coze" | "gemini" }
|
||||
*/
|
||||
@ApiOperation(value = "获取AI模型配置", notes = "获取当前系统配置的AI对话模型")
|
||||
@GetMapping("/ai-model-config")
|
||||
public CommonResult<Map<String, String>> getAiModelConfig() {
|
||||
String model = systemConfigService.getValueByKey(SysConfigConstants.CONFIG_KEY_AI_CHAT_MODEL);
|
||||
if (model == null || model.isEmpty()) {
|
||||
model = "doubao"; // 默认使用豆包
|
||||
}
|
||||
Map<String, String> result = new HashMap<>();
|
||||
result.put("model", model.trim().toLowerCase());
|
||||
return CommonResult.success(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 非流式对话
|
||||
*/
|
||||
@ApiOperation(value = "对话", notes = "与豆包模型进行对话")
|
||||
@PostMapping("/chat")
|
||||
public Map<String, Object> chat(@RequestBody @Validated DoubaoChatRequest request) {
|
||||
request.setStream(false);
|
||||
return toolDoubaoService.chat(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* 流式对话(SSE)
|
||||
*/
|
||||
@ApiOperation(value = "流式对话", notes = "与豆包模型进行流式对话,使用 SSE 实时推送响应")
|
||||
@PostMapping(value = "/chat/stream", produces = "text/event-stream")
|
||||
public SseEmitter chatStream(@RequestBody @Validated DoubaoChatRequest request,
|
||||
HttpServletResponse response) {
|
||||
response.setHeader("X-Accel-Buffering", "no");
|
||||
response.setHeader("Cache-Control", "no-cache");
|
||||
request.setStream(true);
|
||||
return toolDoubaoService.chatStream(request);
|
||||
}
|
||||
}
|
||||
@@ -41,7 +41,7 @@ spring:
|
||||
min-idle: 0 # 连接池中的最小空闲连接
|
||||
time-between-eviction-runs: -1 #逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1
|
||||
second:
|
||||
database: 23 # 微信accessToken存储库
|
||||
database: 3 # 微信accessToken存储库
|
||||
|
||||
|
||||
debug: true
|
||||
@@ -87,6 +87,15 @@ coze:
|
||||
nutrition-analysis-workflow-id: 1180790412263 #饮食打卡记录ai分析
|
||||
diet-analysis-id: 1180790412263
|
||||
|
||||
# 豆包(火山引擎 Ark)API 配置
|
||||
doubao:
|
||||
api:
|
||||
base-url: https://ark.cn-beijing.volces.com/api/v3
|
||||
api-key: 18480c26-ebcd-4263-8a6f-48359b8bd65d
|
||||
model: doubao-seed-2-0-pro-260215
|
||||
connect-timeout: 30000
|
||||
read-timeout: 120000
|
||||
|
||||
# 腾讯云语音识别配置
|
||||
tencent-asr:
|
||||
# API密钥ID
|
||||
|
||||
@@ -21,6 +21,7 @@ crmeb:
|
||||
- api/front/upload/imageOuter
|
||||
- api/front/coze/**
|
||||
- api/front/kieai/**
|
||||
- api/front/doubao/**
|
||||
|
||||
# 配置端口
|
||||
server:
|
||||
|
||||
Reference in New Issue
Block a user