首页 > 其他分享 >使用OpenAI API进行Model Fine-tuning

使用OpenAI API进行Model Fine-tuning

时间:2023-05-31 21:24:45浏览次数:72  
标签:completion prompt tuning positive negative API OpenAI so my

目录

1 基本信息

参考资料:

基本步骤:

  • 制作并上传训练数据
  • 训练一个新的微调模型
  • 使用训练好的微调模型

2 操作示例

2.1 准备数据

最佳实践:https://platform.openai.com/docs/guides/fine-tuning/preparing-your-dataset

  • prompt建议以固定的分隔符结尾,比如: :, ->\n\n###\n\n,该分隔符只用于标识prompt的结束,不在prompt的其他任何地方出现
  • completion建议以空格开头
  • completion同样建议以固定的分隔符结尾,比如: \n###,该分隔符只用于标识completion的结束,不在completion的其他任何地方出现
  • 建议准备至少 数百条 数据用于微调训练
  • 在使用微调模型进行推理时,输入的prompt需要遵循与训练时一致的句式格式,并用微调数据中使用的completion结束符对模型返回的completion进行截断

训练数据需要整理成以下 JSONL 格式:

{"prompt": "<prompt text>", "completion": "<ideal generated text>"}
{"prompt": "<prompt text>", "completion": "<ideal generated text>"}
{"prompt": "<prompt text>", "completion": "<ideal generated text>"}
...

以情感分析任务为例,官方给出的示例数据为:

{"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"}
{"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"}

我们让ChatGPT参考该样例,再生成20条类似格式的数据:

{"prompt":"I just got a promotion at work! ->", "completion":" positive"}
{"prompt":"The traffic today was absolutely terrible. ->", "completion":" negative"}
{"prompt":"I'm so grateful for my supportive friends and family. ->", "completion":" positive"}
{"prompt":"I can't believe how rude the customer service was at that store. ->", "completion":" negative"}
{"prompt":"I'm so excited to go on vacation next week! ->", "completion":" positive"}
{"prompt":"I'm feeling really down today. ->", "completion":" negative"}
{"prompt":"I just finished reading a great book. ->", "completion":" positive"}
{"prompt":"The weather has been awful lately. ->", "completion":" negative"}
{"prompt":"I had a wonderful time at the concert last night. ->", "completion":" positive"}
{"prompt":"I'm so frustrated with my job right now. ->", "completion":" negative"}
{"prompt":"I love spending time with my family. ->", "completion":" positive"}
{"prompt":"I'm so disappointed with the election results. ->", "completion":" negative"}
{"prompt":"I'm so grateful for my health. ->", "completion":" positive"}
{"prompt":"That movie was terrible. ->", "completion":" negative"}
{"prompt":"I'm so proud of my child for doing well in school. ->", "completion":" positive"}
{"prompt":"I had a terrible experience at the restaurant last night. ->", "completion":" negative"}
{"prompt":"I'm so happy with my new car. ->", "completion":" positive"}
{"prompt":"I'm feeling really anxious about the upcoming deadline. ->", "completion":" negative"}
{"prompt":"I had a fantastic time at the party last night. ->", "completion":" positive"}
{"prompt":"I'm so upset about the recent news. ->", "completion":" negative"}

保存数据:

import json

data = [
    {"prompt":"Overjoyed with the new iPhone! ->", "completion":" positive"},
    {"prompt":"@lakers disappoint for a third straight night https://t.co/38EFe43 ->", "completion":" negative"},
    {"prompt":"I just got a promotion at work! ->", "completion":" positive"},
    {"prompt":"The traffic today was absolutely terrible. ->", "completion":" negative"},
    {"prompt":"I'm so grateful for my supportive friends and family. ->", "completion":" positive"},
    {"prompt":"I can't believe how rude the customer service was at that store. ->", "completion":" negative"},
    {"prompt":"I'm so excited to go on vacation next week! ->", "completion":" positive"},
    {"prompt":"I'm feeling really down today. ->", "completion":" negative"},
    {"prompt":"I just finished reading a great book. ->", "completion":" positive"},
    {"prompt":"The weather has been awful lately. ->", "completion":" negative"},
    {"prompt":"I had a wonderful time at the concert last night. ->", "completion":" positive"},
    {"prompt":"I'm so frustrated with my job right now. ->", "completion":" negative"},
    {"prompt":"I love spending time with my family. ->", "completion":" positive"},
    {"prompt":"I'm so disappointed with the election results. ->", "completion":" negative"},
    {"prompt":"I'm so grateful for my health. ->", "completion":" positive"},
    {"prompt":"That movie was terrible. ->", "completion":" negative"},
    {"prompt":"I'm so proud of my child for doing well in school. ->", "completion":" positive"},
    {"prompt":"I had a terrible experience at the restaurant last night. ->", "completion":" negative"},
    {"prompt":"I'm so happy with my new car. ->", "completion":" positive"},
    {"prompt":"I'm feeling really anxious about the upcoming deadline. ->", "completion":" negative"},
    {"prompt":"I had a fantastic time at the party last night. ->", "completion":" positive"},
    {"prompt":"I'm so upset about the recent news. ->", "completion":" negative"},
]

with open("fine-tuning.jsonl", "w") as f:
    for item in data:
        json.dump(item, f)
        f.write("\n")

上传数据到OpenAI:

import os

openai.File.create(
  file=open("fine-tuning.jsonl", "rb"),
  purpose='fine-tune'
)

记录返回的文件id:

<File file id=xxx at 0x7fe7d80b4890> JSON: {
  "bytes": 1918,
  "created_at": 1685516851,
  "filename": "file",
  "id": "【文件id】",
  "object": "file",
  "purpose": "fine-tune",
  "status": "uploaded",
  "status_details": null
}

