ดูใน ai.google.dev | เรียกใช้ใน Google Colab | ดูซอร์สบน GitHub |
ในสมุดบันทึกนี้ คุณจะได้เรียนรู้วิธีเริ่มต้นใช้งานบริการปรับแต่งโดยใช้ไลบรารีของไคลเอ็นต์ Python สำหรับ Gemini API ในที่นี้ คุณจะได้ศึกษาวิธีปรับแต่งรูปแบบข้อความที่อยู่เบื้องหลังบริการสร้างข้อความของ Gemini API
ตั้งค่า
ตรวจสอบสิทธิ์
Gemini API ให้คุณปรับแต่งโมเดลจากข้อมูลของคุณเอง และเนื่องจากเป็นข้อมูลของคุณและโมเดลที่มีการปรับแต่ง สิ่งนี้จึงต้องการการควบคุมการเข้าถึงที่เข้มงวดกว่าที่คีย์ API สามารถทำได้
คุณจะต้องตั้งค่า OAuth สำหรับโปรเจ็กต์ก่อนจึงจะเรียกใช้บทแนะนำนี้ได้
การตั้งค่าที่ง่ายที่สุดใน Colab คือคัดลอกเนื้อหาของไฟล์ client_secret.json
ลงใน "Secret Manager" ของ Colab (ใต้ไอคอนคีย์ในแผงด้านซ้าย) โดยใช้ชื่อข้อมูลลับ CLIENT_SECRET
คำสั่ง gcloud นี้จะเปลี่ยนไฟล์ client_secret.json
เป็นข้อมูลเข้าสู่ระบบที่ใช้ตรวจสอบสิทธิ์กับบริการได้
import os
if 'COLAB_RELEASE_TAG' in os.environ:
from google.colab import userdata
import pathlib
pathlib.Path('client_secret.json').write_text(userdata.get('CLIENT_SECRET'))
# Use `--no-browser` in colab
!gcloud auth application-default login --no-browser --client-id-file client_secret.json --scopes='https://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/generative-language.tuning'
else:
!gcloud auth application-default login --client-id-file client_secret.json --scopes='https://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/generative-language.tuning'
ติดตั้งไลบรารีของไคลเอ็นต์
pip install -q google-generativeai
นำเข้าไลบรารี
import google.generativeai as genai
คุณตรวจสอบโมเดลที่มีการปรับแต่งที่มีอยู่ได้โดยใช้เมธอด genai.list_tuned_model
for i, m in zip(range(5), genai.list_tuned_models()):
print(m.name)
tunedModels/my-model-8527 tunedModels/my-model-7092 tunedModels/my-model-2778 tunedModels/my-model-1298 tunedModels/my-model-3883
สร้างโมเดลที่ปรับแต่ง
หากต้องการสร้างโมเดลที่มีการปรับแต่ง คุณต้องส่งชุดข้อมูลไปยังโมเดลในเมธอด genai.create_tuned_model
ซึ่งทำได้โดยกำหนดค่าอินพุตและเอาต์พุตในการเรียกใช้โดยตรง หรือนำเข้าจากไฟล์ลงใน DataFrame เพื่อส่งผ่านไปยังเมธอดนั้น
สำหรับตัวอย่างนี้ คุณจะปรับแต่งโมเดลเพื่อสร้างตัวเลขถัดไปในลำดับ เช่น หากอินพุตคือ 1
โมเดลควรเอาต์พุต 2
หากอินพุตคือ one hundred
เอาต์พุตก็ควรเป็น one hundred one
base_model = [
m for m in genai.list_models()
if "createTunedModel" in m.supported_generation_methods][0]
base_model
Model(name='models/gemini-1.0-pro-001', base_model_id='', version='001', display_name='Gemini 1.0 Pro', description=('The best model for scaling across a wide range of tasks. This is a stable ' 'model that supports tuning.'), input_token_limit=30720, output_token_limit=2048, supported_generation_methods=['generateContent', 'countTokens', 'createTunedModel'], temperature=0.9, top_p=1.0, top_k=1)
import random
name = f'generate-num-{random.randint(0,10000)}'
operation = genai.create_tuned_model(
# You can use a tuned model here too. Set `source_model="tunedModels/..."`
source_model=base_model.name,
training_data=[
{
'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',
}
],
id = name,
epoch_count = 100,
batch_size=4,
learning_rate=0.001,
)
ระบบจะเพิ่มโมเดลที่ปรับแต่งลงในรายการโมเดลที่ปรับแต่งทันที แต่จะตั้งค่าสถานะเป็น "กำลังสร้าง" ขณะที่โมเดลนั้นได้รับการปรับแต่ง
model = genai.get_tuned_model(f'tunedModels/{name}')
model
TunedModel(name='tunedModels/generate-num-2946', source_model='models/gemini-1.0-pro-001', base_model='models/gemini-1.0-pro-001', display_name='', description='', temperature=0.9, top_p=1.0, top_k=1, state=<State.CREATING: 1>, create_time=datetime.datetime(2024, 2, 21, 20, 4, 16, 448050, tzinfo=datetime.timezone.utc), update_time=datetime.datetime(2024, 2, 21, 20, 4, 16, 448050, tzinfo=datetime.timezone.utc), tuning_task=TuningTask(start_time=datetime.datetime(2024, 2, 21, 20, 4, 16, 890698, tzinfo=datetime.timezone.utc), complete_time=None, snapshots=[], hyperparameters=Hyperparameters(epoch_count=100, batch_size=4, learning_rate=0.001)))
model.state
<State.CREATING: 1>
ตรวจสอบความคืบหน้าของการปรับแต่ง
ใช้ metadata
เพื่อตรวจสอบสถานะ
operation.metadata
total_steps: 375 tuned_model: "tunedModels/generate-num-2946"
รอให้การฝึกใช้ operation.result()
เสร็จสิ้นหรือoperation.wait_bar()
import time
for status in operation.wait_bar():
time.sleep(30)
0%| | 0/375 [00:00<?, ?it/s]
คุณยกเลิกงานการปรับแต่งได้ทุกเมื่อโดยใช้เมธอด cancel()
ยกเลิกการแสดงความคิดเห็นที่บรรทัดด้านล่าง และเรียกใช้เซลล์โค้ดเพื่อยกเลิกงานก่อนที่งานจะเสร็จสิ้น
# operation.cancel()
เมื่อการปรับแต่งเสร็จสมบูรณ์แล้ว คุณสามารถดูเส้นโค้งการสูญเสียได้จากผลการปรับแต่ง เส้นโค้งการสูญเสียจะแสดงให้เห็นว่าการคาดการณ์ของโมเดลเบี่ยงเบนจากเอาต์พุตที่ดีที่สุดมากน้อยเพียงใด
import pandas as pd
import seaborn as sns
model = operation.result()
snapshots = pd.DataFrame(model.tuning_task.snapshots)
sns.lineplot(data=snapshots, x = 'epoch', y='mean_loss')
<Axes: xlabel='epoch', ylabel='mean_loss'>
ประเมินโมเดล
คุณใช้เมธอด genai.generate_text
และระบุชื่อโมเดลเพื่อทดสอบประสิทธิภาพของโมเดลได้
model = genai.GenerativeModel(model_name=f'tunedModels/{name}')
result = model.generate_content('55')
result.text
'56'
result = model.generate_content('123455')
result.text
'123456'
result = model.generate_content('four')
result.text
'five'
result = model.generate_content('quatre') # French 4
result.text # French 5 is "cinq"
'cinq'
result = model.generate_content('III') # Roman numeral 3
result.text # Roman numeral 4 is IV
'IV'
result = model.generate_content('七') # Japanese 7
result.text # Japanese 8 is 八!
'八'
ดูเหมือนว่าจะทำงานสำเร็จแม้จะมีตัวอย่างที่จำกัด แต่ "ถัดไป" เป็นแนวคิดง่ายๆ โปรดดูคู่มือการปรับแต่งเพื่อดูคำแนะนำเพิ่มเติมเกี่ยวกับการปรับปรุงประสิทธิภาพ
อัปเดตคำอธิบาย
คุณอัปเดตคำอธิบายของโมเดลที่คุณปรับแต่งได้ทุกเมื่อโดยใช้เมธอด genai.update_tuned_model
genai.update_tuned_model(f'tunedModels/{name}', {"description":"This is my model."});
model = genai.get_tuned_model(f'tunedModels/{name}')
model.description
'This is my model.'
ลบโมเดล
คุณสามารถล้างรายการโมเดลที่ปรับแต่งได้โดยลบโมเดลที่ไม่ต้องการแล้ว ใช้เมธอด genai.delete_tuned_model
เพื่อลบโมเดล หากยกเลิกงานการปรับแต่งแล้ว คุณอาจต้องลบงานเหล่านั้นเนื่องจากอาจคาดเดาประสิทธิภาพไม่ได้
genai.delete_tuned_model(f'tunedModels/{name}')
ไม่มีโมเดลนี้แล้ว:
try:
m = genai.get_tuned_model(f'tunedModels/{name}')
print(m)
except Exception as e:
print(f"{type(e)}: {e}")
<class 'google.api_core.exceptions.NotFound'>: 404 Tuned model tunedModels/generate-num-2946 does not exist.