全网最详细中英文ChatGPT-GPT-4示例文档-从0到1快速入门解释代码应用——官网推荐的48种最佳应用场景(附python/node.js/curl命令源代码,小白也能学)

image

ChatGPT是目前最先进的AI聊天机器人,它能够理解图片和文字,生成流畅和有趣的回答。如果你想跟上AI时代的潮流,你一定要学会使用ChatGPT。如果你想了解OpenAI最新发布的GPT-4模型,以及它如何为ChatGPT聊天机器人带来更强大的功能,那么你一定不要错过OpenAI官网推荐的48种最佳应用场景,不管你是资深开发者、初学者,你都能够从0到1快速入门,并掌握他们。

在这个AI大时代,如果不想被人颠覆,就要先颠覆别人。如果你颠覆不了别人,那你就努力运用ChatGPT提高你的技术水平和创造力。

ChatGPT可以对复杂代码进行解释,这意味着ChatGPT可以根据相关代码的语法和语义,为每一行或每一段代码添加相应的自然语言解释,帮助用户理解和学习复杂代码。这样,ChatGPT可以提高用户的编程能力和兴趣,以及用户的编程思维和逻辑。

Introduce 简介

Explain code 代码解释
Explain a complicated piece of code.
解释一段复杂的代码。

setting 设置

