芋道源码相信很多朋友都很了解了,今天我们试着基于FastGPT实现芋道框架的代码生成。芋道的代码生成,是基于数据库表字段实现的,那我们的思路就是看看如何使用GPT帮我们生成数据库表结构,只要数据库表字段有了,代码也就生成好了。实现这个需求我们就需要用到FastGPT的高级编排功能。编排的整体思路如下:

FastGPT应用编排
- 1.登录FastGPT,打开应用菜单,点击右上角"新建"按钮,创建一个AI应用(选择"简单的对话"模板即可)。

- 2.进入高级编排页面,先定义"对话开场白",然后点击右上角保存按钮。

- 3.根据前述的整体思路,开始第一个AI环节:需求分析和模型设计。我们需要选择该环节对话模型(本文我选择GPT4-Turbo演示)和设计需求分析提示词。提示词主要是预设身份,并给出需求分析的要求,我这里主要是要求将需求抽象为模型和字段,并以markdown格式输出。配置完成后,保存,然后测试看下效果。


- 4.第二个AI环节:判断用户是否已对需求分析和模型设计是否满意,是否可以进入下一环节。在这里我们需要在AI每一次输出需求分析和模型设计后,调用FastGPT的"问题分类"模块,判断用户意图。同样的,我们需要选择分类模型,设计分类提示词,并测试分类结果。


- 5.第三个AI环节:用户已经对需求分析和模型设计结果完成确认,那么接下来就需要让AI帮我们生成SQL语句了。在这里我们为方便后续SQL语句的提取,要求AI以json数组形式返回建表SQL语句,并且要求AI在每张表按芋道规范添加公共字段(如创建者、创建时间、修改者、修改时间、逻辑删除、租户ID字段),大家也可以添加自己的要求。


- 6.第四个AI环节(可选):如果只是为满足单表代码生成,这一步就可以直接跳过。这个环节主要是让AI根据模型设计和生成的SQL来分析这些表之间是否存在主子表、树表,并且将分析结果使用json格式将分析结果返回,方便后续调用接口生成主子表和树表代码用。


- 7.将AI生成好的SQL和表关系数据做为参数,调用我们自定义的接口,生成代码并返回代码下载地址,我们将代码下载地址显示在页面上,用户就可以愉快下载了。


- 8.扫码关注我的微信公众号,回复"芋道助手"就可以获取完整的FastGPT应用配置了(包含提示词)。

附:芋道自定义接口示例(供参考)
- 1.我们在芋道工程中自定义一个接口(使用路径传参是因为这样在FastGPT接口配置中传递固定参数很方便,接口也比较好管理),接收上述参数,提取SQL并在数据库建表、按芋道规范生成代码。这里给出接口示例:
- @PermitAll
- @PostMapping("busi/{busiType}")
- public FastCommonRespVO busi(@PathVariable("busiType") String busiType, @RequestBody Map<String, Object> reqMap) {
- log.info("请求参数: {}", JSONUtil.toJsonStr(reqMap));
- FastCommonRespVO fastCommonRespVO = new FastCommonRespVO();
- fastCommonRespVO.setResponse("\\n失败了");
- FastBusiTypeEnum fastBusiTypeEnum = FastBusiTypeEnum.getByBusiCode(busiType);
- if (fastBusiTypeEnum != null) {
- FastService fastService = SpringUtil.getBean("fast".concat(fastBusiTypeEnum.getBusiCode()).concat("ServiceImpl"));
- try {
- // doBusi方法会返回一个完整的代码下载地址
- fastCommonRespVO.setResponse("\\n".concat(fastService.doBusi(reqMap)));
- } catch (Exception e) {
- log.error("执行业务逻辑失败,异常原因:{}", ExceptionUtils.getStackTrace(e));
- fastCommonRespVO.setResponse("\\n执行业务逻辑失败:".concat(e.getMessage()));
- }
- } else {
- fastCommonRespVO.setResponse("\\n非法的业务类型(busiType参数)");
- }
- log.info("响应结果: {}", JSONUtil.toJsonStr(fastCommonRespVO));
- return fastCommonRespVO;
- }
复制代码
- 2.这里还需要在芋道中自定义一个代码下载接口,方便用户下载。
- @PermitAll
- @GetMapping("/download/codegen/{codegenId}")
- public void downloadCodegen(@PathVariable("codegenId") String codegenId,
- HttpServletResponse response) throws IOException {
- log.info("请求参数: codegenId={}", codegenId);
- // 根据生成代码记录ID查询相关表记录ID
- Set<String> tableIds = stringRedisTemplate.opsForSet().members(codegenId);
- if (CollectionUtils.isEmpty(tableIds)) {
- throw exception(ErrorCodeConstants.AIGC_FAST_NOT_FOUND_RECORD);
- }
- // 生成代码
- Map<String, String> codes = new HashMap<>();
- for (String tableId : tableIds) {
- codes.putAll(codegenApi.generationCodes(Long.valueOf(tableId)));
- }
- // 构建 zip 包
- String[] paths = codes.keySet().toArray(new String[0]);
- ByteArrayInputStream[] ins = codes.values().stream().map(IoUtil::toUtf8Stream).toArray(ByteArrayInputStream[]::new);
- ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
- ZipUtil.zip(outputStream, paths, ins);
- // 输出
- ServletUtils.writeAttachment(response, "codegen.zip", outputStream.toByteArray());
- }
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |