中文(繁體)

目前位置: 首頁> Gemini 教學> 如何使用Gemini API 生成文本

如何使用Gemini API 生成文本

作者: LoRA 時間:

Gemini API 可以根據各種輸入(包括文本、圖片、視頻和音頻)生成文本輸出。

在調用Gemini API 之前,請確保您已安裝所選的SDK,並已配置好Gemini API 密鑰,可以使用。

一、文字輸入

使用Gemini API 生成文本的最簡單方法是向模型提供單個純文本輸入,如以下示例所示:

Python

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

JavaScript

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

Go

 // import packages here
func main() {
 ctx := context.Background()
 client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
 if err != nil {
   log.Fatal(err)
 }
 defer client.Close()
 model := client.GenerativeModel("gemini-2.0-flash")
 resp, err := model.GenerateContent(ctx, genai.Text("How does AI work?"))
 if err != nil {
   log.Fatal(err)
 }
 printResponse(resp) // helper function for printing content parts
}

REST

 curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?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.0-flash:generateContent?key=${apiKey}`;
 const options = {
   method: 'POST',
   contentType: 'application/json',
   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 API 支持將文本和媒體文件組合在一起的多模態輸入。以下示例展示瞭如何根據文本和圖片輸入生成文本:

Python

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

JavaScript

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

Go

 model := client.GenerativeModel("gemini-2.0-flash")
imgData, err := os.ReadFile(filepath.Join(testDataDir, "organ.jpg"))
if err != nil {
 log.Fatal(err)
}
resp, err := model.GenerateContent(ctx,
 genai.Text("Tell me about this instrument"),
 genai.ImageData("jpeg", imgData))
if err != nil {
 log.Fatal(err)
}
printResponse(resp)

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.0-flash:generateContent?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.0-flash:generateContent?key=${apiKey}`;
 const options = {
   method: 'POST',
   contentType: 'application/json',
   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())
 };
}

三、流式輸出

默認情況下,模型會在完成整個文本生成流程後返回回答。您可以使用流式傳輸在GenerateContentResponse 實例生成時返回這些實例,從而實現更快的互動。

Python

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

JavaScript

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

Go

 model := client.GenerativeModel("gemini-1.5-flash")
iter := model.GenerateContentStream(ctx, genai.Text("Write a story about a magic backpack."))
for {
 resp, err := iter.Next()
 if err == iterator.Done {
   break
 }
 if err != nil {
   log.Fatal(err)
 }
 printResponse(resp)
}