Engine:text-davinci-003
Max tokens:150
Temperature:0
Top p:1.0
Frequency penalty:0.0
Presence penalty:0.0
Stop sequence:"""

说明:
0、Engine 设置定义了你要使用的模型,例如 text-davinci-003 是一个文本生成模型。这种模型可以根据输入的文本,生成新的、相关的文本。
1、Max tokens是指在请求中最多允许返回的 token 数目,比如你可以指定 chatGPT 返回最多 64个 token。这可以帮助你控制输出的内容大小,以便更好地控制响应速度和结果。一般1个token约4个字符或者0.75个单词
2、Temperature 是一个参数,用于控制 chatGPT 的输出。它决定了 chatGPT 在生成文本时会多么“随意”。值越高,chatGPT 生成的文本就越不可预测;值越低,chatGPT 生成的文本就越可预测。它在0.0到2.0之间,Temperature设置为0意味着ChatGPT将会生成更加保守的回复,即更少的随机性和更多的准确性,这可以帮助你在聊天中更好地控制语义,并且可以防止ChatGPT产生不相关的内容。通常建议更改此值或 Top P,但不要同时更改这两个值。
3、Top p 是随温度采样的替代方案,称为核采样,其中模型考虑具有top_p概率质量的标记的结果。因此0.1意味着仅考虑包括前10%概率质量的记号。通常建议更改此值或 temperature,但不要同时更改这两个值。
4、Frequency penalty 是指在训练时,模型会根据词频来调整每个单词的重要性。它可以帮助模型更好地理解文本,并减少过拟合。介于-2.0和2.0之间的数字。正值会根据新标记在文本中的现有频率惩罚新标记,从而降低模型逐字重复同一行的可能性。Frequency penalty设置为0意味着模型不会对重复的词进行惩罚。它可以帮助模型生成更多的新词,而不是重复使用已有的词。
5、Presence penalty 是指在ChatGPT中,一些预先定义的条件或者状态可能会影响机器人回答的质量,介于-2.0和2.0之间的数字。正值会根据新标记到目前为止是否出现在文本中来惩罚它们,从而增加模型谈论新主题的可能性。如果将 Presence penalty 设置为 0,则表示不会有任何惩罚。
6、Stop sequence 是一种设置,通过它可以确定你的聊天会话的结束标志。当你在 ChatGPT中设置 Stop sequence为 """ 时,表示你的聊天会话结束标志是注释符(""")。

Prompt 提示

class Log:

def __init__(self, path):
    dirname = os.path.dirname(path)
    os.makedirs(dirname, exist_ok=True)
    f = open(path, "a+")
    
    # Check that the file is newline-terminated
    size = os.path.getsize(path)
    if size > 0:
        f.seek(size - 1)
        end = f.read(1)
        if end != "\n":
            f.write("\n")
    self.f = f
    self.path = path

def log(self, event):
    event["_event_id"] = str(uuid.uuid4())
    json.dump(event, self.f)
    self.f.write("\n")

def state(self):
    state = {"complete": set(), "last": None}
    for line in open(self.path):
        event = json.loads(line)
        if event["type"] == "submit" and event["success"]:
            state["complete"].add(event["id"])
            state["last"] = event
    return state
    """
    Here's what the above class is doing, explained in a concise way:
    下面是上面的类正在做的事情,以简洁的方式解释:
    1.

Sample response 回复样本

The init method creates a new file at the given path if it doesn't already exist, and ensures that the file is newline-terminated.
2.The log method adds a new event to the log file, with a unique ID.
3.The state method reads the log file and returns a dictionary containing the set of completed tasks and the last successful event.
__init__方法在给定路径下创建一个新文件(如果该文件不存在),并确保该文件以换行符结尾。
2. log方法向日志文件中添加一个新事件,该事件具有唯一的ID。
3.state方法读取日志文件并返回一个字典,其中包含已完成的任务集和最后一个成功事件。

API request 接口请求

python接口请求示例

import os
import openai

openai.api_key = os.getenv("OPENAI_API_KEY")

response = openai.Completion.create(
  model="text-davinci-003",
  prompt="class Log:\n    def __init__(self, path):\n        dirname = os.path.dirname(path)\n        os.makedirs(dirname, exist_ok=True)\n        f = open(path, \"a+\")\n\n        # Check that the file is newline-terminated\n        size = os.path.getsize(path)\n        if size > 0:\n            f.seek(size - 1)\n            end = f.read(1)\n            if end != \"\\n\":\n                f.write(\"\\n\")\n        self.f = f\n        self.path = path\n\n    def log(self, event):\n        event[\"_event_id\"] = str(uuid.uuid4())\n        json.dump(event, self.f)\n        self.f.write(\"\\n\")\n\n    def state(self):\n        state = {\"complete\": set(), \"last\": None}\n        for line in open(self.path):\n            event = json.loads(line)\n            if event[\"type\"] == \"submit\" and event[\"success\"]:\n                state[\"complete\"].add(event[\"id\"])\n                state[\"last\"] = event\n        return state\n\n\"\"\"\nHere's what the above class is doing, explained in a concise way:\n1.",
  temperature=0,
  max_tokens=150,
  top_p=1.0,
  frequency_penalty=0.0,
  presence_penalty=0.0,
  stop=["\"\"\""]
)

node.js接口请求示例

const { Configuration, OpenAIApi } = require("openai");

const configuration = new Configuration({
  apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);

const response = await openai.createCompletion({
  model: "text-davinci-003",
  prompt: "class Log:\n    def __init__(self, path):\n        dirname = os.path.dirname(path)\n        os.makedirs(dirname, exist_ok=True)\n        f = open(path, \"a+\")\n\n        # Check that the file is newline-terminated\n        size = os.path.getsize(path)\n        if size > 0:\n            f.seek(size - 1)\n            end = f.read(1)\n            if end != \"\\n\":\n                f.write(\"\\n\")\n        self.f = f\n        self.path = path\n\n    def log(self, event):\n        event[\"_event_id\"] = str(uuid.uuid4())\n        json.dump(event, self.f)\n        self.f.write(\"\\n\")\n\n    def state(self):\n        state = {\"complete\": set(), \"last\": None}\n        for line in open(self.path):\n            event = json.loads(line)\n            if event[\"type\"] == \"submit\" and event[\"success\"]:\n                state[\"complete\"].add(event[\"id\"])\n                state[\"last\"] = event\n        return state\n\n\"\"\"\nHere's what the above class is doing, explained in a concise way:\n1.",
  temperature: 0,
  max_tokens: 150,
  top_p: 1.0,
  frequency_penalty: 0.0,
  presence_penalty: 0.0,
  stop: ["\"\"\""],
});

curl命令示例

curl https://api.openai.com/v1/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
  "model": "text-davinci-003",
  "prompt": "class Log:\n    def __init__(self, path):\n        dirname = os.path.dirname(path)\n        os.makedirs(dirname, exist_ok=True)\n        f = open(path, \"a+\")\n\n        # Check that the file is newline-terminated\n        size = os.path.getsize(path)\n        if size > 0:\n            f.seek(size - 1)\n            end = f.read(1)\n            if end != \"\\n\":\n                f.write(\"\\n\")\n        self.f = f\n        self.path = path\n\n    def log(self, event):\n        event[\"_event_id\"] = str(uuid.uuid4())\n        json.dump(event, self.f)\n        self.f.write(\"\\n\")\n\n    def state(self):\n        state = {\"complete\": set(), \"last\": None}\n        for line in open(self.path):\n            event = json.loads(line)\n            if event[\"type\"] == \"submit\" and event[\"success\"]:\n                state[\"complete\"].add(event[\"id\"])\n                state[\"last\"] = event\n        return state\n\n\"\"\"\nHere's what the above class is doing, explained in a concise way:\n1.",
  "temperature": 0,
  "max_tokens": 150,
  "top_p": 1.0,
  "frequency_penalty": 0.0,
  "presence_penalty": 0.0,
  "stop": ["\"\"\""]
}'

json格式示例

{
  "model": "text-davinci-003",
  "prompt": "class Log:\n    def __init__(self, path):\n        dirname = os.path.dirname(path)\n        os.makedirs(dirname, exist_ok=True)\n        f = open(path, \"a+\")\n\n        # Check that the file is newline-terminated\n        size = os.path.getsize(path)\n        if size > 0:\n            f.seek(size - 1)\n            end = f.read(1)\n            if end != \"\\n\":\n                f.write(\"\\n\")\n        self.f = f\n        self.path = path\n\n    def log(self, event):\n        event[\"_event_id\"] = str(uuid.uuid4())\n        json.dump(event, self.f)\n        self.f.write(\"\\n\")\n\n    def state(self):\n        state = {\"complete\": set(), \"last\": None}\n        for line in open(self.path):\n            event = json.loads(line)\n            if event[\"type\"] == \"submit\" and event[\"success\"]:\n                state[\"complete\"].add(event[\"id\"])\n                state[\"last\"] = event\n        return state\n\n\"\"\"\nHere's what the above class is doing, explained in a concise way:\n1.",
  "temperature": 0,
  "max_tokens": 150,
  "top_p": 1.0,
  "frequency_penalty": 0.0,
  "presence_penalty": 0.0,
  "stop": ["\"\"\""]
}

其它资料下载

如果大家想继续了解人工智能相关学习路线和知识体系,欢迎大家翻阅我的另外一篇博客《重磅 | 完备的人工智能AI 学习——基础知识学习路线,所有资料免关注免套路直接网盘下载
这篇博客参考了Github知名开源平台,AI技术平台以及相关领域专家:Datawhale,ApacheCN,AI有道和黄海广博士等约有近100G相关资料,希望能帮助到所有小伙伴们。

posted @ 2023-03-29 14:45  虎啸AI  阅读(410)  评论(0编辑  收藏  举报