미세 조정 튜토리얼

이 튜토리얼에서는 Python SDK 또는 curl을 사용하여 REST API를 사용하여 Gemini API 조정 서비스를 시작하는 방법을 설명합니다. 이 예에서는 Gemini API 텍스트 생성 서비스의 텍스트 모델을 조정하는 방법을 보여줍니다.

ai.google.dev에서 보기 Colab 노트북 사용해 보기 GitHub에서 노트북 보기

제한사항

모델을 조정하기 전에 다음 제한사항을 알아야 합니다.

미세 조정 데이터 세트

Gemini 1.5 Flash의 데이터 세트를 미세 조정하는 데는 다음과 같은 제한사항이 있습니다.

  • 예시당 최대 입력 크기는 40,000자(영문 기준)입니다.
  • 예시당 최대 출력 크기는 5,000자(영문 기준)입니다.
  • 입력-출력 쌍 예시만 지원됩니다. 채팅 스타일의 여러 번의 대화는 지원되지 않습니다.

조정된 모델

조정된 모델에는 다음과 같은 제한사항이 있습니다.

  • 조정된 Gemini 1.5 Flash 모델의 입력 한도는 40,000자(영문 기준)입니다.
  • 조정된 모델에서는 JSON 모드가 지원되지 않습니다.
  • 텍스트 입력만 지원됩니다.

시작하기 전에: 프로젝트 및 API 키 설정

Gemini API를 호출하기 전에 프로젝트를 설정하고 API 키를 구성해야 합니다.

조정된 모델 목록

tunedModels.list 메서드를 사용하여 기존의 조정된 모델을 확인할 수 있습니다.

# Sending a page_size is optional
curl -X GET https://generativelanguage.googleapis.com/v1beta/tunedModels?page_size=5 \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer ${access_token}" \
    -H "x-goog-user-project: ${project_id}" > tuned_models.json

jq .tunedModels[].name < tuned_models.json

# Send the nextPageToken to get the next page.
page_token=$(jq .nextPageToken < tuned_models.json | tr -d '"')

if [[ "$page_token" != "null"" ]]; then
curl -X GET https://generativelanguage.googleapis.com/v1beta/tunedModels?page_size=5\&page_token=${page_token}?key=$GOOGLE_API_KEY \
    -H "Content-Type: application/json"  > tuned_models2.json
jq .tunedModels[].name < tuned_models.json
fi

조정된 모델 만들기

조정된 모델을 만들려면 tunedModels.create 메서드에서 데이터 세트를 모델에 전달해야 합니다.

이 예에서는 시퀀스의 다음 숫자를 생성하도록 모델을 조정합니다. 예를 들어 입력이 1이면 모델은 2을 출력해야 합니다. 입력이 one hundred이면 출력은 one hundred one입니다.

curl -X POST "https://generativelanguage.googleapis.com/v1beta/tunedModels?key=$GOOGLE_API_KEY" \
    -H 'Content-Type: application/json' \
    -d '
      {
        "display_name": "number generator model",
        "base_model": "models/gemini-1.5-flash-001-tuning",
        "tuning_task": {
          "hyperparameters": {
            "batch_size": 2,
            "learning_rate": 0.001,
            "epoch_count":5,
          },
          "training_data": {
            "examples": {
              "examples": [
                {
                    "text_input": "1",
                    "output": "2",
                },{
                    "text_input": "3",
                    "output": "4",
                },{
                    "text_input": "-3",
                    "output": "-2",
                },{
                    "text_input": "twenty two",
                    "output": "twenty three",
                },{
                    "text_input": "two hundred",
                    "output": "two hundred one",
                },{
                    "text_input": "ninety nine",
                    "output": "one hundred",
                },{
                    "text_input": "8",
                    "output": "9",
                },{
                    "text_input": "-98",
                    "output": "-97",
                },{
                    "text_input": "1,000",
                    "output": "1,001",
                },{
                    "text_input": "10,100,000",
                    "output": "10,100,001",
                },{
                    "text_input": "thirteen",
                    "output": "fourteen",
                },{
                    "text_input": "eighty",
                    "output": "eighty one",
                },{
                    "text_input": "one",
                    "output": "two",
                },{
                    "text_input": "three",
                    "output": "four",
                },{
                    "text_input": "seven",
                    "output": "eight",
                }
              ]
            }
          }
        }
      }' | tee tunemodel.json

# Check the operation for status updates during training.
# Note: you can only check the operation on v1/
operation=$(cat tunemodel.json | jq ".name" | tr -d '"')
tuning_done=false

while [[ "$tuning_done" != "true" ]];
do
  sleep 5
  curl -X GET "https://generativelanguage.googleapis.com/v1/${operation}?key=$GOOGLE_API_KEY" \
    -H 'Content-Type: application/json' \
     2> /dev/null > tuning_operation.json

  complete=$(jq .metadata.completedPercent < tuning_operation.json)
  tput cuu1
  tput el
  echo "Tuning...${complete}%"
  tuning_done=$(jq .done < tuning_operation.json)
done

# Or get the TunedModel and check it's state. The model is ready to use if the state is active.
modelname=$(cat tunemodel.json | jq ".metadata.tunedModel" | tr -d '"')
curl -X GET  https://generativelanguage.googleapis.com/v1beta/${modelname}?key=$GOOGLE_API_KEY \
    -H 'Content-Type: application/json' > tuned_model.json

cat tuned_model.json | jq ".state"

에포크 수, 배치 크기, 학습률의 최적값은 데이터 세트와 사용 사례의 다른 제약 조건에 따라 다릅니다. 이러한 값에 관한 자세한 내용은 고급 조정 설정초매개변수를 참고하세요.

조정된 모델은 조정된 모델 목록에 즉시 추가되지만 모델이 조정되는 동안 상태는 '만들기 중'으로 설정됩니다.

모델 사용해 보기

tunedModels.generateContent 메서드를 사용하고 조정된 모델의 이름을 지정하여 성능을 테스트할 수 있습니다.

curl -X POST https://generativelanguage.googleapis.com/v1beta/$modelname:generateContent?key=$GOOGLE_API_KEY \
    -H 'Content-Type: application/json' \
    -d '{
        "contents": [{
        "parts": [{
          "text": "LXIII"
          }]
        }]
        }' 2> /dev/null

모델 삭제

더 이상 필요 없는 모델을 삭제하여 조정된 모델 목록을 정리할 수 있습니다. tunedModels.delete 메서드를 사용하여 모델을 삭제합니다. 조정 작업을 취소한 경우 성능을 예측할 수 없으므로 이러한 작업을 삭제하는 것이 좋습니다.

curl -X DELETE https://generativelanguage.googleapis.com/v1beta/${modelname}?key=$GOOGLE_API_KEY \
    -H 'Content-Type: application/json'