当前位置:   article > 正文

最新ChatGPT GPT-4 NLU应用之实体分类识别与模型微调(附ipynb与python源码及视频)——开源DataWhale发布入门ChatGPT技术新手从0到1必备使用指南手册(六)_chatgpt4 接口可以微调模型

chatgpt4 接口可以微调模型

在这里插入图片描述

前言

想象一下,如果您也可以训练一个为特定场景和目的量身打造的大型自然语言模型,以帮助您在实时聊天中更快地、更准确地与用户互动。是不是特别地牛!

ChatGPT可以帮助您非常简单地做到这一点。利用微调模型技术,ChatGPT结合您的自定义语料集,重新训练先前的预训练语言模型,提高模型的精度和可靠性。这使得ChatGPT在特定环境中表现更佳,从而帮助您优化模型,在与客户进行对话时发挥最佳效果。

让我们从零开始,一步一步地学习使用ChatGPT开始构建您的定制化自然语言模型吧。

最新ChatGPT GPT-4 自然语言理解NLU实战之实体分类识别与模型微调

主题分类

  在前面"相关API"一节,我们已经介绍了各种分类和实体提取的用法。这里会给大家介绍更具体、常见的任务。

  首先是主题分类,简单来说就是给定文本,判断属于哪一类主题。

  我们找一个新闻主题分类的数据集看看,数据集取自:CLUEbenchmark/CLUE,共15个类别。

import pnlp
lines = pnlp.read_file_to_list_dict("./dataset/tnews.json")
len(lines)
  • 1
  • 2
  • 3
10000
  • 1
from collections import Counter
ct = Counter([v["label_desc"] for v in lines])
  • 1
  • 2
ct.most_common()
  • 1
[('news_tech', 1089),
 ('news_finance', 956),
 ('news_entertainment', 910),
 ('news_world', 905),
 ('news_car', 791),
 ('news_sports', 767),
 ('news_culture', 736),
 ('news_military', 716),
 ('news_travel', 693),
 ('news_game', 659),
 ('news_edu', 646),
 ('news_agriculture', 494),
 ('news_house', 378),
 ('news_story', 215),
 ('news_stock', 45)]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

  stock这个类别太少了,我们先给它去掉:

lines = [v for v in lines if v["label_desc"] != "news_stock"]
len(lines)
  • 1
  • 2
9955
  • 1
def get_prompt(text):
    prompt = f"""对给定文本进行分类,类别包括:科技、金融、娱乐、世界、汽车、文化、军事、旅游、游戏、教育、农业、房产、社会、股票。

给定文本:
{text}
类别:
"""
    return prompt
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
lines[0]
  • 1
{'label': '102',
 'label_desc': 'news_entertainment',
 'sentence': '江疏影甜甜圈自拍,迷之角度竟这么好看,美吸引一切事物',
 'keywords': '江疏影,美少女,经纪人,甜甜圈'}
  • 1
  • 2
  • 3
  • 4
prompt = get_prompt(lines[0]["sentence"])
print(prompt)
  • 1
  • 2
对给定文本进行分类,类别包括:科技、金融、娱乐、世界、汽车、文化、军事、旅游、游戏、教育、农业、房产、社会、股票。

给定文本:
江疏影甜甜圈自拍,迷之角度竟这么好看,美吸引一切事物
类别:
  • 1
  • 2
  • 3
  • 4
  • 5
import openai
OPENAI_API_KEY = "填入专属的API key"

openai.api_key = OPENAI_API_KEY
  • 1
  • 2
  • 3
  • 4
def complete(prompt):
    response = openai.Completion.create(
        prompt=prompt,
        temperature=0,
        max_tokens=10,
        top_p=1,
        frequency_penalty=0,
        presence_penalty=0,
        model="text-davinci-003"
    )
    ans = response["choices"][0]["text"].strip(" \n")
    return ans
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
complete(prompt)
  • 1
'娱乐'
  • 1
def ask(content):
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo", 
        messages=[{"role": "user", "content": content}],
        temperature=0,
        max_tokens=10,
        top_p=1,
        frequency_penalty=0,
        presence_penalty=0,
    )

    ans = response.get("choices")[0].get("message").get("content")
    return ans
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
ask(prompt)
  • 1
'娱乐'
  • 1

  再试几个其他类别的:

lines[1]
  • 1
{'label': '110',
 'label_desc': 'news_military',
 'sentence': '以色列大规模空袭开始!伊朗多个军事目标遭遇打击,誓言对等反击',
 'keywords': '伊朗,圣城军,叙利亚,以色列国防军,以色列'}
  • 1
  • 2
  • 3
  • 4
