• 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 = 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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94

    要使用 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"
    """
    
    • 1
    • 2
    • 3

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

    def sum_numbers(a, b):
      return a + b
    
    # Unit test
    def
    
    • 1
    • 2
    • 3
    • 4
    • 5

    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?
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    解释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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    与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"""
    
    • 1
    • 2

    Copilot

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

    Codex

    cheat.sh

    下面对比下Javascript

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

    Copilot

    Codex

    cheat.sh

    总结

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

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

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

    作者:alpha2phi

  • 相关阅读:
    服务器防火墙的应用技术有哪些?
    怎么团队合作,协作开发
    关于MySQL日期函数你不知道的用法
    Master JavaScript Coding
    unity 性能优化指标
    wuzhicms代码审计
    腾讯云服务器竞价实例是什么意思?
    学习Python,你至少要练习这些代码之13
    个人博客项目中遇到的 mongodb 操作
    黑客入门教程(非常详细)从零基础入门到精通,看完这一篇就够了
  • 原文地址:https://blog.csdn.net/m0_46510245/article/details/121587502