Skip to content

Commit 8a29017

Browse files
REST for tuned models (#496)
* REST for tuned models * Some fixes. Change-Id: I70d066ceacc7c07e27ac59359da87d9b9747353b * add progress reporting, add page_token Change-Id: I7881d6e5703d5bb027329aa22d0572132e024703 * Add delete tuned models * Update readme --------- Co-authored-by: Mark Daoust <[email protected]>
1 parent a8edb40 commit 8a29017

File tree

2 files changed

+160
-0
lines changed

2 files changed

+160
-0
lines changed

samples/rest/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ Each file is structured as a runnable script, ensuring that samples are executab
2323
| [safety_settings.sh](./safety_settings.sh) | Setting and using safety controls |
2424
| [system_instruction.sh](./system_instruction.sh) | Setting system instructions |
2525
| [text_generation.sh](./text_generation.sh) | Generating text |
26+
| [tuned_models.sh](./tuned_models.sh) | Tuned models |

samples/rest/tuned_models.sh

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
set -eu
2+
3+
access_token=$(gcloud auth application-default print-access-token)
4+
5+
6+
echo "[START tuned_models_create]"
7+
# [START tuned_models_create]
8+
curl -X POST https://generativelanguage.googleapis.com/v1beta/tunedModels \
9+
-H 'Content-Type: application/json' \
10+
-H "Authorization: Bearer ${access_token}" \
11+
-H "x-goog-user-project: ${project_id}" \
12+
-d '
13+
{
14+
"display_name": "number generator model",
15+
"base_model": "models/gemini-1.0-pro-001",
16+
"tuning_task": {
17+
"hyperparameters": {
18+
"batch_size": 2,
19+
"learning_rate": 0.001,
20+
"epoch_count":5,
21+
},
22+
"training_data": {
23+
"examples": {
24+
"examples": [
25+
{
26+
"text_input": "1",
27+
"output": "2",
28+
},{
29+
"text_input": "3",
30+
"output": "4",
31+
},{
32+
"text_input": "-3",
33+
"output": "-2",
34+
},{
35+
"text_input": "twenty two",
36+
"output": "twenty three",
37+
},{
38+
"text_input": "two hundred",
39+
"output": "two hundred one",
40+
},{
41+
"text_input": "ninety nine",
42+
"output": "one hundred",
43+
},{
44+
"text_input": "8",
45+
"output": "9",
46+
},{
47+
"text_input": "-98",
48+
"output": "-97",
49+
},{
50+
"text_input": "1,000",
51+
"output": "1,001",
52+
},{
53+
"text_input": "10,100,000",
54+
"output": "10,100,001",
55+
},{
56+
"text_input": "thirteen",
57+
"output": "fourteen",
58+
},{
59+
"text_input": "eighty",
60+
"output": "eighty one",
61+
},{
62+
"text_input": "one",
63+
"output": "two",
64+
},{
65+
"text_input": "three",
66+
"output": "four",
67+
},{
68+
"text_input": "seven",
69+
"output": "eight",
70+
}
71+
]
72+
}
73+
}
74+
}
75+
}' | tee tunemodel.json
76+
77+
# Check the operation for status updates during training.
78+
# Note: you can only check the operation on v1/
79+
operation=$(cat tunemodel.json | jq ".name" | tr -d '"')
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} \
86+
-H 'Content-Type: application/json' \
87+
-H "Authorization: Bearer ${access_token}" \
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
96+
97+
# Or get the TunedModel and check it's state. The model is ready to use if the state is active.
98+
modelname=$(cat tunemodel.json | jq ".metadata.tunedModel" | tr -d '"')
99+
curl -X GET https://generativelanguage.googleapis.com/v1beta/${modelname} \
100+
-H 'Content-Type: application/json' \
101+
-H "Authorization: Bearer ${access_token}" \
102+
-H "x-goog-user-project: ${project_id}" > tuned_model.json
103+
104+
cat tuned_model.json | jq ".state"
105+
# [END tuned_models_create]
106+
107+
108+
echo "[START tuned_models_generate_content]"
109+
# [START tuned_models_generate_content]
110+
curl -X POST https://generativelanguage.googleapis.com/v1beta/$modelname:generateContent \
111+
-H 'Content-Type: application/json' \
112+
-H "Authorization: Bearer ${access_token}" \
113+
-H "x-goog-user-project: ${project_id}" \
114+
-d '{
115+
"contents": [{
116+
"parts": [{
117+
"text": "LXIII"
118+
}]
119+
}]
120+
}' 2> /dev/null
121+
# [END tuned_models_generate_content]
122+
123+
echo "[START tuned_models_get]"
124+
# [START tuned_models_get]
125+
curl -X GET https://generativelanguage.googleapis.com/v1beta/${modelname} \
126+
-H 'Content-Type: application/json' \
127+
-H "Authorization: Bearer ${access_token}" \
128+
-H "x-goog-user-project: ${project_id}" | grep state
129+
# [END tuned_models_get]
130+
131+
echo "[START tuned_models_list]"
132+
# [START tuned_models_list]
133+
# Sending a page_size is optional
134+
curl -X GET https://generativelanguage.googleapis.com/v1beta/tunedModels?page_size=5 \
135+
-H "Content-Type: application/json" \
136+
-H "Authorization: Bearer ${access_token}" \
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
151+
# [END tuned_models_list]
152+
153+
echo "[START tuned_models_delete]"
154+
# [START tuned_models_delete]
155+
curl -X DELETE https://generativelanguage.googleapis.com/v1beta/${modelname} \
156+
-H 'Content-Type: application/json' \
157+
-H "Authorization: Bearer ${access_token}" \
158+
-H "x-goog-user-project: ${project_id}"
159+
# [END tuned_models_delete]

0 commit comments

Comments
 (0)