prompt = get_prompt(lines[1]["sentence"])
print(prompt)
  • 1
  • 2
对给定文本进行分类,类别包括:科技、金融、娱乐、世界、汽车、文化、军事、旅游、游戏、教育、农业、房产、社会、股票。

给定文本:
以色列大规模空袭开始!伊朗多个军事目标遭遇打击,誓言对等反击
类别:
  • 1
  • 2
  • 3
  • 4
  • 5
ask(prompt)
  • 1
'军事'
  • 1
complete(prompt)
  • 1
'军事'
  • 1
lines[2]
  • 1
{'label': '104',
 'label_desc': 'news_finance',
 'sentence': '出栏一头猪亏损300元,究竟谁能笑到最后!',
 'keywords': '商品猪,养猪,猪价,仔猪,饲料'}
  • 1
  • 2
  • 3
  • 4
prompt = get_prompt(lines[2]["sentence"])
print(prompt)
  • 1
  • 2
对给定文本进行分类,类别包括:科技、金融、娱乐、世界、汽车、文化、军事、旅游、游戏、教育、农业、房产、社会、股票。

给定文本:
出栏一头猪亏损300元,究竟谁能笑到最后!
类别:
  • 1
  • 2
  • 3
  • 4
  • 5
ask(prompt)
  • 1
'农业'
  • 1
complete(prompt)
  • 1
'社会'
  • 1

精准分类解决手段

  这个有点迷糊,不过「农业」这个类别感觉也没问题。当然,遇到有错误的情况,我们起码还有两种手段来解决:

  • Few-Shot,可以每次随机从数据集里抽几条出来作为Prompt的一部分。
  • Fine-Tuning,把我们自己的数据集按指定格式准备好,提交给API,让它帮我们微调一个属于我们自己的模型,它在我们自己的数据集上学习过。

  Few-Shot最关键的是如何找到这个「Few」,换句话说,我们拿什么Case给模型当做参考样本。对于类别比较多的多分类(实际工作中,成百上千中Label是很常见的),Few-Shot即使每个Label一个例子,这上下文长度也不得了。不太现实。这时候其实Few-Shot有点不太方便了。当然,如果我们非要用也不是不行,还是最常用的策略:先召回几个相似句,然后把相似句的内容和类别作为Few-Shot的例子,让接口来预测给定句子的类别。

模型微调步骤

  不过,我们还可以使用微调(Fine-Tuning)方法在自己的数据集上对模型进行微调,简单来说就是让模型「熟悉」我们独特的数据,进而让其具备在类似数据集上识别出类别的能力。

  接下来,就让我们看看具体怎么做,一般包括三个主要步骤:

  • 准备数据:按接口要求的格式把数据准备好,这里的数据就是我们自己的数据集,至少包含一段文本和一个类别。
  • 微调:使用微调接口将刚刚的数据传递过去,由服务器自动完成微调,微调完成后可以得到一个新的model_id。注意,这个model_id只属于你自己,不要将它公开给其他人。
  • 使用新的模型进行推理:嗯,这个很简单,把原来接口里的model参数内容换成我们的model_id即可。

核心代码

  咱们接下来就来调调这个多分类模型,我们只取后500条作为训练集(为了快速和省钱……)。

train_lines = lines[-500:]
  • 1
import pandas as pd
  • 1
train = pd.DataFrame(train_lines)
train.shape
  • 1
  • 2
(500, 4)
  • 1
train.head()
  • 1
labellabel_descsentencekeywords
0103news_sports为什么斯凯奇与阿迪达斯脚感很相似,价格却差了近一倍?达斯勒,阿迪达斯,FOAM,BOOST,斯凯奇
1100news_story女儿日渐消瘦,父母发现有怪物,每天吃女儿一遍大将军,怪物
2104news_finance另类逼空确认反弹,剑指3200点以上股票,另类逼空,金融,创业板,快速放大
3100news_story老公在聚会上让我向他的上司敬酒,现在老公哭了,我笑了远走高飞
4108news_edu女孩上初中之后成绩下降,如何才能提升成绩?
train.label_desc.value_counts()
  • 1
news_finance          48
news_tech             47
news_game             46
news_entertainment    46
news_travel           44
news_sports           42
news_military         40
news_world            38
news_car              36
news_culture          35
news_edu              27
news_agriculture      20
news_house            19
news_story            12
Name: label_desc, dtype: int64
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

股票数据稍微少了些,问题不大。

Step1:准备数据

  要保证有两列为:prompt和completion。

