@@ -4,6 +4,7 @@ SCRIPT_DIR=$(dirname "$0")
4
4
MEDIA_DIR=$( realpath ${SCRIPT_DIR} /../../third_party)
5
5
6
6
TEXT_PATH=${MEDIA_DIR} /poem.txt
7
+ A11_PATH=${MEDIA_DIR} /a11.txt
7
8
IMG_PATH=${MEDIA_DIR} /organ.jpg
8
9
AUDIO_PATH=${MEDIA_DIR} /sample.mp3
9
10
VIDEO_PATH=${MEDIA_DIR} /Big_Buck_Bunny.mp4
16
17
B64FLAGS=" -w0"
17
18
fi
18
19
20
+ echo " [START tokens_context_window]"
21
+ # [START tokens_context_window]
22
+ curl https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro? key=$GOOGLE_API_KEY > model.json
23
+ jq .inputTokenLimit model.json
24
+ jq .outputTokenLimit model.json
25
+ # [END tokens_context_window]
26
+
19
27
echo " [START tokens_text_only]"
20
28
# [START tokens_text_only]
21
29
curl https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:countTokens? key=$GOOGLE_API_KEY \
@@ -97,7 +105,6 @@ curl "${upload_url}" \
97
105
--data-binary " @${IMG_PATH} " 2> /dev/null > file_info.json
98
106
99
107
file_uri=$( jq " .file.uri" file_info.json)
100
- echo file_uri=$file_uri
101
108
102
109
curl " https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:countTokens?key=$GOOGLE_API_KEY " \
103
110
-H ' Content-Type: application/json' \
@@ -143,13 +150,10 @@ curl "${upload_url}" \
143
150
--data-binary " @${VIDEO_PATH} " 2> /dev/null > file_info.json
144
151
145
152
file_uri=$( jq " .file.uri" file_info.json)
146
- echo file_uri=$file_uri
147
153
148
154
state=$( jq " .file.state" file_info.json)
149
- echo state=$state
150
155
151
156
name=$( jq " .file.name" file_info.json)
152
- echo name=$name
153
157
154
158
while [[ " ($state )" = * " PROCESSING" * ]];
155
159
do
@@ -170,4 +174,115 @@ curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:c
170
174
{"file_data":{"mime_type": "video/mp4", "file_uri": ' $file_uri ' }}]
171
175
}]
172
176
}'
173
- # [END tokens_multimodal_video_audio_file_api]
177
+ # [END tokens_multimodal_video_audio_file_api]
178
+
179
+ echo " [START tokens_cached_content]"
180
+ # [START tokens_cached_content]
181
+ echo ' {
182
+ "model": "models/gemini-1.5-flash-001",
183
+ "contents":[
184
+ {
185
+ "parts":[
186
+ {
187
+ "inline_data": {
188
+ "mime_type":"text/plain",
189
+ "data": "' $( base64 $B64FLAGS $A11_PATH ) ' "
190
+ }
191
+ }
192
+ ],
193
+ "role": "user"
194
+ }
195
+ ],
196
+ "systemInstruction": {
197
+ "parts": [
198
+ {
199
+ "text": "You are an expert at analyzing transcripts."
200
+ }
201
+ ]
202
+ },
203
+ "ttl": "300s"
204
+ }' > request.json
205
+
206
+ curl -X POST " https://generativelanguage.googleapis.com/v1beta/cachedContents?key=$GOOGLE_API_KEY " \
207
+ -H ' Content-Type: application/json' \
208
+ -d @request.json \
209
+ > cache.json
210
+
211
+ jq .usageMetadata.totalTokenCount cache.json
212
+ # [END tokens_cached_content]
213
+
214
+ echo " [START tokens_system_instruction]"
215
+ # [START tokens_system_instruction]
216
+ curl " https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro-latest:generateContent?key=$GOOGLE_API_KEY " \
217
+ -H ' Content-Type: application/json' \
218
+ -d ' { "system_instruction": {
219
+ "parts":
220
+ { "text": "You are a cat. Your name is Neko."}},
221
+ "contents": {
222
+ "parts": {
223
+ "text": "Hello there"}}}' > system_instructions.json
224
+
225
+ jq .usageMetadata.totalTokenCount system_instructions.json
226
+ # [END tokens_system_instruction]
227
+
228
+ echo " [START tokens_tools]"
229
+ # [START tokens_tools]
230
+ cat > tools.json << EOF
231
+ {
232
+ "function_declarations": [
233
+ {
234
+ "name": "enable_lights",
235
+ "description": "Turn on the lighting system.",
236
+ "parameters": { "type": "object" }
237
+ },
238
+ {
239
+ "name": "set_light_color",
240
+ "description": "Set the light color. Lights must be enabled for this to work.",
241
+ "parameters": {
242
+ "type": "object",
243
+ "properties": {
244
+ "rgb_hex": {
245
+ "type": "string",
246
+ "description": "The light color as a 6-digit hex string, e.g. ff0000 for red."
247
+ }
248
+ },
249
+ "required": [
250
+ "rgb_hex"
251
+ ]
252
+ }
253
+ },
254
+ {
255
+ "name": "stop_lights",
256
+ "description": "Turn off the lighting system.",
257
+ "parameters": { "type": "object" }
258
+ }
259
+ ]
260
+ }
261
+ EOF
262
+
263
+ curl " https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro-latest:generateContent?key=$GOOGLE_API_KEY " \
264
+ -H ' Content-Type: application/json' \
265
+ -d '
266
+ {
267
+ "system_instruction": {
268
+ "parts": {
269
+ "text": "You are a helpful lighting system bot. You can turn lights on and off, and you can set the color. Do not perform any other tasks."
270
+ }
271
+ },
272
+ "tools": [' $( source " $tools " ) ' ],
273
+
274
+ "tool_config": {
275
+ "function_calling_config": {"mode": "none"}
276
+ },
277
+
278
+ "contents": {
279
+ "role": "user",
280
+ "parts": {
281
+ "text": "What can you do?"
282
+ }
283
+ }
284
+ }
285
+ ' > tools_output.json
286
+
287
+ jq .usageMetadata.totalTokenCount tools_output.json
288
+ # [END tokens_tools]
0 commit comments