Gemini API は、テキスト、画像、動画、音声の入力からテキスト出力を生成できます。
基本的な例を以下に示します。
Python
from google import genai
client = genai.Client()
interaction = client.interactions.create(
model="gemini-3.6-flash",
input="How does AI work?"
)
print(interaction.output_text)
JavaScript
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({});
async function main() {
const interaction = await ai.interactions.create({
model: "gemini-3.6-flash",
input: "How does AI work?",
});
console.log(interaction.output_text);
}
await main();
REST
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
"model": "gemini-3.6-flash",
"input": "How does AI work?"
}'
Google GenAI SDK は、返された Interaction オブジェクトに便利なプロパティを直接提供し、モデルのレスポンスにアクセスできるようにします。
最も一般的なヘルパーは interaction.output_text (文字列)で、モデルのレスポンスの最後のテキスト ブロックを返します。レスポンスが複数の連続する TextContent ブロックに分割されている場合は、自動的に結合されます。
.output_text には、テキスト以外のコンテンツ(思考、画像、音声、ツール呼び出しなど)で区切られた以前のテキスト ブロックは含まれません。複雑なマルチモーダル レスポンスやインターリーブされたマルチモーダル レスポンスの場合は、代わりに steps を手動で反復処理する必要があります。その他のメディアの便利なプロパティの詳細については、
インタラクションの概要をご覧ください。
Gemini による思考
Gemini モデルでは、多くの場合、「思考」 がデフォルトで有効になっています。これにより、モデルはリクエストに応答する前に推論できます。
各モデルは、さまざまな思考構成をサポートしており、費用、レイテンシ、インテリジェンスを制御できます。詳細については、 思考ガイドをご覧ください。
Python
from google import genai
client = genai.Client()
interaction = client.interactions.create(
model="gemini-3.6-flash",
input="How does AI work?",
generation_config={
"thinking_level": "low"
}
)
print(interaction.output_text)
JavaScript
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({});
async function main() {
const interaction = await ai.interactions.create({
model: "gemini-3.6-flash",
input: "How does AI work?",
generation_config: {
thinking_level: "low",
},
});
console.log(interaction.output_text);
}
await main();
REST
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
"model": "gemini-3.6-flash",
"input": "How does AI work?",
"generation_config": {
"thinking_level": "low"
}
}'
システム指示とその他の構成
システム指示を使用して、Gemini モデルの動作をガイドできます。system_instruction パラメータを渡して、モデルの動作を構成します。
Python
from google import genai
client = genai.Client()
interaction = client.interactions.create(
model="gemini-3.6-flash",
system_instruction="You are a cat. Your name is Neko.",
input="Hello there"
)
print(interaction.output_text)
JavaScript
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({});
async function main() {
const interaction = await ai.interactions.create({
model: "gemini-3.6-flash",
input: "Hello there",
system_instruction: "You are a cat. Your name is Neko.",
});
console.log(interaction.output_text);
}
await main();
REST
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
"model": "gemini-3.6-flash",
"system_instruction": "You are a cat. Your name is Neko.",
"input": "Hello there"
}'
generation_config パラメータを使用して、Temperature などのデフォルトの生成パラメータをオーバーライドすることもできます。
Python
from google import genai
client = genai.Client()
interaction = client.interactions.create(
model="gemini-3.6-flash",
input="Explain how AI works",
generation_config={
"temperature": 1.0
}
)
print(interaction.output_text)
JavaScript
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({});
async function main() {
const interaction = await ai.interactions.create({
model: "gemini-3.6-flash",
input: "Explain how AI works",
generation_config: {
temperature: 1.0,
},
});
console.log(interaction.output_text);
}
await main();
REST
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
"model": "gemini-3.6-flash",
"input": "Explain how AI works",
"generation_config": {
"temperature": 1.0
}
}'
構成可能なパラメータとその説明の完全なリストについては、Interactions API リファレンスをご覧ください。
マルチモーダル入力
Gemini API はマルチモーダル入力をサポートしているため、テキストとメディア ファイルを組み合わせることができます。次の例は、画像を提供する方法を示しています。
Python
from google import genai
client = genai.Client()
uploaded_file = client.files.upload(file="path/to/organ.jpg")
interaction = client.interactions.create(
model="gemini-3.6-flash",
input=[
{"type": "text", "text": "Tell me about this instrument"},
{
"type": "image",
"uri": uploaded_file.uri,
"mime_type": uploaded_file.mime_type
}
]
)
print(interaction.output_text)
JavaScript
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({});
async function main() {
const uploadedFile = await ai.files.upload({
file: "path/to/organ.jpg",
config: { mimeType: "image/jpeg" }
});
const interaction = await ai.interactions.create({
model: "gemini-3.6-flash",
input: [
{type: "text", text: "Tell me about this instrument"},
{
type: "image",
uri: uploadedFile.uri,
mime_type: uploadedFile.mimeType
}
],
});
console.log(interaction.output_text);
}
await main();
REST
# First upload the file using the Files API, then use the URI:
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
"model": "gemini-3.6-flash",
"input": [
{"type": "text", "text": "Tell me about this instrument"},
{
"type": "image",
"uri": "YOUR_FILE_URI",
"mime_type": "image/jpeg"
}
]
}'
画像の提供方法や高度な画像処理については、 画像理解ガイドをご覧ください。 この API は、ドキュメント、動画、および 音声の入力と理解もサポートしています。
ストリーミング レスポンス
デフォルトでは、モデルは生成プロセス全体が完了した後にのみレスポンスを返します。
よりスムーズなインタラクションを実現するには、ストリーミングを使用して、生成されたレスポンス チャンクを処理します。イベントタイプ、 ツールを使用したストリーミング、思考、エージェント、画像生成を網羅したガイドについては、 専用のストリーミング インタラクション ガイドをご覧ください。