-
Notifications
You must be signed in to change notification settings - Fork 260
/
tuned_models.py
236 lines (199 loc) · 8 KB
/
tuned_models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# -*- coding: utf-8 -*-
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from absl.testing import absltest
import google
import google.generativeai as genai
import pathlib
samples = pathlib.Path(__file__).parent
class UnitTests(absltest.TestCase):
def test_tuned_models_create(self):
# [START tuned_models_create]
import time
base_model = "models/gemini-1.5-flash-001-tuning"
training_data = [
{"text_input": "1", "output": "2"},
# ... more examples ...
# [START_EXCLUDE]
{"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"},
# [END_EXCLUDE]
{"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
# [END tuned_models_create]
def test_tuned_models_generate_content(self):
# [START tuned_models_generate_content]
model = genai.GenerativeModel(model_name="tunedModels/my-increment-model")
result = model.generate_content("III")
print(result.text) # "IV"
# [END tuned_models_generate_content]
def test_tuned_models_get(self):
# [START tuned_models_get]
model_info = genai.get_model("tunedModels/my-increment-model")
print(model_info)
# [END tuned_models_get]
def test_tuned_models_list(self):
# [START tuned_models_list]
for model_info in genai.list_tuned_models():
print(model_info.name)
# [END tuned_models_list]
def test_tuned_models_delete(self):
import time
base_model = "models/gemini-1.5-flash-001-tuning"
training_data = samples / "increment_tuning_data.json"
try:
operation = genai.create_tuned_model(
id="delete-this-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,
)
except google.api_core.exceptions.AlreadyExists:
pass
else:
for status in operation.wait_bar():
time.sleep(10)
# [START tuned_models_delete]
model_name = "tunedModels/delete-this-model"
model_info = genai.get_model(model_name)
print(model_info)
# You can pass the model_info or name here.
genai.delete_tuned_model(model_name)
# [END tuned_models_delete]
def test_tuned_models_permissions_create(self):
# [START tuned_models_permissions_create]
model_info = genai.get_model("tunedModels/my-increment-model")
# [START_EXCLUDE]
for p in model_info.permissions.list():
if p.role.name != "OWNER":
p.delete()
# [END_EXCLUDE]
public_permission = model_info.permissions.create(
role="READER",
grantee_type="EVERYONE",
)
group_permission = model_info.permissions.create(
role="READER",
# Use "user" for an individual email address.
grantee_type="group",
email_address="genai-samples-test-group@googlegroups.com",
)
# [END tuned_models_permissions_create]
public_permission.delete()
group_permission.delete()
def test_tuned_models_permissions_list(self):
# [START tuned_models_permissions_list]
model_info = genai.get_model("tunedModels/my-increment-model")
# [START_EXCLUDE]
for p in model_info.permissions.list():
if p.role.name != "OWNER":
p.delete()
public_permission = model_info.permissions.create(
role="READER",
grantee_type="EVERYONE",
)
group_permission = model_info.permissions.create(
role="READER",
grantee_type="group",
email_address="genai-samples-test-group@googlegroups.com",
)
# [END_EXCLUDE]
for p in model_info.permissions.list():
print(p)
# [END tuned_models_permissions_list]
public_permission.delete()
group_permission.delete()
def test_tuned_models_permissions_get(self):
# [START tuned_models_permissions_get]
model_info = genai.get_model("tunedModels/my-increment-model")
# [START_EXCLUDE]
for p in model_info.permissions.list():
if p.role.name != "OWNER":
p.delete()
# [END_EXCLUDE]
public = model_info.permissions.create(
role="READER",
grantee_type="EVERYONE",
)
print(public)
name = public.name
print(name) # tunedModels/{tunedModel}/permissions/{permission}
from_name = genai.types.Permissions.get(name)
print(from_name)
# [END tuned_models_permissions_get]
def test_tuned_models_permissions_update(self):
# [START tuned_models_permissions_update]
model_info = genai.get_model("tunedModels/my-increment-model")
# [START_EXCLUDE]
for p in model_info.permissions.list():
if p.role.name != "OWNER":
p.delete()
# [END_EXCLUDE]
test_group = model_info.permissions.create(
role="writer",
grantee_type="group",
email_address="genai-samples-test-group@googlegroups.com",
)
test_group.update({"role": "READER"})
# [END tuned_models_permissions_get]
def test_tuned_models_permission_delete(self):
# [START tuned_models_permissions_delete]
model_info = genai.get_model("tunedModels/my-increment-model")
# [START_EXCLUDE]
for p in model_info.permissions.list():
if p.role.name != "OWNER":
p.delete()
# [END_EXCLUDE]
public_permission = model_info.permissions.create(
role="READER",
grantee_type="EVERYONE",
)
public_permission.delete()
# [END tuned_models_permissions_delete]
if __name__ == "__main__":
absltest.main()