Gemini API は、アプリケーション開発やプロトタイピングで使用できるプログラミング API として、Gemma へのホスト型アクセスを提供します。この API は、Gemma のローカル インスタンスとウェブサービスを独自に設定して生成 AI タスクを処理する代わりに利用できる便利な方法です。
次の例は、Gemini API で Gemma を使用する方法を示しています。
Python
from google import genai
client = genai.Client(api_key="YOUR_API_KEY")
response = client.models.generate_content(
model="gemma-4-31b-it",
contents="Roses are red...",
)
print(response.text)
Node.js
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({ apiKey: "YOUR_API_KEY"});
const response = await ai.models.generateContent({
model: "gemma-4-31b-it",
contents: "Roses are red...",
});
console.log(response.text);
REST
curl "https://generativelanguage.googleapis.com/v1beta/models/gemma-4-31b-it:generateContent?key=YOUR_API_KEY" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [{
"parts":[{"text": "Roses are red..."}]
}]
}'
Gemini API には、モバイル、ウェブ、クラウド サービスなど、多くのプラットフォームで、複数のプログラミング言語を使用してアクセスできます。Gemini API SDK パッケージの詳細については、Gemini API SDK のダウンロード ページをご覧ください。Gemini API の概要については、 Gemini API のクイックスタートをご覧ください。
思考モード
Gemma 4 は、内部の「思考プロセス」を使用して、複数ステップの推論を最適化し、アルゴリズム コーディングや高度な数学的証明など、論理的に要求の厳しい分野で優れたパフォーマンスを実現します。
Gemma 4 では、この機能のオンとオフの切り替えのみが厳密にサポートされています。API でこの機能を有効にするには、
思考レベルを "high" に設定します。
次の例は、思考プロセスを有効にする方法を示しています。
Python
from google import genai
client = genai.Client(api_key="YOUR_API_KEY")
response = client.models.generate_content(
model="gemma-4-31b-it",
contents="What is the water formula?",
config=types.GenerateContentConfig(
thinking_config=types.ThinkingConfig(thinking_level="high")
),
)
print(response.text)
Node.js
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({ apiKey: "YOUR_API_KEY"});
const response = await ai.models.generateContent({
model: "gemma-4-31b-it",
contents: "What is the water formula?",
config: {
thinkingConfig: {
thinkingLevel: ThinkingLevel.HIGH,
},
},
});
console.log(response.text);
REST
curl "https://generativelanguage.googleapis.com/v1beta/models/gemma-4-31b-it:generateContent?key=YOUR_API_KEY" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [{
"parts":[{"text": "What is the water formula?"}]
}],
"generationConfig": {
"thinkingConfig": {
"thinkingLevel": "high"
}
}
}'
思考モードの詳細:
- Gemini API の思考モード (概要)
- Gemma の思考モード (Gemma 固有の機能)
画像理解
Gemma 4 モデルは画像を処理できるため、これまでドメイン固有のモデルが必要だった多くのフロンティア デベロッパーのユースケースを実現できます。
次の例は、Gemini API で Gemma 画像入力を使用する方法を示しています。
Python
from google import genai
client = genai.Client(api_key="YOUR_API_KEY")
my_file = client.files.upload(file="path/to/sample.jpg")
response = client.models.generate_content(
model="gemma-4-31b-it",
contents=[my_file, "Caption this image."],
)
print(response.text)
Node.js
import {
GoogleGenAI,
createUserContent,
createPartFromUri,
} from "@google/genai";
const ai = new GoogleGenAI({ apiKey: "YOUR_API_KEY" });
const myfile = await ai.files.upload({
file: "path/to/sample.jpg",
config: { mimeType: "image/jpeg" },
});
const response = await ai.models.generateContent({
model: "gemma-4-31b-it",
contents: createUserContent([
createPartFromUri(myfile.uri, myfile.mimeType),
"Caption this image.",
]),
});
console.log(response.text);
```
REST
IMAGE_PATH="cats-and-dogs.jpg"
MIME_TYPE=$(file -b --mime-type "${IMAGE_PATH}")
NUM_BYTES=$(wc -c < "${IMAGE_PATH}")
DISPLAY_NAME=IMAGE
tmp_header_file=upload-header.tmp
# Initial resumable request defining metadata.
# The upload url is in the response headers dump them to a file.
curl "https://generativelanguage.googleapis.com/upload/v1beta/files?key=YOUR_API_KEY" \
-D upload-header.tmp \
-H "X-Goog-Upload-Protocol: resumable" \
-H "X-Goog-Upload-Command: start" \
-H "X-Goog-Upload-Header-Content-Length: ${NUM_BYTES}" \
-H "X-Goog-Upload-Header-Content-Type: ${MIME_TYPE}" \
-H "Content-Type: application/json" \
-d "{'file': {'display_name': '${DISPLAY_NAME}'}}" 2> /dev/null
upload_url=$(grep -i "x-goog-upload-url: " "${tmp_header_file}" | cut -d" " -f2 | tr -d "\r")
rm "${tmp_header_file}"
# Upload the actual bytes.
curl "${upload_url}" \
-H "Content-Length: ${NUM_BYTES}" \
-H "X-Goog-Upload-Offset: 0" \
-H "X-Goog-Upload-Command: upload, finalize" \
--data-binary "@${IMAGE_PATH}" 2> /dev/null > file_info.json
file_uri=$(jq -r ".file.uri" file_info.json)
echo file_uri=$file_uri
# Now generate content using that file
curl "https://generativelanguage.googleapis.com/v1beta/models/gemma-4-31b-it:generateContent?key=YOUR_API_KEY" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [{
"parts":[
{"file_data":{"mime_type": "'"${MIME_TYPE}"'", "file_uri": "'"${file_uri}"'"}},
{"text": "Caption this image."}]
}]
}' 2> /dev/null > response.json
cat response.json
echo
jq -r ".candidates[].content.parts[].text" response.json
画像理解の詳細:
- Gemini API の画像理解 (概要)
- Gemma の画像理解 (Gemma 固有の機能)