Skip to content

Commit 5437427

Browse files
author
Prabhjit Singh Dhillon
committed
Improve key naming clarity and consistency across REST API examples - Standardize on camelCase for REST API parameter names - Update README.md to document parameter naming conventions - Create new inline_pdf_example.sh with consistent naming - Fix inconsistent naming in several example files - Fixes google-gemini#683
1 parent 2c570fb commit 5437427

9 files changed

+124
-25
lines changed

samples/rest/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ These samples are embedded in parts of the [documentation](https://ai.google.dev
66

77
Each file is structured as a runnable script, ensuring that samples are executable and functional. Each filee contains region tags that are used to demarcate the script from the spotlight code. If you are contributing, code within region tags should follow sample code best practices - being clear, complete and concise.
88

9+
## Parameter Naming Conventions
10+
11+
The Gemini API accepts REST parameters in camelCase format, which is the convention used in these examples and the official API reference. For example:
12+
- `generationConfig`
13+
- `maxOutputTokens`
14+
- `systemInstruction`
15+
- `topP`, `topK`
16+
17+
**Note:** While the API also accepts snake_case format for compatibility (e.g., `system_instruction` instead of `systemInstruction`), the official and recommended style is camelCase for REST API requests.
18+
919
## Contents
1020

1121
| File | Description |
@@ -19,6 +29,7 @@ Each file is structured as a runnable script, ensuring that samples are executab
1929
| [embed.sh](./embed.sh) | Generating embeddings |
2030
| [files.sh](./files.sh) | Managing files with the File API |
2131
| [function_calling.sh](./function_calling.sh) | Using function calling |
32+
| [inline_pdf_example.sh](./inline_pdf_example.sh) | Using inline PDF data with the API |
2233
| [models.sh](./models.sh) | Listing models and model metadata |
2334
| [safety_settings.sh](./safety_settings.sh) | Setting and using safety controls |
2435
| [system_instruction.sh](./system_instruction.sh) | Setting system instructions |

samples/rest/cache.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ echo '{
1515
{
1616
"parts":[
1717
{
18-
"inline_data": {
19-
"mime_type":"text/plain",
18+
"inlineData": {
19+
"mimeType":"text/plain",
2020
"data": "'$(base64 $B64FLAGS a11.txt)'"
2121
}
2222
}

samples/rest/chat.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ curl https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:st
8181
"text": "Tell me about this instrument"
8282
},
8383
{
84-
"inline_data": {
85-
"mime_type": "image/jpeg",
84+
"inlineData": {
85+
"mimeType": "image/jpeg",
8686
"data": "'$(base64 $B64FLAGS $IMG_PATH)'"
8787
}
8888
}

samples/rest/controlled_generation.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:g
1111
]
1212
}],
1313
"generationConfig": {
14-
"response_mime_type": "application/json",
15-
"response_schema": {
14+
"responseMimeType": "application/json",
15+
"responseSchema": {
1616
"type": "ARRAY",
1717
"items": {
1818
"type": "OBJECT",
@@ -39,6 +39,6 @@ curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:g
3939
}
4040
]
4141
}],
42-
"generationConfig": { "response_mime_type": "application/json" }
42+
"generationConfig": { "responseMimeType": "application/json" }
4343
}' 2> /dev/null | head
4444
# [END json_no_schema]

samples/rest/count_tokens.sh

