Global Trend Radar
Web: ai.google.dev US web_search 2026-05-04 13:06

Using files | Gemini API | Google AI for Developers

元記事を開く →

分析結果

カテゴリ
AI
重要度
60
トレンドスコア
24
要約
Using files | Gemini API | Google AI for Developers メイン コンテンツにスキップ / English Deutsch Español – América Latina Français Indonesia Italiano Polski Português – Brasil Shqip Tiếng Việt Türkçe Русский עברית العربيّة فارسی ह
キーワード
Using files | Gemini API | Google AI for Developers メイン コンテンツにスキップ / English Deutsch Español – América Latina Français Indonesia Italiano Polski Português – Brasil Shqip Tiếng Việt Türkçe Русский עברית العربيّة فارسی हिंदी বাংলা ภาษาไทย 中文 – 简体 中文 – 繁體 日本語 한국어 API キーを取得する クックブック Community ログイン Gemini Deep Research がプレビュー版で利用可能になりました。共同プランニング、可視化、MCP サポートなどが含まれています。 ホーム Gemini API API リファレンス フィードバックを送信 Using files Gemini API は、プロンプト入力とは別にメディア ファイルのアップロードをサポートしているため、複数のリクエストや複数のプロンプトでメディアを再利用できます。詳しくは、 メディアを使用したプロンプト のガイドをご覧ください。 メソッド: media.upload エンドポイント リクエストの本文 JSON 表現 レスポンスの本文 JSON 表現 認可スコープ リクエストの例 画像 音声 テキスト 動画 PDF File を作成します。 エンドポイント アップロード URI(メディアのアップロード リクエストの場合): post https: / /generativelanguage.googleapis.com /upload /v1beta /files メタデータ URI(メタデータのみのリクエストの場合): post https: / /generativelanguage.googleapis.com /v1beta /files リクエストの本文 リクエストの本文には、次の構造のデータが含まれます。 フィールド file object ( File ) 省略可。作成するファイルのメタデータ。 リクエスト例 画像 Python from google import genai client = genai . Client () myfile = client . files . upload ( file = media / "Cajun_instruments.jpg" ) print ( f " { myfile =} " ) result = client . models . generate_content ( model = "gemini-2.0-flash" , contents = [ myfile , " \n\n " , "Can you tell me about the instruments in this photo?" , ], ) print ( f " { result . text =} " ) files . py Node.js // Make sure to include the following import: // import {GoogleGenAI} from '@google/genai'; const ai = new GoogleGenAI ({ apiKey : process . env . GEMINI_API_KEY }); const myfile = await ai . files . upload ({ file : path . join ( media , "Cajun_instruments.jpg" ), config : { mimeType : "image/jpeg" }, }); console . log ( "Uploaded file:" , myfile ); const result = await ai . models . generateContent ({ model : "gemini-2.0-flash" , contents : createUserContent ([ createPartFromUri ( myfile . uri , myfile . mimeType ), "\n\n" , "Can you tell me about the instruments in this photo?" , ]), }); console . log ( "result.text=" , result . text ); files . js Go ctx := context . Background () client , err := genai . NewClient ( ctx , & genai . ClientConfig { APIKey : os . Getenv ( "GEMINI_API_KEY" ), Backend : genai . BackendGeminiAPI , }) if err != nil { log . Fatal ( err ) } myfile , err := client . Files . UploadFromPath ( ctx , filepath . Join ( getMedia (), "Cajun_instruments.jpg" ), & genai . UploadFileConfig { MIMEType : "image/jpeg" , }, ) if err != nil { log . Fatal ( err ) } fmt . Printf ( "myfile=%+v\n" , myfile ) parts := [] * genai . Part { genai . NewPartFromURI ( myfile . URI , myfile . MIMEType ), genai . NewPartFromText ( "\n\n" ), genai . NewPartFromText ( "Can you tell me about the instruments in this photo?" ), } contents := [] * genai . Content { genai . NewContentFromParts ( parts , genai . RoleUser ), } response , err := client . Models . GenerateContent ( ctx , "gemini-2.0-flash" , contents , nil ) if err != nil { log . Fatal ( err ) } text := response . Text () fmt . Printf ( "result.text=%s\n" , text ) files . go Shell MIME_TYPE = $( file -b --mime-type " ${ IMG_PATH_2 } " ) NUM_BYTES = $( wc -c < " ${ IMG_PATH_2 } " ) DISPLAY_NAME = TEXT 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 " ${ BASE_URL } /upload/v1beta/files?key= ${ GEMINI_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 "@ ${ IMG_PATH_2 } " 2 > /dev/null > file_info.json file_uri = $( jq ".file.uri" file_info.json ) echo file_uri = $file_uri # Now generate content using that file curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key= $GEMINI_API_KEY " \ -H 'Content-Type: application/json' \ -X POST \ -d '{ "contents": [{ "parts":[ {"text": "Can you tell me about the instruments in this photo?"}, {"file_data": {"mime_type": "image/jpeg", "file_uri": ' $file_uri '} }] }] }' 2 > /dev/null > response.json cat response.json echo jq ".candidates[].content.parts[].text" response.json files.sh 音声 Python from google import genai client = genai . Client () myfile = client . files . upload ( file = media / "sample.mp3" ) print ( f " { myfile =} " ) result = client . models . generate_content ( model = "gemini-2.0-flash" , contents = [ myfile , "Describe this audio clip" ] ) print ( f " { result . text =} " ) files . py Node.js // Make sure to include the following import: // import {GoogleGenAI} from '@google/genai'; const ai = new GoogleGenAI ({ apiKey : process . env . GEMINI_API_KEY }); const myfile = await ai . files . upload ({ file : path . join ( media , "sample.mp3" ), config : { mimeType : "audio/mpeg" }, }); console . log ( "Uploaded file:" , myfile ); const result = await ai . models . generateContent ({ model : "gemini-2.0-flash" , contents : createUserContent ([ createPartFromUri ( myfile . uri , myfile . mimeType ), "Describe this audio clip" , ]), }); console . log ( "result.text=" , result . text ); files . js Go ctx := context . Background () client , err := genai . NewClient ( ctx , & genai . ClientConfig { APIKey : os . Getenv ( "GEMINI_API_KEY" ), Backend : genai . BackendGeminiAPI , }) if err != nil { log . Fatal ( err ) } myfile , err := client . Files . UploadFromPath ( ctx , filepath . Join ( getMedia (), "sample.mp3" ), & genai . UploadFileConfig { MIMEType : "audio/mpeg" , }, ) if err != nil { log . Fatal ( err ) } fmt . Printf ( "myfile=%+v\n" , myfile ) parts := [] * genai . Part { genai . NewPartFromURI ( myfile . URI , myfile . MIMEType ), genai . NewPartFromText ( "Describe this audio clip" ), } contents := [] * genai . Content { genai . NewContentFromParts ( parts , genai . RoleUser ), } response , err := client . Models . GenerateContent ( ctx , "gemini-2.0-flash" , contents , nil ) if err != nil { log . Fatal ( err ) } text := response . Text () fmt . Printf ( "result.text=%s\n" , text ) files . go Shell MIME_TYPE = $( file -b --mime-type " ${ AUDIO_PATH } " ) NUM_BYTES = $( wc -c < " ${ AUDIO_PATH } " ) DISPLAY_NAME = AUDIO 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 " ${ BASE_URL } /upload/v1beta/files?key= ${ GEMINI_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 "@ ${ AUDIO_PATH } " 2 > /dev/null > file_info.json file_uri = $( jq ".file.uri" file_info.json ) echo file_uri = $file_uri # Now generate content using that file curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key= $GEMINI_API_KEY " \ -H 'Content-Type: application/json' \ -X POST \ -d '{ "contents": [{ "parts":[ {"text": "Describe this audio clip"}, {"file_data":{"mime_type": "audio/mp3", "file_uri": ' $file_uri '}}] }] }' 2 > /dev/null > response.json cat response.json echo jq ".candidates[].content.parts[].text" response.json files.sh テキスト Python from google import genai client = genai . Client () myfile = client . files . upload ( file = media / "poem.txt" ) print ( f " { myfile =} " ) result = client . models . generate_content ( model = "gemini-2.0-flash" , contents = [ myfile , " \n\n " , "Can you add a few more lines to this poem?" ], ) print ( f " { result . text =} " ) files . py Node.js // Make sure to include the following import: // import {GoogleGenAI} from '@google/genai'; const ai = new GoogleGenAI ({ apiKey : process . env . GEMINI_API_KEY }); const myfile = await ai . files . upload ({ file : path . join ( media , "poem.txt" ), }); console . log ( "Uploaded file:" , myfile ); const result = await ai . models . generateContent ({ model : "gemini-2.0-flash" , contents : createUserContent ([ createPartFromUri ( myfile . uri , myfile . mimeType ), "\n\n" , "Can you add a few more lines to this poem?" , ]), }); console . log ( "result.text=" , result . text ); files . js Go ctx := context . Background () client , err := genai . NewClient ( ctx , & genai . ClientConfig { APIKey : os . Getenv ( "GEMINI_API_KEY" ), Backend : genai . BackendGeminiAPI , }) if err != nil { log . Fatal ( err ) } myfile , err := client . Files . UploadFromPath ( ctx , filepath . Join ( getMedia (), "poem.txt" ), & genai . UploadFileConfig { MIMEType : "text/plain" , }, ) if err != nil { log . Fatal ( err ) } fmt . Printf ( "myfile=%+v\n" , myfile ) parts := [] * genai . Part { genai . NewPartFromURI ( myfile . URI , myfile . MIMEType ), genai . NewPartFromText ( "\n\n" ), genai . NewPartFromText ( "Can you add a few more lines to this poem?" ), } contents := [] * genai . Content { genai . NewContentFromParts ( parts , genai . RoleUser ), } response , err := client . Models . GenerateContent ( ctx , "gemini-2.0-flash" , contents , nil ) if err != nil { log . Fatal ( err ) } text := response . Text () fmt . Printf ( "result.text=%s\n" , text ) files . go Shell MIM

類似記事(ベクトル近傍)