mirror of
https://gitee.com/myxzgzs/boyue-vue-pro.git
synced 2025-08-08 16:32:46 +08:00
Merge branch 'feature/ai' of https://gitee.com/zhijiantianya/ruoyi-vue-pro into master-jdk17
This commit is contained in:
commit
fd2d067324
@ -61,4 +61,8 @@ public interface ErrorCodeConstants {
|
||||
ErrorCode TOOL_NOT_EXISTS = new ErrorCode(1_040_010_000, "工具不存在");
|
||||
ErrorCode TOOL_NAME_NOT_EXISTS = new ErrorCode(1_040_010_001, "工具({})找不到 Bean");
|
||||
|
||||
// ========== AI 工作流 1-040-011-000 ==========
|
||||
ErrorCode WORKFLOW_NOT_EXISTS = new ErrorCode(1_040_011_000, "工作流不存在");
|
||||
ErrorCode WORKFLOW_CODE_EXISTS = new ErrorCode(1_040_011_001, "工作流标识已存在");
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,77 @@
|
||||
package cn.iocoder.yudao.module.ai.controller.admin.workflow;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
||||
import cn.iocoder.yudao.module.ai.controller.admin.workflow.vo.*;
|
||||
import cn.iocoder.yudao.module.ai.dal.dataobject.workflow.AiWorkflowDO;
|
||||
import cn.iocoder.yudao.module.ai.service.workflow.AiWorkflowService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
|
||||
|
||||
@Tag(name = "管理后台 - AI 工作流")
|
||||
@RestController
|
||||
@RequestMapping("/ai/workflow")
|
||||
@Slf4j
|
||||
public class AiWorkflowController {
|
||||
|
||||
@Resource
|
||||
private AiWorkflowService workflowService;
|
||||
|
||||
@PostMapping("/create")
|
||||
@Operation(summary = "创建 AI 工作流")
|
||||
@PreAuthorize("@ss.hasPermission('ai:workflow:create')")
|
||||
public CommonResult<Long> createWorkflow(@Valid @RequestBody AiWorkflowSaveReqVO createReqVO) {
|
||||
return success(workflowService.createWorkflow(createReqVO));
|
||||
}
|
||||
|
||||
@PutMapping("/update")
|
||||
@Operation(summary = "更新 AI 工作流")
|
||||
@PreAuthorize("@ss.hasPermission('ai:workflow:update')")
|
||||
public CommonResult<Boolean> updateWorkflow(@Valid @RequestBody AiWorkflowSaveReqVO updateReqVO) {
|
||||
workflowService.updateWorkflow(updateReqVO);
|
||||
return success(true);
|
||||
}
|
||||
|
||||
@DeleteMapping("/delete")
|
||||
@Operation(summary = "删除 AI 工作流")
|
||||
@Parameter(name = "id", description = "编号", required = true)
|
||||
@PreAuthorize("@ss.hasPermission('ai:workflow:delete')")
|
||||
public CommonResult<Boolean> deleteWorkflow(@RequestParam("id") Long id) {
|
||||
workflowService.deleteWorkflow(id);
|
||||
return success(true);
|
||||
}
|
||||
|
||||
@GetMapping("/get")
|
||||
@Operation(summary = "获得 AI 工作流")
|
||||
@Parameter(name = "id", description = "编号", required = true, example = "1024")
|
||||
@PreAuthorize("@ss.hasPermission('ai:workflow:query')")
|
||||
public CommonResult<AiWorkflowRespVO> getWorkflow(@RequestParam("id") Long id) {
|
||||
AiWorkflowDO workflow = workflowService.getWorkflow(id);
|
||||
return success(BeanUtils.toBean(workflow, AiWorkflowRespVO.class));
|
||||
}
|
||||
|
||||
@GetMapping("/page")
|
||||
@Operation(summary = "获得 AI 工作流分页")
|
||||
@PreAuthorize("@ss.hasPermission('ai:workflow:query')")
|
||||
public CommonResult<PageResult<AiWorkflowRespVO>> getWorkflowPage(@Valid AiWorkflowPageReqVO pageReqVO) {
|
||||
PageResult<AiWorkflowDO> pageResult = workflowService.getWorkflowPage(pageReqVO);
|
||||
return success(BeanUtils.toBean(pageResult, AiWorkflowRespVO.class));
|
||||
}
|
||||
|
||||
@PostMapping("/test")
|
||||
@Operation(summary = "测试 AI 工作流")
|
||||
@PreAuthorize("@ss.hasPermission('ai:workflow:test')")
|
||||
public CommonResult<Object> testWorkflow(@Valid @RequestBody AiWorkflowTestReqVO testReqVO) {
|
||||
return success(workflowService.testWorkflow(testReqVO));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package cn.iocoder.yudao.module.ai.controller.admin.workflow.vo;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageParam;
|
||||
import cn.iocoder.yudao.framework.common.validation.InEnum;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
|
||||
|
||||
@Schema(description = "管理后台 - AI 工作流分页 Request VO")
|
||||
@Data
|
||||
public class AiWorkflowPageReqVO extends PageParam {
|
||||
|
||||
@Schema(description = "名称", example = "工作流")
|
||||
private String name;
|
||||
|
||||
@Schema(description = "标识", example = "FLOW")
|
||||
private String code;
|
||||
|
||||
@Schema(description = "状态", example = "1")
|
||||
@InEnum(CommonStatusEnum.class)
|
||||
private Integer status;
|
||||
|
||||
@Schema(description = "创建时间")
|
||||
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
|
||||
private LocalDateTime[] createTime;
|
||||
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package cn.iocoder.yudao.module.ai.controller.admin.workflow.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Schema(description = "管理后台 - AI 工作流 Response VO")
|
||||
@Data
|
||||
public class AiWorkflowRespVO {
|
||||
|
||||
@Schema(description = "编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
|
||||
private Long id;
|
||||
|
||||
@Schema(description = "工作流标识", requiredMode = Schema.RequiredMode.REQUIRED, example = "FLOW")
|
||||
private String code;
|
||||
|
||||
@Schema(description = "工作流名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "工作流")
|
||||
private String name;
|
||||
|
||||
@Schema(description = "备注", requiredMode = Schema.RequiredMode.REQUIRED, example = "工作流")
|
||||
private String remark;
|
||||
|
||||
@Schema(description = "状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
|
||||
private Integer status;
|
||||
|
||||
@Schema(description = "工作流模型 JSON", requiredMode = Schema.RequiredMode.REQUIRED, example = "{}")
|
||||
private String graph;
|
||||
|
||||
@Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED, example = "时间戳格式")
|
||||
private LocalDateTime createTime;
|
||||
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package cn.iocoder.yudao.module.ai.controller.admin.workflow.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.validation.constraints.NotEmpty;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.Data;
|
||||
|
||||
@Schema(description = "管理后台 - AI 工作流新增/修改 Request VO")
|
||||
@Data
|
||||
public class AiWorkflowSaveReqVO {
|
||||
|
||||
@Schema(description = "编号", example = "1")
|
||||
private Long id;
|
||||
|
||||
@Schema(description = "工作流标识", requiredMode = Schema.RequiredMode.REQUIRED, example = "FLOW")
|
||||
@NotEmpty(message = "工作流标识不能为空")
|
||||
private String code;
|
||||
|
||||
@Schema(description = "工作流名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "工作流")
|
||||
@NotEmpty(message = "工作流名称不能为空")
|
||||
private String name;
|
||||
|
||||
@Schema(description = "备注", example = "FLOW")
|
||||
private String remark;
|
||||
|
||||
@Schema(description = "工作流模型", requiredMode = Schema.RequiredMode.REQUIRED, example = "{}")
|
||||
@NotEmpty(message = "工作流模型不能为空")
|
||||
private String graph;
|
||||
|
||||
@Schema(description = "状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "FLOW")
|
||||
@NotNull(message = "状态不能为空")
|
||||
private Integer status;
|
||||
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package cn.iocoder.yudao.module.ai.controller.admin.workflow.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.validation.constraints.NotEmpty;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Schema(description = "管理后台 - AI 工作流测试 Request VO")
|
||||
@Data
|
||||
public class AiWorkflowTestReqVO {
|
||||
|
||||
@Schema(description = "工作流模型", requiredMode = Schema.RequiredMode.REQUIRED, example = "{}")
|
||||
@NotEmpty(message = "工作流模型不能为空")
|
||||
private String graph;
|
||||
|
||||
@Schema(description = "参数", requiredMode = Schema.RequiredMode.REQUIRED, example = "{}")
|
||||
private Map<String, Object> params;
|
||||
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
package cn.iocoder.yudao.module.ai.dal.dataobject.workflow;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
||||
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
|
||||
import com.baomidou.mybatisplus.annotation.KeySequence;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* AI 工作流 DO
|
||||
*
|
||||
* @author lesan
|
||||
*/
|
||||
@TableName(value = "ai_workflow", autoResultMap = true)
|
||||
@KeySequence("ai_workflow") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库,可不写。
|
||||
@Data
|
||||
public class AiWorkflowDO extends BaseDO {
|
||||
|
||||
/**
|
||||
* 编号
|
||||
*/
|
||||
@TableId
|
||||
private Long id;
|
||||
/**
|
||||
* 工作流名称
|
||||
*/
|
||||
private String name;
|
||||
/**
|
||||
* 工作流标识
|
||||
*/
|
||||
private String code;
|
||||
|
||||
/**
|
||||
* 工作流模型 JSON 数据
|
||||
*/
|
||||
private String graph;
|
||||
|
||||
/**
|
||||
* 备注
|
||||
*/
|
||||
private String remark;
|
||||
|
||||
/**
|
||||
* 状态
|
||||
*
|
||||
* 枚举 {@link CommonStatusEnum}
|
||||
*/
|
||||
private Integer status;
|
||||
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package cn.iocoder.yudao.module.ai.dal.mysql.workflow;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
|
||||
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
|
||||
import cn.iocoder.yudao.module.ai.controller.admin.workflow.vo.AiWorkflowPageReqVO;
|
||||
import cn.iocoder.yudao.module.ai.dal.dataobject.workflow.AiWorkflowDO;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* AI 工作流 Mapper
|
||||
*
|
||||
* @author lesan
|
||||
*/
|
||||
@Mapper
|
||||
public interface AiWorkflowMapper extends BaseMapperX<AiWorkflowDO> {
|
||||
|
||||
default AiWorkflowDO selectByCode(String code) {
|
||||
return selectOne(AiWorkflowDO::getCode, code);
|
||||
}
|
||||
|
||||
default PageResult<AiWorkflowDO> selectPage(AiWorkflowPageReqVO pageReqVO) {
|
||||
return selectPage(pageReqVO, new LambdaQueryWrapperX<AiWorkflowDO>()
|
||||
.eqIfPresent(AiWorkflowDO::getStatus, pageReqVO.getStatus())
|
||||
.likeIfPresent(AiWorkflowDO::getName, pageReqVO.getName())
|
||||
.likeIfPresent(AiWorkflowDO::getCode, pageReqVO.getCode())
|
||||
.betweenIfPresent(AiWorkflowDO::getCreateTime, pageReqVO.getCreateTime()));
|
||||
}
|
||||
|
||||
}
|
@ -11,6 +11,7 @@ import cn.hutool.extra.spring.SpringUtil;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import cn.iocoder.yudao.framework.ai.core.enums.AiPlatformEnum;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.midjourney.api.MidjourneyApi;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.siliconflow.SiliconFlowImageOptions;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
||||
import cn.iocoder.yudao.module.ai.controller.admin.image.vo.AiImageDrawReqVO;
|
||||
@ -144,6 +145,11 @@ public class AiImageServiceImpl implements AiImageService {
|
||||
.withStyle(MapUtil.getStr(draw.getOptions(), "style")) // 风格
|
||||
.withResponseFormat("b64_json")
|
||||
.build();
|
||||
} else if (ObjUtil.equal(model.getPlatform(), AiPlatformEnum.SILICON_FLOW.getPlatform())) {
|
||||
// https://docs.siliconflow.cn/cn/api-reference/images/images-generations
|
||||
return SiliconFlowImageOptions.builder().model(model.getModel())
|
||||
.height(draw.getHeight()).width(draw.getWidth())
|
||||
.build();
|
||||
} else if (ObjUtil.equal(model.getPlatform(), AiPlatformEnum.STABLE_DIFFUSION.getPlatform())) {
|
||||
// https://platform.stability.ai/docs/api-reference#tag/SDXL-and-SD1.6/operation/textToImage
|
||||
// https://platform.stability.ai/docs/api-reference#tag/Text-to-Image/operation/textToImage
|
||||
|
@ -0,0 +1,62 @@
|
||||
package cn.iocoder.yudao.module.ai.service.workflow;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.module.ai.controller.admin.workflow.vo.AiWorkflowPageReqVO;
|
||||
import cn.iocoder.yudao.module.ai.controller.admin.workflow.vo.AiWorkflowSaveReqVO;
|
||||
import cn.iocoder.yudao.module.ai.controller.admin.workflow.vo.AiWorkflowTestReqVO;
|
||||
import cn.iocoder.yudao.module.ai.dal.dataobject.workflow.AiWorkflowDO;
|
||||
import jakarta.validation.Valid;
|
||||
|
||||
/**
|
||||
* AI 工作流 Service 接口
|
||||
*
|
||||
* @author lesan
|
||||
*/
|
||||
public interface AiWorkflowService {
|
||||
|
||||
/**
|
||||
* 创建 AI 工作流
|
||||
*
|
||||
* @param createReqVO 创建信息
|
||||
* @return 编号
|
||||
*/
|
||||
Long createWorkflow(@Valid AiWorkflowSaveReqVO createReqVO);
|
||||
|
||||
/**
|
||||
* 更新 AI 工作流
|
||||
*
|
||||
* @param updateReqVO 更新信息
|
||||
*/
|
||||
void updateWorkflow(@Valid AiWorkflowSaveReqVO updateReqVO);
|
||||
|
||||
/**
|
||||
* 删除 AI 工作流
|
||||
*
|
||||
* @param id 编号
|
||||
*/
|
||||
void deleteWorkflow(Long id);
|
||||
|
||||
/**
|
||||
* 获得 AI 工作流
|
||||
*
|
||||
* @param id 编号
|
||||
* @return AI 工作流
|
||||
*/
|
||||
AiWorkflowDO getWorkflow(Long id);
|
||||
|
||||
/**
|
||||
* 获得 AI 工作流分页
|
||||
*
|
||||
* @param pageReqVO 分页查询
|
||||
* @return AI 工作流分页
|
||||
*/
|
||||
PageResult<AiWorkflowDO> getWorkflowPage(AiWorkflowPageReqVO pageReqVO);
|
||||
|
||||
/**
|
||||
* 测试 AI 工作流
|
||||
*
|
||||
* @param testReqVO 测试数据
|
||||
*/
|
||||
Object testWorkflow(AiWorkflowTestReqVO testReqVO);
|
||||
|
||||
}
|
@ -0,0 +1,150 @@
|
||||
package cn.iocoder.yudao.module.ai.service.workflow;
|
||||
|
||||
import cn.hutool.core.util.ObjUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
||||
import cn.iocoder.yudao.module.ai.controller.admin.workflow.vo.AiWorkflowPageReqVO;
|
||||
import cn.iocoder.yudao.module.ai.controller.admin.workflow.vo.AiWorkflowSaveReqVO;
|
||||
import cn.iocoder.yudao.module.ai.controller.admin.workflow.vo.AiWorkflowTestReqVO;
|
||||
import cn.iocoder.yudao.module.ai.dal.dataobject.model.AiApiKeyDO;
|
||||
import cn.iocoder.yudao.module.ai.dal.dataobject.workflow.AiWorkflowDO;
|
||||
import cn.iocoder.yudao.module.ai.dal.mysql.workflow.AiWorkflowMapper;
|
||||
import cn.iocoder.yudao.module.ai.service.model.AiApiKeyService;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import dev.tinyflow.core.Tinyflow;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
import static cn.iocoder.yudao.module.ai.enums.ErrorCodeConstants.WORKFLOW_CODE_EXISTS;
|
||||
import static cn.iocoder.yudao.module.ai.enums.ErrorCodeConstants.WORKFLOW_NOT_EXISTS;
|
||||
|
||||
/**
|
||||
* AI 工作流 Service 实现类
|
||||
*
|
||||
* @author lesan
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
public class AiWorkflowServiceImpl implements AiWorkflowService {
|
||||
|
||||
@Resource
|
||||
private AiWorkflowMapper workflowMapper;
|
||||
|
||||
@Resource
|
||||
private AiApiKeyService apiKeyService;
|
||||
|
||||
@Override
|
||||
public Long createWorkflow(AiWorkflowSaveReqVO createReqVO) {
|
||||
validateWorkflowForCreateOrUpdate(null, createReqVO.getCode());
|
||||
AiWorkflowDO workflow = BeanUtils.toBean(createReqVO, AiWorkflowDO.class);
|
||||
workflowMapper.insert(workflow);
|
||||
return workflow.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateWorkflow(AiWorkflowSaveReqVO updateReqVO) {
|
||||
validateWorkflowForCreateOrUpdate(updateReqVO.getId(), updateReqVO.getCode());
|
||||
AiWorkflowDO workflow = BeanUtils.toBean(updateReqVO, AiWorkflowDO.class);
|
||||
workflowMapper.updateById(workflow);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteWorkflow(Long id) {
|
||||
validateWorkflowExists(id);
|
||||
workflowMapper.deleteById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AiWorkflowDO getWorkflow(Long id) {
|
||||
return workflowMapper.selectById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageResult<AiWorkflowDO> getWorkflowPage(AiWorkflowPageReqVO pageReqVO) {
|
||||
return workflowMapper.selectPage(pageReqVO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object testWorkflow(AiWorkflowTestReqVO testReqVO) {
|
||||
Map<String, Object> variables = testReqVO.getParams();
|
||||
Tinyflow tinyflow = parseFlowParam(testReqVO.getGraph());
|
||||
return tinyflow.toChain().executeForResult(variables);
|
||||
}
|
||||
|
||||
private void validateWorkflowForCreateOrUpdate(Long id, String code) {
|
||||
validateWorkflowExists(id);
|
||||
validateCodeUnique(id, code);
|
||||
}
|
||||
|
||||
private void validateWorkflowExists(Long id) {
|
||||
if (ObjUtil.isNull(id)) {
|
||||
return;
|
||||
}
|
||||
AiWorkflowDO workflow = workflowMapper.selectById(id);
|
||||
if (ObjUtil.isNull(workflow)) {
|
||||
throw exception(WORKFLOW_NOT_EXISTS);
|
||||
}
|
||||
}
|
||||
|
||||
private void validateCodeUnique(Long id, String code) {
|
||||
if (StrUtil.isBlank(code)) {
|
||||
return;
|
||||
}
|
||||
AiWorkflowDO workflow = workflowMapper.selectByCode(code);
|
||||
if (ObjUtil.isNull(workflow)) {
|
||||
return;
|
||||
}
|
||||
if (ObjUtil.isNull(id)) {
|
||||
throw exception(WORKFLOW_CODE_EXISTS);
|
||||
}
|
||||
if (ObjUtil.notEqual(workflow.getId(), id)) {
|
||||
throw exception(WORKFLOW_CODE_EXISTS);
|
||||
}
|
||||
}
|
||||
|
||||
private Tinyflow parseFlowParam(String graph) {
|
||||
// TODO @lesan:可以使用 jackson 哇?
|
||||
JSONObject json = JSONObject.parseObject(graph);
|
||||
JSONArray nodeArr = json.getJSONArray("nodes");
|
||||
Tinyflow tinyflow = new Tinyflow(json.toJSONString());
|
||||
for (int i = 0; i < nodeArr.size(); i++) {
|
||||
JSONObject node = nodeArr.getJSONObject(i);
|
||||
switch (node.getString("type")) {
|
||||
case "llmNode":
|
||||
JSONObject data = node.getJSONObject("data");
|
||||
AiApiKeyDO apiKey = apiKeyService.getApiKey(data.getLong("llmId"));
|
||||
switch (apiKey.getPlatform()) {
|
||||
// TODO @lesan 需要讨论一下这里怎么弄
|
||||
// TODO @lesan llmId 对应 model 的编号如何?这样的话,就是 apiModelService 提供一个获取 LLM 的方法。然后,创建的方法,也在 AiModelFactory 提供。可以先接个 deepseek 先。deepseek yyds!
|
||||
case "OpenAI":
|
||||
break;
|
||||
case "Ollama":
|
||||
break;
|
||||
case "YiYan":
|
||||
break;
|
||||
case "XingHuo":
|
||||
break;
|
||||
case "TongYi":
|
||||
break;
|
||||
case "DeepSeek":
|
||||
break;
|
||||
case "ZhiPu":
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case "internalNode":
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
return tinyflow;
|
||||
}
|
||||
|
||||
}
|
@ -15,6 +15,7 @@
|
||||
<description>AI 大模型拓展,接入国内外大模型</description>
|
||||
<properties>
|
||||
<spring-ai.version>1.0.0-M6</spring-ai.version>
|
||||
<tinyflow.version>1.0.0-rc.3</tinyflow.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
@ -117,6 +118,13 @@
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<!-- TinyFlow:AI 工作流 -->
|
||||
<dependency>
|
||||
<groupId>dev.tinyflow</groupId>
|
||||
<artifactId>tinyflow-java-core</artifactId>
|
||||
<version>${tinyflow.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Test 测试相关 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
@ -4,10 +4,12 @@ import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import cn.iocoder.yudao.framework.ai.core.factory.AiModelFactory;
|
||||
import cn.iocoder.yudao.framework.ai.core.factory.AiModelFactoryImpl;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.baichuan.BaiChuanChatModel;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.deepseek.DeepSeekChatModel;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.doubao.DouBaoChatModel;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.hunyuan.HunYuanChatModel;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.midjourney.api.MidjourneyApi;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.siliconflow.SiliconFlowApiConstants;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.siliconflow.SiliconFlowChatModel;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.suno.api.SunoApi;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.xinghuo.XingHuoChatModel;
|
||||
@ -113,11 +115,11 @@ public class YudaoAiAutoConfiguration {
|
||||
|
||||
public SiliconFlowChatModel buildSiliconFlowChatClient(YudaoAiProperties.SiliconFlowProperties properties) {
|
||||
if (StrUtil.isEmpty(properties.getModel())) {
|
||||
properties.setModel(SiliconFlowChatModel.MODEL_DEFAULT);
|
||||
properties.setModel(SiliconFlowApiConstants.MODEL_DEFAULT);
|
||||
}
|
||||
OpenAiChatModel openAiChatModel = OpenAiChatModel.builder()
|
||||
.openAiApi(OpenAiApi.builder()
|
||||
.baseUrl(SiliconFlowChatModel.BASE_URL)
|
||||
.baseUrl(SiliconFlowApiConstants.DEFAULT_BASE_URL)
|
||||
.apiKey(properties.getApiKey())
|
||||
.build())
|
||||
.defaultOptions(OpenAiChatOptions.builder()
|
||||
@ -192,6 +194,33 @@ public class YudaoAiAutoConfiguration {
|
||||
return new XingHuoChatModel(openAiChatModel);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnProperty(value = "yudao.ai.baichuan.enable", havingValue = "true")
|
||||
public BaiChuanChatModel baiChuanChatClient(YudaoAiProperties yudaoAiProperties) {
|
||||
YudaoAiProperties.BaiChuanProperties properties = yudaoAiProperties.getBaichuan();
|
||||
return buildBaiChuanChatClient(properties);
|
||||
}
|
||||
|
||||
public BaiChuanChatModel buildBaiChuanChatClient(YudaoAiProperties.BaiChuanProperties properties) {
|
||||
if (StrUtil.isEmpty(properties.getModel())) {
|
||||
properties.setModel(BaiChuanChatModel.MODEL_DEFAULT);
|
||||
}
|
||||
OpenAiChatModel openAiChatModel = OpenAiChatModel.builder()
|
||||
.openAiApi(OpenAiApi.builder()
|
||||
.baseUrl(BaiChuanChatModel.BASE_URL)
|
||||
.apiKey(properties.getApiKey())
|
||||
.build())
|
||||
.defaultOptions(OpenAiChatOptions.builder()
|
||||
.model(properties.getModel())
|
||||
.temperature(properties.getTemperature())
|
||||
.maxTokens(properties.getMaxTokens())
|
||||
.topP(properties.getTopP())
|
||||
.build())
|
||||
.toolCallingManager(getToolCallingManager())
|
||||
.build();
|
||||
return new BaiChuanChatModel(openAiChatModel);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnProperty(value = "yudao.ai.midjourney.enable", havingValue = "true")
|
||||
public MidjourneyApi midjourneyApi(YudaoAiProperties yudaoAiProperties) {
|
||||
|
@ -43,6 +43,12 @@ public class YudaoAiProperties {
|
||||
@SuppressWarnings("SpellCheckingInspection")
|
||||
private XingHuoProperties xinghuo;
|
||||
|
||||
/**
|
||||
* 百川
|
||||
*/
|
||||
@SuppressWarnings("SpellCheckingInspection")
|
||||
private BaiChuanProperties baichuan;
|
||||
|
||||
/**
|
||||
* Midjourney 绘图
|
||||
*/
|
||||
@ -122,6 +128,19 @@ public class YudaoAiProperties {
|
||||
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class BaiChuanProperties {
|
||||
|
||||
private String enable;
|
||||
private String apiKey;
|
||||
|
||||
private String model;
|
||||
private Double temperature;
|
||||
private Integer maxTokens;
|
||||
private Double topP;
|
||||
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class MidjourneyProperties {
|
||||
|
||||
|
@ -27,6 +27,7 @@ public enum AiPlatformEnum implements ArrayValuable<String> {
|
||||
SILICON_FLOW("SiliconFlow", "硅基流动"), // 硅基流动
|
||||
MINI_MAX("MiniMax", "MiniMax"), // 稀宇科技
|
||||
MOONSHOT("Moonshot", "月之暗灭"), // KIMI
|
||||
BAI_CHUAN("BaiChuan", "百川智能"), // 百川智能
|
||||
|
||||
// ========== 国外平台 ==========
|
||||
|
||||
|
@ -11,11 +11,15 @@ import cn.hutool.extra.spring.SpringUtil;
|
||||
import cn.iocoder.yudao.framework.ai.config.YudaoAiAutoConfiguration;
|
||||
import cn.iocoder.yudao.framework.ai.config.YudaoAiProperties;
|
||||
import cn.iocoder.yudao.framework.ai.core.enums.AiPlatformEnum;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.baichuan.BaiChuanChatModel;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.deepseek.DeepSeekChatModel;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.doubao.DouBaoChatModel;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.hunyuan.HunYuanChatModel;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.midjourney.api.MidjourneyApi;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.siliconflow.SiliconFlowApiConstants;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.siliconflow.SiliconFlowChatModel;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.siliconflow.SiliconFlowImageApi;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.siliconflow.SiliconFlowImageModel;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.suno.api.SunoApi;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.xinghuo.XingHuoChatModel;
|
||||
import cn.iocoder.yudao.framework.common.util.spring.SpringUtils;
|
||||
@ -42,6 +46,7 @@ import org.springframework.ai.autoconfigure.moonshot.MoonshotAutoConfiguration;
|
||||
import org.springframework.ai.autoconfigure.ollama.OllamaAutoConfiguration;
|
||||
import org.springframework.ai.autoconfigure.openai.OpenAiAutoConfiguration;
|
||||
import org.springframework.ai.autoconfigure.qianfan.QianFanAutoConfiguration;
|
||||
import org.springframework.ai.autoconfigure.stabilityai.StabilityAiImageAutoConfiguration;
|
||||
import org.springframework.ai.autoconfigure.vectorstore.milvus.MilvusServiceClientConnectionDetails;
|
||||
import org.springframework.ai.autoconfigure.vectorstore.milvus.MilvusServiceClientProperties;
|
||||
import org.springframework.ai.autoconfigure.vectorstore.milvus.MilvusVectorStoreAutoConfiguration;
|
||||
@ -146,6 +151,8 @@ public class AiModelFactoryImpl implements AiModelFactory {
|
||||
return buildMoonshotChatModel(apiKey, url);
|
||||
case XING_HUO:
|
||||
return buildXingHuoChatModel(apiKey);
|
||||
case BAI_CHUAN:
|
||||
return buildBaiChuanChatModel(apiKey);
|
||||
case OPENAI:
|
||||
return buildOpenAiChatModel(apiKey, url);
|
||||
case AZURE_OPENAI:
|
||||
@ -182,6 +189,8 @@ public class AiModelFactoryImpl implements AiModelFactory {
|
||||
return SpringUtil.getBean(MoonshotChatModel.class);
|
||||
case XING_HUO:
|
||||
return SpringUtil.getBean(XingHuoChatModel.class);
|
||||
case BAI_CHUAN:
|
||||
return SpringUtil.getBean(AzureOpenAiChatModel.class);
|
||||
case OPENAI:
|
||||
return SpringUtil.getBean(OpenAiChatModel.class);
|
||||
case AZURE_OPENAI:
|
||||
@ -203,6 +212,8 @@ public class AiModelFactoryImpl implements AiModelFactory {
|
||||
return SpringUtil.getBean(QianFanImageModel.class);
|
||||
case ZHI_PU:
|
||||
return SpringUtil.getBean(ZhiPuAiImageModel.class);
|
||||
case SILICON_FLOW:
|
||||
return SpringUtil.getBean(SiliconFlowImageModel.class);
|
||||
case OPENAI:
|
||||
return SpringUtil.getBean(OpenAiImageModel.class);
|
||||
case STABLE_DIFFUSION:
|
||||
@ -224,6 +235,8 @@ public class AiModelFactoryImpl implements AiModelFactory {
|
||||
return buildZhiPuAiImageModel(apiKey, url);
|
||||
case OPENAI:
|
||||
return buildOpenAiImageModel(apiKey, url);
|
||||
case SILICON_FLOW:
|
||||
return buildSiliconFlowImageModel(apiKey,url);
|
||||
case STABLE_DIFFUSION:
|
||||
return buildStabilityAiImageModel(apiKey, url);
|
||||
default:
|
||||
@ -433,6 +446,15 @@ public class AiModelFactoryImpl implements AiModelFactory {
|
||||
return new YudaoAiAutoConfiguration().buildXingHuoChatClient(properties);
|
||||
}
|
||||
|
||||
/**
|
||||
* 可参考 {@link YudaoAiAutoConfiguration#baiChuanChatClient(YudaoAiProperties)}
|
||||
*/
|
||||
private BaiChuanChatModel buildBaiChuanChatModel(String apiKey) {
|
||||
YudaoAiProperties.BaiChuanProperties properties = new YudaoAiProperties.BaiChuanProperties()
|
||||
.setApiKey(apiKey);
|
||||
return new YudaoAiAutoConfiguration().buildBaiChuanChatClient(properties);
|
||||
}
|
||||
|
||||
/**
|
||||
* 可参考 {@link OpenAiAutoConfiguration} 的 openAiChatModel 方法
|
||||
*/
|
||||
@ -468,6 +490,15 @@ public class AiModelFactoryImpl implements AiModelFactory {
|
||||
return new OpenAiImageModel(openAiApi);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建 SiliconFlowImageModel 对象
|
||||
*/
|
||||
private SiliconFlowImageModel buildSiliconFlowImageModel(String apiToken, String url) {
|
||||
url = StrUtil.blankToDefault(url, SiliconFlowApiConstants.DEFAULT_BASE_URL);
|
||||
SiliconFlowImageApi openAiApi = new SiliconFlowImageApi(url, apiToken);
|
||||
return new SiliconFlowImageModel(openAiApi);
|
||||
}
|
||||
|
||||
/**
|
||||
* 可参考 {@link OllamaAutoConfiguration} 的 ollamaApi 方法
|
||||
*/
|
||||
@ -476,6 +507,9 @@ public class AiModelFactoryImpl implements AiModelFactory {
|
||||
return OllamaChatModel.builder().ollamaApi(ollamaApi).toolCallingManager(getToolCallingManager()).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 可参考 {@link StabilityAiImageAutoConfiguration} 的 stabilityAiImageModel 方法
|
||||
*/
|
||||
private StabilityAiImageModel buildStabilityAiImageModel(String apiKey, String url) {
|
||||
url = StrUtil.blankToDefault(url, StabilityAiApi.DEFAULT_BASE_URL);
|
||||
StabilityAiApi stabilityAiApi = new StabilityAiApi(apiKey, StabilityAiApi.DEFAULT_IMAGE_MODEL, url);
|
||||
|
@ -0,0 +1,45 @@
|
||||
package cn.iocoder.yudao.framework.ai.core.model.baichuan;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.ai.chat.model.ChatModel;
|
||||
import org.springframework.ai.chat.model.ChatResponse;
|
||||
import org.springframework.ai.chat.prompt.ChatOptions;
|
||||
import org.springframework.ai.chat.prompt.Prompt;
|
||||
import org.springframework.ai.openai.OpenAiChatModel;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
/**
|
||||
* 百川 {@link ChatModel} 实现类
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class BaiChuanChatModel implements ChatModel {
|
||||
|
||||
public static final String BASE_URL = "https://api.baichuan-ai.com";
|
||||
|
||||
public static final String MODEL_DEFAULT = "Baichuan4-Turbo";
|
||||
|
||||
/**
|
||||
* 兼容 OpenAI 接口,进行复用
|
||||
*/
|
||||
private final OpenAiChatModel openAiChatModel;
|
||||
|
||||
@Override
|
||||
public ChatResponse call(Prompt prompt) {
|
||||
return openAiChatModel.call(prompt);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<ChatResponse> stream(Prompt prompt) {
|
||||
return openAiChatModel.stream(prompt);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatOptions getDefaultOptions() {
|
||||
return openAiChatModel.getDefaultOptions();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright 2023-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package cn.iocoder.yudao.framework.ai.core.model.siliconflow;
|
||||
|
||||
/**
|
||||
* SiliconFlow API 枚举类
|
||||
*
|
||||
* @author zzt
|
||||
*/
|
||||
public final class SiliconFlowApiConstants {
|
||||
|
||||
public static final String DEFAULT_BASE_URL = "https://api.siliconflow.cn";
|
||||
|
||||
public static final String MODEL_DEFAULT = "deepseek-ai/DeepSeek-R1-Distill-Qwen-7B";
|
||||
|
||||
public static final String DEFAULT_IMAGE_MODEL = "Kwai-Kolors/Kolors";
|
||||
|
||||
public static final String PROVIDER_NAME = "Siiconflow";
|
||||
|
||||
}
|
@ -20,10 +20,6 @@ import reactor.core.publisher.Flux;
|
||||
@RequiredArgsConstructor
|
||||
public class SiliconFlowChatModel implements ChatModel {
|
||||
|
||||
public static final String BASE_URL = "https://api.siliconflow.cn";
|
||||
|
||||
public static final String MODEL_DEFAULT = "deepseek-ai/DeepSeek-R1-Distill-Qwen-7B";
|
||||
|
||||
/**
|
||||
* 兼容 OpenAI 接口,进行复用
|
||||
*/
|
||||
|
@ -0,0 +1,115 @@
|
||||
/*
|
||||
* Copyright 2023-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package cn.iocoder.yudao.framework.ai.core.model.siliconflow;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import org.springframework.ai.model.ApiKey;
|
||||
import org.springframework.ai.model.NoopApiKey;
|
||||
import org.springframework.ai.model.SimpleApiKey;
|
||||
import org.springframework.ai.openai.api.OpenAiImageApi;
|
||||
import org.springframework.ai.retry.RetryUtils;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.client.ResponseErrorHandler;
|
||||
import org.springframework.web.client.RestClient;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 硅基流动 Image API
|
||||
*
|
||||
* @see <a href= "https://docs.siliconflow.cn/cn/api-reference/images/images-generations">Images</a>
|
||||
*
|
||||
* @author zzt
|
||||
*/
|
||||
public class SiliconFlowImageApi {
|
||||
|
||||
private final RestClient restClient;
|
||||
|
||||
public SiliconFlowImageApi(String aiToken) {
|
||||
this(SiliconFlowApiConstants.DEFAULT_BASE_URL, aiToken, RestClient.builder());
|
||||
}
|
||||
|
||||
public SiliconFlowImageApi(String baseUrl, String openAiToken) {
|
||||
this(baseUrl, openAiToken, RestClient.builder());
|
||||
}
|
||||
|
||||
public SiliconFlowImageApi(String baseUrl, String openAiToken, RestClient.Builder restClientBuilder) {
|
||||
this(baseUrl, openAiToken, restClientBuilder, RetryUtils.DEFAULT_RESPONSE_ERROR_HANDLER);
|
||||
}
|
||||
|
||||
public SiliconFlowImageApi(String baseUrl, String apiKey, RestClient.Builder restClientBuilder,
|
||||
ResponseErrorHandler responseErrorHandler) {
|
||||
this(baseUrl, apiKey, CollectionUtils.toMultiValueMap(Map.of()), restClientBuilder, responseErrorHandler);
|
||||
}
|
||||
|
||||
public SiliconFlowImageApi(String baseUrl, String apiKey, MultiValueMap<String, String> headers,
|
||||
RestClient.Builder restClientBuilder, ResponseErrorHandler responseErrorHandler) {
|
||||
this(baseUrl, new SimpleApiKey(apiKey), headers, restClientBuilder, responseErrorHandler);
|
||||
}
|
||||
|
||||
public SiliconFlowImageApi(String baseUrl, ApiKey apiKey, MultiValueMap<String, String> headers,
|
||||
RestClient.Builder restClientBuilder, ResponseErrorHandler responseErrorHandler) {
|
||||
|
||||
// @formatter:off
|
||||
this.restClient = restClientBuilder.baseUrl(baseUrl)
|
||||
.defaultHeaders(h -> {
|
||||
if(!(apiKey instanceof NoopApiKey)) {
|
||||
h.setBearerAuth(apiKey.getValue());
|
||||
}
|
||||
h.setContentType(MediaType.APPLICATION_JSON);
|
||||
h.addAll(headers);
|
||||
})
|
||||
.defaultStatusHandler(responseErrorHandler)
|
||||
.build();
|
||||
// @formatter:on
|
||||
}
|
||||
|
||||
public ResponseEntity<OpenAiImageApi.OpenAiImageResponse> createImage(SiliconflowImageRequest siliconflowImageRequest) {
|
||||
Assert.notNull(siliconflowImageRequest, "Image request cannot be null.");
|
||||
Assert.hasLength(siliconflowImageRequest.prompt(), "Prompt cannot be empty.");
|
||||
|
||||
return this.restClient.post()
|
||||
.uri("v1/images/generations")
|
||||
.body(siliconflowImageRequest)
|
||||
.retrieve()
|
||||
.toEntity(OpenAiImageApi.OpenAiImageResponse.class);
|
||||
}
|
||||
|
||||
|
||||
// @formatter:off
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
public record SiliconflowImageRequest (
|
||||
@JsonProperty("prompt") String prompt,
|
||||
@JsonProperty("model") String model,
|
||||
@JsonProperty("batch_size") Integer batchSize,
|
||||
@JsonProperty("negative_prompt") String negativePrompt,
|
||||
@JsonProperty("seed") Integer seed,
|
||||
@JsonProperty("num_inference_steps") Integer numInferenceSteps,
|
||||
@JsonProperty("guidance_scale") Float guidanceScale,
|
||||
@JsonProperty("image") String image) {
|
||||
|
||||
public SiliconflowImageRequest(String prompt, String model) {
|
||||
this(prompt, model, null, null, null, null, null, null);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,159 @@
|
||||
/*
|
||||
* Copyright 2023-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package cn.iocoder.yudao.framework.ai.core.model.siliconflow;
|
||||
|
||||
import io.micrometer.observation.ObservationRegistry;
|
||||
import lombok.Setter;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.ai.image.*;
|
||||
import org.springframework.ai.image.observation.DefaultImageModelObservationConvention;
|
||||
import org.springframework.ai.image.observation.ImageModelObservationContext;
|
||||
import org.springframework.ai.image.observation.ImageModelObservationConvention;
|
||||
import org.springframework.ai.image.observation.ImageModelObservationDocumentation;
|
||||
import org.springframework.ai.model.ModelOptionsUtils;
|
||||
import org.springframework.ai.openai.OpenAiImageModel;
|
||||
import org.springframework.ai.openai.api.OpenAiImageApi;
|
||||
import org.springframework.ai.openai.metadata.OpenAiImageGenerationMetadata;
|
||||
import org.springframework.ai.retry.RetryUtils;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.retry.support.RetryTemplate;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 硅基流动 {@link ImageModel} 实现类
|
||||
*
|
||||
* 参考 {@link OpenAiImageModel} 实现
|
||||
*
|
||||
* @author zzt
|
||||
*/
|
||||
public class SiliconFlowImageModel implements ImageModel {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(SiliconFlowImageModel.class);
|
||||
|
||||
private static final ImageModelObservationConvention DEFAULT_OBSERVATION_CONVENTION = new DefaultImageModelObservationConvention();
|
||||
|
||||
private final SiliconFlowImageOptions defaultOptions;
|
||||
|
||||
private final RetryTemplate retryTemplate;
|
||||
|
||||
private final SiliconFlowImageApi siliconFlowImageApi;
|
||||
|
||||
private final ObservationRegistry observationRegistry;
|
||||
|
||||
@Setter
|
||||
private ImageModelObservationConvention observationConvention = DEFAULT_OBSERVATION_CONVENTION;
|
||||
|
||||
public SiliconFlowImageModel(SiliconFlowImageApi siliconFlowImageApi) {
|
||||
this(siliconFlowImageApi, SiliconFlowImageOptions.builder().build(), RetryUtils.DEFAULT_RETRY_TEMPLATE);
|
||||
}
|
||||
|
||||
public SiliconFlowImageModel(SiliconFlowImageApi siliconFlowImageApi, SiliconFlowImageOptions options, RetryTemplate retryTemplate) {
|
||||
this(siliconFlowImageApi, options, retryTemplate, ObservationRegistry.NOOP);
|
||||
}
|
||||
|
||||
public SiliconFlowImageModel(SiliconFlowImageApi siliconFlowImageApi, SiliconFlowImageOptions options, RetryTemplate retryTemplate,
|
||||
ObservationRegistry observationRegistry) {
|
||||
Assert.notNull(siliconFlowImageApi, "OpenAiImageApi must not be null");
|
||||
Assert.notNull(options, "options must not be null");
|
||||
Assert.notNull(retryTemplate, "retryTemplate must not be null");
|
||||
Assert.notNull(observationRegistry, "observationRegistry must not be null");
|
||||
this.siliconFlowImageApi = siliconFlowImageApi;
|
||||
this.defaultOptions = options;
|
||||
this.retryTemplate = retryTemplate;
|
||||
this.observationRegistry = observationRegistry;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImageResponse call(ImagePrompt imagePrompt) {
|
||||
SiliconFlowImageOptions requestImageOptions = mergeOptions(imagePrompt.getOptions(), this.defaultOptions);
|
||||
SiliconFlowImageApi.SiliconflowImageRequest imageRequest = createRequest(imagePrompt, requestImageOptions);
|
||||
|
||||
var observationContext = ImageModelObservationContext.builder()
|
||||
.imagePrompt(imagePrompt)
|
||||
.provider(SiliconFlowApiConstants.PROVIDER_NAME)
|
||||
.requestOptions(imagePrompt.getOptions())
|
||||
.build();
|
||||
|
||||
return ImageModelObservationDocumentation.IMAGE_MODEL_OPERATION
|
||||
.observation(this.observationConvention, DEFAULT_OBSERVATION_CONVENTION, () -> observationContext,
|
||||
this.observationRegistry)
|
||||
.observe(() -> {
|
||||
ResponseEntity<OpenAiImageApi.OpenAiImageResponse> imageResponseEntity = this.retryTemplate
|
||||
.execute(ctx -> this.siliconFlowImageApi.createImage(imageRequest));
|
||||
|
||||
ImageResponse imageResponse = convertResponse(imageResponseEntity, imageRequest);
|
||||
|
||||
observationContext.setResponse(imageResponse);
|
||||
|
||||
return imageResponse;
|
||||
});
|
||||
}
|
||||
|
||||
private SiliconFlowImageApi.SiliconflowImageRequest createRequest(ImagePrompt imagePrompt,
|
||||
SiliconFlowImageOptions requestImageOptions) {
|
||||
String instructions = imagePrompt.getInstructions().get(0).getText();
|
||||
|
||||
SiliconFlowImageApi.SiliconflowImageRequest imageRequest = new SiliconFlowImageApi.SiliconflowImageRequest(instructions,
|
||||
SiliconFlowApiConstants.DEFAULT_IMAGE_MODEL);
|
||||
|
||||
return ModelOptionsUtils.merge(requestImageOptions, imageRequest, SiliconFlowImageApi.SiliconflowImageRequest.class);
|
||||
}
|
||||
|
||||
private ImageResponse convertResponse(ResponseEntity<OpenAiImageApi.OpenAiImageResponse> imageResponseEntity,
|
||||
SiliconFlowImageApi.SiliconflowImageRequest siliconflowImageRequest) {
|
||||
OpenAiImageApi.OpenAiImageResponse imageApiResponse = imageResponseEntity.getBody();
|
||||
if (imageApiResponse == null) {
|
||||
logger.warn("No image response returned for request: {}", siliconflowImageRequest);
|
||||
return new ImageResponse(List.of());
|
||||
}
|
||||
|
||||
List<ImageGeneration> imageGenerationList = imageApiResponse.data()
|
||||
.stream()
|
||||
.map(entry -> new ImageGeneration(new Image(entry.url(), entry.b64Json()),
|
||||
new OpenAiImageGenerationMetadata(entry.revisedPrompt())))
|
||||
.toList();
|
||||
|
||||
ImageResponseMetadata openAiImageResponseMetadata = new ImageResponseMetadata(imageApiResponse.created());
|
||||
return new ImageResponse(imageGenerationList, openAiImageResponseMetadata);
|
||||
}
|
||||
|
||||
private SiliconFlowImageOptions mergeOptions(@Nullable ImageOptions runtimeOptions, SiliconFlowImageOptions defaultOptions) {
|
||||
var runtimeOptionsForProvider = ModelOptionsUtils.copyToTarget(runtimeOptions, ImageOptions.class,
|
||||
SiliconFlowImageOptions.class);
|
||||
|
||||
if (runtimeOptionsForProvider == null) {
|
||||
return defaultOptions;
|
||||
}
|
||||
|
||||
return SiliconFlowImageOptions.builder()
|
||||
// Handle portable image options
|
||||
.model(ModelOptionsUtils.mergeOption(runtimeOptionsForProvider.getModel(), defaultOptions.getModel()))
|
||||
.batchSize(ModelOptionsUtils.mergeOption(runtimeOptionsForProvider.getN(), defaultOptions.getN()))
|
||||
.width(ModelOptionsUtils.mergeOption(runtimeOptionsForProvider.getWidth(), defaultOptions.getWidth()))
|
||||
.height(ModelOptionsUtils.mergeOption(runtimeOptionsForProvider.getHeight(), defaultOptions.getHeight()))
|
||||
// Handle SiliconFlow specific image options
|
||||
.negativePrompt(ModelOptionsUtils.mergeOption(runtimeOptionsForProvider.getNegativePrompt(), defaultOptions.getNegativePrompt()))
|
||||
.numInferenceSteps(ModelOptionsUtils.mergeOption(runtimeOptionsForProvider.getNumInferenceSteps(), defaultOptions.getNumInferenceSteps()))
|
||||
.guidanceScale(ModelOptionsUtils.mergeOption(runtimeOptionsForProvider.getGuidanceScale(), defaultOptions.getGuidanceScale()))
|
||||
.seed(ModelOptionsUtils.mergeOption(runtimeOptionsForProvider.getSeed(), defaultOptions.getSeed()))
|
||||
.build();
|
||||
}
|
||||
}
|
@ -0,0 +1,105 @@
|
||||
package cn.iocoder.yudao.framework.ai.core.model.siliconflow;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.springframework.ai.image.ImageOptions;
|
||||
|
||||
/**
|
||||
* 硅基流动 {@link ImageOptions}
|
||||
*
|
||||
* @author zzt
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class SiliconFlowImageOptions implements ImageOptions {
|
||||
|
||||
@JsonProperty("model")
|
||||
private String model;
|
||||
|
||||
@JsonProperty("negative_prompt")
|
||||
private String negativePrompt;
|
||||
|
||||
/**
|
||||
* The number of images to generate. Must be between 1 and 4.
|
||||
*/
|
||||
@JsonProperty("image_size")
|
||||
private String imageSize;
|
||||
|
||||
/**
|
||||
* The number of images to generate. Must be between 1 and 4.
|
||||
*/
|
||||
@JsonProperty("batch_size")
|
||||
private Integer batchSize = 1;
|
||||
|
||||
/**
|
||||
* number of inference steps
|
||||
*/
|
||||
@JsonProperty("num_inference_steps")
|
||||
private Integer numInferenceSteps = 25;
|
||||
|
||||
/**
|
||||
* This value is used to control the degree of match between the generated image and the given prompt. The higher the value, the more the generated image will tend to strictly match the text prompt. The lower the value, the more creative and diverse the generated image will be, potentially containing more unexpected elements.
|
||||
*
|
||||
* Required range: 0 <= x <= 20
|
||||
*/
|
||||
@JsonProperty("guidance_scale")
|
||||
private Float guidanceScale = 0.75F;
|
||||
|
||||
/**
|
||||
* 如果想要每次都生成固定的图片,可以把 seed 设置为固定值
|
||||
*
|
||||
*/
|
||||
@JsonProperty("seed")
|
||||
private Integer seed = (int)(Math.random() * 1_000_000_000);
|
||||
|
||||
/**
|
||||
* The image that needs to be uploaded should be converted into base64 format.
|
||||
*/
|
||||
@JsonProperty("image")
|
||||
private String image;
|
||||
|
||||
/**
|
||||
* 宽
|
||||
*/
|
||||
private Integer width;
|
||||
|
||||
/**
|
||||
* 高
|
||||
*/
|
||||
private Integer height;
|
||||
|
||||
public void setHeight(Integer height) {
|
||||
this.height = height;
|
||||
if (this.width != null && this.height != null) {
|
||||
this.imageSize = this.width + "x" + this.height;
|
||||
}
|
||||
}
|
||||
|
||||
public void setWidth(Integer width) {
|
||||
this.width = width;
|
||||
if (this.width != null && this.height != null) {
|
||||
this.imageSize = this.width + "x" + this.height;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getN() {
|
||||
return batchSize;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getResponseFormat() {
|
||||
return "url";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getStyle() {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,385 @@
|
||||
package cn.iocoder.yudao.framework.ai.core.model.wenduoduo.api;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.http.HttpRequest;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.reactive.function.BodyInserters;
|
||||
import org.springframework.web.reactive.function.client.ClientResponse;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
// TODO @新:要不改成 WenDuoDuoPptApi
|
||||
/**
|
||||
* 文多多 API
|
||||
*
|
||||
* @see <a href="https://docmee.cn/open-platform/api">PPT 生成 API</a>
|
||||
*
|
||||
* @author xiaoxin
|
||||
*/
|
||||
@Slf4j
|
||||
public class WddPptApi {
|
||||
|
||||
public static final String BASE_URL = "https://docmee.cn";
|
||||
|
||||
private final WebClient webClient;
|
||||
|
||||
private final Predicate<HttpStatusCode> STATUS_PREDICATE = status -> !status.is2xxSuccessful();
|
||||
|
||||
private final Function<Object, Function<ClientResponse, Mono<? extends Throwable>>> EXCEPTION_FUNCTION =
|
||||
reqParam -> response -> response.bodyToMono(String.class).handle((responseBody, sink) -> {
|
||||
HttpRequest request = response.request();
|
||||
log.error("[wdd-api] 调用失败!请求方式:[{}],请求地址:[{}],请求参数:[{}],响应数据: [{}]",
|
||||
request.getMethod(), request.getURI(), reqParam, responseBody);
|
||||
sink.error(new IllegalStateException("[wdd-api] 调用失败!"));
|
||||
});
|
||||
|
||||
// TODO @新:是不是不用 baseUrl 哈
|
||||
public WddPptApi(String baseUrl) {
|
||||
this.webClient = WebClient.builder()
|
||||
.baseUrl(baseUrl)
|
||||
// TODO @新:建议,token 作为 defaultHeader
|
||||
.defaultHeaders((headers) -> headers.setContentType(MediaType.APPLICATION_JSON))
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建 token
|
||||
*
|
||||
* @param request 请求信息
|
||||
* @return token
|
||||
*/
|
||||
public String createApiToken(CreateTokenRequest request) {
|
||||
return this.webClient.post()
|
||||
.uri("/api/user/createApiToken")
|
||||
.header("Api-Key", request.apiKey)
|
||||
.body(Mono.just(request), CreateTokenRequest.class)
|
||||
.retrieve()
|
||||
.onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(request))
|
||||
.bodyToMono(ApiResponse.class)
|
||||
.<String>handle((response, sink) -> {
|
||||
if (response.code != 0) {
|
||||
sink.error(new IllegalStateException("创建 token 异常," + response.message));
|
||||
return;
|
||||
}
|
||||
sink.next(response.data.get("token").toString());
|
||||
})
|
||||
.block();
|
||||
}
|
||||
|
||||
// TODO @xin:是不是给个 API 连接,这样 type、content、files 都不用写注释太细了
|
||||
/**
|
||||
* 创建任务
|
||||
*
|
||||
* @param type 类型
|
||||
* 1.智能生成(主题、要求)
|
||||
* 2.上传文件生成
|
||||
* 3.上传思维导图生成
|
||||
* 4.通过word精准转ppt
|
||||
* 5.通过网页链接生成
|
||||
* 6.粘贴文本内容生成
|
||||
* 7.Markdown大纲生成
|
||||
* @param content 内容
|
||||
* type=1 用户输入主题或要求(不超过1000字符)
|
||||
* type=2、4 不传
|
||||
* type=3 幕布等分享链接
|
||||
* type=5 网页链接地址(http/https)
|
||||
* type=6 粘贴文本内容(不超过20000字符)
|
||||
* type=7 大纲内容(markdown)
|
||||
* @param files 文件列表
|
||||
* 文件列表(文件数不超过5个,总大小不超过50M):
|
||||
* type=1 上传参考文件(非必传,支持多个)
|
||||
* type=2 上传文件(支持多个)
|
||||
* type=3 上传思维导图(xmind/mm/md)(仅支持一个)
|
||||
* type=4 上传word文件(仅支持一个)
|
||||
* type=5、6、7 不传
|
||||
* <p>
|
||||
* 支持格式:doc/docx/pdf/ppt/pptx/txt/md/xls/xlsx/csv/html/epub/mobi/xmind/mm
|
||||
* @return 任务ID
|
||||
*/
|
||||
public ApiResponse createTask(String token, Integer type, String content, List<MultipartFile> files) {
|
||||
MultiValueMap<String, Object> formData = new LinkedMultiValueMap<>();
|
||||
formData.add("type", type);
|
||||
if (content != null) {
|
||||
formData.add("content", content);
|
||||
}
|
||||
if (files != null) {
|
||||
for (MultipartFile file : files) {
|
||||
formData.add("file", file.getResource());
|
||||
}
|
||||
}
|
||||
return this.webClient.post()
|
||||
.uri("/api/ppt/v2/createTask")
|
||||
.header("token", token)
|
||||
.contentType(MediaType.MULTIPART_FORM_DATA)
|
||||
.body(BodyInserters.fromMultipartData(formData))
|
||||
.retrieve()
|
||||
.onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(formData))
|
||||
.bodyToMono(ApiResponse.class)
|
||||
.block();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取生成选项
|
||||
*
|
||||
* @param lang 语种
|
||||
* @return 生成选项
|
||||
*/
|
||||
public Map<String, Object> getOptions(String lang) {
|
||||
String uri = "/api/ppt/v2/options";
|
||||
if (lang != null) {
|
||||
uri += "?lang=" + lang;
|
||||
}
|
||||
return this.webClient.get()
|
||||
.uri(uri)
|
||||
.retrieve()
|
||||
.onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(lang))
|
||||
.bodyToMono(new ParameterizedTypeReference<ApiResponse>() {
|
||||
})
|
||||
.<Map<String, Object>>handle((response, sink) -> {
|
||||
if (response.code != 0) {
|
||||
sink.error(new IllegalStateException("获取生成选项异常," + response.message));
|
||||
return;
|
||||
}
|
||||
sink.next(response.data);
|
||||
})
|
||||
.block();
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页查询 PPT 模板
|
||||
*
|
||||
* @param token 令牌
|
||||
* @param request 请求体
|
||||
* @return 模板列表
|
||||
*/
|
||||
public PagePptTemplateInfo getTemplatePage(String token, TemplateQueryRequest request) {
|
||||
return this.webClient.post()
|
||||
.uri("/api/ppt/templates")
|
||||
.header("token", token)
|
||||
.bodyValue(request)
|
||||
.retrieve()
|
||||
.onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(request))
|
||||
.bodyToMono(new ParameterizedTypeReference<PagePptTemplateInfo>() {
|
||||
})
|
||||
.block();
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成大纲内容
|
||||
*
|
||||
* @return 大纲内容流
|
||||
*/
|
||||
public Flux<Map<String, Object>> createOutline(String token, CreateOutlineRequest request) {
|
||||
return this.webClient.post()
|
||||
.uri("/api/ppt/v2/generateContent")
|
||||
.header("token", token)
|
||||
.body(Mono.just(request), CreateOutlineRequest.class)
|
||||
.retrieve()
|
||||
.onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(request))
|
||||
.bodyToFlux(new ParameterizedTypeReference<>() {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改大纲内容
|
||||
*
|
||||
* @param request 请求体
|
||||
* @return 大纲内容流
|
||||
*/
|
||||
public Flux<Map<String, Object>> updateOutline(String token, UpdateOutlineRequest request) {
|
||||
return this.webClient.post()
|
||||
.uri("/api/ppt/v2/updateContent")
|
||||
.header("token", token)
|
||||
.body(Mono.just(request), UpdateOutlineRequest.class)
|
||||
.retrieve()
|
||||
.onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(request))
|
||||
.bodyToFlux(new ParameterizedTypeReference<>() {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成 PPT
|
||||
*
|
||||
* @return PPT信息
|
||||
*/
|
||||
public PptInfo create(String token, CreatePptRequest request) {
|
||||
return this.webClient.post()
|
||||
.uri("/api/ppt/v2/generatePptx")
|
||||
.header("token", token)
|
||||
.body(Mono.just(request), CreatePptRequest.class)
|
||||
.retrieve()
|
||||
.onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(request))
|
||||
.bodyToMono(ApiResponse.class)
|
||||
.<PptInfo>handle((response, sink) -> {
|
||||
if (response.code != 0) {
|
||||
sink.error(new IllegalStateException("生成 PPT 异常," + response.message));
|
||||
return;
|
||||
}
|
||||
sink.next(Objects.requireNonNull(JsonUtils.parseObject(JsonUtils.toJsonString(response.data.get("pptInfo")), PptInfo.class)));
|
||||
})
|
||||
.block();
|
||||
}
|
||||
|
||||
|
||||
@JsonInclude(value = JsonInclude.Include.NON_NULL)
|
||||
public record CreateTokenRequest(
|
||||
String apiKey,
|
||||
String uid,
|
||||
Integer limit
|
||||
) {
|
||||
|
||||
public CreateTokenRequest(String apiKey) {
|
||||
this(apiKey, null, null);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* API 通用响应
|
||||
*/
|
||||
@JsonInclude(value = JsonInclude.Include.NON_NULL)
|
||||
public record ApiResponse(
|
||||
Integer code,
|
||||
String message,
|
||||
Map<String, Object> data
|
||||
) { }
|
||||
|
||||
/**
|
||||
* 创建任务
|
||||
*/
|
||||
@JsonInclude(value = JsonInclude.Include.NON_NULL)
|
||||
public record CreateTaskRequest(
|
||||
Integer type,
|
||||
String content,
|
||||
List<MultipartFile> files
|
||||
) { }
|
||||
|
||||
/**
|
||||
* 生成大纲内容请求
|
||||
*/
|
||||
@JsonInclude(value = JsonInclude.Include.NON_NULL)
|
||||
public record CreateOutlineRequest(
|
||||
String id,
|
||||
String length,
|
||||
String scene,
|
||||
String audience,
|
||||
String lang,
|
||||
String prompt
|
||||
) { }
|
||||
|
||||
/**
|
||||
* 修改大纲内容请求
|
||||
*/
|
||||
@JsonInclude(value = JsonInclude.Include.NON_NULL)
|
||||
public record UpdateOutlineRequest(
|
||||
String id,
|
||||
String markdown,
|
||||
String question
|
||||
) { }
|
||||
|
||||
/**
|
||||
* 生成 PPT 请求
|
||||
*/
|
||||
// TODO @新:要不按照 PptCreateRequest 这样的风格
|
||||
@JsonInclude(value = JsonInclude.Include.NON_NULL)
|
||||
public record CreatePptRequest(
|
||||
String id,
|
||||
String templateId,
|
||||
String markdown
|
||||
) { }
|
||||
|
||||
// TODO @新:要不写下类注释
|
||||
@JsonInclude(value = JsonInclude.Include.NON_NULL)
|
||||
public record PptInfo(
|
||||
String id,
|
||||
String name,
|
||||
String subject,
|
||||
String coverUrl,
|
||||
String fileUrl,
|
||||
String templateId,
|
||||
String pptxProperty,
|
||||
String userId,
|
||||
String userName,
|
||||
int companyId,
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
LocalDateTime updateTime,
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
LocalDateTime createTime,
|
||||
String createUser,
|
||||
String updateUser
|
||||
) { }
|
||||
|
||||
// TODO @新:要不写下类注释
|
||||
@JsonInclude(value = JsonInclude.Include.NON_NULL)
|
||||
public record TemplateQueryRequest(
|
||||
int page,
|
||||
int size,
|
||||
Filter filters
|
||||
) {
|
||||
|
||||
@JsonInclude(value = JsonInclude.Include.NON_NULL)
|
||||
public record Filter(
|
||||
int type,
|
||||
String category,
|
||||
String style,
|
||||
String themeColor
|
||||
) { }
|
||||
|
||||
}
|
||||
|
||||
// TODO @新:要不写下类注释
|
||||
@JsonInclude(value = JsonInclude.Include.NON_NULL)
|
||||
public record PagePptTemplateInfo(
|
||||
List<PptTemplateInfo> data,
|
||||
String total
|
||||
) {}
|
||||
|
||||
|
||||
// TODO @新:要不写下类注释
|
||||
@JsonInclude(value = JsonInclude.Include.NON_NULL)
|
||||
public record PptTemplateInfo(
|
||||
String id,
|
||||
int type,
|
||||
Integer subType,
|
||||
String layout,
|
||||
String category,
|
||||
String style,
|
||||
String themeColor,
|
||||
String lang,
|
||||
boolean animation,
|
||||
String subject,
|
||||
String coverUrl,
|
||||
String fileUrl,
|
||||
List<String> pageCoverUrls,
|
||||
String pptxProperty,
|
||||
int sort,
|
||||
int num,
|
||||
Integer imgNum,
|
||||
int isDeleted,
|
||||
String userId,
|
||||
int companyId,
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
LocalDateTime updateTime,
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
LocalDateTime createTime,
|
||||
String createUser,
|
||||
String updateUser
|
||||
) { }
|
||||
|
||||
}
|
@ -0,0 +1,759 @@
|
||||
package cn.iocoder.yudao.framework.ai.core.model.xinghuo.api;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.reactive.function.BodyInserters;
|
||||
import org.springframework.web.reactive.function.client.ClientResponse;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import javax.crypto.Mac;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Base64;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
// TODO @新:要不改成 XunFeiPptApi
|
||||
/**
|
||||
* 讯飞智能 PPT 生成 API
|
||||
*
|
||||
* @see <a href="https://www.xfyun.cn/doc/spark/PPTv2.html">智能 PPT 生成 API</a>
|
||||
*
|
||||
* @author xiaoxin
|
||||
*/
|
||||
@Slf4j
|
||||
public class XunfeiPptApi {
|
||||
|
||||
public static final String BASE_URL = "https://zwapi.xfyun.cn/api/ppt/v2";
|
||||
|
||||
private final WebClient webClient;
|
||||
private final String appId;
|
||||
private final String apiSecret;
|
||||
|
||||
private final Predicate<HttpStatusCode> STATUS_PREDICATE = status -> !status.is2xxSuccessful();
|
||||
|
||||
private final Function<Object, Function<ClientResponse, Mono<? extends Throwable>>> EXCEPTION_FUNCTION =
|
||||
reqParam -> response -> response.bodyToMono(String.class).handle((responseBody, sink) -> {
|
||||
log.error("[xunfei-ppt-api] 调用失败!请求参数:[{}],响应数据: [{}]", reqParam, responseBody);
|
||||
sink.error(new IllegalStateException("[xunfei-ppt-api] 调用失败!"));
|
||||
});
|
||||
|
||||
// TODO @新:是不是不用 baseUrl 哈
|
||||
public XunfeiPptApi(String baseUrl, String appId, String apiSecret) {
|
||||
// TODO @新:建议,增加 defaultheaders,例如说 appid 之类的;或者每个请求,通过 headers customer 处理。
|
||||
this.webClient = WebClient.builder()
|
||||
.baseUrl(baseUrl)
|
||||
.build();
|
||||
this.appId = appId;
|
||||
this.apiSecret = apiSecret;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取签名
|
||||
*
|
||||
* @return 签名信息
|
||||
*/
|
||||
private SignatureInfo getSignature() {
|
||||
long timestamp = System.currentTimeMillis() / 1000;
|
||||
String ts = String.valueOf(timestamp);
|
||||
String signature = generateSignature(appId, apiSecret, timestamp);
|
||||
return new SignatureInfo(appId, ts, signature);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成签名
|
||||
*
|
||||
* @param appId 应用ID
|
||||
* @param apiSecret 应用密钥
|
||||
* @param timestamp 时间戳(秒)
|
||||
* @return 签名
|
||||
*/
|
||||
private String generateSignature(String appId, String apiSecret, long timestamp) {
|
||||
try {
|
||||
// TODO @新:使用 hutool 简化
|
||||
String auth = md5(appId + timestamp);
|
||||
return hmacSHA1Encrypt(auth, apiSecret);
|
||||
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
|
||||
log.error("[xunfei-ppt-api] 生成签名失败", e);
|
||||
throw new IllegalStateException("[xunfei-ppt-api] 生成签名失败");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* HMAC SHA1 加密
|
||||
*/
|
||||
private String hmacSHA1Encrypt(String encryptText, String encryptKey)
|
||||
throws NoSuchAlgorithmException, InvalidKeyException {
|
||||
SecretKeySpec keySpec = new SecretKeySpec(
|
||||
encryptKey.getBytes(StandardCharsets.UTF_8), "HmacSHA1");
|
||||
|
||||
Mac mac = Mac.getInstance("HmacSHA1");
|
||||
mac.init(keySpec);
|
||||
byte[] result = mac.doFinal(encryptText.getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
return Base64.getEncoder().encodeToString(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* MD5 哈希
|
||||
*/
|
||||
private String md5(String text) throws NoSuchAlgorithmException {
|
||||
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||
byte[] digest = md.digest(text.getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (byte b : digest) {
|
||||
sb.append(String.format("%02x", b));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 PPT 模板列表
|
||||
*
|
||||
* @param style 风格,如"商务"
|
||||
* @param pageSize 每页数量
|
||||
* @return 模板列表
|
||||
*/
|
||||
public TemplatePageResponse getTemplatePage(String style, Integer pageSize) {
|
||||
SignatureInfo signInfo = getSignature();
|
||||
Map<String, Object> requestBody = new HashMap<>();
|
||||
requestBody.put("style", style);
|
||||
// TODO @新:可以使用 ObjUtil.defaultIfNull
|
||||
requestBody.put("pageSize", pageSize != null ? pageSize : 10);
|
||||
return this.webClient.post()
|
||||
.uri("/template/list")
|
||||
.header("appId", signInfo.appId)
|
||||
.header("timestamp", signInfo.timestamp)
|
||||
.header("signature", signInfo.signature)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.bodyValue(requestBody)
|
||||
.retrieve()
|
||||
.onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(requestBody))
|
||||
.bodyToMono(TemplatePageResponse.class)
|
||||
.block();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建大纲(通过文本)
|
||||
*
|
||||
* @param query 查询文本
|
||||
* @return 大纲创建响应
|
||||
*/
|
||||
public CreateResponse createOutline(String query) {
|
||||
SignatureInfo signInfo = getSignature();
|
||||
MultiValueMap<String, Object> formData = new LinkedMultiValueMap<>();
|
||||
formData.add("query", query);
|
||||
return this.webClient.post()
|
||||
.uri("/createOutline")
|
||||
.header("appId", signInfo.appId)
|
||||
.header("timestamp", signInfo.timestamp)
|
||||
.header("signature", signInfo.signature)
|
||||
.contentType(MediaType.MULTIPART_FORM_DATA)
|
||||
.body(BodyInserters.fromMultipartData(formData))
|
||||
.retrieve()
|
||||
.onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(formData))
|
||||
.bodyToMono(CreateResponse.class)
|
||||
.block();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 直接创建 PPT(简化版 - 通过文本)
|
||||
*
|
||||
* @param query 查询文本
|
||||
* @return 创建响应
|
||||
*/
|
||||
public CreateResponse create(String query) {
|
||||
CreatePptRequest request = CreatePptRequest.builder()
|
||||
.query(query)
|
||||
.build();
|
||||
return create(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* 直接创建 PPT(简化版 - 通过文件)
|
||||
*
|
||||
* @param file 文件
|
||||
* @param fileName 文件名
|
||||
* @return 创建响应
|
||||
*/
|
||||
public CreateResponse create(MultipartFile file, String fileName) {
|
||||
CreatePptRequest request = CreatePptRequest.builder()
|
||||
.file(file).fileName(fileName).build();
|
||||
return create(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* 直接创建 PPT(完整版)
|
||||
*
|
||||
* @param request 请求参数
|
||||
* @return 创建响应
|
||||
*/
|
||||
public CreateResponse create(CreatePptRequest request) {
|
||||
SignatureInfo signInfo = getSignature();
|
||||
MultiValueMap<String, Object> formData = buildCreateFormData(request);
|
||||
return this.webClient.post()
|
||||
.uri("/create")
|
||||
.header("appId", signInfo.appId)
|
||||
.header("timestamp", signInfo.timestamp)
|
||||
.header("signature", signInfo.signature)
|
||||
.contentType(MediaType.MULTIPART_FORM_DATA)
|
||||
.body(BodyInserters.fromMultipartData(formData))
|
||||
.retrieve()
|
||||
.onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(formData))
|
||||
.bodyToMono(CreateResponse.class)
|
||||
.block();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 通过大纲创建 PPT(简化版)
|
||||
*
|
||||
* @param outline 大纲内容
|
||||
* @param query 查询文本
|
||||
* @return 创建响应
|
||||
*/
|
||||
public CreateResponse createPptByOutline(OutlineData outline, String query) {
|
||||
CreatePptByOutlineRequest request = CreatePptByOutlineRequest.builder()
|
||||
.outline(outline)
|
||||
.query(query)
|
||||
.build();
|
||||
return createPptByOutline(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过大纲创建 PPT(完整版)
|
||||
*
|
||||
* @param request 请求参数
|
||||
* @return 创建响应
|
||||
*/
|
||||
public CreateResponse createPptByOutline(CreatePptByOutlineRequest request) {
|
||||
SignatureInfo signInfo = getSignature();
|
||||
return this.webClient.post()
|
||||
.uri("/createPptByOutline")
|
||||
.header("appId", signInfo.appId)
|
||||
.header("timestamp", signInfo.timestamp)
|
||||
.header("signature", signInfo.signature)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.bodyValue(request)
|
||||
.retrieve()
|
||||
.onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(request))
|
||||
.bodyToMono(CreateResponse.class)
|
||||
.block();
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查 PPT 生成进度
|
||||
*
|
||||
* @param sid 任务 ID
|
||||
* @return 进度响应
|
||||
*/
|
||||
public ProgressResponse checkProgress(String sid) {
|
||||
SignatureInfo signInfo = getSignature();
|
||||
return this.webClient.get()
|
||||
.uri(uriBuilder -> uriBuilder
|
||||
.path("/progress")
|
||||
.queryParam("sid", sid)
|
||||
.build())
|
||||
.header("appId", signInfo.appId)
|
||||
.header("timestamp", signInfo.timestamp)
|
||||
.header("signature", signInfo.signature)
|
||||
.retrieve()
|
||||
.onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(sid))
|
||||
.bodyToMono(ProgressResponse.class)
|
||||
.block();
|
||||
}
|
||||
|
||||
/**
|
||||
* 签名信息
|
||||
*/
|
||||
@JsonInclude(value = JsonInclude.Include.NON_NULL)
|
||||
private record SignatureInfo(
|
||||
String appId,
|
||||
String timestamp,
|
||||
String signature
|
||||
) { }
|
||||
|
||||
/**
|
||||
* 模板列表响应
|
||||
*/
|
||||
@JsonInclude(value = JsonInclude.Include.NON_NULL)
|
||||
public record TemplatePageResponse(
|
||||
boolean flag,
|
||||
int code,
|
||||
String desc,
|
||||
Integer count,
|
||||
TemplatePageData data
|
||||
) { }
|
||||
|
||||
/**
|
||||
* 模板列表数据
|
||||
*/
|
||||
@JsonInclude(value = JsonInclude.Include.NON_NULL)
|
||||
public record TemplatePageData(
|
||||
String total,
|
||||
List<TemplateInfo> records,
|
||||
Integer pageNum
|
||||
) { }
|
||||
|
||||
/**
|
||||
* 模板信息
|
||||
*/
|
||||
@JsonInclude(value = JsonInclude.Include.NON_NULL)
|
||||
public record TemplateInfo(
|
||||
String templateIndexId,
|
||||
Integer pageCount,
|
||||
String type,
|
||||
String color,
|
||||
String industry,
|
||||
String style,
|
||||
String detailImage
|
||||
) { }
|
||||
|
||||
/**
|
||||
* 创建响应
|
||||
*/
|
||||
@JsonInclude(value = JsonInclude.Include.NON_NULL)
|
||||
public record CreateResponse(
|
||||
boolean flag,
|
||||
int code,
|
||||
String desc,
|
||||
Integer count,
|
||||
CreateResponseData data
|
||||
) { }
|
||||
|
||||
/**
|
||||
* 创建响应数据
|
||||
*/
|
||||
@JsonInclude(value = JsonInclude.Include.NON_NULL)
|
||||
public record CreateResponseData(
|
||||
String sid,
|
||||
String coverImgSrc,
|
||||
String title,
|
||||
String subTitle,
|
||||
OutlineData outline
|
||||
) { }
|
||||
|
||||
/**
|
||||
* 大纲数据结构
|
||||
*/
|
||||
@JsonInclude(value = JsonInclude.Include.NON_NULL)
|
||||
public record OutlineData(
|
||||
String title,
|
||||
String subTitle,
|
||||
List<Chapter> chapters
|
||||
) {
|
||||
|
||||
/**
|
||||
* 章节结构
|
||||
*/
|
||||
@JsonInclude(value = JsonInclude.Include.NON_NULL)
|
||||
public record Chapter(
|
||||
String chapterTitle,
|
||||
List<ChapterContent> chapterContents
|
||||
) {
|
||||
|
||||
/**
|
||||
* 章节内容
|
||||
*/
|
||||
@JsonInclude(value = JsonInclude.Include.NON_NULL)
|
||||
public record ChapterContent(
|
||||
String chapterTitle
|
||||
) { }
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 将大纲对象转换为JSON字符串
|
||||
*
|
||||
* @return 大纲JSON字符串
|
||||
*/
|
||||
public String toJsonString() {
|
||||
return JsonUtils.toJsonString(this);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 进度响应
|
||||
*/
|
||||
@JsonInclude(value = JsonInclude.Include.NON_NULL)
|
||||
public record ProgressResponse(
|
||||
int code,
|
||||
String desc,
|
||||
ProgressResponseData data
|
||||
) { }
|
||||
|
||||
/**
|
||||
* 进度响应数据
|
||||
*/
|
||||
@JsonInclude(value = JsonInclude.Include.NON_NULL)
|
||||
public record ProgressResponseData(
|
||||
int process,
|
||||
String pptId,
|
||||
String pptUrl,
|
||||
// TODO @新:字段注释,去掉
|
||||
String pptStatus, // PPT构建状态:building(构建中),done(已完成),build_failed(生成失败)
|
||||
String aiImageStatus, // ai配图状态:building(构建中),done(已完成)
|
||||
String cardNoteStatus, // 演讲备注状态:building(构建中),done(已完成)
|
||||
String errMsg, // 生成PPT的失败信息
|
||||
Integer totalPages, // 生成PPT的总页数
|
||||
Integer donePages // 生成PPT的完成页数
|
||||
) {
|
||||
|
||||
/**
|
||||
* 是否全部完成
|
||||
*
|
||||
* @return 是否全部完成
|
||||
*/
|
||||
public boolean isAllDone() {
|
||||
return "done".equals(pptStatus)
|
||||
&& ("done".equals(aiImageStatus) || aiImageStatus == null)
|
||||
&& ("done".equals(cardNoteStatus) || cardNoteStatus == null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否失败
|
||||
*
|
||||
* @return 是否失败
|
||||
*/
|
||||
public boolean isFailed() {
|
||||
return "build_failed".equals(pptStatus);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取进度百分比
|
||||
*
|
||||
* @return 进度百分比
|
||||
*/
|
||||
public int getProgressPercent() {
|
||||
if (totalPages == null || totalPages == 0 || donePages == null) {
|
||||
return process; // 兼容旧版返回
|
||||
}
|
||||
return (int) (donePages * 100.0 / totalPages);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过大纲创建 PPT 请求参数
|
||||
*/
|
||||
@JsonInclude(value = JsonInclude.Include.NON_NULL)
|
||||
public record CreatePptByOutlineRequest(
|
||||
String query, // 用户生成PPT要求(最多8000字)
|
||||
String outlineSid, // 已生成大纲后,响应返回的请求大纲唯一id
|
||||
OutlineData outline, // 大纲内容
|
||||
String templateId, // 模板ID
|
||||
String businessId, // 业务ID(非必传)
|
||||
String author, // PPT作者名
|
||||
Boolean isCardNote, // 是否生成PPT演讲备注
|
||||
Boolean search, // 是否联网搜索
|
||||
String language, // 语种
|
||||
String fileUrl, // 文件地址
|
||||
String fileName, // 文件名(带文件名后缀)
|
||||
Boolean isFigure, // 是否自动配图
|
||||
String aiImage // ai配图类型:normal、advanced
|
||||
) {
|
||||
|
||||
/**
|
||||
* 创建构建器
|
||||
*
|
||||
* @return 构建器
|
||||
*/
|
||||
public static Builder builder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
// TODO @新:这个可以用 lombok 简化么?
|
||||
/**
|
||||
* 构建器类
|
||||
*/
|
||||
public static class Builder {
|
||||
|
||||
private String query;
|
||||
private String outlineSid;
|
||||
private OutlineData outline;
|
||||
private String templateId;
|
||||
private String businessId;
|
||||
private String author;
|
||||
private Boolean isCardNote;
|
||||
private Boolean search;
|
||||
private String language;
|
||||
private String fileUrl;
|
||||
private String fileName;
|
||||
private Boolean isFigure;
|
||||
private String aiImage;
|
||||
|
||||
public Builder query(String query) {
|
||||
this.query = query;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder outlineSid(String outlineSid) {
|
||||
this.outlineSid = outlineSid;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder outline(OutlineData outline) {
|
||||
this.outline = outline;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder templateId(String templateId) {
|
||||
this.templateId = templateId;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder businessId(String businessId) {
|
||||
this.businessId = businessId;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder author(String author) {
|
||||
this.author = author;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder isCardNote(Boolean isCardNote) {
|
||||
this.isCardNote = isCardNote;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder search(Boolean search) {
|
||||
this.search = search;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder language(String language) {
|
||||
this.language = language;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder fileUrl(String fileUrl) {
|
||||
this.fileUrl = fileUrl;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder fileName(String fileName) {
|
||||
this.fileName = fileName;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder isFigure(Boolean isFigure) {
|
||||
this.isFigure = isFigure;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder aiImage(String aiImage) {
|
||||
this.aiImage = aiImage;
|
||||
return this;
|
||||
}
|
||||
|
||||
public CreatePptByOutlineRequest build() {
|
||||
return new CreatePptByOutlineRequest(
|
||||
query, outlineSid, outline, templateId, businessId, author,
|
||||
isCardNote, search, language, fileUrl, fileName, isFigure, aiImage
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建创建 PPT 的表单数据
|
||||
*
|
||||
* @param request 请求参数
|
||||
* @return 表单数据
|
||||
*/
|
||||
private MultiValueMap<String, Object> buildCreateFormData(CreatePptRequest request) {
|
||||
MultiValueMap<String, Object> formData = new LinkedMultiValueMap<>();
|
||||
// 添加请求参数
|
||||
if (request.query() != null) {
|
||||
formData.add("query", request.query());
|
||||
}
|
||||
if (request.file() != null) {
|
||||
try {
|
||||
formData.add("file", new ByteArrayResource(request.file().getBytes()) {
|
||||
@Override
|
||||
public String getFilename() {
|
||||
return request.file().getOriginalFilename();
|
||||
}
|
||||
});
|
||||
} catch (IOException e) {
|
||||
log.error("[xunfei-ppt-api] 文件处理失败", e);
|
||||
throw new IllegalStateException("[xunfei-ppt-api] 文件处理失败", e);
|
||||
}
|
||||
}
|
||||
// TODO @新:要不搞个 MapUtil.addIfPresent 方法?
|
||||
if (request.fileUrl() != null) {
|
||||
formData.add("fileUrl", request.fileUrl());
|
||||
}
|
||||
if (request.fileName() != null) {
|
||||
formData.add("fileName", request.fileName());
|
||||
}
|
||||
if (request.templateId() != null) {
|
||||
formData.add("templateId", request.templateId());
|
||||
}
|
||||
if (request.businessId() != null) {
|
||||
formData.add("businessId", request.businessId());
|
||||
}
|
||||
if (request.author() != null) {
|
||||
formData.add("author", request.author());
|
||||
}
|
||||
if (request.isCardNote() != null) {
|
||||
formData.add("isCardNote", request.isCardNote().toString());
|
||||
}
|
||||
if (request.search() != null) {
|
||||
formData.add("search", request.search().toString());
|
||||
}
|
||||
if (request.language() != null) {
|
||||
formData.add("language", request.language());
|
||||
}
|
||||
if (request.isFigure() != null) {
|
||||
formData.add("isFigure", request.isFigure().toString());
|
||||
}
|
||||
if (request.aiImage() != null) {
|
||||
formData.add("aiImage", request.aiImage());
|
||||
}
|
||||
return formData;
|
||||
}
|
||||
|
||||
/**
|
||||
* 直接生成PPT请求参数
|
||||
*/
|
||||
@JsonInclude(value = JsonInclude.Include.NON_NULL)
|
||||
public record CreatePptRequest(
|
||||
String query, // 用户生成PPT要求(最多8000字)
|
||||
MultipartFile file, // 上传文件
|
||||
String fileUrl, // 文件地址
|
||||
String fileName, // 文件名(带文件名后缀)
|
||||
String templateId, // 模板ID
|
||||
String businessId, // 业务ID(非必传)
|
||||
String author, // PPT作者名
|
||||
Boolean isCardNote, // 是否生成PPT演讲备注
|
||||
Boolean search, // 是否联网搜索
|
||||
String language, // 语种
|
||||
Boolean isFigure, // 是否自动配图
|
||||
String aiImage // ai配图类型:normal、advanced
|
||||
) {
|
||||
|
||||
/**
|
||||
* 创建构建器
|
||||
*
|
||||
* @return 构建器
|
||||
*/
|
||||
public static Builder builder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建器类
|
||||
*/
|
||||
public static class Builder {
|
||||
|
||||
private String query;
|
||||
private MultipartFile file;
|
||||
private String fileUrl;
|
||||
private String fileName;
|
||||
private String templateId;
|
||||
private String businessId;
|
||||
private String author;
|
||||
private Boolean isCardNote;
|
||||
private Boolean search;
|
||||
private String language;
|
||||
private Boolean isFigure;
|
||||
private String aiImage;
|
||||
|
||||
// TODO @新:这个可以用 lombok 简化么?
|
||||
|
||||
public Builder query(String query) {
|
||||
this.query = query;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder file(MultipartFile file) {
|
||||
this.file = file;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder fileUrl(String fileUrl) {
|
||||
this.fileUrl = fileUrl;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder fileName(String fileName) {
|
||||
this.fileName = fileName;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder templateId(String templateId) {
|
||||
this.templateId = templateId;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder businessId(String businessId) {
|
||||
this.businessId = businessId;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder author(String author) {
|
||||
this.author = author;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder isCardNote(Boolean isCardNote) {
|
||||
this.isCardNote = isCardNote;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder search(Boolean search) {
|
||||
this.search = search;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder language(String language) {
|
||||
this.language = language;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder isFigure(Boolean isFigure) {
|
||||
this.isFigure = isFigure;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder aiImage(String aiImage) {
|
||||
this.aiImage = aiImage;
|
||||
return this;
|
||||
}
|
||||
|
||||
public CreatePptRequest build() {
|
||||
// 验证参数
|
||||
if (query == null && file == null && fileUrl == null) {
|
||||
throw new IllegalArgumentException("query、file、fileUrl必填其一");
|
||||
}
|
||||
if ((file != null || fileUrl != null) && fileName == null) {
|
||||
throw new IllegalArgumentException("如果传file或者fileUrl,fileName必填");
|
||||
}
|
||||
return new CreatePptRequest(
|
||||
query, file, fileUrl, fileName, templateId, businessId, author,
|
||||
isCardNote, search, language, isFigure, aiImage
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -53,6 +53,7 @@ public class AiUtils {
|
||||
case HUN_YUAN: // 复用 OpenAI 客户端
|
||||
case XING_HUO: // 复用 OpenAI 客户端
|
||||
case SILICON_FLOW: // 复用 OpenAI 客户端
|
||||
case BAI_CHUAN: // 复用 OpenAI 客户端
|
||||
return OpenAiChatOptions.builder().model(model).temperature(temperature).maxTokens(maxTokens)
|
||||
.toolNames(toolNames).build();
|
||||
case AZURE_OPENAI:
|
||||
|
@ -0,0 +1,68 @@
|
||||
package cn.iocoder.yudao.framework.ai.chat;
|
||||
|
||||
import cn.iocoder.yudao.framework.ai.core.model.baichuan.BaiChuanChatModel;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.deepseek.DeepSeekChatModel;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.ai.chat.messages.Message;
|
||||
import org.springframework.ai.chat.messages.SystemMessage;
|
||||
import org.springframework.ai.chat.messages.UserMessage;
|
||||
import org.springframework.ai.chat.model.ChatResponse;
|
||||
import org.springframework.ai.chat.prompt.Prompt;
|
||||
import org.springframework.ai.openai.OpenAiChatModel;
|
||||
import org.springframework.ai.openai.OpenAiChatOptions;
|
||||
import org.springframework.ai.openai.api.OpenAiApi;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* {@link BaiChuanChatModel} 集成测试
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
public class BaiChuanChatModelTests {
|
||||
|
||||
private final OpenAiChatModel openAiChatModel = OpenAiChatModel.builder()
|
||||
.openAiApi(OpenAiApi.builder()
|
||||
.baseUrl(BaiChuanChatModel.BASE_URL)
|
||||
.apiKey("sk-61b6766a94c70786ed02673f5e16af3c") // apiKey
|
||||
.build())
|
||||
.defaultOptions(OpenAiChatOptions.builder()
|
||||
.model("Baichuan4-Turbo") // 模型(https://platform.baichuan-ai.com/docs/api)
|
||||
.temperature(0.7)
|
||||
.build())
|
||||
.build();
|
||||
|
||||
private final DeepSeekChatModel chatModel = new DeepSeekChatModel(openAiChatModel);
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void testCall() {
|
||||
// 准备参数
|
||||
List<Message> messages = new ArrayList<>();
|
||||
messages.add(new SystemMessage("你是一个优质的文言文作者,用文言文描述着各城市的人文风景。"));
|
||||
messages.add(new UserMessage("1 + 1 = ?"));
|
||||
|
||||
// 调用
|
||||
ChatResponse response = chatModel.call(new Prompt(messages));
|
||||
// 打印结果
|
||||
System.out.println(response);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void testStream() {
|
||||
// 准备参数
|
||||
List<Message> messages = new ArrayList<>();
|
||||
messages.add(new SystemMessage("你是一个优质的文言文作者,用文言文描述着各城市的人文风景。"));
|
||||
messages.add(new UserMessage("1 + 1 = ?"));
|
||||
|
||||
// 调用
|
||||
Flux<ChatResponse> flux = chatModel.stream(new Prompt(messages));
|
||||
// 打印结果
|
||||
flux.doOnNext(System.out::println).then().block();
|
||||
}
|
||||
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
package cn.iocoder.yudao.framework.ai.chat;
|
||||
|
||||
import cn.iocoder.yudao.framework.ai.core.model.siliconflow.SiliconFlowApiConstants;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.siliconflow.SiliconFlowChatModel;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@ -25,11 +26,11 @@ public class SiliconFlowChatModelTests {
|
||||
|
||||
private final OpenAiChatModel openAiChatModel = OpenAiChatModel.builder()
|
||||
.openAiApi(OpenAiApi.builder()
|
||||
.baseUrl(SiliconFlowChatModel.BASE_URL)
|
||||
.baseUrl(SiliconFlowApiConstants.DEFAULT_BASE_URL)
|
||||
.apiKey("sk-epsakfenqnyzoxhmbucsxlhkdqlcbnimslqoivkshalvdozz") // apiKey
|
||||
.build())
|
||||
.defaultOptions(OpenAiChatOptions.builder()
|
||||
.model(SiliconFlowChatModel.MODEL_DEFAULT) // 模型
|
||||
.model(SiliconFlowApiConstants.MODEL_DEFAULT) // 模型
|
||||
// .model("deepseek-ai/DeepSeek-R1") // 模型(deepseek-ai/DeepSeek-R1)可用赠费
|
||||
// .model("Pro/deepseek-ai/DeepSeek-R1") // 模型(Pro/deepseek-ai/DeepSeek-R1)需要付费
|
||||
.temperature(0.7)
|
||||
|
@ -0,0 +1,35 @@
|
||||
package cn.iocoder.yudao.framework.ai.image;
|
||||
|
||||
import cn.iocoder.yudao.framework.ai.core.model.siliconflow.SiliconFlowImageApi;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.siliconflow.SiliconFlowImageModel;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.siliconflow.SiliconFlowImageOptions;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.ai.image.ImagePrompt;
|
||||
import org.springframework.ai.image.ImageResponse;
|
||||
|
||||
/**
|
||||
* {@link SiliconFlowImageModel} 集成测试
|
||||
*/
|
||||
public class SiliconFlowImageModelTests {
|
||||
|
||||
private final SiliconFlowImageModel imageModel = new SiliconFlowImageModel(
|
||||
new SiliconFlowImageApi("sk-epsakfenqnyzoxhmbucsxlhkdqlcbnimslqoivkshalvdozz") // 密钥
|
||||
);
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void testCall() {
|
||||
// 准备参数
|
||||
SiliconFlowImageOptions imageOptions = SiliconFlowImageOptions.builder()
|
||||
.model("Kwai-Kolors/Kolors")
|
||||
.build();
|
||||
ImagePrompt prompt = new ImagePrompt("万里长城", imageOptions);
|
||||
|
||||
// 方法调用
|
||||
ImageResponse response = imageModel.call(prompt);
|
||||
// 打印结果
|
||||
System.out.println(response);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,314 @@
|
||||
package cn.iocoder.yudao.framework.ai.ppt.wdd;
|
||||
|
||||
import cn.iocoder.yudao.framework.ai.core.model.wenduoduo.api.WddPptApi;
|
||||
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* {@link WddPptApi} 集成测试
|
||||
*
|
||||
* @author xiaoxin
|
||||
*/
|
||||
public class WddPptApiTests {
|
||||
|
||||
private final WddPptApi wddPptApi = new WddPptApi("https://docmee.cn");
|
||||
|
||||
private final String token = ""; // API Token
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void testCreateApiToken() {
|
||||
// 准备参数
|
||||
String apiKey = "";
|
||||
WddPptApi.CreateTokenRequest request = new WddPptApi.CreateTokenRequest(apiKey);
|
||||
// 调用方法
|
||||
String token = wddPptApi.createApiToken(request);
|
||||
// 打印结果
|
||||
System.out.println(token);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建任务
|
||||
*/
|
||||
@Test
|
||||
@Disabled
|
||||
public void testCreateTask() {
|
||||
WddPptApi.ApiResponse apiResponse = wddPptApi.createTask(token, 1, "dify 介绍", null);
|
||||
System.out.println(apiResponse);
|
||||
}
|
||||
|
||||
|
||||
@Test // 创建大纲
|
||||
@Disabled
|
||||
public void testGenerateOutlineRequest() {
|
||||
WddPptApi.CreateOutlineRequest request = new WddPptApi.CreateOutlineRequest(
|
||||
"1901539019628613632", "medium", null, null, null, null);
|
||||
// 调用
|
||||
Flux<Map<String, Object>> flux = wddPptApi.createOutline(token, request);
|
||||
StringBuffer contentBuffer = new StringBuffer();
|
||||
flux.doOnNext(chunk -> {
|
||||
contentBuffer.append(chunk.get("text"));
|
||||
if (Objects.equals(Integer.parseInt(String.valueOf(chunk.get("status"))), 4)) {
|
||||
// status 为 4,最终 markdown 结构树
|
||||
System.out.println(JsonUtils.toJsonString(chunk.get("result")));
|
||||
System.out.println(" ########################################################################");
|
||||
}
|
||||
}).then().block();
|
||||
// 打印结果
|
||||
System.out.println(contentBuffer);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改大纲
|
||||
*/
|
||||
@Test
|
||||
@Disabled
|
||||
public void testUpdateOutlineRequest() {
|
||||
WddPptApi.UpdateOutlineRequest request = new WddPptApi.UpdateOutlineRequest(
|
||||
"1901539019628613632", TEST_OUT_LINE_CONTENT, "精简一点,三个章节即可");
|
||||
// 调用
|
||||
Flux<Map<String, Object>> flux = wddPptApi.updateOutline(token, request);
|
||||
StringBuffer contentBuffer = new StringBuffer();
|
||||
flux.doOnNext(chunk -> {
|
||||
contentBuffer.append(chunk.get("text"));
|
||||
if (Objects.equals(Integer.parseInt(String.valueOf(chunk.get("status"))), 4)) {
|
||||
// status 为 4,最终 markdown 结构树
|
||||
System.out.println(JsonUtils.toJsonString(chunk.get("result")));
|
||||
System.out.println(" ########################################################################");
|
||||
}
|
||||
}).then().block();
|
||||
// 打印结果
|
||||
System.out.println(contentBuffer);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 PPT 模版分页
|
||||
*/
|
||||
@Test
|
||||
@Disabled
|
||||
public void testGetPptTemplatePage() {
|
||||
// 准备参数
|
||||
WddPptApi.TemplateQueryRequest.Filter filter = new WddPptApi.TemplateQueryRequest.Filter(
|
||||
1, null, null, null);
|
||||
WddPptApi.TemplateQueryRequest request = new WddPptApi.TemplateQueryRequest(1, 10, filter);
|
||||
// 调用
|
||||
WddPptApi.PagePptTemplateInfo pptTemplatePage = wddPptApi.getTemplatePage(token, request);
|
||||
// 打印结果
|
||||
System.out.println(pptTemplatePage);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成 PPT
|
||||
*/
|
||||
@Test
|
||||
@Disabled
|
||||
public void testGeneratePptx() {
|
||||
// 准备参数
|
||||
WddPptApi.CreatePptRequest request = new WddPptApi.CreatePptRequest("1901539019628613632", "1805081814809960448", TEST_OUT_LINE_CONTENT);
|
||||
// 调用
|
||||
WddPptApi.PptInfo pptInfo = wddPptApi.create(token, request);
|
||||
// 打印结果
|
||||
System.out.println(pptInfo);
|
||||
}
|
||||
|
||||
private final String TEST_OUT_LINE_CONTENT = """
|
||||
# Dify:新一代AI应用开发平台
|
||||
|
||||
## 1 什么是Dify
|
||||
### 1.1 Dify定义:AI应用开发平台
|
||||
#### 1.1.1 低代码开发
|
||||
Dify是一个低代码AI应用开发平台,旨在简化AI应用的构建过程,让开发者无需编写大量代码即可快速创建各种智能应用。
|
||||
#### 1.1.2 核心功能
|
||||
Dify的核心功能包括数据集成、模型选择、流程编排和应用部署,提供一站式解决方案,加速AI应用的落地和迭代。
|
||||
#### 1.1.3 开源与商业
|
||||
Dify提供开源版本和商业版本,满足不同用户的需求,开源版本适合个人开发者和小型团队,商业版本则提供更强大的功能和技术支持。
|
||||
|
||||
### 1.2 Dify解决的问题:AI开发痛点
|
||||
#### 1.2.1 开发周期长
|
||||
传统AI应用开发周期长,需要大量的人力和时间投入,Dify通过可视化界面和预置组件,大幅缩短开发周期。
|
||||
#### 1.2.2 技术门槛高
|
||||
AI技术门槛高,需要专业的知识和技能,Dify降低技术门槛,让更多开发者能够参与到AI应用的开发中来。
|
||||
#### 1.2.3 部署和维护复杂
|
||||
AI应用的部署和维护复杂,需要专业的运维团队,Dify提供自动化的部署和维护工具,简化流程,降低成本。
|
||||
|
||||
### 1.3 Dify发展历程
|
||||
#### 1.3.1 早期探索
|
||||
Dify的早期版本主要关注于自然语言处理领域的应用,通过集成各种NLP模型,提供文本分类、情感分析等功能。
|
||||
#### 1.3.2 功能扩展
|
||||
随着用户需求的不断增长,Dify的功能逐渐扩展到图像识别、语音识别等领域,支持更多类型的AI应用。
|
||||
#### 1.3.3 生态建设
|
||||
Dify积极建设开发者生态,提供丰富的文档、教程和案例,帮助开发者更好地使用Dify平台,共同推动AI技术的发展。
|
||||
|
||||
## 2 Dify的核心功能
|
||||
### 2.1 数据集成:连接各种数据源
|
||||
#### 2.1.1 支持多种数据源
|
||||
Dify支持连接各种数据源,包括关系型数据库、NoSQL数据库、文件系统、云存储等,满足不同场景的数据需求。
|
||||
#### 2.1.2 数据转换和清洗
|
||||
Dify提供数据转换和清洗功能,可以将不同格式的数据转换为统一的格式,并去除无效数据,提高数据质量。
|
||||
#### 2.1.3 数据安全
|
||||
Dify注重数据安全,采用各种安全措施保护用户的数据,包括数据加密、访问控制、权限管理等。
|
||||
|
||||
### 2.2 模型选择:丰富的AI模型库
|
||||
#### 2.2.1 预置模型
|
||||
Dify预置了丰富的AI模型,包括自然语言处理、图像识别、语音识别等领域的模型,开发者可以直接使用这些模型,无需自行训练,极大的简化了开发流程。
|
||||
#### 2.2.2 自定义模型
|
||||
Dify支持开发者上传自定义模型,满足个性化的需求。开发者可以将自己训练的模型部署到Dify平台上,与其他开发者共享。
|
||||
#### 2.2.3 模型评估
|
||||
Dify提供模型评估功能,可以对不同模型进行评估,选择最优的模型,提高应用性能。
|
||||
|
||||
### 2.3 流程编排:可视化流程设计器
|
||||
#### 2.3.1 可视化界面
|
||||
Dify提供可视化的流程设计器,开发者可以通过拖拽组件的方式,设计AI应用的流程,无需编写代码,简单高效。
|
||||
#### 2.3.2 灵活的流程控制
|
||||
Dify支持灵活的流程控制,可以根据不同的条件执行不同的分支,实现复杂的业务逻辑。
|
||||
#### 2.3.3 实时调试
|
||||
Dify提供实时调试功能,可以在设计流程的过程中,实时查看流程的执行结果,及时发现和解决问题。
|
||||
|
||||
### 2.4 应用部署:一键部署和管理
|
||||
#### 2.4.1 快速部署
|
||||
Dify提供一键部署功能,可以将AI应用快速部署到各种环境,包括本地环境、云环境、容器环境等。
|
||||
#### 2.4.2 自动伸缩
|
||||
Dify支持自动伸缩,可以根据应用的负载自动调整资源,保证应用的稳定性和性能。
|
||||
#### 2.4.3 监控和告警
|
||||
Dify提供监控和告警功能,可以实时监控应用的状态,并在出现问题时及时告警,方便运维人员进行处理。
|
||||
|
||||
## 3 Dify的特点和优势
|
||||
### 3.1 低代码:降低开发门槛
|
||||
#### 3.1.1 可视化开发
|
||||
Dify采用可视化开发模式,开发者无需编写大量代码,只需通过拖拽组件即可完成AI应用的开发,降低了开发门槛。
|
||||
#### 3.1.2 预置组件
|
||||
Dify预置了丰富的组件,包括数据源组件、模型组件、流程控制组件等,开发者可以直接使用这些组件,提高开发效率。
|
||||
#### 3.1.3 减少代码量
|
||||
Dify可以显著减少代码量,降低开发难度,让更多开发者能够参与到AI应用的开发中来。
|
||||
|
||||
### 3.2 灵活:满足不同场景需求
|
||||
#### 3.2.1 支持多种数据源
|
||||
Dify支持多种数据源,可以连接各种数据源,满足不同场景的数据需求。
|
||||
#### 3.2.2 支持自定义模型
|
||||
Dify支持自定义模型,开发者可以将自己训练的模型部署到Dify平台上,满足个性化的需求。
|
||||
#### 3.2.3 灵活的流程控制
|
||||
Dify支持灵活的流程控制,可以根据不同的条件执行不同的分支,实现复杂的业务逻辑。
|
||||
|
||||
### 3.3 高效:加速应用落地
|
||||
#### 3.3.1 快速开发
|
||||
Dify通过可视化界面和预置组件,大幅缩短开发周期,加速AI应用的落地。
|
||||
#### 3.3.2 快速部署
|
||||
Dify提供一键部署功能,可以将AI应用快速部署到各种环境,提高部署效率。
|
||||
#### 3.3.3 自动化运维
|
||||
Dify提供自动化的运维工具,简化运维流程,降低运维成本。
|
||||
|
||||
### 3.4 开放:构建繁荣生态
|
||||
#### 3.4.1 开源社区
|
||||
Dify拥有活跃的开源社区,开发者可以在社区中交流经验、分享资源、共同推动Dify的发展。
|
||||
#### 3.4.2 丰富的文档
|
||||
Dify提供丰富的文档、教程和案例,帮助开发者更好地使用Dify平台。
|
||||
#### 3.4.3 API支持
|
||||
Dify提供API支持,开发者可以通过API将Dify集成到自己的系统中,扩展Dify的功能。
|
||||
|
||||
## 4 Dify的使用场景
|
||||
### 4.1 智能客服:提升客户服务质量
|
||||
#### 4.1.1 自动回复
|
||||
Dify可以用于构建智能客服系统,实现自动回复客户的常见问题,提高客户服务效率。
|
||||
#### 4.1.2 情感分析
|
||||
Dify可以对客户的语音或文本进行情感分析,判断客户的情绪,并根据情绪提供个性化的服务。
|
||||
#### 4.1.3 知识库问答
|
||||
Dify可以构建知识库问答系统,让客户通过提问的方式获取所需的信息,提高客户满意度。
|
||||
|
||||
### 4.2 金融风控:提高风险识别能力
|
||||
#### 4.2.1 欺诈检测
|
||||
Dify可以用于构建金融风控系统,实现欺诈检测,识别可疑交易,降低风险。
|
||||
#### 4.2.2 信用评估
|
||||
Dify可以对用户的信用进行评估,并根据评估结果提供不同的金融服务。
|
||||
#### 4.2.3 反洗钱
|
||||
Dify可以用于反洗钱,识别可疑资金流动,防止犯罪行为。
|
||||
|
||||
### 4.3 智慧医疗:提升医疗服务水平
|
||||
#### 4.3.1 疾病诊断
|
||||
Dify可以用于辅助疾病诊断,提高诊断准确率,缩短诊断时间。
|
||||
#### 4.3.2 药物研发
|
||||
Dify可以用于药物研发,加速新药的发现和开发。
|
||||
#### 4.3.3 智能健康管理
|
||||
Dify可以构建智能健康管理系统,为用户提供个性化的健康建议和服务。
|
||||
|
||||
### 4.4 智慧城市:提升城市管理效率
|
||||
#### 4.4.1 交通优化
|
||||
Dify可以用于交通优化,提高交通效率,缓解交通拥堵。
|
||||
#### 4.4.2 环境监测
|
||||
Dify可以用于环境监测,实时监测空气质量、水质等环境指标,及时发现和解决环境问题。
|
||||
#### 4.4.3 智能安防
|
||||
Dify可以用于智能安防,提高城市安全水平,预防犯罪行为。
|
||||
|
||||
## 5 Dify的成功案例
|
||||
### 5.1 Case 1:某电商平台的智能客服
|
||||
#### 5.1.1 项目背景
|
||||
该电商平台客户服务压力大,人工客服成本高,需要一种智能化的解决方案。
|
||||
#### 5.1.2 解决方案
|
||||
使用Dify构建智能客服系统,实现自动回复客户的常见问题,并根据客户的情绪提供个性化的服务。
|
||||
#### 5.1.3 效果
|
||||
客户服务效率提高50%,客户满意度提高20%,人工客服成本降低30%。
|
||||
|
||||
### 5.2 Case 2:某银行的金融风控系统
|
||||
#### 5.2.1 项目背景
|
||||
该银行面临日益增长的金融风险,需要一种更有效的风险识别和控制手段。
|
||||
#### 5.2.2 解决方案
|
||||
使用Dify构建金融风控系统,实现欺诈检测、信用评估和反洗钱等功能,提高风险识别能力。
|
||||
#### 5.2.3 效果
|
||||
欺诈交易识别率提高40%,信用评估准确率提高30%,洗钱风险降低25%。
|
||||
|
||||
### 5.3 Case 3:某医院的辅助疾病诊断系统
|
||||
#### 5.3.1 项目背景
|
||||
该医院医生工作压力大,疾病诊断准确率有待提高,需要一种辅助诊断工具。
|
||||
#### 5.3.2 解决方案
|
||||
使用Dify构建辅助疾病诊断系统,根据患者的病历和症状,提供诊断建议,提高诊断准确率。
|
||||
#### 5.3.3 效果
|
||||
疾病诊断准确率提高20%,诊断时间缩短15%,医生工作效率提高10%。
|
||||
|
||||
## 6 Dify的未来展望
|
||||
### 6.1 技术升级
|
||||
#### 6.1.1 模型优化
|
||||
Dify将不断优化预置模型,提高模型性能,并支持更多类型的AI模型。
|
||||
#### 6.1.2 流程引擎升级
|
||||
Dify将升级流程引擎,提高流程的灵活性和可扩展性,支持更复杂的业务逻辑。
|
||||
#### 6.1.3 平台性能优化
|
||||
Dify将不断优化平台性能,提高平台的稳定性和可靠性,满足大规模应用的需求。
|
||||
|
||||
### 6.2 生态建设
|
||||
#### 6.2.1 社区建设
|
||||
Dify将继续加强开源社区建设,吸引更多开发者参与,共同推动Dify的发展。
|
||||
#### 6.2.2 合作伙伴拓展
|
||||
Dify将拓展合作伙伴,与更多的企业和机构合作,共同推动AI技术的应用。
|
||||
#### 6.2.3 应用商店
|
||||
Dify将构建应用商店,让开发者可以分享自己的应用,用户可以购买和使用这些应用,构建繁荣的生态系统。
|
||||
|
||||
### 6.3 应用领域拓展
|
||||
#### 6.3.1 智能制造
|
||||
Dify将拓展到智能制造领域,为企业提供智能化的生产管理和质量控制解决方案。
|
||||
#### 6.3.2 智慧农业
|
||||
Dify将拓展到智慧农业领域,为农民提供智能化的种植和养殖管理解决方案。
|
||||
#### 6.3.3 更多领域
|
||||
Dify将拓展到更多领域,为各行各业提供智能化的解决方案,推动社会发展。
|
||||
|
||||
## 7 总结
|
||||
### 7.1 Dify的价值
|
||||
#### 7.1.1 降低AI开发门槛
|
||||
Dify通过低代码的方式,让更多开发者能够参与到AI应用的开发中来。
|
||||
#### 7.1.2 加速AI应用落地
|
||||
Dify提供一站式解决方案,加速AI应用的落地和迭代。
|
||||
#### 7.1.3 构建繁荣的AI生态
|
||||
Dify通过开源社区和应用商店,构建繁荣的AI生态系统。
|
||||
|
||||
### 7.2 共同发展
|
||||
#### 7.2.1 欢迎加入Dify社区
|
||||
欢迎更多开发者加入Dify社区,共同推动Dify的发展。
|
||||
#### 7.2.2 合作共赢
|
||||
期待与更多的企业和机构合作,共同推动AI技术的应用。
|
||||
#### 7.2.3 共创未来
|
||||
让我们一起用AI技术改变世界,共创美好未来。""";
|
||||
|
||||
}
|
@ -0,0 +1,318 @@
|
||||
package cn.iocoder.yudao.framework.ai.ppt.xunfei;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.xinghuo.api.XunfeiPptApi;
|
||||
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.mock.web.MockMultipartFile;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* {@link XunfeiPptApi} 集成测试
|
||||
*
|
||||
* @author xiaoxin
|
||||
*/
|
||||
public class XunfeiPptApiTests {
|
||||
|
||||
// 讯飞 API 配置信息,实际使用时请替换为您的应用信息
|
||||
private static final String APP_ID = "";
|
||||
private static final String API_SECRET = "";
|
||||
|
||||
private final XunfeiPptApi xunfeiPptApi = new XunfeiPptApi(XunfeiPptApi.BASE_URL, APP_ID, API_SECRET);
|
||||
|
||||
/**
|
||||
* 获取 PPT 模板列表
|
||||
*/
|
||||
@Test
|
||||
@Disabled
|
||||
public void testGetTemplatePage() {
|
||||
// 调用方法
|
||||
XunfeiPptApi.TemplatePageResponse response = xunfeiPptApi.getTemplatePage("商务", 10);
|
||||
// 打印结果
|
||||
System.out.println("模板列表响应:" + JsonUtils.toJsonString(response));
|
||||
|
||||
if (response != null && response.data() != null && response.data().records() != null) {
|
||||
System.out.println("模板总数:" + response.data().total());
|
||||
System.out.println("当前页码:" + response.data().pageNum());
|
||||
System.out.println("模板数量:" + response.data().records().size());
|
||||
|
||||
// 打印第一个模板的信息(如果存在)
|
||||
if (!response.data().records().isEmpty()) {
|
||||
XunfeiPptApi.TemplateInfo firstTemplate = response.data().records().get(0);
|
||||
System.out.println("模板ID:" + firstTemplate.templateIndexId());
|
||||
System.out.println("模板风格:" + firstTemplate.style());
|
||||
System.out.println("模板颜色:" + firstTemplate.color());
|
||||
System.out.println("模板行业:" + firstTemplate.industry());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建大纲(通过文本)
|
||||
*/
|
||||
@Test
|
||||
@Disabled
|
||||
public void testCreateOutline() {
|
||||
XunfeiPptApi.CreateResponse response = getCreateResponse();
|
||||
// 打印结果
|
||||
System.out.println("创建大纲响应:" + JsonUtils.toJsonString(response));
|
||||
|
||||
// 保存 sid 和 outline 用于后续测试
|
||||
if (response != null && response.data() != null) {
|
||||
System.out.println("sid: " + response.data().sid());
|
||||
if (response.data().outline() != null) {
|
||||
// 使用 OutlineData 的 toJsonString 方法
|
||||
System.out.println("outline: " + response.data().outline().toJsonString());
|
||||
// 将 outline 对象转换为 JSON 字符串,用于后续 createPptByOutline 测试
|
||||
String outlineJson = response.data().outline().toJsonString();
|
||||
System.out.println("可用于 createPptByOutline 的 outline 字符串: " + outlineJson);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建大纲(通过文本)
|
||||
* @return 创建大纲响应
|
||||
*/
|
||||
private XunfeiPptApi.CreateResponse getCreateResponse() {
|
||||
String param = "智能体平台 Dify 介绍";
|
||||
return xunfeiPptApi.createOutline(param);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过大纲创建 PPT(完整参数)
|
||||
*/
|
||||
@Test
|
||||
@Disabled
|
||||
public void testCreatePptByOutlineWithFullParams() {
|
||||
// 创建大纲对象
|
||||
XunfeiPptApi.CreateResponse createResponse = getCreateResponse();
|
||||
// 调用方法
|
||||
XunfeiPptApi.CreateResponse response = xunfeiPptApi.createPptByOutline(createResponse.data().outline(), "精简一些,不要超过6个章节");
|
||||
// 打印结果
|
||||
System.out.println("通过大纲创建 PPT 响应:" + JsonUtils.toJsonString(response));
|
||||
|
||||
// 保存sid用于后续进度查询
|
||||
if (response != null && response.data() != null) {
|
||||
System.out.println("sid: " + response.data().sid());
|
||||
if (response.data().coverImgSrc() != null) {
|
||||
System.out.println("封面图片: " + response.data().coverImgSrc());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查 PPT 生成进度
|
||||
*/
|
||||
@Test
|
||||
@Disabled
|
||||
public void testCheckProgress() {
|
||||
// 准备参数 - 使用之前创建 PPT 时返回的 sid
|
||||
String sid = "e96dac09f2ec4ee289f029a5fb874ecd"; // 替换为实际的sid
|
||||
|
||||
// 调用方法
|
||||
XunfeiPptApi.ProgressResponse response = xunfeiPptApi.checkProgress(sid);
|
||||
// 打印结果
|
||||
System.out.println("检查进度响应:" + JsonUtils.toJsonString(response));
|
||||
|
||||
// 安全地访问响应数据
|
||||
if (response != null && response.data() != null) {
|
||||
XunfeiPptApi.ProgressResponseData data = response.data();
|
||||
|
||||
// 打印PPT生成状态
|
||||
System.out.println("PPT 构建状态: " + data.pptStatus());
|
||||
System.out.println("AI 配图状态: " + data.aiImageStatus());
|
||||
System.out.println("演讲备注状态: " + data.cardNoteStatus());
|
||||
|
||||
// 打印进度信息
|
||||
if (data.totalPages() != null && data.donePages() != null) {
|
||||
System.out.println("总页数: " + data.totalPages());
|
||||
System.out.println("已完成页数: " + data.donePages());
|
||||
System.out.println("完成进度: " + data.getProgressPercent() + "%");
|
||||
} else {
|
||||
System.out.println("进度: " + data.process() + "%");
|
||||
}
|
||||
|
||||
// 检查是否完成
|
||||
if (data.isAllDone()) {
|
||||
System.out.println("PPT 生成已完成!");
|
||||
System.out.println("PPT 下载链接: " + data.pptUrl());
|
||||
}
|
||||
// 检查是否失败
|
||||
else if (data.isFailed()) {
|
||||
System.out.println("PPT 生成失败!");
|
||||
System.out.println("错误信息: " + data.errMsg());
|
||||
}
|
||||
// 正在进行中
|
||||
else {
|
||||
System.out.println("PPT 生成中,请稍后再查询...");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 轮询检查 PPT 生成进度直到完成
|
||||
*/
|
||||
@Test
|
||||
@Disabled
|
||||
public void testPollCheckProgress() throws InterruptedException {
|
||||
// 准备参数 - 使用之前创建 PP T时返回的 sid
|
||||
String sid = "fa36e926f2ed434987fcb4c1f0776ffb"; // 替换为实际的sid
|
||||
|
||||
// 最大轮询次数
|
||||
int maxPolls = 20;
|
||||
// 轮询间隔(毫秒)- 讯飞 API 限流为 3 秒一次
|
||||
long pollInterval = 3500;
|
||||
|
||||
for (int i = 0; i < maxPolls; i++) {
|
||||
System.out.println("第" + (i + 1) + "次查询进度...");
|
||||
|
||||
// 调用方法
|
||||
XunfeiPptApi.ProgressResponse response = xunfeiPptApi.checkProgress(sid);
|
||||
|
||||
// 安全地访问响应数据
|
||||
if (response != null && response.data() != null) {
|
||||
XunfeiPptApi.ProgressResponseData data = response.data();
|
||||
|
||||
// 打印进度信息
|
||||
System.out.println("PPT 构建状态: " + data.pptStatus());
|
||||
if (data.totalPages() != null && data.donePages() != null) {
|
||||
System.out.println("完成进度: " + data.donePages() + "/" + data.totalPages()
|
||||
+ " (" + data.getProgressPercent() + "%)");
|
||||
}
|
||||
|
||||
// 检查是否完成
|
||||
if (data.isAllDone()) {
|
||||
System.out.println("PPT 生成已完成!");
|
||||
System.out.println("PPT 下载链接: " + data.pptUrl());
|
||||
break;
|
||||
}
|
||||
// 检查是否失败
|
||||
else if (data.isFailed()) {
|
||||
System.out.println("PPT 生成失败!");
|
||||
System.out.println("错误信息: " + data.errMsg());
|
||||
break;
|
||||
}
|
||||
// 正在进行中,继续轮询
|
||||
else {
|
||||
System.out.println("PPT 生成中,等待" + (pollInterval / 1000) + "秒后继续查询...");
|
||||
Thread.sleep(pollInterval);
|
||||
}
|
||||
} else {
|
||||
System.out.println("查询失败,等待" + (pollInterval / 1000) + "秒后重试...");
|
||||
Thread.sleep(pollInterval);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 直接创建 PPT(通过文本)
|
||||
*/
|
||||
@Test
|
||||
@Disabled
|
||||
public void testCreatePptByText() {
|
||||
// 准备参数
|
||||
String query = "合肥天气趋势分析,包括近5年的气温变化、降水量变化、极端天气事件,以及对城市生活的影响";
|
||||
|
||||
// 调用方法
|
||||
XunfeiPptApi.CreateResponse response = xunfeiPptApi.create(query);
|
||||
// 打印结果
|
||||
System.out.println("直接创建 PPT 响应:" + JsonUtils.toJsonString(response));
|
||||
|
||||
// 保存 sid 用于后续进度查询
|
||||
if (response != null && response.data() != null) {
|
||||
System.out.println("sid: " + response.data().sid());
|
||||
if (response.data().coverImgSrc() != null) {
|
||||
System.out.println("封面图片: " + response.data().coverImgSrc());
|
||||
}
|
||||
System.out.println("标题: " + response.data().title());
|
||||
System.out.println("副标题: " + response.data().subTitle());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 直接创建 PPT(通过文件)
|
||||
*/
|
||||
@Test
|
||||
@Disabled
|
||||
public void testCreatePptByFile() {
|
||||
// 准备参数
|
||||
File file = new File("src/test/resources/test.txt"); // 请确保此文件存在
|
||||
MultipartFile multipartFile = convertFileToMultipartFile(file);
|
||||
|
||||
// 调用方法
|
||||
XunfeiPptApi.CreateResponse response = xunfeiPptApi.create(multipartFile, file.getName());
|
||||
// 打印结果
|
||||
System.out.println("通过文件创建PPT响应:" + JsonUtils.toJsonString(response));
|
||||
|
||||
// 保存 sid 用于后续进度查询
|
||||
if (response != null && response.data() != null) {
|
||||
System.out.println("sid: " + response.data().sid());
|
||||
if (response.data().coverImgSrc() != null) {
|
||||
System.out.println("封面图片: " + response.data().coverImgSrc());
|
||||
}
|
||||
System.out.println("标题: " + response.data().title());
|
||||
System.out.println("副标题: " + response.data().subTitle());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 直接创建 PPT(完整参数)
|
||||
*/
|
||||
@Test
|
||||
@Disabled
|
||||
public void testCreatePptWithFullParams() {
|
||||
// 准备参数
|
||||
String query = "合肥天气趋势分析,包括近 5 年的气温变化、降水量变化、极端天气事件,以及对城市生活的影响";
|
||||
|
||||
// 创建请求对象
|
||||
XunfeiPptApi.CreatePptRequest request = XunfeiPptApi.CreatePptRequest.builder()
|
||||
.query(query)
|
||||
.language("cn")
|
||||
.isCardNote(true)
|
||||
.search(true)
|
||||
.isFigure(true)
|
||||
.aiImage("advanced")
|
||||
.author("测试用户")
|
||||
.build();
|
||||
|
||||
// 调用方法
|
||||
XunfeiPptApi.CreateResponse response = xunfeiPptApi.create(request);
|
||||
// 打印结果
|
||||
System.out.println("使用完整参数创建 PPT 响应:" + JsonUtils.toJsonString(response));
|
||||
|
||||
// 保存 sid 用于后续进度查询
|
||||
if (response != null && response.data() != null) {
|
||||
String sid = response.data().sid();
|
||||
System.out.println("sid: " + sid);
|
||||
if (response.data().coverImgSrc() != null) {
|
||||
System.out.println("封面图片: " + response.data().coverImgSrc());
|
||||
}
|
||||
System.out.println("标题: " + response.data().title());
|
||||
System.out.println("副标题: " + response.data().subTitle());
|
||||
|
||||
// 立即查询一次进度
|
||||
System.out.println("立即查询进度...");
|
||||
XunfeiPptApi.ProgressResponse progressResponse = xunfeiPptApi.checkProgress(sid);
|
||||
if (progressResponse != null && progressResponse.data() != null) {
|
||||
XunfeiPptApi.ProgressResponseData progressData = progressResponse.data();
|
||||
System.out.println("PPT 构建状态: " + progressData.pptStatus());
|
||||
if (progressData.totalPages() != null && progressData.donePages() != null) {
|
||||
System.out.println("完成进度: " + progressData.donePages() + "/" + progressData.totalPages()
|
||||
+ " (" + progressData.getProgressPercent() + "%)");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 File 转换为 MultipartFile
|
||||
*/
|
||||
private MultipartFile convertFileToMultipartFile(File file) {
|
||||
return new MockMultipartFile("file", file.getName(), "text/plain", FileUtil.readBytes(file));
|
||||
}
|
||||
|
||||
}
|
@ -212,6 +212,10 @@ yudao:
|
||||
appKey: 75b161ed2aef4719b275d6e7f2a4d4cd
|
||||
secretKey: YWYxYWI2MTA4ODI2NGZlYTQyNjAzZTcz
|
||||
model: generalv3.5
|
||||
baichuan: # 百川智能
|
||||
enable: true
|
||||
api-key: sk-abc
|
||||
model: Baichuan4-Turbo
|
||||
midjourney:
|
||||
enable: true
|
||||
# base-url: https://api.holdai.top/mj-relax/mj
|
||||
|
Loading…
x
Reference in New Issue
Block a user