文本生成

Gemini API 可以利用 Gemini 模型,根据各种输入(包括文本、图片、视频和音频)生成文本输出。

下面是一个采用单个文本输入的基本示例:

Python

from google import genai  client = genai.Client()  response = client.models.generate_content(     model="gemini-2.5-flash",     contents="How does AI work?" ) print(response.text) 

JavaScript

import { GoogleGenAI } from "@google/genai";  const ai = new GoogleGenAI({});  async function main() {   const response = await ai.models.generateContent({     model: "gemini-2.5-flash",     contents: "How does AI work?",   });   console.log(response.text); }  await main(); 

Go

package main  import (   "context"   "fmt"   "os"   "google.golang.org/genai" )  func main() {    ctx := context.Background()   client, err := genai.NewClient(ctx, nil)   if err != nil {       log.Fatal(err)   }    result, _ := client.Models.GenerateContent(       ctx,       "gemini-2.5-flash",       genai.Text("Explain how AI works in a few words"),       nil,   )    fmt.Println(result.Text()) } 

REST

curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent" \   -H "x-goog-api-key: $GEMINI_API_KEY" \   -H 'Content-Type: application/json' \   -X POST \   -d '{     "contents": [       {         "parts": [           {             "text": "How does AI work?"           }         ]       }     ]   }' 

Apps 脚本

// See https://developers.google.com/apps-script/guides/properties // for instructions on how to set the API key. const apiKey = PropertiesService.getScriptProperties().getProperty('GEMINI_API_KEY');  function main() {   const payload = {     contents: [       {         parts: [           { text: 'How AI does work?' },         ],       },     ],   };    const url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent';   const options = {     method: 'POST',     contentType: 'application/json',     headers: {       'x-goog-api-key': apiKey,     },     payload: JSON.stringify(payload)   };    const response = UrlFetchApp.fetch(url, options);   const data = JSON.parse(response);   const content = data['candidates'][0]['content']['parts'][0]['text'];   console.log(content); } 

使用 Gemini 2.5 进行思考

2.5 Flash 和 Pro 模型默认启用“思考”功能,以提升回答质量,但可能会延长运行时间并增加令牌用量。

使用 2.5 Flash 时,您可以将思考预算设置为零,以停用思考功能。

如需了解详情,请参阅思考指南

Python