+6-6
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:c
6565
"parts":[
6666
{"text": "Tell me about this instrument"},
6767
{
68-
"inline_data": {
69-
"mime_type":"image/jpeg",
68+
"inlineData": {
69+
"mimeType":"image/jpeg",
7070
"data": "'$(base64 $B64FLAGS $IMG_PATH)'"
7171
}
7272
}
@@ -184,8 +184,8 @@ echo '{
184184
{
185185
"parts":[
186186
{
187-
"inline_data": {
188-
"mime_type":"text/plain",
187+
"inlineData": {
188+
"mimeType":"text/plain",
189189
"data": "'$(base64 $B64FLAGS $A11_PATH)'"
190190
}
191191
}
@@ -215,7 +215,7 @@ echo "[START tokens_system_instruction]"
215215
# [START tokens_system_instruction]
216216
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro-latest:generateContent?key=$GOOGLE_API_KEY" \
217217
-H 'Content-Type: application/json' \
218-
-d '{ "system_instruction": {
218+
-d '{ "systemInstruction": {
219219
"parts":
220220
{ "text": "You are a cat. Your name is Neko."}},
221221
"contents": {
@@ -264,7 +264,7 @@ curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro-lat
264264
-H 'Content-Type: application/json' \
265265
-d '
266266
{
267-
"system_instruction": {
267+
"systemInstruction": {
268268
"parts": {
269269
"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."
270270
}

samples/rest/function_calling.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:g
3838
-H 'Content-Type: application/json' \
3939
-d @<(echo '
4040
{
41-
"system_instruction": {
41+
"systemInstruction": {
4242
"parts": {
4343
"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."
4444
}

samples/rest/inline_pdf_example.sh

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/bin/bash
2+
# -*- coding: utf-8 -*-
3+
# Copyright 2023 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# This example demonstrates how to use inline PDF data with the generative API
18+
19+
# Set default values for environment variables if they don't exist
20+
MEDIA_DIR="${MEDIA_DIR:-$(dirname "$0")/../../third_party}"
21+
22+
# Check if we're on macOS or Linux
23+
if [[ "$OSTYPE" == "darwin"* ]]; then
24+
B64FLAGS="-b 0"
25+
else
26+
B64FLAGS="--wrap=0"
27+
fi
28+
29+
echo "[START text_gen_multimodal_two_pdf_inline]"
30+
# Use temporary files to hold the base64 encoded pdf data
31+
PDF_PATH_1=${MEDIA_DIR}/test.pdf
32+
PDF_PATH_2=${MEDIA_DIR}/test.pdf
33+
34+
TEMP_1_B64=$(mktemp)
35+
trap 'rm -f "$TEMP_1_B64"' EXIT
36+
base64 $B64FLAGS $PDF_PATH_1 > "$TEMP_1_B64"
37+
38+
TEMP_2_B64=$(mktemp)
39+
trap 'rm -f "$TEMP_2_B64"' EXIT
40+
base64 $B64FLAGS $PDF_PATH_2 > "$TEMP_2_B64"
41+
42+
# Use a temporary file to hold the JSON payload
43+
TEMP_JSON=$(mktemp)
44+
trap 'rm -f "$TEMP_JSON"' EXIT
45+
46+
cat > "$TEMP_JSON" << EOF
47+
{
48+
"contents": [{
49+
"role": "user",
50+
"parts":[
51+
{"text": "Extract the pet names, type and ages from these documents."},
52+
{
53+
"inlineData": {
54+
"mimeType":"application/pdf",
55+
"data": "$(cat "$TEMP_1_B64")"
56+
}
57+
},
58+
{
59+
"inlineData": {
60+
"mimeType":"application/pdf",
61+
"data": "$(cat "$TEMP_2_B64")"
62+
}
63+
}
64+
]
65+
}],
66+
"systemInstruction": {
67+
"parts": [
68+
{"text": "Extract the pet names and ages from these documents and return them in the following JSON format:
69+
70+
Pet = {\"name\": str, \"type\": str, \"age\": int}
71+
Return: list[Pet]"
72+
}
73+
]
74+
},
75+
"generationConfig": {
76+
"temperature": 0.2,
77+
"topP": 0.95,
78+
"topK": 40,
79+
"maxOutputTokens": 1000,
80+
"responseMimeType": "application/json"
81+
}
82+
}
83+
EOF
84+
85+
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=$GOOGLE_API_KEY" \
86+
-H 'Content-Type: application/json' \
87+
-X POST \
88+
-d "@$TEMP_JSON" 2> /dev/null

samples/rest/system_instruction.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ echo "[START system_instruction]"
44
# [START system_instruction]
55
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=$GEMINI_API_KEY" \
66
-H 'Content-Type: application/json' \
7-
-d '{ "system_instruction": {
7+
-d '{ "systemInstruction": {
88
"parts":
99
{ "text": "You are a cat. Your name is Neko."}},
1010
"contents": {

samples/rest/text_generation.sh

+10-10
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ cat > "$TEMP_JSON" << EOF
5454
"parts":[
5555
{"text": "Tell me about this instrument"},
5656
{
57-
"inline_data": {
58-
"mime_type":"image/jpeg",
57+
"inlineData": {
58+
"mimeType":"image/jpeg",
5959
"data": "$(cat "$TEMP_B64")"
6060
}
6161
}
@@ -78,8 +78,8 @@ cat > "$TEMP_JSON" << EOF
7878
"parts":[
7979
{"text": "Tell me about this instrument"},
8080
{
81-
"inline_data": {
82-
"mime_type":"image/jpeg",
81+
"inlineData": {
82+
"mimeType":"image/jpeg",
8383
"data": "$(cat "$TEMP_B64")"
8484
}
8585
}
@@ -109,14 +109,14 @@ cat > "$TEMP_JSON" << EOF
109109
"contents": [{
110110
"parts":[
111111
{
112-
"inline_data": {
113-
"mime_type": "image/jpeg",
112+
"inlineData": {
113+
"mimeType": "image/jpeg",
114114
"data": "$(cat "$TEMP_B64_1")"
115115
}
116116
},
117117
{
118-
"inline_data": {
119-
"mime_type": "image/jpeg",
118+
"inlineData": {
119+
"mimeType": "image/jpeg",
120120
"data": "$(cat "$TEMP_B64_2")"
121121
}
122122
},
@@ -148,8 +148,8 @@ cat > "$TEMP_JSON" << EOF
148148
"contents": [{
149149
"parts":[
150150
{
151-
"inline_data": {
152-
"mime_type": "image/jpeg",
151+
"inlineData": {
152+
"mimeType": "image/jpeg",
153153
"data": "$(cat "$TEMP_B64_2")"
154154
}
155155
},

0 commit comments

Comments
 (0)