目录

OpenAI-Codex,GitHub-Copilot-和cheat.sh-三个代码建议工具对比

OpenAI Codex,GitHub Copilot 和cheat.sh 三个代码建议工具对比

https://img-home.csdnimg.cn/images/20240715101418.png

关键词由CSDN通过智能技术生成

在本文中,我们将对比 OpenAI Codex、GitHub Copilot 和cheat.sh 的智能感知和代码建议。

https://i-blog.csdnimg.cn/blog_migrate/95279d2c339405b0443a814d2e6841d7.jpeg#pic_center

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 = 300

local 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 content
end

local function trim(s)
return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
end

function 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

end
end

return 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"
"""

https://i-blog.csdnimg.cn/blog_migrate/881c686e10f406a3cca15695b538196e.gif#pic_center

def getUserBalance(id):
"""
Look up the user in the database ‘UserData' and return their current account balance.
"""

https://i-blog.csdnimg.cn/blog_migrate/45e56ffbb3927d6336323ad6db280186.gif#pic_center

def sum_numbers(a, b):
return a + b

# Unit test

def

https://i-blog.csdnimg.cn/blog_migrate/60323aa7fc78f730461925585d8d5ff3.gif#pic_center

Javascript 也是可以的

// Function 1
var 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?

https://i-blog.csdnimg.cn/blog_migrate/b5315e94f24ee40f3459977fe6ee7d51.gif#pic_center

解释 SQL 语句

SELECT DISTINCT department.name
FROM department
JOIN employee ON department.id = employee.department_id
JOIN salary_payments ON employee.id = salary_payments.employee_id
WHERE salary_payments.date BETWEEN '2020-06-01' AND '2020-06-30'
GROUP BY department.name
HAVING COUNT(employee.id) > 10;
-- Explanation of the above query in human readable format

https://i-blog.csdnimg.cn/blog_migrate/3e497cece2f1af997218bd31acd91d69.gif#pic_center

与 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

除了默认建议外,还提供了替代建议。

https://i-blog.csdnimg.cn/blog_migrate/aea360dc8d231fd34dbbfd1889d50b24.png#pic_center

Codex

https://i-blog.csdnimg.cn/blog_migrate/aee1aa6e552a1adc7abb26c865917e38.png#pic_center

cheat.sh

https://i-blog.csdnimg.cn/blog_migrate/cb720477227751b3ee95a7a281d50453.png#pic_center

下面对比下 Javascript

// Find out the minimum and maximum values of the input array
function process(arr) {

Copilot

https://i-blog.csdnimg.cn/blog_migrate/4da9ec7bada9cd665190720b9bc463f5.png#pic_center

Codex

https://i-blog.csdnimg.cn/blog_migrate/e2a13e7ca73ac04424ec5aee09f27b02.png#pic_center

cheat.sh

https://i-blog.csdnimg.cn/blog_migrate/68ab48224963401f0f71f0c0b1b712a2.png#pic_center

总结

cheat.sh 正如他的名字 cheatsheet 一样,如果你是进行刷题的,那么他将非常的好用因为他能能够匹配到原题。

但是对于是实际开发来说,Copilot 是非常好用的,因为毕竟其后面有着 Github 的代码库。Codex 的变现也不错,总体来说 AI 辅助编程绝对是降低编程门槛的一大进步。 现在提出最佳编码实践可能不够聪明,但随着模型的改进,应该会变得更好。

译者注:我最近在开发中一直在使用 Copilot,它甚至能够完成完整的代码,因为毕竟他背后有着 Github 的庞大代码库。我觉得他之所以做的这么好是因为目前我们写的代码大部分都出现在了 github 中(别人也写过类似的功能),其实这对于 AI 模型来说就是数据泄露了,模型在用训练的数据做推理所以准确性非常的高。其实把 Copilot 作为代码搜索模型我觉得更恰当,简单的讲就是在 Github 这么庞大的代码库中,它可以搜索到适用我们需求的代码,并且给我们提示或者能够给我们一些别人写代码的思路。但是无论怎样,Copilot 真的很好用,推荐大家都试试。

作者:alpha2phi