chatgpt生成代码尝试

讲师: 万维读客  分类:学习  时间: January 7, 2023

chatgpt生成代码尝试

chatgpt

看了几个chatgpt的视频,看别人生成代码挺溜的,自己想试试。试了下安装github copilot,结果需要交钱,而且不太稳定。 不过看openai官方网站有个演示的视频,的确很厉害,chatgpt有两个模型,一个是基于gpt3的就是我们知道的最火的聊天机器人。还有一个就是基于codex模型,主要是能够写代码做题,这个也很厉害。

看github copilot接受底层是接入了chatgpt服务的,所以拿来试一下。不知道有多精确,但是基于注释来写代码,还要看注释描述的有多精确。

modelscope魔搭社区

这个是阿里云去年云栖大会宣布开放的社区,我试一试的心态看上面有没有类似的代码生成模型,结果真找到了:

  • CodeGeeX-代码生成-13B,一家叫做智谱AI公司做的,看介绍挺牛逼的,模型比较大,但是提供免费的vscode插件使用,使用方法基本和github的copilot一样
  • InCoder,Facebook公司出的一个开源模型,有小模型和大模型,背后是一篇论文介绍了具体的方法,底层基于transformer。小模型下下来本地可以跑。
  • CodeGen,salesforce公司出的,同样有两个模型,可以拿来测试。

测试

我用InCoder模型测了下,反复测试写代码能力,如果注释写的清楚,采用填充函数的方式,还是可以接受的。但是开放模式写的代码就很乱,生成很多没用的代码。


// load the image '/static/img/road' and add to the body
const body = document.body;

// create new image element and set its src
//--- generate---
const img = new Image();
img.src = '/static/img/road.png';
img.onload = () => {
  // set the image element to show in the body
  body.appendChild(img);
};
//---

// create a div and make the img as the div background, set the background repeatin x not in y
const div = document.createElement('div');
//--- generate---
div.setAttribute('id', 'road');
div.setAttribute('style', 'background-image: url(' + img.src + ');background-repeat: repeat no-repeat;');
div.style.width = '100%';
div.style.height = '400px';
body.appendChild(div);
//---

// set the div width '100%' height 400px, background repeat 'no-repeat', background position bottom
function setDivStyle(div){
    //--- generate---
    div.style.width = '100%';
    div.style.height = '400px';
    div.style.backgroundRepeat = 'repeat no-repeat';
    div.style.backgroundPosition = 'bottom';
    //--- 
}

基于注释和函数定义填充的方式其实也有些弊端,我自己既然写了这么多,我自己写就好了,何必来生成呢。还看和修改,比较麻烦。不过我们能看到的趋势是,模型现在做的越来越好了,越来越智能了,说不定未来哪一天,真的就会被替代了。



请遵守《互联网环境法规》文明发言,欢迎讨论问题
扫码反馈

扫一扫,反馈当前页面

咨询反馈
扫码关注
返回顶部