df_train = train[["sentence", "label_desc"]]
df_train.columns = ["prompt", "completion"]
df_train.head()
  • 1
  • 2
  • 3
promptcompletion
0为什么斯凯奇与阿迪达斯脚感很相似,价格却差了近一倍?news_sports
1女儿日渐消瘦,父母发现有怪物,每天吃女儿一遍news_story
2另类逼空确认反弹,剑指3200点以上news_finance
3老公在聚会上让我向他的上司敬酒,现在老公哭了,我笑了news_story
4女孩上初中之后成绩下降,如何才能提升成绩?news_edu

  存起来:

df_train.to_json("dataset/tnews-finetuning.jsonl", orient='records', lines=True)
  • 1

  使用openai命令行工具进行转换:

!openai tools fine_tunes.prepare_data -f dataset/tnews-finetuning.jsonl -q
  • 1
Analyzing...

- Your file contains 500 prompt-completion pairs
- Based on your data it seems like you're trying to fine-tune a model for classification
- For classification, we recommend you try one of the faster and cheaper models, such as `ada`
- For classification, you can estimate the expected model performance by keeping a held out dataset, which is not used for training
- More than a third of your `prompt` column/key is uppercase. Uppercase prompts tends to perform worse than a mixture of case encountered in normal language. We recommend to lower case the data if that makes sense in your domain. See https://platform.openai.com/docs/guides/fine-tuning/preparing-your-dataset for more details
- Your data does not contain a common separator at the end of your prompts. Having a separator string appended to the end of the prompt makes it clearer to the fine-tuned model where the completion should begin. See https://platform.openai.com/docs/guides/fine-tuning/preparing-your-dataset for more detail and examples. If you intend to do open-ended generation, then you should leave the prompts empty
- All completions start with prefix `news_`. Most of the time you should only add the output data into the completion, without any prefix
- The completion should start with a whitespace character (` `). This tends to produce better results due to the tokenization we use. See https://platform.openai.com/docs/guides/fine-tuning/preparing-your-dataset for more details

Based on the analysis we will perform the following actions:
- [Recommended] Lowercase all your data in column/key `prompt` [Y/n]: Y
- [Recommended] Add a suffix separator ` ->` to all prompts [Y/n]: Y
- [Recommended] Remove prefix `news_` from all completions [Y/n]: Y
- [Recommended] Add a whitespace character to the beginning of the completion [Y/n]: Y
- [Recommended] Would you like to split into training and validation set? [Y/n]: Y


Your data will be written to a new JSONL file. Proceed [Y/n]: Y

Wrote modified files to `dataset/tnews-finetuning_prepared_train.jsonl` and `dataset/tnews-finetuning_prepared_valid.jsonl`
Feel free to take a look!

Now use that file when fine-tuning:
> openai api fine_tunes.create -t "dataset/tnews-finetuning_prepared_train.jsonl" -v "dataset/tnews-finetuning_prepared_valid.jsonl" --compute_classification_metrics --classification_n_classes 14

After you’ve fine-tuned a model, remember that your prompt has to end with the indicator string ` ->` for the model to start generating completions, rather than continuing with the prompt.
Once your model starts training, it'll approximately take 14.33 minutes to train a `curie` model, and less for `ada` and `babbage`. Queue will approximately take half an hour per job ahead of you.
  • 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

  看一下处理成什么样子了:

!head dataset/tnews-finetuning_prepared_train.jsonl
  • 1
{"prompt":"cf生存特训:火箭弹狂野复仇,为兄弟报仇就要不死不休 ->","completion":" game"}

{"prompt":"哈尔滨 东北抗日联军博物馆 ->","completion":" culture"}

{"prompt":"中国股市中,庄家为何如此猖獗?一文告诉你真相 ->","completion":" finance"}

{"prompt":"天府锦绣又重来 ->","completion":" agriculture"}

{"prompt":"生活,游戏,电影中有哪些词汇稍加修改便可以成为一个非常霸气的名字? ->","completion":" game"}

{"prompt":"法庭上,生父要争夺孩子抚养权,小男孩的发言让生父当场哑口无言 ->","completion":" entertainment"}

{"prompt":"如何才能选到好的深圳大数据培训机构? ->","completion":" edu"}

{"prompt":"有哪些娱乐圈里面的明星追星? ->","completion":" entertainment"}

{"prompt":"东坞原生态野生茶 ->","completion":" culture"}

{"prompt":"亚冠:恒大不胜早有预示,全北失利命中注定 ->","completion":" sports"}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

  最好再验证一下每个数据集的类型:

