OpenAI недавно выпустила возможность тонкой настройки своих современных моделей, включая gpt-3.5-turbo. Это важное событие, поскольку оно позволяет разработчикам настраивать модель ИИ в соответствии со своими конкретными потребностями. В этом сообщении блога мы познакомим вас с пошаговым руководством по тонкой настройке OpenAI GPT-3.5.

Подготовка набора обучающих данных

Первым шагом в тонкой настройке любой модели ИИ является подготовка набора обучающих данных. В нашем примере мы будем использовать CSV-файл с именем dataset.csv, содержащий пары вопросов и ответов, связанных с концепциями машинного обучения.

"question","answer"
"What is supervised learning?","Supervised learning is a type of machine learning where the model is trained on labeled data. The model makes predictions based on this data and is adjusted based on the known outcomes."
"How does unsupervised learning differ from supervised learning?","Unsupervised learning deals with unlabeled data and aims to find patterns or structures in the data, such as clustering or dimensionality reduction. There's no explicit ""correct"" answer, unlike supervised learning."
"What is a neural network?","A neural network is a series of algorithms that endeavors to recognize underlying relationships in a set of data through a process that mimics the way the human brain operates."
"How does a convolutional neural network (CNN) differ from a traditional feedforward neural network?","A CNN is specifically designed for grid-like data such as images. It uses convolutional layers to automatically and adaptively learn spatial hierarchies from the data, while a feedforward network relies solely on fully connected layers."
"Define ""overfitting"" in machine learning.","Overfitting occurs when a model learns the training data too closely, including its noise and outliers, leading to poor performance on new, unseen data."
"What is the purpose of a loss function?","A loss function quantifies how well a model's predictions match the true values. During training, the goal is typically to minimize this function."
"Name one common algorithm used for regression tasks.","One common algorithm for regression is linear regression."
"What is the difference between a parameter and a hyperparameter in machine learning?","Parameters are learned from the training data, like weights in neural networks. Hyperparameters, on the other hand, are set before training and influence the model's structure or the training process, such as the learning rate."
"Why do we split data into training and test sets?","We split data to evaluate how well the model will generalize to new, unseen data. Training is done on the training set, while evaluation happens on the test set to avoid overfitting."
"What is the role of an activation function in a neural network?","An activation function introduces non-linearity into the network, allowing it to learn and approximate more complex functions."

Затем мы конвертируем этот CSV-файл в формат JSONL (JSON Lines), который больше подходит для обучения моделей ИИ. Приведенный ниже скрипт Python считывает файл CSV и преобразует его в формат JSONL:

import json  
import pandas as pd  

DEFAULT_SYSTEM_PROMPT = 'You are customer support bot. You should help to user to answer on his question.'  

def get_example(question, answer):  
    return {  
        "messages": [  
            {"role": "system", "content": DEFAULT_SYSTEM_PROMPT},  
            {"role": "user", "content": question},  
            {"role": "assistant", "content": answer},  
        ]  
    }  

if __name__ == "__main__":  
    df = pd.read_csv("dataset.csv")  
    with open("data/train.jsonl", "w") as f:  
        for i, row in list(df.iterrows()):  
            question = row["question"]  
            answer = row["answer"]  
            example = get_example(question, answer)  
            example_str = json.dumps(example)  
            f.write(example_str + "\n")

Запуск тонкой настройки

Как только наш набор обучающих данных будет готов, мы можем приступить к точной настройке нашей модели с помощью API OpenAI:

import json
from time import sleep
import openai

def wait_untill_done(job_id): 
    events = {}
    while True: 
        response = openai.FineTuningJob.list_events(id=job_id, limit=10)
        # collect all events
        for event in response["data"]:
            if "data" in event and event["data"]:
                events[event["data"]["step"]] = event["data"]["train_loss"]
        messages = [it["message"] for it in response.data]
        for m in messages:
            if m.startswith("New fine-tuned model created: "):
                return m.split("created: ")[1], events
        sleep(10)

if __name__ == "__main__":
    response = openai.File.create(file=open("data/train.jsonl", "rb"), purpose="fine-tune")
    uploaded_id = response.id
    print("Dataset is uploaded")
    print("Sleep 30 seconds...")
    sleep(30)  # wait until dataset would be prepared
    response = openai.FineTuningJob.create(training_file=uploaded_id,model=params["model"],hypeparameters={"n_epochs": 10})
    print("Fine-tune job is started")
    ft_job_id = response.id
    new_model_name, events = wait_untill_done(ft_job_id)
    with open("result/new_model_name.txt", "w") as fp:
        fp.write(new_model_name)
    print(new_model_name)

Использование точно настроенной модели

После успешной настройки нашей модели мы теперь можем использовать ее для генерации ответов на основе пользовательского ввода:

import json
import openai

DEFAULT_SYSTEM_PROMPT ='You are customer support bot. You should help to user to answer on his question.'

def get_fine_tuned_model_name(): 
    with open("result/new_model_name.txt") as fp:
        return fp.read()

def call_openai(model_name, messages):
    response=openai.ChatCompletion.create(
        messages=messages,
        model=model_name
    )
    return response.choices[0]["message"]["content"]

if __name__=="__main__":
    model_name = get_fine_tuned_model_name()
    history = [  
            {"role": "system", "content": DEFAULT_SYSTEM_PROMPT},  
        ]
    while True:
        user_input = input("User: ")
        if user_input.lower() == 'exit':
            print("AI: Goodbye!")
            break
        history.append({'role': 'user', 'content': user_input})
        response = call_openai(model_name, history)
        print("AI:", response)
        history.append({'role': :'assistant', 'content': response})

Этот сценарий запрашивает у пользователя ввод данных и генерирует на основе этих данных ответ, сгенерированный искусственным интеллектом.

В заключение, тонкая настройка GPT-3.5 OpenAI позволяет разработчикам настраивать модель ИИ в соответствии со своими конкретными потребностями и улучшать ее производительность при выполнении определенных задач. Я надеюсь, что это руководство поможет вам понять, как можно использовать этот мощный