Google Geminiの対話形式(Generate Content)
Google Gemini の対話フォーマット(Generate Content)
Section titled “Google Gemini の対話フォーマット(Generate Content)”本ページの概要
「公式ドキュメント」Google Gemini Generating content API
📝 はじめに
Section titled “📝 はじめに”Google Gemini API は、画像、音声、コード、ツールなどを使ってコンテンツを生成できます。入力として GenerateContentRequest を与えると、モデルが応答を生成します。テキスト生成、画像理解、音声処理、長文コンテキスト、コード実行、JSON モード、関数呼び出しなど、さまざまな機能をサポートしています。
💡 リクエスト例
Section titled “💡 リクエスト例”基本的なテキスト対話 ✅
Section titled “基本的なテキスト対話 ✅”curl "https://你的4All API服务器地址/v1beta/models/gemini-2.0-flash:generateContent?key=$4All API_API_KEY" \ -H 'Content-Type: application/json' \ -X POST \ -d '{ "contents": [{ "parts":[{"text": "Write a story about a magic backpack."}] }] }' 2> /dev/null画像分析対話 ✅
Section titled “画像分析対話 ✅”# 使用临时文件保存base64编码的图片数据TEMP_B64=$(mktemp)trap 'rm -f "$TEMP_B64"' EXITbase64 $B64FLAGS $IMG_PATH > "$TEMP_B64"
# 使用临时文件保存JSON载荷TEMP_JSON=$(mktemp)trap 'rm -f "$TEMP_JSON"' EXIT
cat > "$TEMP_JSON" << EOF{ "contents": [{ "parts":[ {"text": "Tell me about this instrument"}, { "inline_data": { "mime_type":"image/jpeg", "data": "$(cat "$TEMP_B64")" } } ] }]}EOF
curl "https://你的4All API服务器地址/v1beta/models/gemini-2.0-flash:generateContent?key=$4All API_API_KEY" \ -H 'Content-Type: application/json' \ -X POST \ -d "@$TEMP_JSON" 2> /dev/null関数呼び出し ✅
Section titled “関数呼び出し ✅”cat > tools.json << EOF{ "function_declarations": [ { "name": "enable_lights", "description": "Turn on the lighting system." }, { "name": "set_light_color", "description": "Set the light color. Lights must be enabled for this to work.", "parameters": { "type": "object", "properties": { "rgb_hex": { "type": "string", "description": "The light color as a 6-digit hex string, e.g. ff0000 for red." } }, "required": [ "rgb_hex" ] } }, { "name": "stop_lights", "description": "Turn off the lighting system." } ]}EOF
curl "https://你的4All API服务器地址/v1beta/models/gemini-2.0-flash:generateContent?key=$4All API_API_KEY" \ -H 'Content-Type: application/json' \ -d @<(echo ' { "system_instruction": { "parts": { "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." } }, "tools": ['$(cat tools.json)'],
"tool_config": { "function_calling_config": {"mode": "auto"} },
"contents": { "role": "user", "parts": { "text": "Turn on the lights please." } } }') 2>/dev/null |sed -n '/"content"/,/"finishReason"/p'JSON モード応答 ✅
Section titled “JSON モード応答 ✅”curl "https://你的4All API服务器地址/v1beta/models/gemini-2.0-flash:generateContent?key=$4All API_API_KEY" \-H 'Content-Type: application/json' \-d '{ "contents": [{ "parts":[ {"text": "List 5 popular cookie recipes"} ] }], "generationConfig": { "response_mime_type": "application/json", "response_schema": { "type": "ARRAY", "items": { "type": "OBJECT", "properties": { "recipe_name": {"type":"STRING"}, } } } }}' 2> /dev/null | head音声処理 🟡
Section titled “音声処理 🟡”!!! warning “ファイルアップロード制限” 音声のアップロードは inline_data による base64 方式のみ対応しており、file_data.file_uri や File API はサポートされていません。
# 使用File API上传音频数据到API请求# 使用 base64 inline_data 上传音频数据到 API 请求if [[ "$(base64 --version 2>&1)" = *"FreeBSD"* ]]; then B64FLAGS="--input"else B64FLAGS="-w0"fiAUDIO_B64=$(base64 $B64FLAGS "$AUDIO_PATH")
curl "https://你的4All API服务器地址/v1beta/models/gemini-2.0-flash:generateContent?key=$4All API_API_KEY" \ -H 'Content-Type: application/json' \ -X POST \ -d '{ "contents": [{ "parts": [ {"text": "Please describe this audio file."}, {"inline_data": {"mime_type": "audio/mpeg", "data": "'$AUDIO_B64'"}} ] }] }' 2> /dev/null | jq ".candidates[].content.parts[].text"動画処理 🟡
Section titled “動画処理 🟡”!!! warning “ファイルアップロード制限” 動画のアップロードは inline_data による base64 方式のみ対応しており、file_data.file_uri や File API はサポートされていません。
# 使用File API上传视频数据到API请求# 使用 base64 inline_data 上传视频数据到 API 请求if [[ "$(base64 --version 2>&1)" = *"FreeBSD"* ]]; then B64FLAGS="--input"else B64FLAGS="-w0"fiVIDEO_B64=$(base64 $B64FLAGS "$VIDEO_PATH")
curl "https://你的4All API服务器地址/v1beta/models/gemini-2.0-flash:generateContent?key=$4All API_API_KEY" \ -H 'Content-Type: application/json' \ -X POST \ -d '{ "contents": [{ "parts": [ {"text": "Transcribe the audio from this video and provide visual descriptions."}, {"inline_data": {"mime_type": "video/mp4", "data": "'$VIDEO_B64'"}} ] }] }' 2> /dev/null | jq ".candidates[].content.parts[].text"PDF 処理 🟡
Section titled “PDF 処理 🟡”!!! warning “ファイルアップロード制限” PDF のアップロードは inline_data による base64 方式のみ対応しており、file_data.file_uri や File API はサポートされていません。
MIME_TYPE=$(file -b --mime-type "${PDF_PATH}")# 使用 base64 inline_data 上传 PDF 文件到 API 请求if [[ "$(base64 --version 2>&1)" = *"FreeBSD"* ]]; then B64FLAGS="--input"else B64FLAGS="-w0"fiPDF_B64=$(base64 $B64FLAGS "$PDF_PATH")
echo $MIME_TYPE
curl "https://你的4All API服务器地址/v1beta/models/gemini-2.0-flash:generateContent?key=$4All API_API_KEY" \ -H 'Content-Type: application/json' \ -X POST \ -d '{ "contents": [{ "parts": [ {"text": "Can you add a few more lines to this poem?"}, {"inline_data": {"mime_type": "application/pdf", "data": "'$PDF_B64'"}} ] }] }' 2> /dev/null | jq ".candidates[].content.parts[].text"チャット対話 ✅
Section titled “チャット対話 ✅”curl https://你的4All API服务器地址/v1beta/models/gemini-2.0-flash:generateContent?key=$4All API_API_KEY \ -H 'Content-Type: application/json' \ -X POST \ -d '{ "contents": [ {"role":"user", "parts":[{ "text": "Hello"}]}, {"role": "model", "parts":[{ "text": "Great to meet you. What would you like to know?"}]}, {"role":"user", "parts":[{ "text": "I have two dogs in my house. How many paws are in my house?"}]}, ] }' 2> /dev/null | grep "text"ストリーミング応答 ✅
Section titled “ストリーミング応答 ✅”curl "https://你的4All API服务器地址/v1beta/models/gemini-2.0-flash:streamGenerateContent?alt=sse&key=$4All API_API_KEY" \ -H 'Content-Type: application/json' \ --no-buffer \ -d '{ "contents": [{ "parts": [{"text": "魔法のバックパックについての物語を書いて"}] }] }'コード実行 ✅
Section titled “コード実行 ✅”curl "https://你的4All API服务器地址/v1beta/models/gemini-2.0-flash:generateContent?key=$4All API_API_KEY" \ -H 'Content-Type: application/json' \ -X POST \ -d '{ "contents": [{ "parts": [{"text": "计算斐波那契数列的第10项"}] }], "tools": [{ "codeExecution": {} }] }'生成設定 ✅
Section titled “生成設定 ✅”curl https://你的4All API服务器地址/v1beta/models/gemini-2.0-flash:generateContent?key=$4All API_API_KEY \ -H 'Content-Type: application/json' \ -X POST \ -d '{ "contents": [{ "parts":[ {"text": "Explain how AI works"} ] }], "generationConfig": { "stopSequences": [ "Title" ], "temperature": 1.0, "maxOutputTokens": 800, "topP": 0.8, "topK": 10 } }' 2> /dev/null | grep "text"安全設定 ✅
Section titled “安全設定 ✅”echo '{ "safetySettings": [ {"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_ONLY_HIGH"}, {"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_MEDIUM_AND_ABOVE"} ], "contents": [{ "parts":[{ "text": "'I support Martians Soccer Club and I think Jupiterians Football Club sucks! Write a ironic phrase about them.'"}]}]}' > request.json
curl "https://你的4All API服务器地址/v1beta/models/gemini-2.0-flash:generateContent?key=$4All API_API_KEY" \ -H 'Content-Type: application/json' \ -X POST \ -d @request.json 2> /dev/nullシステム指示 ✅
Section titled “システム指示 ✅”curl "https://你的4All API服务器地址/v1beta/models/gemini-2.0-flash:generateContent?key=$4All API_API_KEY" \-H 'Content-Type: application/json' \-d '{ "system_instruction": { "parts": { "text": "You are a cat. Your name is Neko."}}, "contents": { "parts": { "text": "Hello there"}}}'📮 リクエスト
Section titled “📮 リクエスト”エンドポイント
Section titled “エンドポイント”コンテンツ生成
Section titled “コンテンツ生成”POST https://你的4All API服务器地址/v1beta/{model=models/*}:generateContentストリーミングコンテンツ生成
Section titled “ストリーミングコンテンツ生成”POST https://你的4All API服务器地址/v1beta/{model=models/*}:streamGenerateContentリクエスト URL のパラメータに API キーを含めます。
?key=$4All API_API_KEYここでの $4All API_API_KEY は、お使いの Google AI API キーです。
パスパラメータ
Section titled “パスパラメータ”- 型: 文字列
- 必須: はい
補完生成に使用するモデル名です。
形式: models/{model}。例: models/gemini-2.0-flash
リクエストボディのパラメータ
Section titled “リクエストボディのパラメータ”contents
Section titled “contents”- 型: 配列
- 必須: はい
モデルとの現在の対話内容です。単一ターンの問い合わせでは 1 つの要素になります。チャットのような複数ターンの問い合わせでは、会話履歴と最新のリクエストを含む繰り返しフィールドです。
Content オブジェクトの属性:
| 属性 | 型 | 必須 | 説明 |
|---|---|---|---|
| parts | 配列 | はい | 1 つのメッセージを構成する順序付きのコンテンツ部分 |
| role | 文字列 | いいえ | 対話における内容の発信者。user、model、function、tool |
Part オブジェクトの属性:
| 属性 | 型 | 必須 | 説明 |
|---|---|---|---|
| text | 文字列 | いいえ | プレーンテキスト内容 |
| inlineData | オブジェクト | いいえ | インラインのメディアバイトデータ |
| fileData | オブジェクト | いいえ | アップロード済みファイルの URI 参照 |
| functionCall | オブジェクト | いいえ | 関数呼び出しリクエスト |
| functionResponse | オブジェクト | いいえ | 関数呼び出しレスポンス |
| executableCode | オブジェクト | いいえ | 実行可能コード |
| codeExecutionResult | オブジェクト | いいえ | コード実行結果 |
InlineData オブジェクトの属性:
| 属性 | 型 | 必須 | 説明 |
|---|---|---|---|
| mimeType | 文字列 | はい | メディアの MIME タイプ |
| data | 文字列 | はい | base64 エンコードされたメディアデータ |
FileData オブジェクトの属性:
| 属性 | 型 | 必須 | 説明 |
|---|---|---|---|
| mimeType | 文字列 | はい | ファイルの MIME タイプ |
| fileUri | 文字列 | はい | ファイルの URI |
- 型: 配列
- 必須: いいえ
モデルが次の応答を生成する際に使用できるツールの一覧です。サポートされるツールには関数とコード実行があります。
Tool オブジェクトの属性:
| 属性 | 型 | 必須 | 説明 |
|---|---|---|---|
| functionDeclarations | 配列 | いいえ | 任意の関数定義の一覧 |
| codeExecution | オブジェクト | いいえ | モデルによるコード実行を有効化 |
FunctionDeclaration オブジェクトの属性:
| 属性 | 型 | 必須 | 説明 |
|---|---|---|---|
| name | 文字列 | はい | 関数名 |
| description | 文字列 | いいえ | 関数の説明 |
| parameters | オブジェクト | いいえ | 関数パラメータ。JSON Schema 形式 |
FunctionCall オブジェクトの属性:
| 属性 | 型 | 必須 | 説明 |
|---|---|---|---|
| name | 文字列 | はい | 呼び出す関数名 |
| args | オブジェクト | いいえ | 関数パラメータのキーと値 |
FunctionResponse オブジェクトの属性:
| 属性 | 型 | 必須 | 説明 |
|---|---|---|---|
| name | 文字列 | はい | 呼び出した関数名 |
| response | オブジェクト | はい | 関数呼び出しの応答データ |
ExecutableCode オブジェクトの属性:
| 属性 | 型 | 必須 | 説明 |
|---|---|---|---|
| language | 列挙 | はい | コードのプログラミング言語 |
| code | 文字列 | はい | 実行するコード |
CodeExecutionResult オブジェクトの属性:
| 属性 | 型 | 必須 | 説明 |
|---|---|---|---|
| outcome | 列挙 | はい | コード実行の結果ステータス |
| output | 文字列 | いいえ | コード実行の出力内容 |
CodeExecution オブジェクトの属性:
| 属性 | 型 | 必須 | 説明 |
|---|---|---|---|
| {} | 空オブジェクト | - | コード実行機能を有効にするための空の設定オブジェクト |
toolConfig
Section titled “toolConfig”- 型: オブジェクト
- 必須: いいえ
リクエストで指定された任意のツールに対するツール設定です。
ToolConfig オブジェクトの属性:
| 属性 | 型 | 必須 | 説明 |
|---|---|---|---|
| functionCallingConfig | オブジェクト | いいえ | 関数呼び出しの設定 |
FunctionCallingConfig オブジェクトの属性:
| 属性 | 型 | 必須 | 説明 |
|---|---|---|---|
| mode | 列挙 | いいえ | 関数呼び出しモードを指定 |
| allowedFunctionNames | 配列 | いいえ | 呼び出しを許可する関数名の一覧 |
FunctionCallingMode 列挙値:
- MODE_UNSPECIFIED : デフォルトモード。モデルが関数を呼び出すかどうかを判断します
- AUTO : モデルが関数を呼び出すタイミングを自動で判断します
- ANY : モデルは関数を呼び出さなければなりません
- NONE : モデルは関数を呼び出せません
safetySettings
Section titled “safetySettings”- 型: 配列
- 必須: いいえ
安全でないコンテンツをブロックするための SafetySetting インスタンスの一覧です。
SafetySetting オブジェクトの属性:
| 属性 | 型 | 必須 | 説明 |
|---|---|---|---|
| category | 列挙 | はい | 安全カテゴリ |
| threshold | 列挙 | はい | ブロックのしきい値 |
HarmCategory 列挙値:
- HARM_CATEGORY_HARASSMENT : いやがらせ
- HARM_CATEGORY_HATE_SPEECH : ヘイトスピーチおよび差別的内容
- HARM_CATEGORY_SEXUALLY_EXPLICIT : 露骨な性的内容
- HARM_CATEGORY_DANGEROUS_CONTENT : 危険な内容
- HARM_CATEGORY_CIVIC_INTEGRITY : 市民的公正を損なう可能性のある内容
HarmBlockThreshold 列挙値:
- BLOCK_LOW_AND_ABOVE : NEGLIGIBLE 評価の内容のみ許可
- BLOCK_MEDIUM_AND_ABOVE : NEGLIGIBLE と LOW 評価の内容のみ許可
- BLOCK_ONLY_HIGH : NEGLIGIBLE、LOW、MEDIUM の内容を許可
- BLOCK_NONE : すべての内容を許可
- OFF : 安全フィルターを無効化
HarmBlockThreshold 完全な列挙値:
- HARM_BLOCK_THRESHOLD_UNSPECIFIED : しきい値未指定
- BLOCK_LOW_AND_ABOVE : 低確率以上の有害コンテンツをブロックし、NEGLIGIBLE のみ許可
- BLOCK_MEDIUM_AND_ABOVE : 中確率以上の有害コンテンツをブロックし、NEGLIGIBLE と LOW を許可
- BLOCK_ONLY_HIGH : 高確率の有害コンテンツのみブロックし、NEGLIGIBLE、LOW、MEDIUM を許可
- BLOCK_NONE : いかなるコンテンツもブロックせず、すべてのレベルを許可
- OFF : 安全フィルターを完全に無効化
systemInstruction
Section titled “systemInstruction”- 型: オブジェクト(Content)
- 必須: いいえ
開発者が設定するシステム指示です。現在はテキストのみサポートされています。
generationConfig
Section titled “generationConfig”- 型: オブジェクト
- 必須: いいえ
モデルの生成および出力に関する設定オプションです。
GenerationConfig オブジェクトの属性:
| 属性 | 型 | 必須 | 説明 |
|---|---|---|---|
| stopSequences | 配列 | いいえ | 生成を停止するための文字列シーケンスの集合(最大 5 個) |