train = pnlp.read_file_to_list_dict("./dataset/tnews-finetuning_prepared_train.jsonl")
valid = pnlp.read_file_to_list_dict("./dataset/tnews-finetuning_prepared_valid.jsonl")
  • 1
  • 2
len(Counter([v["completion"] for v in train]))
  • 1
14
  • 1
len(Counter([v["completion"] for v in valid]))
  • 1
14
  • 1

  好的,最明显的是给我们每一个prompt后面加了个标记,除此之外还有(请注意看上面的日志):

  • 小写
  • 去除标签news_前缀
  • 在completion前面加空格
  • 切分为训练和验证集

  这些都是常见的、推荐的预处理,我们就按这样。

Step2:微调

import os
  • 1
os.environ.setdefault("OPENAI_API_KEY", "填入专属的API key")
  • 1
'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'
  • 1
!openai api fine_tunes.create \
    -t "./dataset/tnews-finetuning_prepared_train.jsonl" \
    -v "./dataset/tnews-finetuning_prepared_valid.jsonl" \
    --compute_classification_metrics --classification_n_classes 14 \
    -m davinci\
    --no_check_if_files_exist
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
Upload progress: 100%|████████████████████| 41.2k/41.2k [00:00<00:00, 15.6Mit/s]
Uploaded file from ./dataset/tnews-finetuning_prepared_train.jsonl: file-DKjBKHqWFJwo7O8MNZGOcj3F
Upload progress: 100%|████████████████████| 10.5k/10.5k [00:00<00:00, 7.85Mit/s]
Uploaded file from ./dataset/tnews-finetuning_prepared_valid.jsonl: file-j088k3GWqGeqY0o2DDAfWfPh
Created fine-tune: ft-QOkrWkHU0aleR6f5IQw1UpVL
Streaming events until fine-tuning is complete...

(Ctrl-C will interrupt the stream, but not cancel the fine-tune)
[2023-04-04 21:16:33] Created fine-tune: ft-QOkrWkHU0aleR6f5IQw1UpVL

Stream interrupted (client disconnected).
To resume the stream, run:

  openai api fine_tunes.follow -i ft-QOkrWkHU0aleR6f5IQw1UpVL
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

  其中,-t-v分别指定训练集和验证集,接下来那行用来计算指标,-m指定要微调的模型。最后一行是检查文件是否存在,如果之前传过文件的话,这里可以复用。为了便于演示,我们这里不检查。可以微调的模型和价格参见:Pricing

  另外,值得一提的是:只能微调Completion接口,ChatCompletion不支持微调。也就是说InstructGPT的几个模型是可以微调的,但是ChatGPT不能微调,可参阅:https://platform.openai.com/docs/guides/chat/is-fine-tuning-available-for-gpt-3-5-turbo

  我们这里选davinci,也是咱们之前一直用的。

  可以看到,上面跑一下就断掉了,这个是正常的,我们可以通过另一个API去查看任务的进度。注意,这里的ID是上面日志打出来的ID,每次执行都会变。

!openai api fine_tunes.get -i ft-QOkrWkHU0aleR6f5IQw1UpVL
  • 1
{

  "created_at": 1680614193,

  "events": [

    {

      "created_at": 1680614193,

      "level": "info",

      "message": "Created fine-tune: ft-QOkrWkHU0aleR6f5IQw1UpVL",

      "object": "fine-tune-event"

    }

  ],

  "fine_tuned_model": null,

  "hyperparams": {

    "batch_size": null,

    "classification_n_classes": 14,

    "compute_classification_metrics": true,

    "learning_rate_multiplier": null,

    "n_epochs": 4,

    "prompt_loss_weight": 0.01

  },

  "id": "ft-QOkrWkHU0aleR6f5IQw1UpVL",

  "model": "davinci",

  "object": "fine-tune",

  "organization_id": "org-bKXddeZffpMS2CUNCCXsW7m5",

  "result_files": [],

  "status": "pending",

  "training_files": [

    {

      "bytes": 41212,

      "created_at": 1680614191,

      "filename": "./dataset/tnews-finetuning_prepared_train.jsonl",

      "id": "file-DKjBKHqWFJwo7O8MNZGOcj3F",

      "object": "file",

      "purpose": "fine-tune",

      "status": "processed",

      "status_details": null

    }

  ],

  "updated_at": 1680614193,

  "validation_files": [

    {

      "bytes": 10507,

      "created_at": 1680614193,

      "filename": "./dataset/tnews-finetuning_prepared_valid.jsonl",

      "id": "file-j088k3GWqGeqY0o2DDAfWfPh",

      "object": "file",

      "purpose": "fine-tune",

      "status": "processed",

      "status_details": null

    }

  ]

}
  • 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
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101

  或者用它刚刚给的提示。注意换ID!

