Skip to content

Commit 009b1c2

Browse files
Merge branch 'tuned_models_rest' of https://github.com/shilpakancharla/generative-ai-python into tuned_models_rest
2 parents fc7049f + aa5be36 commit 009b1c2

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

samples/rest/tuned_models.sh

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
set -eu
22

3-
export access_token=$(gcloud auth application-default print-access-token)
4-
export project_id=<your project id>
5-
63
access_token=$(gcloud auth application-default print-access-token)
74

85

@@ -80,21 +77,34 @@ curl -X POST https://generativelanguage.googleapis.com/v1beta/tunedModels \
8077
# Check the operation for status updates during training.
8178
# Note: you can only check the operation on v1/
8279
operation=$(cat tunemodel.json | jq ".name" | tr -d '"')
83-
curl -X GET https://generativelanguage.googleapis.com/v1/${operation} \
80+
tuning_done=false
81+
82+
while [[ "$tuning_done" != "true" ]];
83+
do
84+
sleep 5
85+
curl -X GET https://generativelanguage.googleapis.com/v1/${operation} \
8486
-H 'Content-Type: application/json' \
8587
-H "Authorization: Bearer ${access_token}" \
86-
-H "x-goog-user-project: ${project_id}"
88+
-H "x-goog-user-project: ${project_id}" 2> /dev/null > tuning_operation.json
89+
90+
complete=$(jq .metadata.completedPercent < tuning_operation.json)
91+
tput cuu1
92+
tput el
93+
echo "Tuning...${complete}%"
94+
tuning_done=$(jq .done < tuning_operation.json)
95+
done
8796

8897
# Or get the TunedModel and check it's state. The model is ready to use if the state is active.
8998
modelname=$(cat tunemodel.json | jq ".metadata.tunedModel" | tr -d '"')
9099
curl -X GET https://generativelanguage.googleapis.com/v1beta/${modelname} \
91100
-H 'Content-Type: application/json' \
92101
-H "Authorization: Bearer ${access_token}" \
93-
-H "x-goog-user-project: ${project_id}" | tee check.json
102+
-H "x-goog-user-project: ${project_id}" > tuned_model.json
94103

95-
cat check.json | jq ".state"
104+
cat tuned_model.json | jq ".state"
96105
# [END tuned_models_create]
97106

107+
98108
echo "[START tuned_models_generate_content]"
99109
# [START tuned_models_generate_content]
100110
curl -X POST https://generativelanguage.googleapis.com/v1beta/$modelname:generateContent \
@@ -120,10 +130,24 @@ curl -X GET https://generativelanguage.googleapis.com/v1beta/${modelname} \
120130

121131
echo "[START tuned_models_list]"
122132
# [START tuned_models_list]
123-
curl -X GET https://generativelanguage.googleapis.com/v1beta/tunedModels \
133+
# Sending a page_size is optional
134+
curl -X GET https://generativelanguage.googleapis.com/v1beta/tunedModels?page_size=5 \
124135
-H "Content-Type: application/json" \
125136
-H "Authorization: Bearer ${access_token}" \
126-
-H "x-goog-user-project: ${project_id}"
137+
-H "x-goog-user-project: ${project_id}" > tuned_models.json
138+
139+
jq .tunedModels[].name < tuned_models.json
140+
141+
# Send the nextPageToken to get the next page.
142+
page_token=$(jq .nextPageToken < tuned_models.json | tr -d '"')
143+
144+
if [[ "$page_token" != "null"" ]]; then
145+
curl -X GET https://generativelanguage.googleapis.com/v1beta/tunedModels?page_size=5\&page_token=${page_token} \
146+
-H "Content-Type: application/json" \
147+
-H "Authorization: Bearer ${access_token}" \
148+
-H "x-goog-user-project: ${project_id}" > tuned_models2.json
149+
jq .tunedModels[].name < tuned_models.json
150+
fi
127151
# [END tuned_models_list]
128152
129153
echo "[START tuned_models_delete]"

0 commit comments

Comments
 (0)