From 9712be54ee8ad6c4d125a2cc0388a2a7658b22dd Mon Sep 17 00:00:00 2001 From: Shilpa Kancharla Date: Thu, 1 Aug 2024 08:23:39 +0200 Subject: [PATCH 1/5] REST for tuned models --- samples/rest/tuned_models.sh | 118 +++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 samples/rest/tuned_models.sh diff --git a/samples/rest/tuned_models.sh b/samples/rest/tuned_models.sh new file mode 100644 index 000000000..e851c666f --- /dev/null +++ b/samples/rest/tuned_models.sh @@ -0,0 +1,118 @@ +set -eu + +export access_token=$(gcloud auth application-default print-access-token) +export project_id=my-project-id +export base_url=https://generativelanguage.googleapis.com + +import os + +access_token = !gcloud auth application-default print-access-token +access_token = '\n'.join(access_token) + +os.environ['access_token'] = access_token +os.environ['project_id'] = "[Enter your project-id here]" +os.environ['base_url'] = "https://generativelanguage.googleapis.com" + +echo "[START tuned_models_create]" +# [START tuned_models_create] +curl -X POST $base_url/v1beta/tunedModels \ + -H 'Content-Type: application/json' \ + -H "Authorization: Bearer ${access_token}" \ + -H "x-goog-user-project: ${project_id}" \ + -d ' + { + "display_name": "number generator model", + "base_model": "models/gemini-1.0-pro-001", + "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 +# [END tuned_models_create] + +echo "[START tuned_models_generate_content]" +# [START tuned_models_generate_content] +curl -X POST $base_url/v1beta/$modelname:generateContent \ + -H 'Content-Type: application/json' \ + -H "Authorization: Bearer ${access_token}" \ + -H "x-goog-user-project: ${project_id}" \ + -d '{ + "contents": [{ + "parts": [{ + "text": "LXIII" + }] + }] + }' 2> /dev/null +# [END tuned_models_generate_content] + +echo "[START tuned_models_get]" +# [START tuned_models_get] +curl -X GET ${base_url}/v1beta/${modelname} \ + -H 'Content-Type: application/json' \ + -H "Authorization: Bearer ${access_token}" \ + -H "x-goog-user-project: ${project_id}" | grep state +# [END tuned_models_get] + +echo "[START tuned_models_list]" +# [START tuned_models_list] +curl -X GET ${base_url}/v1beta/tunedModels \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer ${access_token}" \ + -H "x-goog-user-project: ${project_id}" +# [END tuned_models_list] + From 96872f41347587804291dcabda0109af22b2890f Mon Sep 17 00:00:00 2001 From: Mark Daoust Date: Thu, 1 Aug 2024 20:50:26 -0700 Subject: [PATCH 2/5] Some fixes. Change-Id: I70d066ceacc7c07e27ac59359da87d9b9747353b --- samples/rest/tuned_models.sh | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/samples/rest/tuned_models.sh b/samples/rest/tuned_models.sh index e851c666f..c8c8d2b59 100644 --- a/samples/rest/tuned_models.sh +++ b/samples/rest/tuned_models.sh @@ -1,21 +1,14 @@ set -eu export access_token=$(gcloud auth application-default print-access-token) -export project_id=my-project-id -export base_url=https://generativelanguage.googleapis.com +export project_id= -import os +access_token=$(gcloud auth application-default print-access-token) -access_token = !gcloud auth application-default print-access-token -access_token = '\n'.join(access_token) - -os.environ['access_token'] = access_token -os.environ['project_id'] = "[Enter your project-id here]" -os.environ['base_url'] = "https://generativelanguage.googleapis.com" echo "[START tuned_models_create]" # [START tuned_models_create] -curl -X POST $base_url/v1beta/tunedModels \ +curl -X POST https://generativelanguage.googleapis.com/v1beta/tunedModels \ -H 'Content-Type: application/json' \ -H "Authorization: Bearer ${access_token}" \ -H "x-goog-user-project: ${project_id}" \ @@ -83,11 +76,28 @@ curl -X POST $base_url/v1beta/tunedModels \ } } }' | 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 '"') +curl -X GET https://generativelanguage.googleapis.com/v1/${operation} \ + -H 'Content-Type: application/json' \ + -H "Authorization: Bearer ${access_token}" \ + -H "x-goog-user-project: ${project_id}" + +# 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} \ + -H 'Content-Type: application/json' \ + -H "Authorization: Bearer ${access_token}" \ + -H "x-goog-user-project: ${project_id}" | tee check.json + +cat check.json | jq ".state" # [END tuned_models_create] echo "[START tuned_models_generate_content]" # [START tuned_models_generate_content] -curl -X POST $base_url/v1beta/$modelname:generateContent \ +curl -X POST https://generativelanguage.googleapis.com/v1beta/$modelname:generateContent \ -H 'Content-Type: application/json' \ -H "Authorization: Bearer ${access_token}" \ -H "x-goog-user-project: ${project_id}" \ @@ -102,7 +112,7 @@ curl -X POST $base_url/v1beta/$modelname:generateContent \ echo "[START tuned_models_get]" # [START tuned_models_get] -curl -X GET ${base_url}/v1beta/${modelname} \ +curl -X GET https://generativelanguage.googleapis.com/v1beta/${modelname} \ -H 'Content-Type: application/json' \ -H "Authorization: Bearer ${access_token}" \ -H "x-goog-user-project: ${project_id}" | grep state @@ -110,7 +120,7 @@ curl -X GET ${base_url}/v1beta/${modelname} \ echo "[START tuned_models_list]" # [START tuned_models_list] -curl -X GET ${base_url}/v1beta/tunedModels \ +curl -X GET https://generativelanguage.googleapis.com/v1beta/tunedModels \ -H "Content-Type: application/json" \ -H "Authorization: Bearer ${access_token}" \ -H "x-goog-user-project: ${project_id}" From aa5be36d17c9d9e7b68e1c4ea24b6aa9e1620520 Mon Sep 17 00:00:00 2001 From: Mark Daoust Date: Wed, 7 Aug 2024 11:02:01 -0700 Subject: [PATCH 3/5] add progress reporting, add page_token Change-Id: I7881d6e5703d5bb027329aa22d0572132e024703 --- samples/rest/tuned_models.sh | 42 ++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/samples/rest/tuned_models.sh b/samples/rest/tuned_models.sh index c8c8d2b59..e5bb9927d 100644 --- a/samples/rest/tuned_models.sh +++ b/samples/rest/tuned_models.sh @@ -1,8 +1,5 @@ set -eu -export access_token=$(gcloud auth application-default print-access-token) -export project_id= - access_token=$(gcloud auth application-default print-access-token) @@ -80,21 +77,34 @@ curl -X POST https://generativelanguage.googleapis.com/v1beta/tunedModels \ # 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 '"') -curl -X GET https://generativelanguage.googleapis.com/v1/${operation} \ +tuning_done=false + +while [[ "$tuning_done" != "true" ]]; +do + sleep 5 + curl -X GET https://generativelanguage.googleapis.com/v1/${operation} \ -H 'Content-Type: application/json' \ -H "Authorization: Bearer ${access_token}" \ - -H "x-goog-user-project: ${project_id}" + -H "x-goog-user-project: ${project_id}" 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} \ -H 'Content-Type: application/json' \ -H "Authorization: Bearer ${access_token}" \ - -H "x-goog-user-project: ${project_id}" | tee check.json + -H "x-goog-user-project: ${project_id}" > tuned_model.json -cat check.json | jq ".state" +cat tuned_model.json | jq ".state" # [END tuned_models_create] + echo "[START tuned_models_generate_content]" # [START tuned_models_generate_content] curl -X POST https://generativelanguage.googleapis.com/v1beta/$modelname:generateContent \ @@ -120,9 +130,23 @@ curl -X GET https://generativelanguage.googleapis.com/v1beta/${modelname} \ echo "[START tuned_models_list]" # [START tuned_models_list] -curl -X GET https://generativelanguage.googleapis.com/v1beta/tunedModels \ +# 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} \ -H "Content-Type: application/json" \ -H "Authorization: Bearer ${access_token}" \ - -H "x-goog-user-project: ${project_id}" + -H "x-goog-user-project: ${project_id}" > tuned_models2.json +jq .tunedModels[].name < tuned_models.json +fi # [END tuned_models_list] From fc7049ff537389149821ac3f3dadc22bc9d18568 Mon Sep 17 00:00:00 2001 From: Shilpa Kancharla Date: Fri, 16 Aug 2024 14:01:43 -0700 Subject: [PATCH 4/5] Add delete tuned models --- samples/rest/tuned_models.sh | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/samples/rest/tuned_models.sh b/samples/rest/tuned_models.sh index c8c8d2b59..ad0fe74a8 100644 --- a/samples/rest/tuned_models.sh +++ b/samples/rest/tuned_models.sh @@ -126,3 +126,10 @@ curl -X GET https://generativelanguage.googleapis.com/v1beta/tunedModels \ -H "x-goog-user-project: ${project_id}" # [END tuned_models_list] +echo "[START tuned_models_delete]" +# [START tuned_models_delete] +curl -X DELETE https://generativelanguage.googleapis.com/v1beta/${modelname} \ + -H 'Content-Type: application/json' \ + -H "Authorization: Bearer ${access_token}" \ + -H "x-goog-user-project: ${project_id}" +# [END tuned_models_delete] \ No newline at end of file From 5440906310ea362fdfcff315765a42103b2e3177 Mon Sep 17 00:00:00 2001 From: Shilpa Kancharla Date: Wed, 21 Aug 2024 16:19:33 -0700 Subject: [PATCH 5/5] Update readme --- samples/rest/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/samples/rest/README.md b/samples/rest/README.md index bff8867cf..7969097d2 100644 --- a/samples/rest/README.md +++ b/samples/rest/README.md @@ -23,3 +23,4 @@ Each file is structured as a runnable script, ensuring that samples are executab | [safety_settings.sh](./safety_settings.sh) | Setting and using safety controls | | [system_instruction.sh](./system_instruction.sh) | Setting system instructions | | [text_generation.sh](./text_generation.sh) | Generating text | +| [tuned_models.sh](./tuned_models.sh) | Tuned models |