!openai api fine_tunes.follow -i ft-QOkrWkHU0aleR6f5IQw1UpVL
  • 1
[2023-04-04 14:32:14] Created fine-tune: ft-5LDv5IiFqPvLob3KkThWLTUG

Stream interrupted (client disconnected).
To resume the stream, run:

  openai api fine_tunes.follow -i ft-5LDv5IiFqPvLob3KkThWLTUG
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

  注意,这个是follow,刚刚上面那个是get。大家可以通过openai api --help查看更多:

!openai api --help
  • 1
usage: openai api [-h]

                  {engines.list,engines.get,engines.update,engines.generate,chat_completions.create,completions.create,deployments.list,deployments.get,deployments.delete,deployments.create,models.list,models.get,models.delete,files.create,files.get,files.delete,files.list,fine_tunes.list,fine_tunes.create,fine_tunes.get,fine_tunes.results,fine_tunes.events,fine_tunes.follow,fine_tunes.cancel,fine_tunes.delete,image.create,image.create_edit,image.create_variation,audio.transcribe,audio.translate}

                  ...



positional arguments:

  {engines.list,engines.get,engines.update,engines.generate,chat_completions.create,completions.create,deployments.list,deployments.get,deployments.delete,deployments.create,models.list,models.get,models.delete,files.create,files.get,files.delete,files.list,fine_tunes.list,fine_tunes.create,fine_tunes.get,fine_tunes.results,fine_tunes.events,fine_tunes.follow,fine_tunes.cancel,fine_tunes.delete,image.create,image.create_edit,image.create_variation,audio.transcribe,audio.translate}

                        All API subcommands



optional arguments:

  -h, --help            show this help message and exit
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

  建议大家过段时间get一下进度就好,不需要一直follow。这里可能要等一段时间,等排队完成后进入训练阶段就很快了。

  主要看status是什么状态,依然注意要换ID。