from google import genai from google.genai import types  client = genai.Client()  response = client.models.generate_content(     model="gemini-2.5-flash",     contents="How does AI work?",     config=types.GenerateContentConfig(         thinking_config=types.ThinkingConfig(thinking_budget=0) # Disables thinking     ), ) print(response.text) 

JavaScript

import { GoogleGenAI } from "@google/genai";  const ai = new GoogleGenAI({});  async function main() {   const response = await ai.models.generateContent({     model: "gemini-2.5-flash",     contents: "How does AI work?",     config: {       thinkingConfig: {         thinkingBudget: 0, // Disables thinking       },     }   });   console.log(response.text); }  await main(); 

Go

package main  import (   "context"   "fmt"   "os"   "google.golang.org/genai" )  func main() {    ctx := context.Background()   client, err := genai.NewClient(ctx, nil)   if err != nil {       log.Fatal(err)   }    result, _ := client.Models.GenerateContent(       ctx,       "gemini-2.5-flash",       genai.Text("How does AI work?"),       &genai.GenerateContentConfig{         ThinkingConfig: &genai.ThinkingConfig{             ThinkingBudget: int32(0), // Disables thinking         },       }   )    fmt.Println(result.Text()) } 

REST

curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent" \   -H "x-goog-api-key: $GEMINI_API_KEY" \   -H 'Content-Type: application/json' \   -X POST \   -d '{     "contents": [       {         "parts": [           {             "text": "How does AI work?"           }         ]       }     ],     "generationConfig": {       "thinkingConfig": {         "thinkingBudget": 0       }     }   }' 

Apps 脚本

// See https://developers.google.com/apps-script/guides/properties // for instructions on how to set the API key. const apiKey = PropertiesService.getScriptProperties().getProperty('GEMINI_API_KEY');  function main() {   const payload = {     contents: [       {         parts: [           { text: 'How AI does work?' },         ],       },     ],   };    const url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent';   const options = {     method: 'POST',     contentType: 'application/json',     headers: {       'x-goog-api-key': apiKey,     },     payload: JSON.stringify(payload)   };    const response = UrlFetchApp.fetch(url, options);   const data = JSON.parse(response);   const content = data['candidates'][0]['content']['parts'][0]['text'];   console.log(content); } 

系统指令和其他配置

您可以使用系统指令来引导 Gemini 模型的行为。为此,请传递一个 GenerateContentConfig 对象。

Python

from google import genai from google.genai import types  client = genai.Client()  response = client.models.generate_content(     model="gemini-2.5-flash",     config=types.GenerateContentConfig(         system_instruction="You are a cat. Your name is Neko."),     contents="Hello there" )  print(response.text) 

JavaScript

import { GoogleGenAI } from "@google/genai";  const ai = new GoogleGenAI({});  async function main() {   const response = await ai.models.generateContent({     model: "gemini-2.5-flash",     contents: "Hello there",     config: {       systemInstruction: "You are a cat. Your name is Neko.",     },   });   console.log(response.text); }  await main(); 

Go

package main  import (   "context"   "fmt"   "os"   "google.golang.org/genai" )  func main() {    ctx := context.Background()   client, err := genai.NewClient(ctx, nil)   if err != nil {       log.Fatal(err)   }    config := &genai.GenerateContentConfig{       SystemInstruction: genai.NewContentFromText("You are a cat. Your name is Neko.", genai.RoleUser),   }    result, _ := client.Models.GenerateContent(       ctx,       "gemini-2.5-flash",       genai.Text("Hello there"),       config,   )    fmt.Println(result.Text()) } 

REST

curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent" \   -H "x-goog-api-key: $GEMINI_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"           }         ]       }     ]   }' 

Apps 脚本

// See https://developers.google.com/apps-script/guides/properties // for instructions on how to set the API key. const apiKey = PropertiesService.getScriptProperties().getProperty('GEMINI_API_KEY');  function main() {   const systemInstruction = {     parts: [{       text: 'You are a cat. Your name is Neko.'     }]   };    const payload = {     systemInstruction,     contents: [       {         parts: [           { text: 'Hello there' },         ],       },     ],   };    const url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent';   const options = {     method: 'POST',     contentType: 'application/json',     headers: {       'x-goog-api-key': apiKey,     },     payload: JSON.stringify(payload)   };    const response = UrlFetchApp.fetch(url, options);   const data = JSON.parse(response);   const content = data['candidates'][0]['content']['parts'][0]['text'];   console.log(content); } 

借助 GenerateContentConfig 对象,您还可以替换默认的生成参数,例如温度

Python

from google import genai from google.genai import types  client = genai.Client()  response = client.models.generate_content(     model="gemini-2.5-flash",     contents=["Explain how AI works"],     config=types.GenerateContentConfig(         temperature=0.1     ) ) print(response.text) 

JavaScript

import { GoogleGenAI } from "@google/genai";  const ai = new GoogleGenAI({});  async function main() {   const response = await ai.models.generateContent({     model: "gemini-2.5-flash",     contents: "Explain how AI works",     config: {       temperature: 0.1,     },   });   console.log(response.text); }  await main(); 

Go

package main  import (   "context"   "fmt"   "os"   "google.golang.org/genai" )  func main() {    ctx := context.Background()   client, err := genai.NewClient(ctx, nil)   if err != nil {       log.Fatal(err)   }    temp := float32(0.9)   topP := float32(0.5)   topK := float32(20.0)    config := &genai.GenerateContentConfig{     Temperature:       &temp,     TopP:              &topP,     TopK:              &topK,     ResponseMIMEType:  "application/json",   }    result, _ := client.Models.GenerateContent(     ctx,     "gemini-2.5-flash",     genai.Text("What is the average size of a swallow?"),     config,   )    fmt.Println(result.Text()) } 

REST

curl https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent \   -H "x-goog-api-key: $GEMINI_API_KEY" \   -H 'Content-Type: application/json' \   -X POST \   -d '{     "contents": [       {         "parts": [           {             "text": "Explain how AI works"           }         ]       }     ],     "generationConfig": {       "stopSequences": [         "Title"       ],       "temperature": 1.0,       "topP": 0.8,       "topK": 10     }   }' 

Apps 脚本

// See https://developers.google.com/apps-script/guides/properties // for instructions on how to set the API key. const apiKey = PropertiesService.getScriptProperties().getProperty('GEMINI_API_KEY');  function main() {   const generationConfig = {     temperature: 1,     topP: 0.95,     topK: 40,     responseMimeType: 'text/plain',   };    const payload = {     generationConfig,     contents: [       {         parts: [           { text: 'Explain how AI works in a few words' },         ],       },     ],   };    const url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent';   const options = {     method: 'POST',     contentType: 'application/json',     headers: {       'x-goog-api-key': apiKey,     },     payload: JSON.stringify(payload)   };    const response = UrlFetchApp.fetch(url, options);   const data = JSON.parse(response);   const content = data['candidates'][0]['content']['parts'][0]['text'];   console.log(content); } 

如需查看可配置参数及其说明的完整列表,请参阅 API 参考文档中的 GenerateContentConfig

多模态输入

Gemini API 支持多模态输入,让您可以将文本与媒体文件相结合。以下示例演示了如何提供图片:

Python

from PIL import Image from google import genai  client = genai.Client()  image = Image.open("/path/to/organ.png") response = client.models.generate_content(     model="gemini-2.5-flash",     contents=[image, "Tell me about this instrument"] ) print(response.text) 

JavaScript

import {   GoogleGenAI,   createUserContent,   createPartFromUri, } from "@google/genai";  const ai = new GoogleGenAI({});  async function main() {   const image = await ai.files.upload({     file: "/path/to/organ.png",   });   const response = await ai.models.generateContent({     model: "gemini-2.5-flash",     contents: [       createUserContent([         "Tell me about this instrument",         createPartFromUri(image.uri, image.mimeType),       ]),     ],   });   console.log(response.text); }  await main(); 

Go

package main  import (   "context"   "fmt"   "os"   "google.golang.org/genai" )  func main() {    ctx := context.Background()   client, err := genai.NewClient(ctx, nil)   if err != nil {       log.Fatal(err)   }    imagePath := "/path/to/organ.jpg"   imgData, _ := os.ReadFile(imagePath)    parts := []*genai.Part{       genai.NewPartFromText("Tell me about this instrument"),       &genai.Part{           InlineData: &genai.Blob{               MIMEType: "image/jpeg",               Data:     imgData,           },       },   }    contents := []*genai.Content{       genai.NewContentFromParts(parts, genai.RoleUser),   }    result, _ := client.Models.GenerateContent(       ctx,       "gemini-2.5-flash",       contents,       nil,   )    fmt.Println(result.Text()) } 

REST

# Use a temporary file to hold the base64 encoded image data TEMP_B64=$(mktemp) trap 'rm -f "$TEMP_B64"' EXIT base64 $B64FLAGS $IMG_PATH > "$TEMP_B64"  # Use a temporary file to hold the JSON payload 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://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent" \   -H "x-goog-api-key: $GEMINI_API_KEY" \   -H 'Content-Type: application/json' \   -X POST \   -d "@$TEMP_JSON" 

Apps 脚本

// See https://developers.google.com/apps-script/guides/properties // for instructions on how to set the API key. const apiKey = PropertiesService.getScriptProperties().getProperty('GEMINI_API_KEY');  function main() {   const imageUrl = 'http://image/url';   const image = getImageData(imageUrl);   const payload = {     contents: [       {         parts: [           { image },           { text: 'Tell me about this instrument' },         ],       },     ],   };    const url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent';   const options = {     method: 'POST',     contentType: 'application/json',     headers: {       'x-goog-api-key': apiKey,     },     payload: JSON.stringify(payload)   };    const response = UrlFetchApp.fetch(url, options);   const data = JSON.parse(response);   const content = data['candidates'][0]['content']['parts'][0]['text'];   console.log(content); }  function getImageData(url) {   const blob = UrlFetchApp.fetch(url).getBlob();    return {     mimeType: blob.getContentType(),     data: Utilities.base64Encode(blob.getBytes())   }; } 

如需了解提供图片的其他方法和更高级的图片处理,请参阅我们的图片理解指南。 该 API 还支持文档视频音频输入和理解。

流式响应

默认情况下,模型会在完成整个生成过程后返回回答。

为了获得更流畅的互动体验,请使用流式传输来逐步接收 GenerateContentResponse 实例(在生成时)。

Python

from google import genai  client = genai.Client()  response = client.models.generate_content_stream(     model="gemini-2.5-flash",     contents=["Explain how AI works"] ) for chunk in response:     print(chunk.text, end="") 

JavaScript

import { GoogleGenAI } from "@google/genai";  const ai = new GoogleGenAI({});  async function main() {   const response = await ai.models.generateContentStream({     model: "gemini-2.5-flash",     contents: "Explain how AI works",   });    for await (const chunk of response) {     console.log(chunk.text);   } }  await main(); 

Go

package main  import (   "context"   "fmt"   "os"   "google.golang.org/genai" )  func main() {    ctx := context.Background()   client, err := genai.NewClient(ctx, nil)   if err != nil {       log.Fatal(err)   }    stream := client.Models.GenerateContentStream(       ctx,       "gemini-2.5-flash",       genai.Text("Write a story about a magic backpack."),       nil,   )    for chunk, _ := range stream {       part := chunk.Candidates[0].Content.Parts[0]       fmt.Print(part.Text)   } } 

REST

curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:streamGenerateContent?alt=sse" \   -H "x-goog-api-key: $GEMINI_API_KEY" \   -H 'Content-Type: application/json' \   --no-buffer \   -d '{     "contents": [       {         "parts": [           {             "text": "Explain how AI works"           }         ]       }     ]   }' 

Apps 脚本

// See https://developers.google.com/apps-script/guides/properties // for instructions on how to set the API key. const apiKey = PropertiesService.getScriptProperties().getProperty('GEMINI_API_KEY');  function main() {   const payload = {     contents: [       {         parts: [           { text: 'Explain how AI works' },         ],       },     ],   };    const url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:streamGenerateContent';   const options = {     method: 'POST',     contentType: 'application/json',     headers: {       'x-goog-api-key': apiKey,     },     payload: JSON.stringify(payload)   };    const response = UrlFetchApp.fetch(url, options);   const data = JSON.parse(response);   const content = data['candidates'][0]['content']['parts'][0]['text'];   console.log(content); } 

多轮对话(聊天)

我们的 SDK 提供相应功能,可将多轮提示和回答收集到对话中,让您轻松跟踪对话历史记录。

Python

from google import genai  client = genai.Client() chat = client.chats.create(model="gemini-2.5-flash")  response = chat.send_message("I have 2 dogs in my house.") print(response.text)  response = chat.send_message("How many paws are in my house?") print(response.text)  for message in chat.get_history():     print(f'role - {message.role}',end=": ")     print(message.parts[0].text) 

JavaScript

import { GoogleGenAI } from "@google/genai";  const ai = new GoogleGenAI({});  async function main() {   const chat = ai.chats.create({     model: "gemini-2.5-flash",     history: [       {         role: "user",         parts: [{ text: "Hello" }],       },       {         role: "model",         parts: [{ text: "Great to meet you. What would you like to know?" }],       },     ],   });    const response1 = await chat.sendMessage({     message: "I have 2 dogs in my house.",   });   console.log("Chat response 1:", response1.text);    const response2 = await chat.sendMessage({     message: "How many paws are in my house?",   });   console.log("Chat response 2:", response2.text); }  await main(); 

Go

package main  import (   "context"   "fmt"   "os"   "google.golang.org/genai" )  func main() {    ctx := context.Background()   client, err := genai.NewClient(ctx, nil)   if err != nil {       log.Fatal(err)   }    history := []*genai.Content{       genai.NewContentFromText("Hi nice to meet you! I have 2 dogs in my house.", genai.RoleUser),       genai.NewContentFromText("Great to meet you. What would you like to know?", genai.RoleModel),   }    chat, _ := client.Chats.Create(ctx, "gemini-2.5-flash", nil, history)   res, _ := chat.SendMessage(ctx, genai.Part{Text: "How many paws are in my house?"})    if len(res.Candidates) > 0 {       fmt.Println(res.Candidates[0].Content.Parts[0].Text)   } } 

REST

curl https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent \   -H "x-goog-api-key: $GEMINI_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?"           }         ]       }     ]   }' 

Apps 脚本

// See https://developers.google.com/apps-script/guides/properties // for instructions on how to set the API key. const apiKey = PropertiesService.getScriptProperties().getProperty('GEMINI_API_KEY');  function main() {   const payload = {     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?' },         ],       },     ],   };    const url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent';   const options = {     method: 'POST',     contentType: 'application/json',     headers: {       'x-goog-api-key': apiKey,     },     payload: JSON.stringify(payload)   };    const response = UrlFetchApp.fetch(url, options);   const data = JSON.parse(response);   const content = data['candidates'][0]['content']['parts'][0]['text'];   console.log(content); } 

流式传输还可用于多轮对话。

Python

from google import genai  client = genai.Client() chat = client.chats.create(model="gemini-2.5-flash")  response = chat.send_message_stream("I have 2 dogs in my house.") for chunk in response:     print(chunk.text, end="")  response = chat.send_message_stream("How many paws are in my house?") for chunk in response:     print(chunk.text, end="")  for message in chat.get_history():     print(f'role - {message.role}', end=": ")     print(message.parts[0].text) 

JavaScript

import { GoogleGenAI } from "@google/genai";  const ai = new GoogleGenAI({});  async function main() {   const chat = ai.chats.create({     model: "gemini-2.5-flash",     history: [       {         role: "user",         parts: [{ text: "Hello" }],       },       {         role: "model",         parts: [{ text: "Great to meet you. What would you like to know?" }],       },     ],   });    const stream1 = await chat.sendMessageStream({     message: "I have 2 dogs in my house.",   });   for await (const chunk of stream1) {     console.log(chunk.text);     console.log("_".repeat(80));   }    const stream2 = await chat.sendMessageStream({     message: "How many paws are in my house?",   });   for await (const chunk of stream2) {     console.log(chunk.text);     console.log("_".repeat(80));   } }  await main(); 

Go

package main  import (   "context"   "fmt"   "os"   "google.golang.org/genai" )  func main() {    ctx := context.Background()   client, err := genai.NewClient(ctx, nil)   if err != nil {       log.Fatal(err)   }    history := []*genai.Content{       genai.NewContentFromText("Hi nice to meet you! I have 2 dogs in my house.", genai.RoleUser),       genai.NewContentFromText("Great to meet you. What would you like to know?", genai.RoleModel),   }    chat, _ := client.Chats.Create(ctx, "gemini-2.5-flash", nil, history)   stream := chat.SendMessageStream(ctx, genai.Part{Text: "How many paws are in my house?"})    for chunk, _ := range stream {       part := chunk.Candidates[0].Content.Parts[0]       fmt.Print(part.Text)   } } 

REST

curl https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:streamGenerateContent?alt=sse \   -H "x-goog-api-key: $GEMINI_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?"           }         ]       }     ]   }' 

Apps 脚本

// See https://developers.google.com/apps-script/guides/properties // for instructions on how to set the API key. const apiKey = PropertiesService.getScriptProperties().getProperty('GEMINI_API_KEY');  function main() {   const payload = {     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?' },         ],       },     ],   };    const url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:streamGenerateContent';   const options = {     method: 'POST',     contentType: 'application/json',     headers: {       'x-goog-api-key': apiKey,     },     payload: JSON.stringify(payload)   };    const response = UrlFetchApp.fetch(url, options);   const data = JSON.parse(response);   const content = data['candidates'][0]['content']['parts'][0]['text'];   console.log(content); } 

支持的模型

Gemini 系列中的所有模型都支持文本生成。如需详细了解这些模型及其功能,请访问模型页面。

最佳做法

提示技巧

对于基本的文本生成,通常只需使用零样本提示,而无需提供示例、系统指令或特定格式。

如需获得更贴合需求的输出:

  • 使用系统说明来引导模型。
  • 提供少量输入和输出示例来引导模型。这通常称为少样本提示。

如需了解更多提示技巧,请参阅我们的提示工程指南

结构化输出

在某些情况下,您可能需要结构化输出,例如 JSON。如需了解具体方法,请参阅我们的结构化输出指南。

后续步骤