经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 大数据/云/AI » 人工智能基础 » 查看文章
我在 vscode 插件里接入了 ChatGPT,解决了代码变量命名的难题
来源:cnblogs  作者:若邪  时间:2023/6/14 11:09:34  对本文有异议

gpt1.gif

lowcode 插件 已经迭代了差不多3年。作为我的生产力工具,平常一些不需要动脑的搬砖活基本上都是用 lowcode 去完成,比如管理脚手架,生成 CURD 页面,根据接口文档生成 TS 类型,生成 mock 等等。

借助 lowcode 的区块物料的功能,能快速生成 CURD 页面,但是前一段时间在做一些财务相关的需求时,变量的命名成了一个难题,一个列表十几二十个字段,而且大部分是那种看着中文都不知道是什么意思的抽象名词。做着做着我简单粗暴的使用 column1 ~ column20 去命名(反正一个个去翻译出来我也不认识)。

同事提了一嘴 "变量命名让 ChatGPT 去做",然后我就开始去研究 ChatGPT 命名:

image.png

看起来问题不大,之后就是在 lowcode 插件里接入 ChatGPT API 了。

开发过程中研究了几个 vscode 上下载量比较多的 ChatGPT 插件,基本上大同小异,都是在右键菜单里加了分析代码,重构代码,给代码写单元测试,给代码找缺陷的固定选项。假如我想要 ChatGPT 将我选中的代码的里的中文变量翻译成英文,需要每次复制粘贴代码,写 Prompt。

借助 lowcode 原有的代码片段的功能,几乎毫不费劲的就实现了预置 Prompt 的功能,如下:

image.png

目前 lowcode 已经支持接入 openai 官方的 api,也可以使用国内的一些收费的中转服务,下面介绍使用方法。

配置 ChatGPT

gpt.png

预置 Prompt 模板

使用 lowcode 原有代码片段功能,可以随意预置 Prompt,支持 EJS 模板语法,可快速创建分析代码、重构代码、代码添加注释等 Prompt。

gpt1.png

拉到最底部,配置 chatGPT 字段:

gpt2.png

commandPrompt 既右键菜单选择模板后发送的内容,支持 EJS 模板语法。

viewPrompt 为 代码片段或者区块物料可视化详情页点 Ask ChatGPT 按钮后发送的内容。

lowcode 代码生成功能结合 ChatGPT

配置生成 CURD 界面的时候,如果全部使用中文命名,根据模板会生成如下的代码:

  1. import { reactive, ref } from "vue";
  2. interface ITableListItem {
  3. id: string;
  4. 成本中心编码: string;
  5. 成本中心名称: string;
  6. 账套编码: string;
  7. 银行核算编码: string;
  8. 订单号: string;
  9. 订单金额: string;
  10. 确收时间: string;
  11. "劳务成本-不含税": string;
  12. }
  13. interface IFormData {
  14. 成本中心编码?: string;
  15. 成本中心名称?: string;
  16. 账套编码?: string;
  17. 银行核算编码?: string;
  18. 订单号?: string;
  19. 订单金额?: string;
  20. 确收时间?: string;
  21. "劳务成本-不含税"?: string;
  22. }
  23. const defaultFormData: IFormData = {
  24. 成本中心编码: undefined,
  25. 成本中心名称: undefined,
  26. 账套编码: undefined,
  27. 银行核算编码: undefined,
  28. 订单号: undefined,
  29. 订单金额: undefined,
  30. 确收时间: undefined,
  31. "劳务成本-不含税": undefined,
  32. };
  33. export const useModel = () => {
  34. const filterForm = reactive<IFormData>({ ...defaultFormData });
  35. const tableList = ref<(ITableListItem & { [propName: string]: unknown })[]>(
  36. [],
  37. );
  38. const pagination = reactive<{
  39. page: number;
  40. pageSize: number;
  41. total: number;
  42. }>({
  43. page: 1,
  44. pageSize: 10,
  45. total: 0,
  46. });
  47. const loading = reactive<{ list: boolean }>({
  48. list: false,
  49. });
  50. return {
  51. filterForm,
  52. tableList,
  53. pagination,
  54. loading,
  55. };
  56. };
  57. export type Model = ReturnType<typeof useModel>;

ChatGPT 处理之后:

  1. import { reactive, ref } from "vue";
  2. interface ITableListItem {
  3. id: string;
  4. costCenterCode: string;
  5. costCenterName: string;
  6. accountingCode: string;
  7. bankAccountingCode: string;
  8. orderNumber: string;
  9. orderAmount: string;
  10. confirmedTime: string;
  11. laborCostExcludingTax: string;
  12. }
  13. interface IFormData {
  14. costCenterCode?: string;
  15. costCenterName?: string;
  16. accountingCode?: string;
  17. bankAccountingCode?: string;
  18. orderNumber?: string;
  19. orderAmount?: string;
  20. confirmedTime?: string;
  21. laborCostExcludingTax?: string;
  22. }
  23. const defaultFormData: IFormData = {
  24. costCenterCode: undefined,
  25. costCenterName: undefined,
  26. accountingCode: undefined,
  27. bankAccountingCode: undefined,
  28. orderNumber: undefined,
  29. orderAmount: undefined,
  30. confirmedTime: undefined,
  31. laborCostExcludingTax: undefined,
  32. };
  33. export const useModel = () => {
  34. const filterForm = reactive<IFormData>({ ...defaultFormData });
  35. const tableList = ref<(ITableListItem & { [propName: string]: unknown })[]>(
  36. [],
  37. );
  38. const pagination = reactive<{
  39. page: number;
  40. pageSize: number;
  41. total: number;
  42. }>({
  43. page: 1,
  44. pageSize: 10,
  45. total: 0,
  46. });
  47. const loading = reactive<{ list: boolean }>({
  48. list: false,
  49. });
  50. return {
  51. filterForm,
  52. tableList,
  53. pagination,
  54. loading,
  55. };
  56. };
  57. export type Model = ReturnType<typeof useModel>;

gpt.gif

原文链接:https://www.cnblogs.com/jaycewu/p/17476198.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号