!openai api fine_tunes.get -i ft-QOkrWkHU0aleR6f5IQw1UpVL
  • 1
{

  "created_at": 1680614193,

  "events": [

    {

      "created_at": 1680614193,

      "level": "info",

      "message": "Created fine-tune: ft-QOkrWkHU0aleR6f5IQw1UpVL",

      "object": "fine-tune-event"

    },

    {

      "created_at": 1680614845,

      "level": "info",

      "message": "Fine-tune costs $2.33",

      "object": "fine-tune-event"

    },

    {

      "created_at": 1680614846,

      "level": "info",

      "message": "Fine-tune enqueued",

      "object": "fine-tune-event"

    },

    {

      "created_at": 1680617657,

      "level": "info",

      "message": "Fine-tune is in the queue. Queue number: 31",

      "object": "fine-tune-event"

    },

    {

      "created_at": 1680617785,

      "level": "info",

      "message": "Fine-tune is in the queue. Queue number: 30",

      "object": "fine-tune-event"

    },

    {

      "created_at": 1680617805,

      "level": "info",

      "message": "Fine-tune is in the queue. Queue number: 29",

      "object": "fine-tune-event"

    },

    {

      "created_at": 1680617809,

      "level": "info",

      "message": "Fine-tune is in the queue. Queue number: 28",

      "object": "fine-tune-event"

    },

    {

      "created_at": 1680617918,

      "level": "info",

      "message": "Fine-tune is in the queue. Queue number: 27",

      "object": "fine-tune-event"

    },

    {

      "created_at": 1680617928,

      "level": "info",

      "message": "Fine-tune is in the queue. Queue number: 26",

      "object": "fine-tune-event"

    },

    {

      "created_at": 1680618038,

      "level": "info",

      "message": "Fine-tune is in the queue. Queue number: 25",

      "object": "fine-tune-event"

    },

    {

      "created_at": 1680618050,

      "level": "info",

      "message": "Fine-tune is in the queue. Queue number: 24",

      "object": "fine-tune-event"

    },

    {

      "created_at": 1680618087,

      "level": "info",

      "message": "Fine-tune is in the queue. Queue number: 23",

      "object": "fine-tune-event"

    },

    {

      "created_at": 1680618096,

      "level": "info",

      "message": "Fine-tune is in the queue. Queue number: 22",

      "object": "fine-tune-event"

    },

    {

      "created_at": 1680618207,

      "level": "info",

      "message": "Fine-tune is in the queue. Queue number: 21",

      "object": "fine-tune-event"

    },

    {

      "created_at": 1680618256,

      "level": "info",

      "message": "Fine-tune is in the queue. Queue number: 20",

      "object": "fine-tune-event"

    },

    {

      "created_at": 1680618268,

      "level": "info",

      "message": "Fine-tune is in the queue. Queue number: 19",

      "object": "fine-tune-event"

    },

    {

      "created_at": 1680618336,

      "level": "info",

      "message": "Fine-tune is in the queue. Queue number: 18",

      "object": "fine-tune-event"

    },

    {

      "created_at": 1680618372,

      "level": "info",

      "message": "Fine-tune is in the queue. Queue number: 17",

      "object": "fine-tune-event"

    },

    {

      "created_at": 1680618445,

      "level": "info",

      "message": "Fine-tune is in the queue. Queue number: 16",

      "object": "fine-tune-event"

    },

    {

      "created_at": 1680618488,

      "level": "info",

      "message": "Fine-tune is in the queue. Queue number: 15",

      "object": "fine-tune-event"

    },

    {

      "created_at": 1680618582,

      "level": "info",

      "message": "Fine-tune is in the queue. Queue number: 14",

      "object": "fine-tune-event"

    },

    {

      "created_at": 1680618612,

      "level": "info",

      "message": "Fine-tune is in the queue. Queue number: 13",

      "object": "fine-tune-event"

    },

    {

      "created_at": 1680618652,

      "level": "info",

      "message": "Fine-tune is in the queue. Queue number: 12",

      "object": "fine-tune-event"

    },

    {

      "created_at": 1680618693,

      "level": "info",

      "message": "Fine-tune is in the queue. Queue number: 11",

      "object": "fine-tune-event"

    },

    {

      "created_at": 1680618717,

      "level": "info",

      "message": "Fine-tune is in the queue. Queue number: 10",

      "object": "fine-tune-event"

    },

    {

      "created_at": 1680618759,

      "level": "info",

      "message": "Fine-tune is in the queue. Queue number: 9",

      "object": "fine-tune-event"

    },

    {

      "created_at": 1680618790,

      "level": "info",

      "message": "Fine-tune is in the queue. Queue number: 8",

      "object": "fine-tune-event"

    },

    {

      "created_at": 1680618841,

      "level": "info",

      "message": "Fine-tune is in the queue. Queue number: 7",

      "object": "fine-tune-event"

    },

    {

      "created_at": 1680618886,

      "level": "info",

      "message": "Fine-tune is in the queue. Queue number: 6",

      "object": "fine-tune-event"

    },

    {

      "created_at": 1680618899,

      "level": "info",

      "message": "Fine-tune is in the queue. Queue number: 5",

      "object": "fine-tune-event"

    },

    {

      "created_at": 1680618928,

      "level": "info",

      "message": "Fine-tune is in the queue. Queue number: 4",

      "object": "fine-tune-event"

    },

    {

      "created_at": 1680618979,

      "level": "info",

      "message": "Fine-tune is in the queue. Queue number: 3",

      "object": "fine-tune-event"

    },

    {

      "created_at": 1680619057,

      "level": "info",

      "message": "Fine-tune is in the queue. Queue number: 2",

      "object": "fine-tune-event"

    },

    {

      "created_at": 1680619065,

      "level": "info",

      "message": "Fine-tune is in the queue. Queue number: 1",

      "object": "fine-tune-event"

    },

    {

      "created_at": 1680619127,

      "level": "info",

      "message": "Fine-tune is in the queue. Queue number: 0",

      "object": "fine-tune-event"

    },

    {

      "created_at": 1680619227,

      "level": "info",

      "message": "Fine-tune started",

      "object": "fine-tune-event"

    },

    {

      "created_at": 1680619443,

      "level": "info",

      "message": "Completed epoch 1/4",

      "object": "fine-tune-event"

    },

    {

      "created_at": 1680619573,

      "level": "info",

      "message": "Completed epoch 2/4",

      "object": "fine-tune-event"

    },

    {

      "created_at": 1680619701,

      "level": "info",

      "message": "Completed epoch 3/4",

      "object": "fine-tune-event"

    },

    {

      "created_at": 1680619829,

      "level": "info",

      "message": "Completed epoch 4/4",

      "object": "fine-tune-event"

    },

    {

      "created_at": 1680619890,

      "level": "info",

      "message": "Uploaded model: davinci:ft-personal-2023-04-04-14-51-29",

      "object": "fine-tune-event"

    },

    {

      "created_at": 1680619891,

      "level": "info",

      "message": "Uploaded result file: file-xvIjsDl5aFEtXWOVY6nZyLcD",

      "object": "fine-tune-event"

    },

    {

      "created_at": 1680619891,

      "level": "info",

      "message": "Fine-tune succeeded",

      "object": "fine-tune-event"

    }

  ],

  "fine_tuned_model": "davinci:ft-personal-2023-04-04-14-51-29",

  "hyperparams": {

    "batch_size": 1,

    "classification_n_classes": 14,

    "compute_classification_metrics": true,

    "learning_rate_multiplier": 0.1,

    "n_epochs": 4,

    "prompt_loss_weight": 0.01

  },

  "id": "ft-QOkrWkHU0aleR6f5IQw1UpVL",

  "model": "davinci",

  "object": "fine-tune",

  "organization_id": "org-bKXddeZffpMS2CUNCCXsW7m5",

  "result_files": [

    {

      "bytes": 82416,

      "created_at": 1680619891,

      "filename": "compiled_results.csv",

      "id": "file-xvIjsDl5aFEtXWOVY6nZyLcD",

      "object": "file",

      "purpose": "fine-tune-results",

      "status": "processed",

      "status_details": null

    }

  ],

  "status": "succeeded",

  "training_files": [

    {

      "bytes": 41212,

      "created_at": 1680614191,

      "filename": "./dataset/tnews-finetuning_prepared_train.jsonl",

      "id": "file-DKjBKHqWFJwo7O8MNZGOcj3F",

      "object": "file",

      "purpose": "fine-tune",

      "status": "processed",

      "status_details": null

    }

  ],

  "updated_at": 1680619892,

  "validation_files": [

    {

      "bytes": 10507,

      "created_at": 1680614193,

      "filename": "./dataset/tnews-finetuning_prepared_valid.jsonl",

      "id": "file-j088k3GWqGeqY0o2DDAfWfPh",

      "object": "file",

      "purpose": "fine-tune",

      "status": "processed",

      "status_details": null

    }

  ]

}
  • 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
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
  • 448
  • 449
  • 450
  • 451
  • 452
  • 453
  • 454
  • 455
  • 456
  • 457
  • 458
  • 459
  • 460
  • 461
  • 462
  • 463
  • 464
  • 465
  • 466
  • 467
  • 468
  • 469
  • 470
  • 471
  • 472
  • 473
  • 474
  • 475
  • 476
  • 477
  • 478
  • 479
  • 480
  • 481
  • 482
  • 483
  • 484
  • 485
  • 486
  • 487
  • 488
  • 489
  • 490
  • 491
  • 492
  • 493
  • 494
  • 495
  • 496
  • 497
  • 498
  • 499
  • 500
  • 501
  • 502
  • 503
  • 504
  • 505
  • 506
  • 507
  • 508
  • 509
  • 510
  • 511
  • 512
  • 513
  • 514
  • 515
  • 516
  • 517
  • 518
  • 519
  • 520
  • 521
  • 522
  • 523
  • 524
  • 525
  • 526
  • 527
  • 528
  • 529
  • 530
  • 531
  • 532
  • 533
  • 534
  • 535
  • 536
  • 537
  • 538
  • 539
  • 540
  • 541
  • 542
  • 543
  • 544
  • 545
  • 546
  • 547
  • 548
  • 549
  • 550
  • 551
  • 552
  • 553
  • 554
  • 555
  • 556
  • 557
  • 558
  • 559
  • 560
  • 561
  • 562
  • 563
  • 564
  • 565
  • 566
  • 567
  • 568
  • 569
  • 570
  • 571
  • 572
  • 573
  • 574
  • 575
  • 576
  • 577
  • 578
  • 579
  • 580
  • 581
  • 582
  • 583
  • 584
  • 585
  • 586
  • 587
  • 588
  • 589
  • 590
  • 591
  • 592
  • 593
  • 594
  • 595
  • 596
  • 597
  • 598
  • 599
  • 600
  • 601
  • 602
  • 603
  • 604
  • 605
  • 606
  • 607
  • 608
  • 609
  • 610
  • 611
  • 612
  • 613
  • 614
  • 615
  • 616
  • 617
  • 618
  • 619
  • 620
  • 621
  • 622
  • 623
  • 624
  • 625
  • 626
  • 627

  过了很长一段时间,终于成功了。微调结束后,我们还可以通过下面的命令查看结果:

