OpenAI Codex,GitHub Copilot 和cheat.sh 三个代码建议工具对比
在本文中,我们将对比 OpenAI Codex、GitHub Copilot 和cheat.sh 的智能感知和代码建议。
OpenAI Codex简单的集成
OpenAI Codex 是 OpenAI 开发的一个人工智能模型,可以解析自然语言并生成响应代码。因为没有提供插件,所以测试的时候需要我们自己调用他的API,这里为了方便,编写一个简单的Neovim Lua 模块来进行集成。
local Job = require "plenary.job"local M = {}local API_KEY_FILE = vim.env.HOME .. "/.config/openai-codex/env"local OPENAI_URL = "https://api.openai.com/v1/engines/davinci-codex/completions"-- local OPENAI_URL = "https://api.openai.com/v1/engines/cushman-codex/completions"local MAX_TOKENS = 300local function get_api_key() local file = io.open(API_KEY_FILE, "rb") if not file then return nil end local content = file:read "*a" content = string.gsub(content, "^%s*(.-)%s*$", "%1") -- strip off any space or newline file:close() return contentendlocal function trim(s) return (string.gsub(s, "^%s*(.-)%s*$", "%1"))endfunction M.complete(v) v = v or true local ft = vim.bo.filetype local buf = vim.api.nvim_get_current_buf() local api_key = get_api_key() if api_key == nil then vim.notify "OpenAI API key not found" return end local text = "" if v then local line1 = vim.api.nvim_buf_get_mark(0, "<")[1] local line2 = vim.api.nvim_buf_get_mark(0, ">")[1] text = vim.api.nvim_buf_get_lines(buf, line1 - 1, line2, false) text = trim(table.concat(text, "\n")) else text = trim(vim.api.nvim_get_current_line()) end local cs = vim.bo.commentstring text = string.format(cs .. "\n%s", ft, text) -- vim.notify(text) local request = {} request["max_tokens"] = MAX_TOKENS request["top_p"] = 1 request["temperature"] = 0 request["frequency_penalty"] = 0 request["presence_penalty"] = 0 request["prompt"] = text local body = vim.fn.json_encode(request) local completion = "" local job = Job:new { command = "curl", args = { OPENAI_URL, "-H", "Content-Type: application/json", "-H", string.format("Authorization: Bearer %s", api_key), "-d", body, }, } local is_completed = pcall(job.sync, job, 10000) if is_completed then local result = job:result() local ok, parsed = pcall(vim.json.decode, table.concat(result, "")) if not ok then vim.notify "Failed to parse OpenAI result" return end if parsed["choices"] ~= nil then completion = parsed["choices"][1]["text"] local lines = {} local delimiter = "\n" for match in (completion .. delimiter):gmatch("(.-)" .. delimiter) do table.insert(lines, match) end vim.api.nvim_buf_set_lines(buf, -1, -1, false, lines) end endendreturn M
要使用 OpenAI Codex,需要有 API 密钥。并在 API_KEY_FILE 指定API 密钥的文件的位置。Codex中有 2 种模型可以选择。 Davinci Codex 是功能最强大的 Codex 模型。 它特别擅长将自然语言翻译成代码。 Cushman Codex 几乎与 Davinci Codex 一样强大,但速度略快。 这种速度优势可能使其更适合实时应用程序。 我通过设置 OPENAI_URL 变量配置 Davinci Codex。
MAX_TOKENS 表示要返回的最大令牌数。 标记可以是单词或只是字符块。
有许多可以配置的参数,如 top_p、temperature、best_of、logprobs 等。这里就不详细介绍了。
现在我们开始测试python代码
"""Ask the user for their name and say "Hello""""
def getUserBalance(id):"""Look up the user in the database ‘UserData' and return their current account balance."""
def sum_numbers(a, b): return a + b# Unit testdef
Javascript也是可以的
// Function 1var fullNames = [];for (var i = 0; i < 50; i++) { fullNames.push(names[Math.floor(Math.random() * names.length)] + " " + lastNames[Math.floor(Math.random() * lastNames.length)]);}// What does Function 1 do?
解释SQL语句
SELECT DISTINCT department.nameFROM departmentJOIN employee ON department.id = employee.department_idJOIN salary_payments ON employee.id = salary_payments.employee_idWHERE salary_payments.date BETWEEN '2020-06-01' AND '2020-06-30'GROUP BY department.nameHAVING COUNT(employee.id) > 10;-- Explanation of the above query in human readable format
与Copilot和cheat.sh的比较
下面我们开始正式比较 Codex、Copilot 和cheat.sh 。
cheat.sh 虽然不是 AI 补全引擎,但是他的确很好用,所以这里也把他加入了进来,在这里我们把它作为非AI补全的天花板。
对于 Copilot,我将使用 VS Code,因为与 Neovim 插件相比,Copilot 扩展更加成熟。
先看看Python
我们输入
def binary_sort(input_list): """Binary sort the input list"""
Copilot
除了默认建议外,还提供了替代建议。
Codex
cheat.sh
下面对比下Javascript
// Find out the minimum and maximum values of the input arrayfunction process(arr) {
Copilot
Codex
cheat.sh
总结
cheat.sh正如他的名字cheatsheet一样,如果你是进行刷题的,那么他将非常的好用因为他能能够匹配到原题。
但是对于是实际开发来说,Copilot是非常好用的,因为毕竟其后面有着Github的代码库。Codex的变现也不错,总体来说AI辅助编程绝对是降低编程门槛的一大进步。 现在提出最佳编码实践可能不够聪明,但随着模型的改进,应该会变得更好。
译者注:我最近在开发中一直在使用Copilot,它甚至能够完成完整的代码,因为毕竟他背后有着Github的庞大代码库。我觉得他之所以做的这么好是因为目前我们写的代码大部分都出现在了github中(别人也写过类似的功能),其实这对于AI模型来说就是数据泄露了,模型在用训练的数据做推理所以准确性非常的高。其实把Copilot作为代码搜索模型我觉得更恰当,简单的讲就是在Github这么庞大的代码库中,它可以搜索到适用我们需求的代码,并且给我们提示或者能够给我们一些别人写代码的思路。但是无论怎样,Copilot真的很好用,推荐大家都试试。
作者:alpha2phi