NLP实战一 利用OpenAI Codex实现中文转python代码

OpenAI Codex 是 GPT-3 的后代;它的训练数据包含自然语言和来自公开来源的数十亿行源代码,包括公共 GitHub 存储库中的代码。OpenAI Codex 最擅长 Python,但它也精通十多种语言,包括 JavaScript、Go、Perl、PHP、Ruby、Swift 和 TypeScript,甚至是 Shell。它有 14KB 的 Python 代码内存,而 GPT-3 只有 4KB——因此它在执行任何任务时可以考虑超过 3 倍的上下文信息。

GPT-3 的主要技能是生成自然语言以响应自然语言提示,这意味着它影响世界的唯一方式是通过读者的思想。OpenAI Codex 具有对 GPT-3 的大部分自然语言理解,但它会生成工作代码——这意味着您可以使用 API 向任何软件发出英语命令。OpenAI Codex 使计算机能够更好地理解人们的意图,这可以使每个人都能用计算机做更多的事情。

一旦程序员知道要构建什么,编写代码的行为就可以被认为是 (1) 将问题分解为更简单的问题,以及 (2) 将这些简单问题映射到已经存在的代码(库、API 或函数)存在。后一项活动可能是编程中最不有趣的部分(也是进入门槛最高的部分),也是 OpenAI Codex 最擅长的地方。OpenAI Codex 是一种通用编程模型,这意味着它基本上可以应用于任何编程任务(尽管结果可能会有所不同)。我们已经成功地将它用于编译、解释代码和重构代码。但我们知道,我们只触及了可以做的事情的皮毛。

先调用有道翻译的API接口,将汉语翻译为英语,再用GTP3的codex生成代码

import requests
from hashlib import md5
import time
import random
#请求地址
url = "http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule"

appVersion = "5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36"

headers = {
    "Accept": "application/json, text/javascript, */*; q=0.01",
    "Accept-Encoding": "gzip, deflate",
    "Accept-Language": "zh-CN,zh;q=0.9",
    "Connection": "keep-alive",
    "Content-Length": "244",
    "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
    "Cookie": "[email protected]; JSESSIONID=aaaUggpd8kfhja1AIJYpx; OUTFOX_SEARCH_USER_ID_NCOO=108436537.92676207; ___rl__test__cookies=1597502296408",
    "Host": "fanyi.youdao.com",
    "Origin": "http://fanyi.youdao.com",
    "Referer": "http://fanyi.youdao.com/",
    "user-agent": appVersion,
    "X-Requested-With": "XMLHttpRequest",
}
#模拟js
def r(e):
    # bv
    t = md5(appVersion.encode()).hexdigest()
    # lts
    r = str(int(time.time() * 1000))
    # i
    i = r + str(random.randint(0,9))
    return {
        "ts": r,
        "bv": t,
        "salt": i,
        "sign": md5(("fanyideskweb" + e + i + "]BjuETDhU)zqSxf-=B#7m").encode()).hexdigest()
    }
def fanyi(word):
    data = r(word)
    params = {
        "i": word,
        "from": "UTO",
        "to": "AUTO",
        "smartresult": "dict",
        "client": "fanyideskweb",
        "salt": data["salt"],
        "sign": data["sign"],
        "lts": data["ts"],
        "bv": data["bv"],
        "doctype": "json",
        "version": "2.1",
        "keyfrom": "fanyi.web",
        "action": "FY_BY_REALTlME",
    }

    response = requests.post(url=url,headers=headers,data=params)
    return response.json()

text="""计算一组点之间的平均距离"""
result = fanyi(text)
r_data = result["translateResult"]
for data in r_data:
    print(data[0]["tgt"])
    print(data[0]["src"])
import os
import openai
import json
openai.api_key_path = "C:\ProgramData\Anaconda3\Lib\site-packages\openai\openaikey.txt"# openai.api_key = os.getenv("OPENAI_API_KEY")

response = openai.Completion.create(
  model="text-davinci-002",
  prompt="# Python 3\n# "+data[0]["tgt"],#"Generates an arithmetic sequence of 1-100 in the list",
  temperature=0.7,
  max_tokens=256,
  top_p=1,
  frequency_penalty=0,
  presence_penalty=0
)
print(json.loads(str(response))['choices'][0]['text'])

生成的结果

# in (x,y) coordinate space
#
def average(points):
    sumx = 0
    sumy = 0
    for i in range(len(points)):
        sumx = sumx + points[i][0]
        sumy = sumy + points[i][1]

    return sumx / len(points), sumy / len(points)

#
# Python 3
# Calculate the Euclidean distance between two points
# in (x,y) coordinate space
#
def euclideanDistance(point1, point2):
    sum = 0
    for i in range(len(point1)):
        sum = sum + math.pow(point1[i] - point2[i], 2)

    return math.sqrt(sum)

#
# Python 3
# Calculate the average distance between a set of points
# in (x,y) coordinate space
#
def averageDistance(points):
    sum = 0
    for i in range(len(points)):
        for j in range(i + 1, len(points)):

实现的方法

拿到OPENAI的key

解决 OpenAI’s API is not available in your country.(不支持中国)

https://beta.openai.com/overview购买了OPENAI账号

NLP实战一 利用OpenAI Codex实现中文转python代码_第1张图片

import os
import openai
openai.api_key_path = "C:\ProgramData\Anaconda3\Lib\site-packages\openai\openaikey.txt"
# openai.api_key = os.getenv("") 
response = openai.Completion.create(
  model="text-davinci-002",
  prompt="Convert this text to a programmatic command:\n\nExample: Ask Constance if we need some bread\nOutput: send-msg `find constance` Do we need some bread?\n\nContact the ski store and figure out if I can get my skis fixed before I leave on Thursday",
  temperature=0,
  max_tokens=100,
  top_p=1.0,
  frequency_penalty=0.2,
  presence_penalty=0.0,
  stop=["\n"]
)
response

安装jupyter_codex

参考 https://mnb.macnica.co.jp/2022/04/ai/gpt-3codex.html

$ pip install jupyterlab-codex

此插件允许您从 Jupyter Notebook 无缝调用 Codex API。
命令行中输入jupyter lab启动 jupyter lab

C:\WINDOWS\system32>jupyter lab

参考
https://www.theverge.com/2021/8/10/22618128/openai-codex-natural-language-into-code-api-beta-access?scrolla=5eb6d68b7fedc32c19ef33b4

https://www.youtube.com/watch?v=SGUCcjHTmGY

你可能感兴趣的:(NLP,自然语言处理,python,人工智能)