# -i 就是上面微调的模型id,就是`id`字段
!openai api fine_tunes.results -i ft-QOkrWkHU0aleR6f5IQw1UpVL > metric.csv
  • 1
  • 2
metric = pd.read_csv('metric.csv')
metric[metric['classification/accuracy'].notnull()].tail(1)
  • 1
  • 2
stepelapsed_tokenselapsed_examplestraining_losstraining_sequence_accuracytraining_token_accuracyvalidation_lossvalidation_sequence_accuracyvalidation_token_accuracyclassification/accuracyclassification/weighted_f1_score
160116028322616020.0087391.01.0NaNNaNNaN0.630.619592
metric[metric['classification/accuracy'].notnull()]['classification/accuracy'].plot()
  • 1
<AxesSubplot:>
  • 1

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-usRfhl1e-1683766805146)(ChatGPT%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97%E2%80%94%E2%80%94%E5%8F%A5%E8%AF%8D%E5%88%86%E7%B1%BB_files/ChatGPT%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97%E2%80%94%E2%80%94%E5%8F%A5%E8%AF%8D%E5%88%86%E7%B1%BB_209_1.png)]

  这个Accuracy其实是非常一般的,应该是咱们给的语料太少的缘故。我们先看下怎么使用。

Step3:使用

prompt = get_prompt(lines[2]["sentence"])
print(prompt)
  • 1
  • 2
