บทแนะนำการปรับแต่ง

บทแนะนำนี้จะช่วยให้คุณเริ่มต้นใช้งานบริการการปรับแต่ง Gemini API ได้โดยใช้ Python SDK หรือ REST API โดยใช้ curl ตัวอย่างแสดงวิธีปรับแต่งโมเดลข้อความที่อยู่เบื้องหลังบริการสร้างข้อความของ Gemini API

ก่อนเริ่มต้น

ก่อนเรียกใช้ Gemini API โปรดตรวจสอบว่าคุณได้ติดตั้ง SDK ที่ต้องการ รวมถึงกําหนดค่าคีย์ API ของ Gemini ให้พร้อมใช้งานแล้ว

ดูใน ai.google.dev ลองใช้ Colab Notebook ดูสมุดบันทึกใน GitHub

ข้อจำกัด

ก่อนที่จะปรับแต่งโมเดล คุณควรทราบข้อจํากัดต่อไปนี้

การปรับแต่งชุดข้อมูล

การปรับแต่งชุดข้อมูลสําหรับ Gemini 1.5 Flash มีข้อจํากัดต่อไปนี้

  • ขนาดอินพุตสูงสุดต่อตัวอย่างคือ 40,000 อักขระ
  • ขนาดเอาต์พุตสูงสุดต่อตัวอย่างคือ 5,000 อักขระ
  • ระบบรองรับเฉพาะตัวอย่างคู่อินพุต-เอาต์พุตเท่านั้น ไม่รองรับการสนทนาแบบหลายรอบในลักษณะแชท

โมเดลที่ปรับแต่งแล้ว

โมเดลที่ปรับแต่งมีข้อจํากัดต่อไปนี้

  • ขีดจํากัดของข้อมูลที่ป้อนของรุ่น Gemini 1.5 Flash ที่ปรับแต่งแล้วคือ 40,000 อักขระ
  • โมเดลที่ปรับแต่งแล้วไม่รองรับโหมด JSON
  • รองรับเฉพาะการป้อนข้อความ

แสดงรายการโมเดลที่ปรับแต่งแล้ว

คุณสามารถตรวจสอบโมเดลที่ปรับแต่งที่มีอยู่ด้วยเมธอด 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=$GEMINI_API_KEY \
    -H "Content-Type: application/json"  > tuned_models2.json
jq .tunedModels[].name < tuned_models.json
fi

สร้างโมเดลที่ปรับแต่งแล้ว

หากต้องการสร้างโมเดลที่ปรับแต่งแล้ว คุณต้องส่งdatasetไปยังโมเดลในเมธอด tunedModels.create

ในตัวอย่างนี้ คุณจะปรับแต่งโมเดลเพื่อสร้างตัวเลขถัดไปในลําดับ เช่น หากอินพุตคือ 1 โมเดลควรแสดงผลเป็น 2 หากป้อน one hundred เอาต์พุตควรเป็น one hundred one

curl -X POST "https://generativelanguage.googleapis.com/v1beta/tunedModels?key=$GEMINI_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=$GEMINI_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=$GEMINI_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=$GEMINI_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=$GEMINI_API_KEY \
    -H 'Content-Type: application/json'