미세 조정 튜토리얼

<ph type="x-smartling-placeholder"></ph>

이 튜토리얼은 Gemini API 조정을 시작하는 데 도움이 됩니다. REST API를 사용하여 REST API를 curl 이 예에서는 이면의 텍스트 모델을 조정하는 방법을 보여줍니다. Gemini API 텍스트 생성 서비스입니다

ai.google.dev에서 보기 Google Colab에서 실행 GitHub에서 소스 보기

인증 설정

Gemini API를 사용하면 자체 데이터를 기반으로 모델을 조정할 수 있습니다. 사용자의 데이터와 API 키가 제공할 수 있는 것보다 더 엄격한 액세스 제어가 필요합니다

이 가이드를 실행하기 전에 먼저 도메인에 OAuth를 설정해야 합니다. 프로젝트의 인스턴스입니다.

조정된 모델 나열

genai.list_tuned_models에서 기존 조정된 모델을 확인할 수 있습니다. 메서드를 사용하여 축소하도록 요청합니다.

for model_info in genai.list_tuned_models():
    print(model_info.name)

조정된 모델 만들기

조정된 모델을 만들려면 데이터 세트를 genai.create_tuned_model 메서드를 사용하여 지도 가장자리에 패딩을 추가할 수 있습니다. 이렇게 하려면 또는 파일에서 DataFrame으로 가져와서 메서드에 전달합니다.

이 예에서는 모델을 조정하여 시퀀스입니다. 예를 들어 입력이 1이면 모델은 2를 출력해야 합니다. 만약 입력은 one hundred이고 출력은 one hundred one입니다.

import time

base_model = "models/gemini-1.5-flash-001-tuning"
training_data = [
    {"text_input": "1", "output": "2"},
    # ... more examples ...
    # ...
    {"text_input": "seven", "output": "eight"},
]
operation = genai.create_tuned_model(
    # You can use a tuned model here too. Set `source_model="tunedModels/..."`
    display_name="increment",
    source_model=base_model,
    epoch_count=20,
    batch_size=4,
    learning_rate=0.001,
    training_data=training_data,
)

for status in operation.wait_bar():
    time.sleep(10)

result = operation.result()
print(result)
# # You can plot the loss curve with:
# snapshots = pd.DataFrame(result.tuning_task.snapshots)
# sns.lineplot(data=snapshots, x='epoch', y='mean_loss')

model = genai.GenerativeModel(model_name=result.name)
result = model.generate_content("III")
print(result.text)  # IV

에포크 수, 배치 크기, 학습률의 최적값은 상황에 따라 다릅니다. 사용 사례의 기타 제약 조건을 적용할 수 있습니다 자세히 알아보려면 자세한 내용은 고급 미세 조정 설정초매개변수.

모델을 조정하는 데 상당한 시간이 걸릴 수 있으므로 이 API는 튜닝을 완료합니다 대신 google.api_core.operation.Operation을 반환합니다. 조정 작업의 상태를 확인하거나 해당 작업이 수행될 때까지 기다릴 수 있는 객체 완료되고 결과를 확인합니다.

조정된 모델은 조정된 모델 목록에 즉시 추가되지만 상태가 '생성 중'으로 설정되어 있습니다. 모델을 조정할 수 있습니다

조정 진행 상황 확인

wait_bar()를 사용하여 조정 작업의 진행 상황을 확인할 수 있습니다. 메서드를 사용하여 축소하도록 요청합니다.

for status in operation.wait_bar():
    time.sleep(10)

operation.metadata를 사용하여 총 조정 단계 수를 확인할 수도 있습니다. operation.update()는 작업 상태를 새로고침합니다.

cancel() 메서드를 사용하여 언제든지 조정 작업을 취소할 수 있습니다.

operation.cancel()

모델 사용해 보기

genai.generate_text 메서드를 사용하여 모델 이름을 지정할 수 있습니다. 모델 성능을 테스트할 수 있습니다

model = genai.GenerativeModel(model_name="tunedModels/my-increment-model")
result = model.generate_content("III")
print(result.text)  # "IV"

설명 업데이트

다음 명령어를 사용하여 언제든지 조정된 모델의 설명을 업데이트할 수 있습니다. genai.update_tuned_model 메서드를 사용하여 지도 가장자리에 패딩을 추가할 수 있습니다.

genai.update_tuned_model('tunedModels/my-increment-model', {"description":"This is my model."})

모델 삭제

더 이상 필요하지 않은 모델을 삭제하여 조정된 모델 목록을 정리할 수 있습니다. genai.delete_tuned_model 메서드를 사용하여 모델을 삭제합니다. 하나라도 취소한 경우 성능 저하가 발생할 수 있으므로 이러한 작업은 삭제하는 것이 좋습니다. 예측할 수 없습니다.

genai.delete_tuned_model("tunedModels/my-increment-model")