对给定文本进行分类,类别包括:科技、金融、娱乐、世界、汽车、文化、军事、旅游、游戏、教育、农业、房产、社会、股票。

给定文本:
出栏一头猪亏损300元,究竟谁能笑到最后!
类别:
  • 1
  • 2
  • 3
  • 4
  • 5
def complete(prompt, model, max_tokens=2):
    response = openai.Completion.create(
        prompt=prompt,
        temperature=0,
        max_tokens=max_tokens,
        top_p=1,
        frequency_penalty=0,
        presence_penalty=0,
        model=model
    )
    ans = response["choices"][0]["text"].strip(" \n")
    return ans
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
# 原来的,微调之前的
complete(prompt, "text-davinci-003", 5)
  • 1
  • 2
'社会'
  • 1

  注意咱们的prompt也要改一下。预期应该要返回一个英文单词,回忆一下训练数据里面的completion。我们这里为了方便说明,特意在前面用了中文标签,微调时用英文(就是希望它能直接输出英文标签,表示微调有效)。大家在实际使用时务必要统一。

  我们把模型换成刚刚微调的(就是上面返回结果中的fine_tuned_model字段)。

# 微调后的
prompt = lines[2]["sentence"] + " ->"
complete(prompt, "davinci:ft-personal-2023-04-04-14-51-29", 1)
  • 1
  • 2
  • 3
'agriculture'
  • 1

  咦——居然变成农业了,它和之前ChatGPT(ChatCompletion)的输出一样,不过依然不是我们训练数据集里的「金融」。我想这个句子看起来确实更加像农业主题,放在农业主题下应该也没问题。而且,我们的训练数据集里并没有包含这条样本。所以,这个问题不太大。

  如果我们非要它变成金融的,可以把这条数据也丢给微调接口,微调后应该就可以得到我们训练集里给的类别了。

  上面我们介绍了主题分类的微调。实体抽取的微调也是类似的,它推荐的输入格式如下:

{"prompt":"<any text, for example news article>\n\n###\n\n", "completion":" <list of entities, separated by a newline> END"}
  • 1
{'prompt': '<any text, for example news article>\n\n###\n\n',
 'completion': ' <list of entities, separated by a newline> END'}
  • 1
  • 2

  举个例子:

{"prompt":"Portugal will be removed from the UK's green travel list from Tuesday, amid rising coronavirus cases and concern over a \"Nepal mutation of the so-called Indian variant\". It will join the amber list, meaning holidaymakers should not visit and returnees must isolate for 10 days...\n\n###\n\n", "completion":" Portugal\nUK\nNepal mutation\nIndian variant END"}
  • 1
{'prompt': 'Portugal will be removed from the UK\'s green travel list from Tuesday, amid rising coronavirus cases and concern over a "Nepal mutation of the so-called Indian variant". It will join the amber list, meaning holidaymakers should not visit and returnees must isolate for 10 days...\n\n###\n\n',
 'completion': ' Portugal\nUK\nNepal mutation\nIndian variant END'}
  • 1
  • 2

  相信大家应该很容易理解,不妨自己做一些尝试。尤其是给一些专业领域的实体进行微调,对比一下微调前后的效果。

  如果大家对这块内容感兴趣,可以进一步阅读【相关文献11和12】。

其它NLU应用及实战

相关文献

推荐阅读
相关标签