OPENAI(ChatGPT)

OpenAI 官网:https://platform.openai.com/
ChatGPT官网:https://chat.openai.com/chat
接码平台:https://sms-activate.org/cn/history#

PLUS

目前只有PLUS会员可以使用GPT4,级一些其他插件。
通过美区IOS账号进入APP Store可以下载chatGPT app,充值更方便。

token

每次请求输入的提示词和返回的答案,都占用token。
请求返回值会展示本次请求token使用情况,也可以通过工具本地计算
每1000token进行一次计费,1000个token大约为700个英文单词或450(davinci)~900(GPT3.5/4)个汉字。
不同模型的token计费方式不同,且每个模型有自己的单次请求token上限(通常为4096)。当遇到需要更多文本的场景时,可考虑使用 fine-tuning


models

可以通过模型对比工具对比不同模型的结果
模型的训练数据大多截止于2021年,其不具备较新的知识。而GPT3则截止于更早的2020年8月。

  • 文本补全模型
    • text-davinci-003 也可以作为单次对话的模型使用
    • code-davinci-002 专用于code
  • 对话模型
    • davinci(gpt-3) 已经被gpt-3.5取代无法直接调用,专用于fine-tune精调
    • gpt-3.5-turbo 和text-davinci-003能力相似而费用只需十分之一
    • gpt-4 相比于3.5,在回答复杂逻辑类问题时更有优势,且支持8,192或32,768 tokens
  • DALL·E 绘画模型
    支持提示词生成图、通过蒙版或提示词修改图、随机生成原图变种
  • Whisper 语音识别
  • Embeddings 嵌入 text-embedding-ada-002
  • Moderation 违规检测

接口调用

接口文档
接口在线测试界面(playground)

  • 直接通过接口调用
    fetch('https://api.openai.com/v1/completions', {
        method: 'post',
        body: JSON.stringify(
            {
                "model": "text-davinci-003",
                "prompt": "用java写一个hello world",
                "max_tokens": 2048,
                "temperature": 0,
                "top_p": 1,
                "frequency_penalty": 0,
                "presence_penalty": 0.6,
                "stop": ["Human:", "AI:"]
            }),
        headers: {
            'Content-Type': 'application/json',
            'Authorization': 'Bearer sk-开头的自己的key'
        }
    }).then(function (data) {


    })
  • 通过openai库调用
import os
import openai

openai.organization = "org-开头的自己的key"
openai.api_key = "sk-开头的自己的key" # https://platform.openai.com/account/api-keys

# 根据科学上网模式不同 可能需要添加
# os.environ["http_proxy"] = "http://127.0.0.1:7890"
# os.environ["https_proxy"] = "http://127.0.0.1:7890"

messages = [
    {"role": "system", "content": "Respond in the voice of cute cat, and be as helpful as possible!"}
]

while True:

    # Add the user's input to the messages list and user the 'user' role
    user_input = input("Q: ")
    messages.append({"role": "user", "content": user_input})

    """
    messages为一个如下格式的列表:
    [
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Who won the world series in 2020?"},
        {"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
        {"role": "user", "content": "Where was it played?"}
    ]
    """

    # Call the OpenAI api for Chat GPT, and pass our complete list of messages
    response = openai.ChatCompletion.create( # https://api.openai.com/v1/chat/completions
        # model="gpt-3.5-turbo",
        model="davinci",
        messages=messages,
        temperature=0.8,  # 默认1 0~2 值越大返回结果越随机
        # top_p=0.1,  # 默认1 0~1 只考虑前百分之多少符合度的内容 不要和temperature同时使用
        n=1,  # 默认1 返回的choices的数量。增大后会增加token消耗
        # stream=False, # 以数据流返回
        # stop=["Jack","Mike","\n"], # 默认null 触发停止的字符串或字符串数组,返回值不会包含停止词
        # max_tokens=500,  # 默认inf 最大返回值token数。该值+输入token数,不得超过模型最大token数
        # logit_bias=None, # 配置对不同token的偏好
        presence_penalty=0,  # -2~2 越大越不容易基于之前的聊天内容
        frequency_penalty=0,  # -2~2 越大越不容易容易输出重复内容
    )

    """
    {
        "id":"chatcmpl-abc123",
        "object":"chat.completion",
        "created":1677858242,
        "model":"gpt-3.5-turbo-0301",
        "usage":{ # 消耗token数量
            "prompt_tokens":13,
            "completion_tokens":7,
            "total_tokens":20
        },
        "choices":[
            {
                "message":{
                    "role":"assistant",
                    "content":"\n\nThis is a test!"
                },
                "finish_reason":"stop", # stop 输出完成或触发停止词  length token长度限制(如max_tokens) content_filter GPT官方内容审核 null 未结束
                "index":0
            }
        ]
    }
    """


    # Show ChatGPT's response and add the response to our list of messages
    print('A: ' + str(response['choices'][0]['message']['content']) + '\n')
    messages.append(response['choices'][0]['message'])


Fine-tuning 微调

选择一个基础模型(仅支持davinci、curie、ada、baddage),输入大量 prompt 和 completion 对照的数组(至少几百条,越多越好),单次付费进行训练。通常用于调整「句式」、「情感」等特征,而非更多知识。
完成后在云端会有一个新模型,之后付费调用该新模型即可(比普通模型调用贵)。

使用场景
  • 想让 GPT-3 按照某种格式来识别 Prompt ,或按照某种格式来回答
    例如代替completion类任务中的举例
  • 想让 GPT-3 按照某种语气、性格来回答
  • 想让 completion 具有某种倾向
  • 满足特殊场景需要,如对抗训练
数据准备
  • prompt 以固定分隔符结尾,并保证数据本身不含该固定分隔符。
  • completion 以\n开头,以固定分隔符结尾,并保证数据本身不含该固定分隔符。
    可以使用官方工具自动格式化数据,并生成JSONL文件:
    openai tools fine_tunes.prepare_data -f xxx.csv

embedding 嵌入

通过text-embedding-ada-002(对应tokenizer为cl100k_base)将语料片段转为向量。当实际提问时,将问题也转为向量,将语料按余弦向量近似程度排序,依次加入 prompt 作为上下文。

使用场景

获取文本特征向量,用于以下场景:

  • 搜索(根据查询字符串的相关性对结果进行排名)
  • 聚类(根据相似性对文本字符串进行分组)
  • 推荐(具有相关文本字符串的项目被推荐)
  • 异常检测(识别关联度小的异常值)
  • 多样性测量(对相似性分布进行分析)
  • 分类(文本串按其最相似的标签进行分类)

请求次数、token数限制

详见官方文档,可以提交申请表申请提高限额,但不一定会通过。
建议通过以下方式自行规避:

Chat Plugins

构建一个插件,让ChatGPT智能地调用外部API
并未开放,需加入wait list


一些配套工具

AIPRM:Chrome插件,用于生成提示词

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容