REST

 curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:streamGenerateContent?alt=sse&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.0-flash:streamGenerateContent?key=${apiKey}`;
 const options = {
   method: 'POST',
   contentType: 'application/json',
   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 SDK,您可以將多輪問題和回答收集到一個對話中。借助聊天格式,用戶可以逐步獲得答案,並在遇到多部分問題時獲得幫助。此SDK 聊天實現提供了一個界面來跟踪對話歷史記錄,但在後台,它使用相同的generateContent 方法來創建響應。

以下代碼示例展示了基本聊天功能的實現:

Python

 from google import genai
client = genai.Client(api_key="GEMINI_API_KEY")
chat = client.chats.create(model="gemini-2.0-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({ apiKey: "GEMINI_API_KEY" });
async function main() {
 const chat = ai.chats.create({
   model: "gemini-2.0-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

 model := client.GenerativeModel("gemini-1.5-flash")
cs := model.StartChat()
cs.History = []*genai.Content{
 {
   Parts: []genai.Part{
     genai.Text("Hello, I have 2 dogs in my house."),
   },
   Role: "user",
 },
 {
   Parts: []genai.Part{
     genai.Text("Great to meet you. What would you like to know?"),
   },
   Role: "model",
 },
}
res, err := cs.SendMessage(ctx, genai.Text("How many paws are in my house?"))
if err != nil {
 log.Fatal(err)
}
printResponse(res)

REST

 curl https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?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.0-flash:generateContent?key=${apiKey}`;
 const options = {
   method: 'POST',
   contentType: 'application/json',
   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(api_key="GEMINI_API_KEY")
chat = client.chats.create(model="gemini-2.0-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({ apiKey: "GEMINI_API_KEY" });
async function main() {
 const chat = ai.chats.create({
   model: "gemini-2.0-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

 model := client.GenerativeModel("gemini-1.5-flash")
cs := model.StartChat()
cs.History = []*genai.Content{
 {
   Parts: []genai.Part{
     genai.Text("Hello, I have 2 dogs in my house."),
   },
   Role: "user",
 },
 {
   Parts: []genai.Part{
     genai.Text("Great to meet you. What would you like to know?"),
   },
   Role: "model",
 },
}
iter := cs.SendMessageStream(ctx, genai.Text("How many paws are in my house?"))
for {
 resp, err := iter.Next()
 if err == iterator.Done {
   break
 }
 if err != nil {
   log.Fatal(err)
 }
 printResponse(resp)
}

REST

 curl https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:streamGenerateContent?alt=sse&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.0-flash:streamGenerateContent?key=${apiKey}`;
 const options = {
   method: 'POST',
   contentType: 'application/json',
   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
from google.genai import types
client = genai.Client(api_key="GEMINI_API_KEY")
response = client.models.generate_content(
   model="gemini-2.0-flash",
   contents=["Explain how AI works"],
   config=types.GenerateContentConfig(
       max_output_tokens=500,
       temperature=0.1
   )
)
print(response.text)

JavaScript

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

Go

 model := client.GenerativeModel("gemini-1.5-pro-latest")
model.SetTemperature(0.9)
model.SetTopP(0.5)
model.SetTopK(20)
model.SetMaxOutputTokens(100)
model.SystemInstruction = genai.NewUserContent(genai.Text("You are Yoda from Star Wars."))
model.ResponseMIMEType = "application/json"
resp, err := model.GenerateContent(ctx, genai.Text("What is the average size of a swallow?"))
if err != nil {
 log.Fatal(err)
}
printResponse(resp)

REST

 curl https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?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,
     "maxOutputTokens": 800,
     "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,
   maxOutputTokens: 8192,
   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.0-flash:generateContent?key=${apiKey}`;
 const options = {
   method: 'POST',
   contentType: 'application/json',
   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);
}

以下是您可以配置的一些模型參數。 (命名慣例因編程語言而異。)

1.stopSequences:指定一組將停止生成輸出的字符序列(最多5 個)。如果指定,API 會在stop_sequence 首次出現時停止。停止序列不會包含在響應中。

2.temperature:控制輸出的隨機性。使用較高的值可獲得更具創造性的回答,使用較低的值可獲得更具確定性的回答。值的範圍為[0.0, 2.0]。

3.maxOutputTokens:設置候選項中包含的詞元數上限。

4.topP:更改模型選擇輸出詞元的方式。系統會按照概率從最高到最低的順序選擇token,直到所選token 的概率總和等於topP 的值。默認的topP 值為0.95。

5.topK:更改模型選擇輸出詞元的方式。如果topK 設為1,則表示所選token 是模型詞彙表的所有token 中概率最高的token;如果topK 設為3,則表示系統將從3 個概率最高的token 中選擇下一個token(通過溫度確定)。系統會根據topP 進一步過濾詞元,並使用溫度採樣選擇最終的詞元。

六、系統指令

借助系統說明,您可以根據具體用例來控制模型的行為。提供系統說明時,您可以為模型提供額外的上下文,幫助其了解任務並生成自定義程度更高的回答。模型應在與用戶的完整互動過程中遵循系統說明,以便您能夠指定產品級行為,而無需考慮最終用戶提供的提示。

您可以在初始化模型時設置系統說明:

Python

 from google import genai
from google.genai import types
client = genai.Client(api_key="GEMINI_API_KEY")
response = client.models.generate_content(
   model="gemini-2.0-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({ apiKey: "GEMINI_API_KEY" });
async function main() {
 const response = await ai.models.generateContent({
   model: "gemini-2.0-flash",
   contents: "Hello there",
   config: {
     systemInstruction: "You are a cat. Your name is Neko.",
   },
 });
 console.log(response.text);
}
await main();

Go

 // import packages here
func main() {
 ctx := context.Background()
 client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
 if err != nil {
   log.Fatal(err)
 }
 defer client.Close()
 model := client.GenerativeModel("gemini-2.0-flash")
 model.SystemInstruction = &genai.Content{
   Parts: []genai.Part{genai.Text(`
     You are a cat. Your name is Neko.
   `)},
 }
 resp, err := model.GenerateContent(ctx, genai.Text("Hello there"))
 if err != nil {
   log.Fatal(err)
 }
 printResponse(resp) // helper function for printing content parts
}

REST

 curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?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.0-flash:generateContent?key=${apiKey}`;
 const options = {
   method: 'POST',
   contentType: 'application/json',
   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系列模型都支持文本生成。

對於基本文本生成應用場景,您的問題可能不需要包含任何輸出示例、系統說明或格式信息。這是一種零樣本方法。對於某些用例,單次或少樣本提示可能會生成更符合用戶預期的輸出。在某些情況下,您可能還需要提供系統說明,以幫助模型了解任務或遵循特定準則。