如何微调GPT-4o mini聊天模型

  每周跟踪AI热点新闻动向和震撼发展 想要探索生成式人工智能的前沿进展吗?订阅我们的简报,深入解析最新的技术突破、实际应用案例和未来的趋势。与全球数同行一同,从行业内部的深度分析和实用指南中受益。不要错过这个机会,成为AI领域的领跑者。点击订阅,与未来同行! 订阅:https://rengongzhineng.io/

微调模型是通过在大量特定任务的数据上进一步训练现有的大型语言模型(LLM),以提升模型在这些任务上的性能。以下是一份关于使用新的GPT-4o mini进行微调的详细指南。我们将使用RecipeNLG数据集进行实体提取任务,这是一个常用于命名实体识别(NER)任务的数据集。

1. 设置

首先,确保使用最新版本的OpenAI Python包,然后加载数据集并筛选出特定领域的数据以用于微调。在本例中,我们使用了RecipeNLG数据集的一个子集,该子集仅包含来自cookbooks.com的文档。

# 更新OpenAI Python包
!pip install --upgrade --quiet openai

import json
import openai
import os
import pandas as pd
from pprint import pprint

# 设置OpenAI客户端
client = openai.OpenAI(
    api_key=os.environ.get("OPENAI_API_KEY"),
    organization="<组织ID>",
    project="<项目ID>",
)

# 读取数据集
recipe_df = pd.read_csv("data/cookbook_recipes_nlg_10k.csv")
recipe_df.head()

2. 数据准备

为了进行微调,需要准备训练和验证数据。每个训练示例应包含一个对话列表,代表模型的输入和输出格式。我们可以使用以下代码准备示例对话:

system_message = "You are a helpful recipe assistant. You are to extract the generic ingredients from each of the recipes provided."

def create_user_message(row):
    return f"Title: {row['title']}\n\nIngredients: {row['ingredients']}\n\nGeneric ingredients: "

def prepare_example_conversation(row):
    return {
        "messages": [
            {"role": "system", "content": system_message},
            {"role": "user", "content": create_user_message(row)},
            {"role": "assistant", "content": row["NER"]},
        ]
    }

pprint(prepare_example_conversation(recipe_df.iloc[0]))

然后,我们从数据集中提取前100行作为训练数据:

training_df = recipe_df.loc[0:100]
training_data = training_df.apply(prepare_example_conversation, axis=1).tolist()

保存训练和验证数据为.jsonl文件:

def write_jsonl(data_list: list, filename: str) -> None:
    with open(filename, "w") as out:
        for ddict in data_list:
            jout = json.dumps(ddict) + "\n"
            out.write(jout)

training_file_name = "tmp_recipe_finetune_training.jsonl"
write_jsonl(training_data, training_file_name)

3. 上传文件

将数据文件上传到OpenAI的Files端点,以便微调模型使用。

def upload_file(file_name: str, purpose: str) -> str:
    with open(file_name, "rb") as file_fd:
        response = client.files.create(file=file_fd, purpose=purpose)
    return response.id

training_file_id = upload_file(training_file_name, "fine-tune")
print("Training file ID:", training_file_id)

4. 微调模型

创建微调任务,指定训练文件和模型基准。使用以下代码进行微调:

MODEL = "gpt-4o-mini-2024-07-18"

response = client.fine_tuning.jobs.create(
    training_file=training_file_id,
    model=MODEL,
    suffix="recipe-ner",
)

job_id = response.id
print("Job ID:", response.id)

5. 检查微调状态

使用以下代码检查微调任务的进度:

response = client.fine_tuning.jobs.retrieve(job_id)
print("Job ID:", response.id)
print("Status:", response.status)

6. 使用微调模型进行推理

当微调完成后,可以使用微调后的模型进行推理。调用ChatCompletions API,并将微调模型的ID作为参数:

response = client.chat.completions.create(
    model=fine_tuned_model_id, messages=test_messages, temperature=0, max_tokens=500
)
print(response.choices[0].message.content)

总结

通过以上步骤,开发者可以成功地微调并部署适合特定任务的GPT-4o mini模型。这一流程为开发者提供了极大的灵活性,使得他们能够更好地利用模型进行特定领域的应用。

  • 7
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值