历史文件的id可通过 openai.File.list() 来查看

2.2 模型训练

创建Fine-tuning任务:

openai.FineTune.create(training_file="【文件id】")

查看组织下所有的Fine-tuning任务:

openai.FineTune.list()

查看某个Fine-tuning任务的详细信息:

openai.FineTune.retrieve(id="【任务id】")
  • 查看详细信息时,其中包含的message会显示本次微调的计费信息:Fine-tune costs $x.xx,以及微调任务对应的结果模型id:Uploaded model: curie:ft-personal-2023-05-31-07-17-56

注:Fine-tuning阶段的计费取决于训练数据的token数和微调时的轮次参数“epochs”(来源:Pricing页面最下方的FAQ):

There are two components to fine-tuning pricing: training and usage.

When training a fine-tuned model, the total tokens used will be billed according to our training rates. Note that the number of training tokens depends on the number of tokens in your training dataset and your chosen number of training epochs. The default number of epochs is 4.

(Tokens in your training file * Number of training epochs) = Total training tokens

Once you fine-tune a model, you’ll be billed only for the tokens you use. Requests sent to fine-tuned models are billed at our usage rates.

2.3 模型推理

使用模型训练时得到的模型ID进行推理:

ft_model = 'curie:ft-personal-2023-05-31-07-17-56'
res = openai.Completion.create(model=ft_model, prompt='I just finished a challenging workout and feel great! ->', max_tokens=1, temperature=0)
res['choices'][0]['text']  # 结果:' positive'

推理阶段的费用可以通过 res['usage'] 中的token数换算得到,具体数值可参考下表:

Model Usage
Ada $0.0016 / 1K tokens
Babbage $0.0024 / 1K tokens
Curie $0.0120 / 1K tokens
Davinci $0.1200 / 1K tokens

标签:completion,prompt,tuning,positive,negative,API,OpenAI,so,my
From: https://www.cnblogs.com/lokvahkoor/p/17447344.html

相关文章

  • kaggle API 命令下载数据集
    1.kaggle介绍Kaggle(官网:https://www.kaggle.com/)是由AnthonyGoldbloom和BenHamner于2010年创立的一个数据科学社区。它为数据科学家和机器学习工程师提供了一个平台,可以在该平台上进行数据分析和建模活动,同时进行竞赛式的数据分析等活动。Kaggle除了提供竞赛外,还有数据......
  • Elasticsearch专题精讲—— REST APIs —— Document APIs —— 索引API
    RESTAPIs——DocumentAPIs——索引APIhttps://www.elastic.co/guide/en/elasticsearch/reference/8.8/docs-index_.html#docs-index_ AddsaJSONdocumenttothespecifieddatastreamorindexandmakesitsearchable.Ifthetargetisanindexandth......
  • 最佳实践:如何设计 RESTful API ?
    良好的API设计是一个经常被提及的话题,特别是对于那些试图完善其API策略的团队来说。一个设计良好的API的好处包括:改进开发者体验、更快速地编写文档以及更高效地推广你的API。但是,到底什么才构成了良好API设计呢?在这篇博客文章中,我将详细介绍几个为RESTfulAPIs设计最佳......
  • Intel Media SDK and Intel® oneAPI Video Processing Library (oneVPL)
    TheIntelMediaSoftwareDevelopmentKit(IntelMediaSDK)isacross-platformapplicationprogramminginterface(API)fordevelopingmediaapplicationsonWindowsandLinux. Itismainlyusedforhardware-acceleratedvideoencoding,decoding,andprocess......
  • Vue+.net core 7 api跨域
    nginx的不说,直接说在项目中配置的。重点:前端要配代理,后端要设置返回的头文件信息。双管齐下1、前端在项目中的vue.config.js配置中进行设置module.exports={publicPath:'/',outputDir:'dist',//发布输入文件assetsDir:'static',//需要/demo目录下放打包后......
  • 《最新出炉》系列初窥篇-Python+Playwright自动化测试-2-playwright的API及其他知识
    1.简介上一篇宏哥已经将Python+Playwright的环境搭建好了,而且也简单的演示了一下三款浏览器的启动和关闭,是不是很简单啊。今天主要是把一篇的中的代码进行一次详细的注释,然后说一下playwright的API和其他相关知识点。那么首先将上一篇中的代码进行一下详细的解释。2.代码解释2.......
  • java api 设置kettle 日志到数据库
    设置到数据库transMeta.addDatabase(getDatabaseMeta(config));TransLogTableorigTransLogTable=transMeta.getTransLogTable();origTransLogTable.setConnectionName(config.getName());origTransLogTable.setTableName("r_log_trans&......
  • Postman请求Azure的OpenAI
    界面是: AZURE_OPENAI_ENDPOINT/openai/deployments/deployment_name/chat/completions?api-version=2023-05-15注意Body下的raw选json格式 {"messages":[{"role":"system","content":"......
  • API NEWS | 三个Argo CD API漏洞
    欢迎大家围观小阑精心整理的API安全最新资讯,在这里你能看到最专业、最前沿的API安全技术和产业资讯,我们提供关于全球API安全资讯与信息安全深度观察。本周,我们带来的分享如下:关于三个ArgoCDAPI漏洞的文章Gartner对API安全的看法分布式标识是现代API安全的关键关于三个ArgoCDAPI......
  • P9370 APIO2023 赛博乐园 / cyberland
    P9370APIO2023赛博乐园/cyberland。题目就是让我们求一个有各种优惠政策的单源点\(0\),单汇点\(H\)的最短路。优惠政策是:到达能力为\(2\)的点,可以让之前走过的距离除以\(2\)。到达能力为\(0\)的点,可以让之前走过的距离直接变成\(0\)。有限制:不能经过\(H\)多次......