Generating content

Gemini API รองรับการสร้างเนื้อหาด้วยรูปภาพ เสียง โค้ด เครื่องมือ และอื่นๆ อ่านต่อเพื่อดูรายละเอียดเกี่ยวกับฟีเจอร์แต่ละอย่าง และดูตัวอย่างโค้ดที่เน้นงาน หรืออ่านคำแนะนำแบบครอบคลุม

เมธอด: models.generateContent

สร้างคำตอบของโมเดลเมื่อได้รับอินพุต GenerateContentRequest ดูข้อมูลการใช้งานโดยละเอียดได้ในคำแนะนำในการสร้างข้อความ ความสามารถในการป้อนข้อมูลจะแตกต่างกันไปในแต่ละรุ่น รวมถึงรุ่นที่ปรับแต่งแล้ว ดูรายละเอียดได้ที่คำแนะนำเกี่ยวกับโมเดลและคำแนะนำในการปรับแต่ง

ปลายทาง

post https://generativelanguage.googleapis.com/v1beta/{model=models/*}:generateContent

พารามิเตอร์เส้นทาง

model string

ต้องระบุ ชื่อของ Model ที่จะใช้ในการสร้างข้อความเติม

รูปแบบ: models/{model} ซึ่งจะอยู่ในรูปแบบ models/{model}

เนื้อความของคำขอ

เนื้อหาของคำขอมีข้อมูลซึ่งมีโครงสร้างดังต่อไปนี้

ฟิลด์
contents[] object (Content)

ต้องระบุ เนื้อหาของการสนทนาปัจจุบันกับโมเดล

สำหรับคำค้นหาแบบเลี้ยวเดียว นี่คืออินสแตนซ์เดียว สำหรับคำค้นหาแบบหลายรอบ เช่น แชท นี่คือฟิลด์ที่ทำซ้ำซึ่งมีประวัติการสนทนาและคำขอที่ล่าสุด

tools[] object (Tool)

ไม่บังคับ รายการของToolsModelที่โมเดลอาจใช้เพื่อสร้างคำตอบถัดไป

Tool คือโค้ดที่ช่วยให้ระบบโต้ตอบกับระบบภายนอกเพื่อดำเนินการหรือชุดการดำเนินการนอกเหนือจากความรู้และขอบเขตของ Model Tool ที่รองรับคือ Function และ codeExecution ดูข้อมูลเพิ่มเติมได้ที่คู่มือการเรียกใช้ฟังก์ชันและการประมวลผลโค้ด

toolConfig object (ToolConfig)

ไม่บังคับ การกำหนดค่าเครื่องมือสำหรับToolที่ระบุในคำขอ ดูตัวอย่างการใช้งานได้ที่คำแนะนำในการเรียกใช้ฟังก์ชัน

safetySettings[] object (SafetySetting)

ไม่บังคับ รายการSafetySettingอินสแตนซ์ที่ไม่ซ้ำกันสำหรับการบล็อกเนื้อหาที่ไม่ปลอดภัย

โดยจะมีผลบังคับใช้ในวันที่ GenerateContentRequest.contents และ GenerateContentResponse.candidates ไม่ควรมีการตั้งค่ามากกว่า 1 รายการสำหรับSafetyCategoryแต่ละประเภท API จะบล็อกเนื้อหาและการตอบกลับที่ไม่เป็นไปตามเกณฑ์ที่กำหนดโดยการตั้งค่าเหล่านี้ รายการนี้จะลบล้างการตั้งค่าเริ่มต้นสำหรับแต่ละรายการที่SafetyCategoryระบุไว้ใน safetySettings หากไม่มี SafetySetting สำหรับ SafetyCategory ที่ระบุในรายการ API จะใช้การตั้งค่าความปลอดภัยเริ่มต้นสำหรับหมวดหมู่นั้น ระบบรองรับหมวดหมู่ที่เป็นอันตราย HARM_CATEGORY_HATE_SPEECH, HARM_CATEGORY_SEXUALLY_EXPLICIT, HARM_CATEGORY_DANGEROUS_CONTENT, HARM_CATEGORY_HARASSMENT, HARM_CATEGORY_CIVIC_INTEGRITY ดูข้อมูลโดยละเอียดเกี่ยวกับการตั้งค่าความปลอดภัยที่มีได้ในคำแนะนำ นอกจากนี้ โปรดดูคำแนะนำด้านความปลอดภัยเพื่อดูวิธีพิจารณาเรื่องความปลอดภัยในแอปพลิเคชัน AI

systemInstruction object (Content)

ไม่บังคับ นักพัฒนาแอปตั้งค่าคำสั่งของระบบ ปัจจุบันมีเฉพาะข้อความ

generationConfig object (GenerationConfig)

ไม่บังคับ ตัวเลือกการกำหนดค่าสำหรับการสร้างโมเดลและเอาต์พุต

cachedContent string

ไม่บังคับ ชื่อของเนื้อหาที่แคชไว้เพื่อใช้เป็นบริบทในการแสดงผลการคาดคะเน รูปแบบ: cachedContents/{cachedContent}

ตัวอย่างคำขอ

ข้อความ

Python

from google import genai

client = genai.Client()
response = client.models.generate_content(
    model="gemini-2.0-flash", contents="Write a story about a magic backpack."
)
print(response.text)

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 response = await ai.models.generateContent({
  model: "gemini-2.0-flash",
  contents: "Write a story about a magic backpack.",
});
console.log(response.text);

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)
}
contents := []*genai.Content{
	genai.NewContentFromText("Write a story about a magic backpack.", genai.RoleUser),
}
response, err := client.Models.GenerateContent(ctx, "gemini-2.0-flash", contents, nil)
if err != nil {
	log.Fatal(err)
}
printResponse(response)

เปลือกหอย

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": "Write a story about a magic backpack."}]
        }]
       }' 2> /dev/null

Java

Client client = new Client();

GenerateContentResponse response =
        client.models.generateContent(
                "gemini-2.0-flash",
                "Write a story about a magic backpack.",
                null);

System.out.println(response.text());

รูปภาพ

Python

from google import genai
import PIL.Image

client = genai.Client()
organ = PIL.Image.open(media / "organ.jpg")
response = client.models.generate_content(
    model="gemini-2.0-flash", contents=["Tell me about this instrument", organ]
)
print(response.text)

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 organ = await ai.files.upload({
  file: path.join(media, "organ.jpg"),
});

const response = await ai.models.generateContent({
  model: "gemini-2.0-flash",
  contents: [
    createUserContent([
      "Tell me about this instrument", 
      createPartFromUri(organ.uri, organ.mimeType)
    ]),
  ],
});
console.log(response.text);

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)
}

file, err := client.Files.UploadFromPath(
	ctx, 
	filepath.Join(getMedia(), "organ.jpg"), 
	&genai.UploadFileConfig{
		MIMEType : "image/jpeg",
	},
)
if err != nil {
	log.Fatal(err)
}
parts := []*genai.Part{
	genai.NewPartFromText("Tell me about this instrument"),
	genai.NewPartFromURI(file.URI, file.MIMEType),
}
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)
}
printResponse(response)

เปลือกหอย

# 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" 2> /dev/null

Java

Client client = new Client();

String path = media_path + "organ.jpg";
byte[] imageData = Files.readAllBytes(Paths.get(path));

Content content =
        Content.fromParts(
                Part.fromText("Tell me about this instrument."),
                Part.fromBytes(imageData, "image/jpeg"));

GenerateContentResponse response = client.models.generateContent("gemini-2.0-flash", content, null);

System.out.println(response.text());

เสียง

Python

from google import genai

client = genai.Client()
sample_audio = client.files.upload(file=media / "sample.mp3")
response = client.models.generate_content(
    model="gemini-2.0-flash",
    contents=["Give me a summary of this audio file.", sample_audio],
)
print(response.text)

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 audio = await ai.files.upload({
  file: path.join(media, "sample.mp3"),
});

const response = await ai.models.generateContent({
  model: "gemini-2.0-flash",
  contents: [
    createUserContent([
      "Give me a summary of this audio file.",
      createPartFromUri(audio.uri, audio.mimeType),
    ]),
  ],
});
console.log(response.text);

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)
}

file, err := client.Files.UploadFromPath(
	ctx, 
	filepath.Join(getMedia(), "sample.mp3"), 
	&genai.UploadFileConfig{
		MIMEType : "audio/mpeg",
	},
)
if err != nil {
	log.Fatal(err)
}

parts := []*genai.Part{
	genai.NewPartFromText("Give me a summary of this audio file."),
	genai.NewPartFromURI(file.URI, file.MIMEType),
}

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)
}
printResponse(response)

เปลือกหอย

# Use File API to upload audio data to API request.
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

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": "Please describe this file."},
          {"file_data":{"mime_type": "audio/mpeg", "file_uri": '$file_uri'}}]
        }]
       }' 2> /dev/null > response.json

cat response.json
echo

jq ".candidates[].content.parts[].text" response.json

วิดีโอ

Python

from google import genai
import time

client = genai.Client()
# Video clip (CC BY 3.0) from https://peach.blender.org/download/
myfile = client.files.upload(file=media / "Big_Buck_Bunny.mp4")
print(f"{myfile=}")

# Poll until the video file is completely processed (state becomes ACTIVE).
while not myfile.state or myfile.state.name != "ACTIVE":
    print("Processing video...")
    print("File state:", myfile.state)
    time.sleep(5)
    myfile = client.files.get(name=myfile.name)

response = client.models.generate_content(
    model="gemini-2.0-flash", contents=[myfile, "Describe this video clip"]
)
print(f"{response.text=}")

Node.js

// Make sure to include the following import:
// import {GoogleGenAI} from '@google/genai';
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });

let video = await ai.files.upload({
  file: path.join(media, 'Big_Buck_Bunny.mp4'),
});

// Poll until the video file is completely processed (state becomes ACTIVE).
while (!video.state || video.state.toString() !== 'ACTIVE') {
  console.log('Processing video...');
  console.log('File state: ', video.state);
  await sleep(5000);
  video = await ai.files.get({name: video.name});
}

const response = await ai.models.generateContent({
  model: "gemini-2.0-flash",
  contents: [
    createUserContent([
      "Describe this video clip",
      createPartFromUri(video.uri, video.mimeType),
    ]),
  ],
});
console.log(response.text);

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)
}

file, err := client.Files.UploadFromPath(
	ctx, 
	filepath.Join(getMedia(), "Big_Buck_Bunny.mp4"), 
	&genai.UploadFileConfig{
		MIMEType : "video/mp4",
	},
)
if err != nil {
	log.Fatal(err)
}

// Poll until the video file is completely processed (state becomes ACTIVE).
for file.State == genai.FileStateUnspecified || file.State != genai.FileStateActive {
	fmt.Println("Processing video...")
	fmt.Println("File state:", file.State)
	time.Sleep(5 * time.Second)

	file, err = client.Files.Get(ctx, file.Name, nil)
	if err != nil {
		log.Fatal(err)
	}
}

parts := []*genai.Part{
	genai.NewPartFromText("Describe this video clip"),
	genai.NewPartFromURI(file.URI, file.MIMEType),
}

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)
}
printResponse(response)

เปลือกหอย

# Use File API to upload audio data to API request.
MIME_TYPE=$(file -b --mime-type "${VIDEO_PATH}")
NUM_BYTES=$(wc -c < "${VIDEO_PATH}")
DISPLAY_NAME=VIDEO

# 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 "${tmp_header_file}" \
  -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 "@${VIDEO_PATH}" 2> /dev/null > file_info.json

file_uri=$(jq ".file.uri" file_info.json)
echo file_uri=$file_uri

state=$(jq ".file.state" file_info.json)
echo state=$state

name=$(jq ".file.name" file_info.json)
echo name=$name

while [[ "($state)" = *"PROCESSING"* ]];
do
  echo "Processing video..."
  sleep 5
  # Get the file of interest to check state
  curl https://generativelanguage.googleapis.com/v1beta/files/$name > file_info.json
  state=$(jq ".file.state" file_info.json)
done

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": "Transcribe the audio from this video, giving timestamps for salient events in the video. Also provide visual descriptions."},
          {"file_data":{"mime_type": "video/mp4", "file_uri": '$file_uri'}}]
        }]
       }' 2> /dev/null > response.json

cat response.json
echo

jq ".candidates[].content.parts[].text" response.json

PDF

Python

from google import genai

client = genai.Client()
sample_pdf = client.files.upload(file=media / "test.pdf")
response = client.models.generate_content(
    model="gemini-2.0-flash",
    contents=["Give me a summary of this document:", sample_pdf],
)
print(f"{response.text=}")

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)
}

file, err := client.Files.UploadFromPath(
	ctx, 
	filepath.Join(getMedia(), "test.pdf"), 
	&genai.UploadFileConfig{
		MIMEType : "application/pdf",
	},
)
if err != nil {
	log.Fatal(err)
}

parts := []*genai.Part{
	genai.NewPartFromText("Give me a summary of this document:"),
	genai.NewPartFromURI(file.URI, file.MIMEType),
}

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)
}
printResponse(response)

เปลือกหอย

MIME_TYPE=$(file -b --mime-type "${PDF_PATH}")
NUM_BYTES=$(wc -c < "${PDF_PATH}")
DISPLAY_NAME=TEXT


echo $MIME_TYPE
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 "@${PDF_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-2.0-flash:generateContent?key=$GEMINI_API_KEY" \
    -H 'Content-Type: application/json' \
    -X POST \
    -d '{
      "contents": [{
        "parts":[
          {"text": "Can you add a few more lines to this poem?"},
          {"file_data":{"mime_type": "application/pdf", "file_uri": '$file_uri'}}]
        }]
       }' 2> /dev/null > response.json

cat response.json
echo

jq ".candidates[].content.parts[].text" response.json

แชท

Python

from google import genai
from google.genai import types

client = genai.Client()
# Pass initial history using the "history" argument
chat = client.chats.create(
    model="gemini-2.0-flash",
    history=[
        types.Content(role="user", parts=[types.Part(text="Hello")]),
        types.Content(
            role="model",
            parts=[
                types.Part(
                    text="Great to meet you. What would you like to know?"
                )
            ],
        ),
    ],
)
response = chat.send_message(message="I have 2 dogs in my house.")
print(response.text)
response = chat.send_message(message="How many paws are in my house?")
print(response.text)

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 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);

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)
}

// Pass initial history using the History field.
history := []*genai.Content{
	genai.NewContentFromText("Hello", genai.RoleUser),
	genai.NewContentFromText("Great to meet you. What would you like to know?", genai.RoleModel),
}

chat, err := client.Chats.Create(ctx, "gemini-2.0-flash", nil, history)
if err != nil {
	log.Fatal(err)
}

firstResp, err := chat.SendMessage(ctx, genai.Part{Text: "I have 2 dogs in my house."})
if err != nil {
	log.Fatal(err)
}
fmt.Println(firstResp.Text())

secondResp, err := chat.SendMessage(ctx, genai.Part{Text: "How many paws are in my house?"})
if err != nil {
	log.Fatal(err)
}
fmt.Println(secondResp.Text())

เปลือกหอย

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?"}]},
      ]
    }' 2> /dev/null | grep "text"

Java

Client client = new Client();

Content userContent = Content.fromParts(Part.fromText("Hello"));
Content modelContent =
        Content.builder()
                .role("model")
                .parts(
                        Collections.singletonList(
                                Part.fromText("Great to meet you. What would you like to know?")
                        )
                ).build();

Chat chat = client.chats.create(
        "gemini-2.0-flash",
        GenerateContentConfig.builder()
                .systemInstruction(userContent)
                .systemInstruction(modelContent)
                .build()
);

GenerateContentResponse response1 = chat.sendMessage("I have 2 dogs in my house.");
System.out.println(response1.text());

GenerateContentResponse response2 = chat.sendMessage("How many paws are in my house?");
System.out.println(response2.text());

แคช

Python

from google import genai
from google.genai import types

client = genai.Client()
document = client.files.upload(file=media / "a11.txt")
model_name = "gemini-1.5-flash-001"

cache = client.caches.create(
    model=model_name,
    config=types.CreateCachedContentConfig(
        contents=[document],
        system_instruction="You are an expert analyzing transcripts.",
    ),
)
print(cache)

response = client.models.generate_content(
    model=model_name,
    contents="Please summarize this transcript",
    config=types.GenerateContentConfig(cached_content=cache.name),
)
print(response.text)

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 filePath = path.join(media, "a11.txt");
const document = await ai.files.upload({
  file: filePath,
  config: { mimeType: "text/plain" },
});
console.log("Uploaded file name:", document.name);
const modelName = "gemini-1.5-flash-001";

const contents = [
  createUserContent(createPartFromUri(document.uri, document.mimeType)),
];

const cache = await ai.caches.create({
  model: modelName,
  config: {
    contents: contents,
    systemInstruction: "You are an expert analyzing transcripts.",
  },
});
console.log("Cache created:", cache);

const response = await ai.models.generateContent({
  model: modelName,
  contents: "Please summarize this transcript",
  config: { cachedContent: cache.name },
});
console.log("Response text:", response.text);

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)
}

modelName := "gemini-1.5-flash-001"
document, err := client.Files.UploadFromPath(
	ctx, 
	filepath.Join(getMedia(), "a11.txt"), 
	&genai.UploadFileConfig{
		MIMEType : "text/plain",
	},
)
if err != nil {
	log.Fatal(err)
}
parts := []*genai.Part{
	genai.NewPartFromURI(document.URI, document.MIMEType),
}
contents := []*genai.Content{
	genai.NewContentFromParts(parts, genai.RoleUser),
}
cache, err := client.Caches.Create(ctx, modelName, &genai.CreateCachedContentConfig{
	Contents: contents,
	SystemInstruction: genai.NewContentFromText(
		"You are an expert analyzing transcripts.", genai.RoleUser,
	),
})
if err != nil {
	log.Fatal(err)
}
fmt.Println("Cache created:")
fmt.Println(cache)

// Use the cache for generating content.
response, err := client.Models.GenerateContent(
	ctx,
	modelName,
	genai.Text("Please summarize this transcript"),
	&genai.GenerateContentConfig{
		CachedContent: cache.Name,
	},
)
if err != nil {
	log.Fatal(err)
}
printResponse(response)

โมเดลที่ปรับแต่งแล้ว

Python

# With Gemini 2 we're launching a new SDK. See the following doc for details.
# https://ai.google.dev/gemini-api/docs/migrate

โหมด JSON

Python

from google import genai
from google.genai import types
from typing_extensions import TypedDict

class Recipe(TypedDict):
    recipe_name: str
    ingredients: list[str]

client = genai.Client()
result = client.models.generate_content(
    model="gemini-2.0-flash",
    contents="List a few popular cookie recipes.",
    config=types.GenerateContentConfig(
        response_mime_type="application/json", response_schema=list[Recipe]
    ),
)
print(result)

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 response = await ai.models.generateContent({
  model: "gemini-2.0-flash",
  contents: "List a few popular cookie recipes.",
  config: {
    responseMimeType: "application/json",
    responseSchema: {
      type: "array",
      items: {
        type: "object",
        properties: {
          recipeName: { type: "string" },
          ingredients: { type: "array", items: { type: "string" } },
        },
        required: ["recipeName", "ingredients"],
      },
    },
  },
});
console.log(response.text);

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)
}

schema := &genai.Schema{
	Type: genai.TypeArray,
	Items: &genai.Schema{
		Type: genai.TypeObject,
		Properties: map[string]*genai.Schema{
			"recipe_name": {Type: genai.TypeString},
			"ingredients": {
				Type:  genai.TypeArray,
				Items: &genai.Schema{Type: genai.TypeString},
			},
		},
		Required: []string{"recipe_name"},
	},
}

config := &genai.GenerateContentConfig{
	ResponseMIMEType: "application/json",
	ResponseSchema:   schema,
}

response, err := client.Models.GenerateContent(
	ctx,
	"gemini-2.0-flash",
	genai.Text("List a few popular cookie recipes."),
	config,
)
if err != nil {
	log.Fatal(err)
}
printResponse(response)

เปลือกหอย

curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=$GEMINI_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

Java

Client client = new Client();

Schema recipeSchema = Schema.builder()
        .type(Array.class.getSimpleName())
        .items(Schema.builder()
                .type(Object.class.getSimpleName())
                .properties(
                        Map.of("recipe_name", Schema.builder()
                                        .type(String.class.getSimpleName())
                                        .build(),
                                "ingredients", Schema.builder()
                                        .type(Array.class.getSimpleName())
                                        .items(Schema.builder()
                                                .type(String.class.getSimpleName())
                                                .build())
                                        .build())
                )
                .required(List.of("recipe_name", "ingredients"))
                .build())
        .build();

GenerateContentConfig config =
        GenerateContentConfig.builder()
                .responseMimeType("application/json")
                .responseSchema(recipeSchema)
                .build();

GenerateContentResponse response =
        client.models.generateContent(
                "gemini-2.0-flash",
                "List a few popular cookie recipes.",
                config);

System.out.println(response.text());

การรันโค้ด

Python

from google import genai
from google.genai import types

client = genai.Client()
response = client.models.generate_content(
    model="gemini-2.0-pro-exp-02-05",
    contents=(
        "Write and execute code that calculates the sum of the first 50 prime numbers. "
        "Ensure that only the executable code and its resulting output are generated."
    ),
)
# Each part may contain text, executable code, or an execution result.
for part in response.candidates[0].content.parts:
    print(part, "\n")

print("-" * 80)
# The .text accessor concatenates the parts into a markdown-formatted text.
print("\n", response.text)

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)
}

response, err := client.Models.GenerateContent(
	ctx,
	"gemini-2.0-pro-exp-02-05",
	genai.Text(
		`Write and execute code that calculates the sum of the first 50 prime numbers.
		 Ensure that only the executable code and its resulting output are generated.`,
	),
	&genai.GenerateContentConfig{},
)
if err != nil {
	log.Fatal(err)
}

// Print the response.
printResponse(response)

fmt.Println("--------------------------------------------------------------------------------")
fmt.Println(response.Text())

Java

Client client = new Client();

String prompt = """
        Write and execute code that calculates the sum of the first 50 prime numbers.
        Ensure that only the executable code and its resulting output are generated.
        """;

GenerateContentResponse response =
        client.models.generateContent(
                "gemini-2.0-pro-exp-02-05",
                prompt,
                null);

for (Part part : response.candidates().get().getFirst().content().get().parts().get()) {
    System.out.println(part + "\n");
}

System.out.println("-".repeat(80));
System.out.println(response.text());

การเรียกใช้ฟังก์ชัน

Python

from google import genai
from google.genai import types

client = genai.Client()

def add(a: float, b: float) -> float:
    """returns a + b."""
    return a + b

def subtract(a: float, b: float) -> float:
    """returns a - b."""
    return a - b

def multiply(a: float, b: float) -> float:
    """returns a * b."""
    return a * b

def divide(a: float, b: float) -> float:
    """returns a / b."""
    return a / b

# Create a chat session; function calling (via tools) is enabled in the config.
chat = client.chats.create(
    model="gemini-2.0-flash",
    config=types.GenerateContentConfig(tools=[add, subtract, multiply, divide]),
)
response = chat.send_message(
    message="I have 57 cats, each owns 44 mittens, how many mittens is that in total?"
)
print(response.text)

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)
}
modelName := "gemini-2.0-flash"

// Create the function declarations for arithmetic operations.
addDeclaration := createArithmeticToolDeclaration("addNumbers", "Return the result of adding two numbers.")
subtractDeclaration := createArithmeticToolDeclaration("subtractNumbers", "Return the result of subtracting the second number from the first.")
multiplyDeclaration := createArithmeticToolDeclaration("multiplyNumbers", "Return the product of two numbers.")
divideDeclaration := createArithmeticToolDeclaration("divideNumbers", "Return the quotient of dividing the first number by the second.")

// Group the function declarations as a tool.
tools := []*genai.Tool{
	{
		FunctionDeclarations: []*genai.FunctionDeclaration{
			addDeclaration,
			subtractDeclaration,
			multiplyDeclaration,
			divideDeclaration,
		},
	},
}

// Create the content prompt.
contents := []*genai.Content{
	genai.NewContentFromText(
		"I have 57 cats, each owns 44 mittens, how many mittens is that in total?", genai.RoleUser,
	),
}

// Set up the generate content configuration with function calling enabled.
config := &genai.GenerateContentConfig{
	Tools: tools,
	ToolConfig: &genai.ToolConfig{
		FunctionCallingConfig: &genai.FunctionCallingConfig{
			// The mode equivalent to FunctionCallingConfigMode.ANY in JS.
			Mode: genai.FunctionCallingConfigModeAny,
		},
	},
}

genContentResp, err := client.Models.GenerateContent(ctx, modelName, contents, config)
if err != nil {
	log.Fatal(err)
}

// Assume the response includes a list of function calls.
if len(genContentResp.FunctionCalls()) == 0 {
	log.Println("No function call returned from the AI.")
	return nil
}
functionCall := genContentResp.FunctionCalls()[0]
log.Printf("Function call: %+v\n", functionCall)

// Marshal the Args map into JSON bytes.
argsMap, err := json.Marshal(functionCall.Args)
if err != nil {
	log.Fatal(err)
}

// Unmarshal the JSON bytes into the ArithmeticArgs struct.
var args ArithmeticArgs
if err := json.Unmarshal(argsMap, &args); err != nil {
	log.Fatal(err)
}

// Map the function name to the actual arithmetic function.
var result float64
switch functionCall.Name {
	case "addNumbers":
		result = add(args.FirstParam, args.SecondParam)
	case "subtractNumbers":
		result = subtract(args.FirstParam, args.SecondParam)
	case "multiplyNumbers":
		result = multiply(args.FirstParam, args.SecondParam)
	case "divideNumbers":
		result = divide(args.FirstParam, args.SecondParam)
	default:
		return fmt.Errorf("unimplemented function: %s", functionCall.Name)
}
log.Printf("Function result: %v\n", result)

// Prepare the final result message as content.
resultContents := []*genai.Content{
	genai.NewContentFromText("The final result is " + fmt.Sprintf("%v", result), genai.RoleUser),
}

// Use GenerateContent to send the final result.
finalResponse, err := client.Models.GenerateContent(ctx, modelName, resultContents, &genai.GenerateContentConfig{})
if err != nil {
	log.Fatal(err)
}

printResponse(finalResponse)

Node.js

  // Make sure to include the following import:
  // import {GoogleGenAI} from '@google/genai';
  const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });

  /**
   * The add function returns the sum of two numbers.
   * @param {number} a
   * @param {number} b
   * @returns {number}
   */
  function add(a, b) {
    return a + b;
  }

  /**
   * The subtract function returns the difference (a - b).
   * @param {number} a
   * @param {number} b
   * @returns {number}
   */
  function subtract(a, b) {
    return a - b;
  }

  /**
   * The multiply function returns the product of two numbers.
   * @param {number} a
   * @param {number} b
   * @returns {number}
   */
  function multiply(a, b) {
    return a * b;
  }

  /**
   * The divide function returns the quotient of a divided by b.
   * @param {number} a
   * @param {number} b
   * @returns {number}
   */
  function divide(a, b) {
    return a / b;
  }

  const addDeclaration = {
    name: "addNumbers",
    parameters: {
      type: "object",
      description: "Return the result of adding two numbers.",
      properties: {
        firstParam: {
          type: "number",
          description:
            "The first parameter which can be an integer or a floating point number.",
        },
        secondParam: {
          type: "number",
          description:
            "The second parameter which can be an integer or a floating point number.",
        },
      },
      required: ["firstParam", "secondParam"],
    },
  };

  const subtractDeclaration = {
    name: "subtractNumbers",
    parameters: {
      type: "object",
      description:
        "Return the result of subtracting the second number from the first.",
      properties: {
        firstParam: {
          type: "number",
          description: "The first parameter.",
        },
        secondParam: {
          type: "number",
          description: "The second parameter.",
        },
      },
      required: ["firstParam", "secondParam"],
    },
  };

  const multiplyDeclaration = {
    name: "multiplyNumbers",
    parameters: {
      type: "object",
      description: "Return the product of two numbers.",
      properties: {
        firstParam: {
          type: "number",
          description: "The first parameter.",
        },
        secondParam: {
          type: "number",
          description: "The second parameter.",
        },
      },
      required: ["firstParam", "secondParam"],
    },
  };

  const divideDeclaration = {
    name: "divideNumbers",
    parameters: {
      type: "object",
      description:
        "Return the quotient of dividing the first number by the second.",
      properties: {
        firstParam: {
          type: "number",
          description: "The first parameter.",
        },
        secondParam: {
          type: "number",
          description: "The second parameter.",
        },
      },
      required: ["firstParam", "secondParam"],
    },
  };

  // Step 1: Call generateContent with function calling enabled.
  const generateContentResponse = await ai.models.generateContent({
    model: "gemini-2.0-flash",
    contents:
      "I have 57 cats, each owns 44 mittens, how many mittens is that in total?",
    config: {
      toolConfig: {
        functionCallingConfig: {
          mode: FunctionCallingConfigMode.ANY,
        },
      },
      tools: [
        {
          functionDeclarations: [
            addDeclaration,
            subtractDeclaration,
            multiplyDeclaration,
            divideDeclaration,
          ],
        },
      ],
    },
  });

  // Step 2: Extract the function call.(
  // Assuming the response contains a 'functionCalls' array.
  const functionCall =
    generateContentResponse.functionCalls &&
    generateContentResponse.functionCalls[0];
  console.log(functionCall);

  // Parse the arguments.
  const args = functionCall.args;
  // Expected args format: { firstParam: number, secondParam: number }

  // Step 3: Invoke the actual function based on the function name.
  const functionMapping = {
    addNumbers: add,
    subtractNumbers: subtract,
    multiplyNumbers: multiply,
    divideNumbers: divide,
  };
  const func = functionMapping[functionCall.name];
  if (!func) {
    console.error("Unimplemented error:", functionCall.name);
    return generateContentResponse;
  }
  const resultValue = func(args.firstParam, args.secondParam);
  console.log("Function result:", resultValue);

  // Step 4: Use the chat API to send the result as the final answer.
  const chat = ai.chats.create({ model: "gemini-2.0-flash" });
  const chatResponse = await chat.sendMessage({
    message: "The final result is " + resultValue,
  });
  console.log(chatResponse.text);
  return chatResponse;
}

เปลือกหอย


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://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=$GEMINI_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'

Java

Client client = new Client();

FunctionDeclaration addFunction =
        FunctionDeclaration.builder()
                .name("addNumbers")
                .parameters(
                        Schema.builder()
                                .type("object")
                                .properties(Map.of(
                                        "firstParam", Schema.builder().type("number").description("First number").build(),
                                        "secondParam", Schema.builder().type("number").description("Second number").build()))
                                .required(Arrays.asList("firstParam", "secondParam"))
                                .build())
                .build();

FunctionDeclaration subtractFunction =
        FunctionDeclaration.builder()
                .name("subtractNumbers")
                .parameters(
                        Schema.builder()
                                .type("object")
                                .properties(Map.of(
                                        "firstParam", Schema.builder().type("number").description("First number").build(),
                                        "secondParam", Schema.builder().type("number").description("Second number").build()))
                                .required(Arrays.asList("firstParam", "secondParam"))
                                .build())
                .build();

FunctionDeclaration multiplyFunction =
        FunctionDeclaration.builder()
                .name("multiplyNumbers")
                .parameters(
                        Schema.builder()
                                .type("object")
                                .properties(Map.of(
                                        "firstParam", Schema.builder().type("number").description("First number").build(),
                                        "secondParam", Schema.builder().type("number").description("Second number").build()))
                                .required(Arrays.asList("firstParam", "secondParam"))
                                .build())
                .build();

FunctionDeclaration divideFunction =
        FunctionDeclaration.builder()
                .name("divideNumbers")
                .parameters(
                        Schema.builder()
                                .type("object")
                                .properties(Map.of(
                                        "firstParam", Schema.builder().type("number").description("First number").build(),
                                        "secondParam", Schema.builder().type("number").description("Second number").build()))
                                .required(Arrays.asList("firstParam", "secondParam"))
                                .build())
                .build();

GenerateContentConfig config = GenerateContentConfig.builder()
        .toolConfig(ToolConfig.builder().functionCallingConfig(
                FunctionCallingConfig.builder().mode("ANY").build()
        ).build())
        .tools(
                Collections.singletonList(
                        Tool.builder().functionDeclarations(
                                Arrays.asList(
                                        addFunction,
                                        subtractFunction,
                                        divideFunction,
                                        multiplyFunction
                                )
                        ).build()

                )
        )
        .build();

GenerateContentResponse response =
        client.models.generateContent(
                "gemini-2.0-flash",
                "I have 57 cats, each owns 44 mittens, how many mittens is that in total?",
                config);


if (response.functionCalls() == null || response.functionCalls().isEmpty()) {
    System.err.println("No function call received");
    return null;
}

var functionCall = response.functionCalls().getFirst();
String functionName = functionCall.name().get();
var arguments = functionCall.args();

Map<String, BiFunction<Double, Double, Double>> functionMapping = new HashMap<>();
functionMapping.put("addNumbers", (a, b) -> a + b);
functionMapping.put("subtractNumbers", (a, b) -> a - b);
functionMapping.put("multiplyNumbers", (a, b) -> a * b);
functionMapping.put("divideNumbers", (a, b) -> b != 0 ? a / b : Double.NaN);

BiFunction<Double, Double, Double> function = functionMapping.get(functionName);

Number firstParam = (Number) arguments.get().get("firstParam");
Number secondParam = (Number) arguments.get().get("secondParam");
Double result = function.apply(firstParam.doubleValue(), secondParam.doubleValue());

System.out.println(result);

การกำหนดค่าการสร้าง

Python

from google import genai
from google.genai import types

client = genai.Client()
response = client.models.generate_content(
    model="gemini-2.0-flash",
    contents="Tell me a story about a magic backpack.",
    config=types.GenerateContentConfig(
        candidate_count=1,
        stop_sequences=["x"],
        max_output_tokens=20,
        temperature=1.0,
    ),
)
print(response.text)

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 response = await ai.models.generateContent({
  model: "gemini-2.0-flash",
  contents: "Tell me a story about a magic backpack.",
  config: {
    candidateCount: 1,
    stopSequences: ["x"],
    maxOutputTokens: 20,
    temperature: 1.0,
  },
});

console.log(response.text);

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)
}

// Create local variables for parameters.
candidateCount := int32(1)
maxOutputTokens := int32(20)
temperature := float32(1.0)

response, err := client.Models.GenerateContent(
	ctx,
	"gemini-2.0-flash",
	genai.Text("Tell me a story about a magic backpack."),
	&genai.GenerateContentConfig{
		CandidateCount:  candidateCount,
		StopSequences:   []string{"x"},
		MaxOutputTokens: maxOutputTokens,
		Temperature:     &temperature,
	},
)
if err != nil {
	log.Fatal(err)
}

printResponse(response)

เปลือกหอย

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
        }
    }'  2> /dev/null | grep "text"

Java

Client client = new Client();

GenerateContentConfig config =
        GenerateContentConfig.builder()
                .candidateCount(1)
                .stopSequences(List.of("x"))
                .maxOutputTokens(20)
                .temperature(1.0F)
                .build();

GenerateContentResponse response =
        client.models.generateContent(
                "gemini-2.0-flash",
                "Tell me a story about a magic backpack.",
                config);

System.out.println(response.text());

การตั้งค่าความปลอดภัย

Python

from google import genai
from google.genai import types

client = genai.Client()
unsafe_prompt = (
    "I support Martians Soccer Club and I think Jupiterians Football Club sucks! "
    "Write a ironic phrase about them including expletives."
)
response = client.models.generate_content(
    model="gemini-2.0-flash",
    contents=unsafe_prompt,
    config=types.GenerateContentConfig(
        safety_settings=[
            types.SafetySetting(
                category="HARM_CATEGORY_HATE_SPEECH",
                threshold="BLOCK_MEDIUM_AND_ABOVE",
            ),
            types.SafetySetting(
                category="HARM_CATEGORY_HARASSMENT", threshold="BLOCK_ONLY_HIGH"
            ),
        ]
    ),
)
try:
    print(response.text)
except Exception:
    print("No information generated by the model.")

print(response.candidates[0].safety_ratings)

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 unsafePrompt =
    "I support Martians Soccer Club and I think Jupiterians Football Club sucks! Write a ironic phrase about them including expletives.";

  const response = await ai.models.generateContent({
    model: "gemini-2.0-flash",
    contents: unsafePrompt,
    config: {
      safetySettings: [
        {
          category: "HARM_CATEGORY_HATE_SPEECH",
          threshold: "BLOCK_MEDIUM_AND_ABOVE",
        },
        {
          category: "HARM_CATEGORY_HARASSMENT",
          threshold: "BLOCK_ONLY_HIGH",
        },
      ],
    },
  });

  try {
    console.log("Generated text:", response.text);
  } catch (error) {
    console.log("No information generated by the model.");
  }
  console.log("Safety ratings:", response.candidates[0].safetyRatings);
  return response;
}

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)
}

unsafePrompt := "I support Martians Soccer Club and I think Jupiterians Football Club sucks! " +
	"Write a ironic phrase about them including expletives."

config := &genai.GenerateContentConfig{
	SafetySettings: []*genai.SafetySetting{
		{
			Category:  "HARM_CATEGORY_HATE_SPEECH",
			Threshold: "BLOCK_MEDIUM_AND_ABOVE",
		},
		{
			Category:  "HARM_CATEGORY_HARASSMENT",
			Threshold: "BLOCK_ONLY_HIGH",
		},
	},
}
contents := []*genai.Content{
	genai.NewContentFromText(unsafePrompt, genai.RoleUser),
}
response, err := client.Models.GenerateContent(ctx, "gemini-2.0-flash", contents, config)
if err != nil {
	log.Fatal(err)
}

// Print the generated text.
text := response.Text()
fmt.Println("Generated text:", text)

// Print the and safety ratings from the first candidate.
if len(response.Candidates) > 0 {
	fmt.Println("Finish reason:", response.Candidates[0].FinishReason)
	safetyRatings, err := json.MarshalIndent(response.Candidates[0].SafetyRatings, "", "  ")
	if err != nil {
		return err
	}
	fmt.Println("Safety ratings:", string(safetyRatings))
} else {
	fmt.Println("No candidate returned.")
}

เปลือกหอย

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://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=$GEMINI_API_KEY" \
    -H 'Content-Type: application/json' \
    -X POST \
    -d @request.json 2> /dev/null

Java

Client client = new Client();

String unsafePrompt = """
         I support Martians Soccer Club and I think Jupiterians Football Club sucks!
         Write a ironic phrase about them including expletives.
        """;

GenerateContentConfig config =
        GenerateContentConfig.builder()
                .safetySettings(Arrays.asList(
                        SafetySetting.builder()
                                .category("HARM_CATEGORY_HATE_SPEECH")
                                .threshold("BLOCK_MEDIUM_AND_ABOVE")
                                .build(),
                        SafetySetting.builder()
                                .category("HARM_CATEGORY_HARASSMENT")
                                .threshold("BLOCK_ONLY_HIGH")
                                .build()
                )).build();

GenerateContentResponse response =
        client.models.generateContent(
                "gemini-2.0-flash",
                unsafePrompt,
                config);

try {
    System.out.println(response.text());
} catch (Exception e) {
    System.out.println("No information generated by the model");
}

System.out.println(response.candidates().get().getFirst().safetyRatings());

คำสั่งของระบบ

Python

from google import genai
from google.genai import types

client = genai.Client()
response = client.models.generate_content(
    model="gemini-2.0-flash",
    contents="Good morning! How are you?",
    config=types.GenerateContentConfig(
        system_instruction="You are a cat. Your name is Neko."
    ),
)
print(response.text)

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 response = await ai.models.generateContent({
  model: "gemini-2.0-flash",
  contents: "Good morning! How are you?",
  config: {
    systemInstruction: "You are a cat. Your name is Neko.",
  },
});
console.log(response.text);

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)
}

// Construct the user message contents.
contents := []*genai.Content{
	genai.NewContentFromText("Good morning! How are you?", genai.RoleUser),
}

// Set the system instruction as a *genai.Content.
config := &genai.GenerateContentConfig{
	SystemInstruction: genai.NewContentFromText("You are a cat. Your name is Neko.", genai.RoleUser),
}

response, err := client.Models.GenerateContent(ctx, "gemini-2.0-flash", contents, config)
if err != nil {
	log.Fatal(err)
}
printResponse(response)

เปลือกหอย

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"}}}'

Java

Client client = new Client();

Part textPart = Part.builder().text("You are a cat. Your name is Neko.").build();

Content content = Content.builder().role("system").parts(ImmutableList.of(textPart)).build();

GenerateContentConfig config = GenerateContentConfig.builder()
        .systemInstruction(content)
        .build();

GenerateContentResponse response =
        client.models.generateContent(
                "gemini-2.0-flash",
                "Good morning! How are you?",
                config);

System.out.println(response.text());

เนื้อหาการตอบกลับ

หากทำสำเร็จ เนื้อหาการตอบกลับจะมีอินสแตนซ์ GenerateContentResponse

เมธอด: models.streamGenerateContent

สร้างการตอบกลับแบบสตรีมจากโมเดลเมื่อได้รับอินพุต GenerateContentRequest

ปลายทาง

post https://generativelanguage.googleapis.com/v1beta/{model=models/*}:streamGenerateContent

พารามิเตอร์เส้นทาง

model string

ต้องระบุ ชื่อของ Model ที่จะใช้ในการสร้างข้อความเติม

รูปแบบ: models/{model} ซึ่งจะอยู่ในรูปแบบ models/{model}

เนื้อความของคำขอ

เนื้อหาของคำขอมีข้อมูลซึ่งมีโครงสร้างดังต่อไปนี้

ฟิลด์
contents[] object (Content)

ต้องระบุ เนื้อหาของการสนทนาปัจจุบันกับโมเดล

สำหรับคำค้นหาแบบเลี้ยวเดียว นี่คืออินสแตนซ์เดียว สำหรับคำค้นหาแบบหลายรอบ เช่น แชท นี่คือฟิลด์ที่ทำซ้ำซึ่งมีประวัติการสนทนาและคำขอที่ล่าสุด

tools[] object (Tool)

ไม่บังคับ รายการของToolsModelที่โมเดลอาจใช้เพื่อสร้างคำตอบถัดไป

Tool คือโค้ดที่ช่วยให้ระบบโต้ตอบกับระบบภายนอกเพื่อดำเนินการหรือชุดการดำเนินการนอกเหนือจากความรู้และขอบเขตของ Model Tool ที่รองรับคือ Function และ codeExecution ดูข้อมูลเพิ่มเติมได้ที่คู่มือการเรียกใช้ฟังก์ชันและการประมวลผลโค้ด

toolConfig object (ToolConfig)

ไม่บังคับ การกำหนดค่าเครื่องมือสำหรับToolที่ระบุในคำขอ ดูตัวอย่างการใช้งานได้ที่คำแนะนำในการเรียกใช้ฟังก์ชัน

safetySettings[] object (SafetySetting)

ไม่บังคับ รายการSafetySettingอินสแตนซ์ที่ไม่ซ้ำกันสำหรับการบล็อกเนื้อหาที่ไม่ปลอดภัย

โดยจะมีผลบังคับใช้ในวันที่ GenerateContentRequest.contents และ GenerateContentResponse.candidates ไม่ควรมีการตั้งค่ามากกว่า 1 รายการสำหรับSafetyCategoryแต่ละประเภท API จะบล็อกเนื้อหาและการตอบกลับที่ไม่เป็นไปตามเกณฑ์ที่กำหนดโดยการตั้งค่าเหล่านี้ รายการนี้จะลบล้างการตั้งค่าเริ่มต้นสำหรับแต่ละรายการที่SafetyCategoryระบุไว้ใน safetySettings หากไม่มี SafetySetting สำหรับ SafetyCategory ที่ระบุในรายการ API จะใช้การตั้งค่าความปลอดภัยเริ่มต้นสำหรับหมวดหมู่นั้น ระบบรองรับหมวดหมู่ที่เป็นอันตราย HARM_CATEGORY_HATE_SPEECH, HARM_CATEGORY_SEXUALLY_EXPLICIT, HARM_CATEGORY_DANGEROUS_CONTENT, HARM_CATEGORY_HARASSMENT, HARM_CATEGORY_CIVIC_INTEGRITY ดูข้อมูลโดยละเอียดเกี่ยวกับการตั้งค่าความปลอดภัยที่มีได้ในคำแนะนำ นอกจากนี้ โปรดดูคำแนะนำด้านความปลอดภัยเพื่อดูวิธีพิจารณาเรื่องความปลอดภัยในแอปพลิเคชัน AI

systemInstruction object (Content)

ไม่บังคับ นักพัฒนาแอปตั้งค่าคำสั่งของระบบ ปัจจุบันมีเฉพาะข้อความ

generationConfig object (GenerationConfig)

ไม่บังคับ ตัวเลือกการกำหนดค่าสำหรับการสร้างโมเดลและเอาต์พุต

cachedContent string

ไม่บังคับ ชื่อของเนื้อหาที่แคชไว้เพื่อใช้เป็นบริบทในการแสดงผลการคาดคะเน รูปแบบ: cachedContents/{cachedContent}

ตัวอย่างคำขอ

ข้อความ

Python

from google import genai

client = genai.Client()
response = client.models.generate_content_stream(
    model="gemini-2.0-flash", contents="Write a story about a magic backpack."
)
for chunk in response:
    print(chunk.text)
    print("_" * 80)

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 response = await ai.models.generateContentStream({
  model: "gemini-2.0-flash",
  contents: "Write a story about a magic backpack.",
});
let text = "";
for await (const chunk of response) {
  console.log(chunk.text);
  text += chunk.text;
}

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)
}
contents := []*genai.Content{
	genai.NewContentFromText("Write a story about a magic backpack.", genai.RoleUser),
}
for response, err := range client.Models.GenerateContentStream(
	ctx,
	"gemini-2.0-flash",
	contents,
	nil,
) {
	if err != nil {
		log.Fatal(err)
	}
	fmt.Print(response.Candidates[0].Content.Parts[0].Text)
}

เปลือกหอย

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": "Write a story about a magic backpack."}]}]}'

Java

Client client = new Client();

ResponseStream<GenerateContentResponse> responseStream =
        client.models.generateContentStream(
                "gemini-2.0-flash",
                "Write a story about a magic backpack.",
                null);

StringBuilder response = new StringBuilder();
for (GenerateContentResponse res : responseStream) {
    System.out.print(res.text());
    response.append(res.text());
}

responseStream.close();

รูปภาพ

Python

from google import genai
import PIL.Image

client = genai.Client()
organ = PIL.Image.open(media / "organ.jpg")
response = client.models.generate_content_stream(
    model="gemini-2.0-flash", contents=["Tell me about this instrument", organ]
)
for chunk in response:
    print(chunk.text)
    print("_" * 80)

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 organ = await ai.files.upload({
  file: path.join(media, "organ.jpg"),
});

const response = await ai.models.generateContentStream({
  model: "gemini-2.0-flash",
  contents: [
    createUserContent([
      "Tell me about this instrument", 
      createPartFromUri(organ.uri, organ.mimeType)
    ]),
  ],
});
let text = "";
for await (const chunk of response) {
  console.log(chunk.text);
  text += chunk.text;
}

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)
}
file, err := client.Files.UploadFromPath(
	ctx, 
	filepath.Join(getMedia(), "organ.jpg"), 
	&genai.UploadFileConfig{
		MIMEType : "image/jpeg",
	},
)
if err != nil {
	log.Fatal(err)
}
parts := []*genai.Part{
	genai.NewPartFromText("Tell me about this instrument"),
	genai.NewPartFromURI(file.URI, file.MIMEType),
}
contents := []*genai.Content{
	genai.NewContentFromParts(parts, genai.RoleUser),
}
for response, err := range client.Models.GenerateContentStream(
	ctx,
	"gemini-2.0-flash",
	contents,
	nil,
) {
	if err != nil {
		log.Fatal(err)
	}
	fmt.Print(response.Candidates[0].Content.Parts[0].Text)
}

เปลือกหอย

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:streamGenerateContent?alt=sse&key=$GEMINI_API_KEY" \
    -H 'Content-Type: application/json' \
    -X POST \
    -d "@$TEMP_JSON" 2> /dev/null

Java

Client client = new Client();

String path = media_path + "organ.jpg";
byte[] imageData = Files.readAllBytes(Paths.get(path));

Content content =
        Content.fromParts(
                Part.fromText("Tell me about this instrument."),
                Part.fromBytes(imageData, "image/jpeg"));


ResponseStream<GenerateContentResponse> responseStream =
        client.models.generateContentStream(
                "gemini-2.0-flash",
                content,
                null);

StringBuilder response = new StringBuilder();
for (GenerateContentResponse res : responseStream) {
    System.out.print(res.text());
    response.append(res.text());
}

responseStream.close();

เสียง

Python

from google import genai

client = genai.Client()
sample_audio = client.files.upload(file=media / "sample.mp3")
response = client.models.generate_content_stream(
    model="gemini-2.0-flash",
    contents=["Give me a summary of this audio file.", sample_audio],
)
for chunk in response:
    print(chunk.text)
    print("_" * 80)

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)
}

file, err := client.Files.UploadFromPath(
	ctx, 
	filepath.Join(getMedia(), "sample.mp3"), 
	&genai.UploadFileConfig{
		MIMEType : "audio/mpeg",
	},
)
if err != nil {
	log.Fatal(err)
}

parts := []*genai.Part{
	genai.NewPartFromText("Give me a summary of this audio file."),
	genai.NewPartFromURI(file.URI, file.MIMEType),
}

contents := []*genai.Content{
	genai.NewContentFromParts(parts, genai.RoleUser),
}

for result, err := range client.Models.GenerateContentStream(
	ctx,
	"gemini-2.0-flash",
	contents,
	nil,
) {
	if err != nil {
		log.Fatal(err)
	}
	fmt.Print(result.Candidates[0].Content.Parts[0].Text)
}

เปลือกหอย

# Use File API to upload audio data to API request.
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

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": [{
        "parts":[
          {"text": "Please describe this file."},
          {"file_data":{"mime_type": "audio/mpeg", "file_uri": '$file_uri'}}]
        }]
       }' 2> /dev/null > response.json

cat response.json
echo

วิดีโอ

Python

from google import genai
import time

client = genai.Client()
# Video clip (CC BY 3.0) from https://peach.blender.org/download/
myfile = client.files.upload(file=media / "Big_Buck_Bunny.mp4")
print(f"{myfile=}")

# Poll until the video file is completely processed (state becomes ACTIVE).
while not myfile.state or myfile.state.name != "ACTIVE":
    print("Processing video...")
    print("File state:", myfile.state)
    time.sleep(5)
    myfile = client.files.get(name=myfile.name)

response = client.models.generate_content_stream(
    model="gemini-2.0-flash", contents=[myfile, "Describe this video clip"]
)
for chunk in response:
    print(chunk.text)
    print("_" * 80)

Node.js

// Make sure to include the following import:
// import {GoogleGenAI} from '@google/genai';
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });

let video = await ai.files.upload({
  file: path.join(media, 'Big_Buck_Bunny.mp4'),
});

// Poll until the video file is completely processed (state becomes ACTIVE).
while (!video.state || video.state.toString() !== 'ACTIVE') {
  console.log('Processing video...');
  console.log('File state: ', video.state);
  await sleep(5000);
  video = await ai.files.get({name: video.name});
}

const response = await ai.models.generateContentStream({
  model: "gemini-2.0-flash",
  contents: [
    createUserContent([
      "Describe this video clip",
      createPartFromUri(video.uri, video.mimeType),
    ]),
  ],
});
let text = "";
for await (const chunk of response) {
  console.log(chunk.text);
  text += chunk.text;
}

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)
}

file, err := client.Files.UploadFromPath(
	ctx, 
	filepath.Join(getMedia(), "Big_Buck_Bunny.mp4"), 
	&genai.UploadFileConfig{
		MIMEType : "video/mp4",
	},
)
if err != nil {
	log.Fatal(err)
}

// Poll until the video file is completely processed (state becomes ACTIVE).
for file.State == genai.FileStateUnspecified || file.State != genai.FileStateActive {
	fmt.Println("Processing video...")
	fmt.Println("File state:", file.State)
	time.Sleep(5 * time.Second)

	file, err = client.Files.Get(ctx, file.Name, nil)
	if err != nil {
		log.Fatal(err)
	}
}

parts := []*genai.Part{
	genai.NewPartFromText("Describe this video clip"),
	genai.NewPartFromURI(file.URI, file.MIMEType),
}

contents := []*genai.Content{
	genai.NewContentFromParts(parts, genai.RoleUser),
}

for result, err := range client.Models.GenerateContentStream(
	ctx,
	"gemini-2.0-flash",
	contents,
	nil,
) {
	if err != nil {
		log.Fatal(err)
	}
	fmt.Print(result.Candidates[0].Content.Parts[0].Text)
}

เปลือกหอย

# Use File API to upload audio data to API request.
MIME_TYPE=$(file -b --mime-type "${VIDEO_PATH}")
NUM_BYTES=$(wc -c < "${VIDEO_PATH}")
DISPLAY_NAME=VIDEO_PATH

# 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 "@${VIDEO_PATH}" 2> /dev/null > file_info.json

file_uri=$(jq ".file.uri" file_info.json)
echo file_uri=$file_uri

state=$(jq ".file.state" file_info.json)
echo state=$state

while [[ "($state)" = *"PROCESSING"* ]];
do
  echo "Processing video..."
  sleep 5
  # Get the file of interest to check state
  curl https://generativelanguage.googleapis.com/v1beta/files/$name > file_info.json
  state=$(jq ".file.state" file_info.json)
done

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": [{
        "parts":[
          {"text": "Please describe this file."},
          {"file_data":{"mime_type": "video/mp4", "file_uri": '$file_uri'}}]
        }]
       }' 2> /dev/null > response.json

cat response.json
echo

PDF

Python

from google import genai

client = genai.Client()
sample_pdf = client.files.upload(file=media / "test.pdf")
response = client.models.generate_content_stream(
    model="gemini-2.0-flash",
    contents=["Give me a summary of this document:", sample_pdf],
)

for chunk in response:
    print(chunk.text)
    print("_" * 80)

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)
}

file, err := client.Files.UploadFromPath(
	ctx, 
	filepath.Join(getMedia(), "test.pdf"), 
	&genai.UploadFileConfig{
		MIMEType : "application/pdf",
	},
)
if err != nil {
	log.Fatal(err)
}

parts := []*genai.Part{
	genai.NewPartFromText("Give me a summary of this document:"),
	genai.NewPartFromURI(file.URI, file.MIMEType),
}

contents := []*genai.Content{
	genai.NewContentFromParts(parts, genai.RoleUser),
}

for result, err := range client.Models.GenerateContentStream(
	ctx,
	"gemini-2.0-flash",
	contents,
	nil,
) {
	if err != nil {
		log.Fatal(err)
	}
	fmt.Print(result.Candidates[0].Content.Parts[0].Text)
}

เปลือกหอย

MIME_TYPE=$(file -b --mime-type "${PDF_PATH}")
NUM_BYTES=$(wc -c < "${PDF_PATH}")
DISPLAY_NAME=TEXT


echo $MIME_TYPE
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 "@${PDF_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-2.0-flash:streamGenerateContent?alt=sse&key=$GEMINI_API_KEY" \
    -H 'Content-Type: application/json' \
    -X POST \
    -d '{
      "contents": [{
        "parts":[
          {"text": "Can you add a few more lines to this poem?"},
          {"file_data":{"mime_type": "application/pdf", "file_uri": '$file_uri'}}]
        }]
       }' 2> /dev/null > response.json

cat response.json
echo

แชท

Python

from google import genai
from google.genai import types

client = genai.Client()
chat = client.chats.create(
    model="gemini-2.0-flash",
    history=[
        types.Content(role="user", parts=[types.Part(text="Hello")]),
        types.Content(
            role="model",
            parts=[
                types.Part(
                    text="Great to meet you. What would you like to know?"
                )
            ],
        ),
    ],
)
response = chat.send_message_stream(message="I have 2 dogs in my house.")
for chunk in response:
    print(chunk.text)
    print("_" * 80)
response = chat.send_message_stream(message="How many paws are in my house?")
for chunk in response:
    print(chunk.text)
    print("_" * 80)

print(chat.get_history())

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 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?" }],
    },
  ],
});

console.log("Streaming response for first message:");
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));
}

console.log("Streaming response for second message:");
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));
}

console.log(chat.getHistory());

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)
}

history := []*genai.Content{
	genai.NewContentFromText("Hello", genai.RoleUser),
	genai.NewContentFromText("Great to meet you. What would you like to know?", genai.RoleModel),
}
chat, err := client.Chats.Create(ctx, "gemini-2.0-flash", nil, history)
if err != nil {
	log.Fatal(err)
}

for chunk, err := range chat.SendMessageStream(ctx, genai.Part{Text: "I have 2 dogs in my house."}) {
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(chunk.Text())
	fmt.Println(strings.Repeat("_", 64))
}

for chunk, err := range chat.SendMessageStream(ctx, genai.Part{Text: "How many paws are in my house?"}) {
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(chunk.Text())
	fmt.Println(strings.Repeat("_", 64))
}

fmt.Println(chat.History(false))

เปลือกหอย

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?"}]},
      ]
    }' 2> /dev/null | grep "text"

เนื้อหาการตอบกลับ

หากทำสำเร็จ เนื้อหาการตอบกลับจะมีสตรีมของอินสแตนซ์ GenerateContentResponse

GenerateContentResponse

คำตอบจากโมเดลที่รองรับคำตอบที่เป็นตัวเลือกหลายรายการ

มีการรายงานการจัดประเภทความปลอดภัยและการกรองเนื้อหาสำหรับทั้งพรอมต์ใน GenerateContentResponse.prompt_feedback และสำหรับแต่ละคำตอบใน finishReason และใน safetyRatings API จะ - แสดงผู้สมัครทั้งหมดที่ขอหรือไม่มีเลย - ไม่แสดงผู้สมัครเลยก็ต่อเมื่อมีบางอย่างผิดปกติกับพรอมต์ (ดู promptFeedback) - รายงานความคิดเห็นเกี่ยวกับผู้สมัครแต่ละรายใน finishReason และ safetyRatings

ฟิลด์
candidates[] object (Candidate)

คำตอบที่เป็นตัวเลือกจากโมเดล

promptFeedback object (PromptFeedback)

แสดงความคิดเห็นของพรอมต์ที่เกี่ยวข้องกับตัวกรองเนื้อหา

usageMetadata object (UsageMetadata)

เอาต์พุตเท่านั้น ข้อมูลเมตาเกี่ยวกับการใช้โทเค็นของคำขอสร้าง

modelVersion string

เอาต์พุตเท่านั้น เวอร์ชันโมเดลที่ใช้สร้างคำตอบ

responseId string

เอาต์พุตเท่านั้น responseId ใช้เพื่อระบุคำตอบแต่ละรายการ

การแสดง JSON
{
  "candidates": [
    {
      object (Candidate)
    }
  ],
  "promptFeedback": {
    object (PromptFeedback)
  },
  "usageMetadata": {
    object (UsageMetadata)
  },
  "modelVersion": string,
  "responseId": string
}

PromptFeedback

ชุดข้อมูลเมตาของความคิดเห็นที่พรอมต์ระบุใน GenerateContentRequest.content

ฟิลด์
blockReason enum (BlockReason)

ไม่บังคับ หากตั้งค่าไว้ ระบบจะบล็อกพรอมต์และไม่แสดงคำตอบ เปลี่ยนคำพูดในพรอมต์

safetyRatings[] object (SafetyRating)

การให้คะแนนความปลอดภัยของพรอมต์ มีการจัดประเภทได้สูงสุด 1 รายการต่อหมวดหมู่

การแสดง JSON
{
  "blockReason": enum (BlockReason),
  "safetyRatings": [
    {
      object (SafetyRating)
    }
  ]
}

BlockReason

ระบุเหตุผลที่ระบบบล็อกพรอมต์

Enum
BLOCK_REASON_UNSPECIFIED ค่าเริ่มต้น ค่านี้ไม่ได้ใช้
SAFETY ระบบบล็อกพรอมต์เนื่องจากเหตุผลด้านความปลอดภัย ตรวจสอบ safetyRatings เพื่อดูว่าหมวดหมู่ความปลอดภัยใดที่บล็อก
OTHER ระบบบล็อกพรอมต์โดยไม่ทราบสาเหตุ
BLOCKLIST ระบบบล็อกพรอมต์เนื่องจากมีคำที่รวมอยู่ในรายการบล็อกคำศัพท์
PROHIBITED_CONTENT พรอมต์ถูกบล็อกเนื่องจากมีเนื้อหาต้องห้าม
IMAGE_SAFETY ผู้สมัครถูกบล็อกเนื่องจากมีเนื้อหาการสร้างรูปภาพที่ไม่ปลอดภัย

UsageMetadata

ข้อมูลเมตาเกี่ยวกับการใช้โทเค็นของคำขอการสร้าง

ฟิลด์
promptTokenCount integer

จำนวนโทเค็นในพรอมต์ เมื่อตั้งค่า cachedContent แล้ว ค่านี้จะยังคงเป็นขนาดพรอมต์ที่มีประสิทธิภาพทั้งหมด ซึ่งหมายความว่าค่านี้รวมถึงจำนวนโทเค็นในเนื้อหาที่แคชไว้

cachedContentTokenCount integer

จำนวนโทเค็นในส่วนที่แคชของพรอมต์ (เนื้อหาที่แคช)

candidatesTokenCount integer

จำนวนโทเค็นทั้งหมดในคำตอบที่สร้างขึ้นทั้งหมด

toolUsePromptTokenCount integer

เอาต์พุตเท่านั้น จำนวนโทเค็นในพรอมต์การใช้เครื่องมือ

thoughtsTokenCount integer

เอาต์พุตเท่านั้น จำนวนโทเค็นของความคิดสำหรับโมเดลการคิด

totalTokenCount integer

จำนวนโทเค็นทั้งหมดสำหรับคำขอการสร้าง (พรอมต์ + ตัวเลือกคำตอบ)

promptTokensDetails[] object (ModalityTokenCount)

เอาต์พุตเท่านั้น รายการรูปแบบที่ประมวลผลในอินพุตคำขอ

cacheTokensDetails[] object (ModalityTokenCount)

เอาต์พุตเท่านั้น รายการรูปแบบของเนื้อหาที่แคชไว้ในอินพุตคำขอ

candidatesTokensDetails[] object (ModalityTokenCount)

เอาต์พุตเท่านั้น รายการรูปแบบที่แสดงในการตอบกลับ

toolUsePromptTokensDetails[] object (ModalityTokenCount)

เอาต์พุตเท่านั้น รายการรูปแบบที่ประมวลผลสำหรับอินพุตคำขอการใช้เครื่องมือ

การแสดง JSON
{
  "promptTokenCount": integer,
  "cachedContentTokenCount": integer,
  "candidatesTokenCount": integer,
  "toolUsePromptTokenCount": integer,
  "thoughtsTokenCount": integer,
  "totalTokenCount": integer,
  "promptTokensDetails": [
    {
      object (ModalityTokenCount)
    }
  ],
  "cacheTokensDetails": [
    {
      object (ModalityTokenCount)
    }
  ],
  "candidatesTokensDetails": [
    {
      object (ModalityTokenCount)
    }
  ],
  "toolUsePromptTokensDetails": [
    {
      object (ModalityTokenCount)
    }
  ]
}

ผู้สมัคร

คำตอบที่เป็นไปได้ซึ่งสร้างขึ้นจากโมเดล

ฟิลด์
content object (Content)

เอาต์พุตเท่านั้น เนื้อหาที่สร้างขึ้นซึ่งโมเดลส่งคืน

finishReason enum (FinishReason)

ไม่บังคับ เอาต์พุตเท่านั้น เหตุผลที่โมเดลหยุดสร้างโทเค็น

หากไม่มีข้อมูล แสดงว่าโมเดลยังไม่ได้หยุดสร้างโทเค็น

safetyRatings[] object (SafetyRating)

รายการคะแนนสำหรับความปลอดภัยของผู้สมัครรับการตอบกลับ

มีการจัดประเภทได้สูงสุด 1 รายการต่อหมวดหมู่

citationMetadata object (CitationMetadata)

เอาต์พุตเท่านั้น ข้อมูลการอ้างอิงสำหรับคำตอบที่โมเดลสร้างขึ้น

ช่องนี้อาจมีข้อมูลการอ่านสำหรับข้อความใดๆ ที่รวมอยู่ใน content โดยเป็นข้อความที่ "ท่อง" มาจากเนื้อหาที่มีลิขสิทธิ์ในข้อมูลการฝึกของ LLM พื้นฐาน

tokenCount integer

เอาต์พุตเท่านั้น จำนวนโทเค็นสำหรับคำตอบนี้

groundingAttributions[] object (GroundingAttribution)

เอาต์พุตเท่านั้น ข้อมูลการระบุแหล่งที่มาสำหรับแหล่งข้อมูลที่มีส่วนร่วมในการตอบคำถามโดยอิงตามข้อเท็จจริง

ระบบจะป้อนข้อมูลในช่องนี้สำหรับการโทร GenerateAnswer

groundingMetadata object (GroundingMetadata)

เอาต์พุตเท่านั้น การอ้างอิงข้อมูลเมตาสำหรับคำตอบที่เป็นไปได้

ระบบจะป้อนข้อมูลในช่องนี้สำหรับการโทร GenerateContent

avgLogprobs number

เอาต์พุตเท่านั้น คะแนนความน่าจะเป็นของบันทึกเฉลี่ยของผู้สมัคร

logprobsResult object (LogprobsResult)

เอาต์พุตเท่านั้น คะแนนความน่าจะเป็นของบันทึกสำหรับโทเค็นการตอบกลับและโทเค็นยอดนิยม

urlContextMetadata object (UrlContextMetadata)

เอาต์พุตเท่านั้น ข้อมูลเมตาที่เกี่ยวข้องกับเครื่องมือดึงข้อมูลบริบท URL

index integer

เอาต์พุตเท่านั้น ดัชนีของผู้สมัครในรายการผู้สมัครรับเลือกตั้งที่ตอบกลับ

การแสดง JSON
{
  "content": {
    object (Content)
  },
  "finishReason": enum (FinishReason),
  "safetyRatings": [
    {
      object (SafetyRating)
    }
  ],
  "citationMetadata": {
    object (CitationMetadata)
  },
  "tokenCount": integer,
  "groundingAttributions": [
    {
      object (GroundingAttribution)
    }
  ],
  "groundingMetadata": {
    object (GroundingMetadata)
  },
  "avgLogprobs": number,
  "logprobsResult": {
    object (LogprobsResult)
  },
  "urlContextMetadata": {
    object (UrlContextMetadata)
  },
  "index": integer
}

FinishReason

กำหนดเหตุผลที่โมเดลหยุดสร้างโทเค็น

Enum
FINISH_REASON_UNSPECIFIED ค่าเริ่มต้น ค่านี้ไม่ได้ใช้
STOP จุดหยุดตามธรรมชาติของโมเดลหรือลำดับการหยุดที่ระบุ
MAX_TOKENS มีโทเค็นถึงจำนวนสูงสุดตามที่ระบุไว้ในคำขอแล้ว
SAFETY เนื้อหาที่เป็นตัวเลือกคำตอบถูกแจ้งว่าไม่ปลอดภัย
RECITATION ระบบแจ้งว่าเนื้อหาของผู้สมัครรับเลือกเป็นผู้ตอบไม่เหมาะสมเนื่องจากเหตุผลด้านการท่องจำ
LANGUAGE ระบบแจ้งว่าเนื้อหาที่ใช้เป็นตัวเลือกคำตอบใช้ภาษาที่ไม่รองรับ
OTHER ไม่ทราบสาเหตุ
BLOCKLIST ระบบหยุดสร้างโทเค็นเนื่องจากเนื้อหามีคำที่ไม่อนุญาต
PROHIBITED_CONTENT ระบบหยุดสร้างโทเค็นเนื่องจากอาจมีเนื้อหาที่ไม่อนุญาต
SPII ระบบหยุดสร้างโทเค็นเนื่องจากเนื้อหาอาจมีข้อมูลส่วนบุคคลที่ละเอียดอ่อนและระบุตัวบุคคลนั้นได้ (SPII)
MALFORMED_FUNCTION_CALL การเรียกใช้ฟังก์ชันที่โมเดลสร้างขึ้นไม่ถูกต้อง
IMAGE_SAFETY ระบบหยุดสร้างโทเค็นเนื่องจากรูปภาพที่สร้างขึ้นมีการละเมิดด้านความปลอดภัย
UNEXPECTED_TOOL_CALL โมเดลสร้างการเรียกใช้เครื่องมือ แต่ไม่ได้เปิดใช้เครื่องมือใดๆ ในคำขอ

GroundingAttribution

การระบุแหล่งที่มาของแหล่งข้อมูลที่ช่วยให้ได้คำตอบ

ฟิลด์
sourceId object (AttributionSourceId)

เอาต์พุตเท่านั้น ตัวระบุแหล่งที่มาที่ทำให้เกิดการระบุแหล่งที่มานี้

content object (Content)

เนื้อหาต้นฉบับที่ประกอบกันเป็นการระบุแหล่งที่มานี้

การแสดง JSON
{
  "sourceId": {
    object (AttributionSourceId)
  },
  "content": {
    object (Content)
  }
}

AttributionSourceId

ตัวระบุแหล่งที่มาที่ทำให้เกิดการระบุแหล่งที่มานี้

ฟิลด์
source Union type
source ต้องเป็นค่าใดค่าหนึ่งต่อไปนี้เท่านั้น
groundingPassage object (GroundingPassageId)

ตัวระบุสำหรับข้อความที่แทรกในบรรทัด

semanticRetrieverChunk object (SemanticRetrieverChunk)

ตัวระบุสำหรับ Chunk ที่ดึงข้อมูลผ่านเครื่องมือดึงข้อมูลเชิงความหมาย

การแสดง JSON
{

  // source
  "groundingPassage": {
    object (GroundingPassageId)
  },
  "semanticRetrieverChunk": {
    object (SemanticRetrieverChunk)
  }
  // Union type
}

GroundingPassageId

ตัวระบุสำหรับชิ้นส่วนภายใน GroundingPassage

ฟิลด์
passageId string

เอาต์พุตเท่านั้น รหัสของข้อความที่ตรงกับGroundingPassage.idของ GenerateAnswerRequest

partIndex integer

เอาต์พุตเท่านั้น ดัชนีของส่วนภายใน GenerateAnswerRequest ของ GroundingPassage.content

การแสดง JSON
{
  "passageId": string,
  "partIndex": integer
}

SemanticRetrieverChunk

ตัวระบุสำหรับ Chunk ที่ดึงข้อมูลผ่าน Semantic Retriever ที่ระบุใน GenerateAnswerRequest โดยใช้ SemanticRetrieverConfig

ฟิลด์
source string

เอาต์พุตเท่านั้น ชื่อของแหล่งที่มาที่ตรงกับ SemanticRetrieverConfig.source ของคำขอ เช่น corpora/123 หรือ corpora/123/documents/abc

chunk string

เอาต์พุตเท่านั้น ชื่อของ Chunk ที่มีข้อความระบุแหล่งที่มา ตัวอย่าง: corpora/123/documents/abc/chunks/xyz

การแสดง JSON
{
  "source": string,
  "chunk": string
}

GroundingMetadata

ข้อมูลเมตาที่แสดงผลต่อไคลเอ็นต์เมื่อเปิดใช้การอ้างอิง

ฟิลด์
groundingChunks[] object (GroundingChunk)

รายการข้อมูลอ้างอิงที่รองรับซึ่งดึงมาจากแหล่งข้อมูลพื้นฐานที่ระบุ

groundingSupports[] object (GroundingSupport)

รายการการสนับสนุนการอ้างอิง

webSearchQueries[] string

คำค้นหาบนเว็บสำหรับการค้นหาบนเว็บที่ตามมา

searchEntryPoint object (SearchEntryPoint)

ไม่บังคับ รายการค้นหาของ Google สำหรับการค้นหาเว็บที่ติดตาม

retrievalMetadata object (RetrievalMetadata)

ข้อมูลเมตาที่เกี่ยวข้องกับการดึงข้อมูลในโฟลว์การอ้างอิง

การแสดง JSON
{
  "groundingChunks": [
    {
      object (GroundingChunk)
    }
  ],
  "groundingSupports": [
    {
      object (GroundingSupport)
    }
  ],
  "webSearchQueries": [
    string
  ],
  "searchEntryPoint": {
    object (SearchEntryPoint)
  },
  "retrievalMetadata": {
    object (RetrievalMetadata)
  }
}

SearchEntryPoint

จุดแรกเข้าของ Google Search

ฟิลด์
renderedContent string

ไม่บังคับ ข้อมูลโค้ดเนื้อหาเว็บที่ฝังในหน้าเว็บหรือ WebView ของแอปได้

sdkBlob string (bytes format)

ไม่บังคับ JSON ที่เข้ารหัส Base64 ซึ่งแสดงอาร์เรย์ของทูเพิล <ข้อความค้นหา, URL การค้นหา>

สตริงที่เข้ารหัส Base64

การแสดง JSON
{
  "renderedContent": string,
  "sdkBlob": string
}

GroundingChunk

Grounding chunk

ฟิลด์
chunk_type Union type
ประเภทก้อน chunk_type ต้องเป็นค่าใดค่าหนึ่งต่อไปนี้เท่านั้น
web object (Web)

ข้อมูลพื้นฐานจากเว็บ

การแสดง JSON
{

  // chunk_type
  "web": {
    object (Web)
  }
  // Union type
}

เว็บ

ตัดเนื้อหาจากเว็บ

ฟิลด์
uri string

การอ้างอิง URI ของก้อน

title string

ชื่อของก้อน

การแสดง JSON
{
  "uri": string,
  "title": string
}

GroundingSupport

การสนับสนุนการอ้างอิง

ฟิลด์
groundingChunkIndices[] integer

รายการดัชนี (ใน "grounding_chunk") ที่ระบุการอ้างอิงที่เชื่อมโยงกับการกล่าวอ้าง เช่น [1,3,4] หมายความว่า grounding_chunk[1], grounding_chunk[3], grounding_chunk[4] คือเนื้อหาที่ดึงมาซึ่งเชื่อมโยงกับคำกล่าวอ้าง

confidenceScores[] number

คะแนนความเชื่อมั่นของข้อมูลอ้างอิงที่ใช้ในการสนับสนุน ค่าอยู่ในช่วง 0 ถึง 1 1 คือมั่นใจมากที่สุด รายการนี้ต้องมีขนาดเท่ากับ groundingChunkIndices

segment object (Segment)

ส่วนของเนื้อหาที่การสนับสนุนนี้เกี่ยวข้อง

การแสดง JSON
{
  "groundingChunkIndices": [
    integer
  ],
  "confidenceScores": [
    number
  ],
  "segment": {
    object (Segment)
  }
}

กลุ่ม

ส่วนของเนื้อหา

ฟิลด์
partIndex integer

เอาต์พุตเท่านั้น ดัชนีของออบเจ็กต์ Part ภายในออบเจ็กต์ Content หลัก

startIndex integer

เอาต์พุตเท่านั้น ดัชนีเริ่มต้นในพาร์ทที่ระบุ ซึ่งวัดเป็นไบต์ ออฟเซ็ตจากจุดเริ่มต้นของพาร์ท โดยรวมจุดเริ่มต้นด้วย และเริ่มที่ 0

endIndex integer

เอาต์พุตเท่านั้น ดัชนีสิ้นสุดในส่วนที่ระบุ ซึ่งวัดเป็นไบต์ ออฟเซ็ตจากจุดเริ่มต้นของพาร์ท โดยไม่รวมจุดเริ่มต้น และเริ่มต้นที่ 0

text string

เอาต์พุตเท่านั้น ข้อความที่สอดคล้องกับส่วนจากคำตอบ

การแสดง JSON
{
  "partIndex": integer,
  "startIndex": integer,
  "endIndex": integer,
  "text": string
}

RetrievalMetadata

ข้อมูลเมตาที่เกี่ยวข้องกับการดึงข้อมูลในโฟลว์การอ้างอิง

ฟิลด์
googleSearchDynamicRetrievalScore number

ไม่บังคับ คะแนนที่บ่งบอกว่าข้อมูลจาก Google Search มีแนวโน้มที่จะช่วยตอบพรอมต์ได้มากน้อยเพียงใด คะแนนอยู่ในช่วง [0, 1] โดยที่ 0 คือความเป็นไปได้น้อยที่สุด และ 1 คือความเป็นไปได้มากที่สุด ระบบจะสร้างคะแนนนี้เมื่อเปิดใช้การอ้างอิงจาก Google Search และการดึงข้อมูลแบบไดนามิกเท่านั้น ระบบจะเปรียบเทียบกับเกณฑ์เพื่อพิจารณาว่าจะเรียกใช้ Google Search หรือไม่

การแสดง JSON
{
  "googleSearchDynamicRetrievalScore": number
}

LogprobsResult

ผลลัพธ์ Logprobs

ฟิลด์
topCandidates[] object (TopCandidates)

ความยาว = จำนวนขั้นตอนการถอดรหัสทั้งหมด

chosenCandidates[] object (Candidate)

ความยาว = จำนวนขั้นตอนการถอดรหัสทั้งหมด ผู้สมัครที่เลือกอาจอยู่ใน topCandidates หรือไม่อยู่ก็ได้

การแสดง JSON
{
  "topCandidates": [
    {
      object (TopCandidates)
    }
  ],
  "chosenCandidates": [
    {
      object (Candidate)
    }
  ]
}

TopCandidates

ผู้สมัครที่มีความน่าจะเป็นของบันทึกสูงสุดในแต่ละขั้นตอนการถอดรหัส

ฟิลด์
candidates[] object (Candidate)

จัดเรียงตามความน่าจะเป็นของบันทึกจากมากไปน้อย

การแสดง JSON
{
  "candidates": [
    {
      object (Candidate)
    }
  ]
}

ผู้สมัคร

ผู้สมัครชิงตำแหน่งโทเค็นและคะแนน logprobs

ฟิลด์
token string

ค่าสตริงโทเค็นของผู้สมัคร

tokenId integer

ค่ารหัสโทเค็นของผู้สมัคร

logProbability number

ความน่าจะเป็นของบันทึกของผู้สมัคร

การแสดง JSON
{
  "token": string,
  "tokenId": integer,
  "logProbability": number
}

UrlContextMetadata

ข้อมูลเมตาที่เกี่ยวข้องกับเครื่องมือดึงข้อมูลบริบท URL

ฟิลด์
urlMetadata[] object (UrlMetadata)

รายการบริบท URL

การแสดง JSON
{
  "urlMetadata": [
    {
      object (UrlMetadata)
    }
  ]
}

UrlMetadata

บริบทของการดึงข้อมูล URL เดียว

ฟิลด์
retrievedUrl string

URL ที่เครื่องมือดึงข้อมูล

urlRetrievalStatus enum (UrlRetrievalStatus)

สถานะของการดึงข้อมูล URL

การแสดง JSON
{
  "retrievedUrl": string,
  "urlRetrievalStatus": enum (UrlRetrievalStatus)
}

UrlRetrievalStatus

สถานะของการดึงข้อมูล URL

Enum
URL_RETRIEVAL_STATUS_UNSPECIFIED ค่าเริ่มต้น ค่านี้ไม่ได้ใช้
URL_RETRIEVAL_STATUS_SUCCESS การดึงข้อมูล URL สำเร็จแล้ว
URL_RETRIEVAL_STATUS_ERROR การดึงข้อมูล URL ล้มเหลวเนื่องจากเกิดข้อผิดพลาด

CitationMetadata

ชุดการระบุแหล่งที่มาของเนื้อหา

ฟิลด์
citationSources[] object (CitationSource)

การอ้างอิงแหล่งที่มาสำหรับคำตอบที่เฉพาะเจาะจง

การแสดง JSON
{
  "citationSources": [
    {
      object (CitationSource)
    }
  ]
}

CitationSource

การอ้างอิงแหล่งที่มาสำหรับส่วนหนึ่งของคำตอบที่เฉพาะเจาะจง

ฟิลด์
startIndex integer

ไม่บังคับ จุดเริ่มต้นของกลุ่มการตอบกลับที่มาจากแหล่งที่มานี้

ดัชนีระบุจุดเริ่มต้นของกลุ่ม โดยวัดเป็นไบต์

endIndex integer

ไม่บังคับ จุดสิ้นสุดของกลุ่มที่มา ไม่รวมจุดสิ้นสุด

uri string

ไม่บังคับ URI ที่ระบุว่าเป็นแหล่งที่มาของข้อความบางส่วน

license string

ไม่บังคับ ใบอนุญาตสำหรับโปรเจ็กต์ GitHub ที่ระบุว่าเป็นแหล่งที่มาของกลุ่ม

ต้องระบุข้อมูลใบอนุญาตสำหรับการอ้างอิงโค้ด

การแสดง JSON
{
  "startIndex": integer,
  "endIndex": integer,
  "uri": string,
  "license": string
}

GenerationConfig

ตัวเลือกการกำหนดค่าสำหรับการสร้างโมเดลและเอาต์พุต พารามิเตอร์บางรายการอาจกำหนดค่าไม่ได้ในบางโมเดล

ฟิลด์
stopSequences[] string

ไม่บังคับ ชุดลำดับอักขระ (สูงสุด 5 รายการ) ที่จะหยุดการสร้างเอาต์พุต หากระบุไว้ API จะหยุดที่ลักษณะแรกของ stop_sequence ระบบจะไม่รวมลำดับการหยุดเป็นส่วนหนึ่งของคำตอบ

responseMimeType string

ไม่บังคับ ประเภท MIME ของข้อความตัวเลือกที่สร้างขึ้น ประเภท MIME ที่รองรับ ได้แก่ text/plain: (ค่าเริ่มต้น) เอาต์พุตข้อความ application/json: การตอบกลับ JSON ในคำตอบที่เป็นไปได้ text/x.enum: ENUM เป็นการตอบกลับสตริงในตัวเลือกการตอบกลับ ดูรายการประเภท MIME ของข้อความทั้งหมดที่รองรับได้ในเอกสาร

responseSchema object (Schema)

ไม่บังคับ สคีมาเอาต์พุตของข้อความผู้สมัครที่สร้างขึ้น สคีมาต้องเป็นส่วนย่อยของสคีมา OpenAPI และอาจเป็นออบเจ็กต์ องค์ประกอบพื้นฐาน หรืออาร์เรย์

หากตั้งค่าไว้ ก็ต้องตั้งค่า responseMimeType ที่เข้ากันได้ด้วย MIME ประเภทที่เข้ากันได้: application/json: สคีมาสำหรับการตอบกลับ JSON ดูรายละเอียดเพิ่มเติมได้ในคู่มือการสร้างข้อความ JSON

responseJsonSchema value (Value format)

ไม่บังคับ รูปแบบเอาต์พุตของคำตอบที่สร้างขึ้น ซึ่งเป็นอีกทางเลือกหนึ่งแทน responseSchema ที่ยอมรับ JSON Schema

หากตั้งค่าไว้ คุณต้องละเว้น responseSchema แต่ต้องระบุ responseMimeType

แม้ว่าจะส่ง JSON Schema แบบเต็มได้ แต่ระบบก็ไม่รองรับฟีเจอร์บางอย่าง โดยเฉพาะอย่างยิ่ง ระบบรองรับเฉพาะพร็อพเพอร์ตี้ต่อไปนี้

  • $id
  • $defs
  • $ref
  • $anchor
  • type
  • format
  • title
  • description
  • enum (สำหรับสตริงและตัวเลข)
  • items
  • prefixItems
  • minItems
  • maxItems
  • minimum
  • maximum
  • anyOf
  • oneOf (ตีความเช่นเดียวกับ anyOf)
  • properties
  • additionalProperties
  • required

นอกจากนี้ยังอาจตั้งค่าพร็อพเพอร์ตี้ propertyOrdering ที่ไม่ใช่มาตรฐานได้ด้วย

การอ้างอิงแบบวนซ้ำจะคลี่ออกในระดับที่จำกัด และด้วยเหตุนี้จึงอาจใช้ได้เฉพาะภายในพร็อพเพอร์ตี้ที่ไม่บังคับ (พร็อพเพอร์ตี้ที่กำหนดให้เป็น Null ได้ไม่เพียงพอ) หากตั้งค่า $ref ในสคีมาย่อย จะตั้งค่าพร็อพเพอร์ตี้อื่นๆ ไม่ได้ ยกเว้นพร็อพเพอร์ตี้ที่ขึ้นต้นด้วย $

responseModalities[] enum (Modality)

ไม่บังคับ รูปแบบการตอบกลับที่ขอ แสดงชุดรูปแบบที่โมเดลสามารถแสดงผลและควรคาดหวังในคำตอบ ซึ่งตรงกับรูปแบบของคำตอบ

โมเดลอาจมีการผสมผสานรูปแบบที่รองรับหลายแบบ หากรูปแบบที่ขอไม่ตรงกับชุดค่าผสมที่รองรับ ระบบจะแสดงข้อผิดพลาด

รายการที่ว่างเปล่าจะเทียบเท่ากับการขอเฉพาะข้อความ

candidateCount integer

ไม่บังคับ จำนวนคำตอบที่สร้างขึ้นที่จะแสดงผล หากไม่ได้ตั้งค่าไว้ ระบบจะใช้ค่าเริ่มต้นเป็น 1 โปรดทราบว่าฟีเจอร์นี้ใช้กับโมเดลรุ่นก่อนหน้า (ตระกูล Gemini 1.0) ไม่ได้

maxOutputTokens integer

ไม่บังคับ จำนวนโทเค็นสูงสุดที่จะรวมไว้ในคำตอบที่เป็นไปได้

หมายเหตุ: ค่าเริ่มต้นจะแตกต่างกันไปตามรุ่น โปรดดูแอตทริบิวต์ Model.output_token_limit ของ Model ที่แสดงผลจากฟังก์ชัน getModel

temperature number

ไม่บังคับ ควบคุมความสุ่มของเอาต์พุต

หมายเหตุ: ค่าเริ่มต้นจะแตกต่างกันไปตามรุ่น โปรดดูแอตทริบิวต์ Model.temperature ของ Model ที่แสดงผลจากฟังก์ชัน getModel

ค่าที่ใช้ได้อยู่ในช่วง [0.0, 2.0]

topP number

ไม่บังคับ ความน่าจะเป็นสะสมสูงสุดของโทเค็นที่ต้องพิจารณาเมื่อทำการสุ่มตัวอย่าง

โมเดลนี้ใช้การสุ่มตัวอย่างแบบรวม Top-k และ Top-p (นิวเคลียส)

ระบบจะจัดเรียงโทเค็นตามความน่าจะเป็นที่กำหนด เพื่อให้พิจารณาเฉพาะโทเค็นที่มีแนวโน้มมากที่สุด การสุ่มตัวอย่าง Top-k จะจำกัดจำนวนโทเค็นสูงสุดที่จะพิจารณาโดยตรง ในขณะที่การสุ่มตัวอย่าง Nucleus จะจำกัดจำนวนโทเค็นตามความน่าจะเป็นสะสม

หมายเหตุ: ค่าเริ่มต้นจะแตกต่างกันไปตามModel และระบุโดยแอตทริบิวต์Model.top_pที่แสดงผลจากฟังก์ชันgetModel แอตทริบิวต์ topK ที่ว่างเปล่าบ่งบอกว่าโมเดลไม่ได้ใช้การสุ่มตัวอย่างแบบ Top-k และไม่อนุญาตให้ตั้งค่า topK ในคำขอ

topK integer

ไม่บังคับ จำนวนโทเค็นสูงสุดที่ต้องพิจารณาเมื่อทำการสุ่มตัวอย่าง

โมเดล Gemini ใช้การสุ่มตัวอย่าง Top-p (นิวเคลียส) หรือใช้ทั้งการสุ่มตัวอย่าง Top-k และนิวเคลียส การสุ่มตัวอย่าง Top-k จะพิจารณาชุดโทเค็นที่มีtopKความน่าจะเป็นสูงสุด โมเดลที่ทำงานด้วยการสุ่มตัวอย่างนิวเคลียสไม่อนุญาตให้ตั้งค่า topK

หมายเหตุ: ค่าเริ่มต้นจะแตกต่างกันไปตามModel และระบุโดยแอตทริบิวต์Model.top_pที่แสดงผลจากฟังก์ชันgetModel แอตทริบิวต์ topK ที่ว่างเปล่าบ่งบอกว่าโมเดลไม่ได้ใช้การสุ่มตัวอย่างแบบ Top-k และไม่อนุญาตให้ตั้งค่า topK ในคำขอ

seed integer

ไม่บังคับ Seed ที่ใช้ในการถอดรหัส หากไม่ได้ตั้งค่า คำขอจะใช้ Seed ที่สร้างขึ้นแบบสุ่ม

presencePenalty number

ไม่บังคับ ค่าปรับการปรากฏใช้กับ logprobs ของโทเค็นถัดไปหากโทเค็นปรากฏในคำตอบแล้ว

การลงโทษนี้จะเปิด/ปิดได้เท่านั้นและไม่ได้ขึ้นอยู่กับจำนวนครั้งที่มีการใช้โทเค็น (หลังจากครั้งแรก) ใช้ frequencyPenalty สำหรับค่าปรับที่จะเพิ่มขึ้นตามการใช้งานแต่ละครั้ง

ค่าปรับที่เป็นบวกจะกีดกันการใช้โทเค็นที่ใช้ไปแล้วในคำตอบ ซึ่งจะช่วยเพิ่มคำศัพท์

การลงโทษเชิงลบจะกระตุ้นให้ใช้โทเค็นที่ใช้ไปแล้วในคำตอบ ซึ่งจะช่วยลดคำศัพท์

frequencyPenalty number

ไม่บังคับ ค่าปรับความถี่ที่ใช้กับ logprobs ของโทเค็นถัดไป คูณด้วยจำนวนครั้งที่โทเค็นแต่ละรายการปรากฏในคำตอบจนถึงตอนนี้

การลงโทษเชิงบวกจะกีดกันการใช้โทเค็นที่ใช้ไปแล้วตามสัดส่วนของจำนวนครั้งที่ใช้โทเค็นนั้น ยิ่งใช้โทเค็นมากเท่าใด โมเดลก็จะยิ่งใช้โทเค็นนั้นอีกได้ยากขึ้นเท่านั้น ซึ่งจะช่วยเพิ่มคำศัพท์ในการตอบกลับ

ข้อควรระวัง: การลงโทษแบบค่าลบจะกระตุ้นให้โมเดลใช้โทเค็นซ้ำตามสัดส่วนของจำนวนครั้งที่มีการใช้โทเค็น ค่าลบขนาดเล็กจะลดคำศัพท์ของคำตอบ ค่าลบที่มากขึ้นจะทำให้โมเดลเริ่มทำซ้ำโทเค็นทั่วไปจนกว่าจะถึงขีดจำกัด maxOutputTokens

responseLogprobs boolean

ไม่บังคับ หากเป็นจริง ให้ส่งออกผลลัพธ์ logprobs ในการตอบกลับ

logprobs integer

ไม่บังคับ ใช้ได้เฉพาะในกรณีที่ responseLogprobs=True ซึ่งจะกำหนดจำนวน logprob สูงสุดที่จะแสดงในแต่ละขั้นตอนการถอดรหัสใน Candidate.logprobs_result

enableEnhancedCivicAnswers boolean

ไม่บังคับ เปิดใช้คำตอบเกี่ยวกับพลเมืองที่ปรับปรุงแล้ว โดยอาจไม่พร้อมใช้งานในบางรุ่น

speechConfig object (SpeechConfig)

ไม่บังคับ การกำหนดค่าการสร้างคำพูด

thinkingConfig object (ThinkingConfig)

ไม่บังคับ กำหนดค่าสำหรับฟีเจอร์การคิด ระบบจะแสดงข้อผิดพลาดหากตั้งค่าฟิลด์นี้สำหรับโมเดลที่ไม่รองรับการคิด

mediaResolution enum (MediaResolution)

ไม่บังคับ หากระบุไว้ ระบบจะใช้ความละเอียดของสื่อที่ระบุ

การแสดง JSON
{
  "stopSequences": [
    string
  ],
  "responseMimeType": string,
  "responseSchema": {
    object (Schema)
  },
  "responseJsonSchema": value,
  "responseModalities": [
    enum (Modality)
  ],
  "candidateCount": integer,
  "maxOutputTokens": integer,
  "temperature": number,
  "topP": number,
  "topK": integer,
  "seed": integer,
  "presencePenalty": number,
  "frequencyPenalty": number,
  "responseLogprobs": boolean,
  "logprobs": integer,
  "enableEnhancedCivicAnswers": boolean,
  "speechConfig": {
    object (SpeechConfig)
  },
  "thinkingConfig": {
    object (ThinkingConfig)
  },
  "mediaResolution": enum (MediaResolution)
}

รูปแบบ

รูปแบบการตอบกลับที่รองรับ

Enum
MODALITY_UNSPECIFIED ค่าเริ่มต้น
TEXT ระบุว่าโมเดลควรแสดงข้อความ
IMAGE ระบุว่าโมเดลควรแสดงรูปภาพ
AUDIO ระบุว่าโมเดลควรส่งคืนเสียง

SpeechConfig

การกำหนดค่าการสร้างคำพูด

ฟิลด์
voiceConfig object (VoiceConfig)

การกำหนดค่าในกรณีที่เอาต์พุตเป็นเสียงเดียว

multiSpeakerVoiceConfig object (MultiSpeakerVoiceConfig)

ไม่บังคับ การกำหนดค่าสำหรับการตั้งค่าลำโพงหลายตัว ซึ่งจะใช้ร่วมกับฟิลด์ voiceConfig ไม่ได้

languageCode string

ไม่บังคับ รหัสภาษา (ในรูปแบบ BCP 47 เช่น "en-US") สำหรับการสังเคราะห์เสียงพูด

ค่าที่ใช้ได้ ได้แก่ de-DE, en-AU, en-GB, en-IN, en-US, es-US, fr-FR, hi-IN, pt-BR, ar-XA, es-ES, fr-CA, id-ID, it-IT, ja-JP, tr-TR, vi-VN, bn-IN, gu-IN, kn-IN, ml-IN, mr-IN, ta-IN, te-IN, nl-NL, ko-KR, cmn-CN, pl-PL, ru-RU และ th-TH

การแสดง JSON
{
  "voiceConfig": {
    object (VoiceConfig)
  },
  "multiSpeakerVoiceConfig": {
    object (MultiSpeakerVoiceConfig)
  },
  "languageCode": string
}

VoiceConfig

การกำหนดค่าสำหรับเสียงที่จะใช้

ฟิลด์
voice_config Union type
การกำหนดค่าสำหรับลำโพงที่จะใช้ voice_config ต้องเป็นค่าใดค่าหนึ่งต่อไปนี้เท่านั้น
prebuiltVoiceConfig object (PrebuiltVoiceConfig)

การกำหนดค่าสำหรับเสียงที่สร้างไว้ล่วงหน้าที่จะใช้

การแสดง JSON
{

  // voice_config
  "prebuiltVoiceConfig": {
    object (PrebuiltVoiceConfig)
  }
  // Union type
}

PrebuiltVoiceConfig

การกำหนดค่าสำหรับลำโพงที่สร้างไว้ล่วงหน้าที่จะใช้

ฟิลด์
voiceName string

ชื่อของเสียงที่กำหนดไว้ล่วงหน้าที่จะใช้

การแสดง JSON
{
  "voiceName": string
}

MultiSpeakerVoiceConfig

การกำหนดค่าสำหรับการตั้งค่าลำโพงหลายตัว

ฟิลด์
speakerVoiceConfigs[] object (SpeakerVoiceConfig)

ต้องระบุ เสียงพูดของลำโพงที่เปิดใช้ทั้งหมด

การแสดง JSON
{
  "speakerVoiceConfigs": [
    {
      object (SpeakerVoiceConfig)
    }
  ]
}

SpeakerVoiceConfig

การกำหนดค่าสำหรับลำโพงตัวเดียวในการตั้งค่าแบบหลายลำโพง

ฟิลด์
speaker string

ต้องระบุ ชื่อของลำโพงที่จะใช้ ควรเหมือนกับในพรอมต์

voiceConfig object (VoiceConfig)

ต้องระบุ การกำหนดค่าสำหรับเสียงที่จะใช้

การแสดง JSON
{
  "speaker": string,
  "voiceConfig": {
    object (VoiceConfig)
  }
}

ThinkingConfig

กำหนดค่าสำหรับฟีเจอร์การคิด

ฟิลด์
includeThoughts boolean

ระบุว่าจะรวมความคิดเห็นไว้ในการตอบกลับหรือไม่ หากเป็นจริง ระบบจะแสดงความคิดเห็นเมื่อพร้อมใช้งานเท่านั้น

thinkingBudget integer

จำนวนโทเค็นความคิดที่โมเดลควรสร้าง

การแสดง JSON
{
  "includeThoughts": boolean,
  "thinkingBudget": integer
}

MediaResolution

ความละเอียดของสื่อสำหรับสื่ออินพุต

Enum
MEDIA_RESOLUTION_UNSPECIFIED ยังไม่ได้ตั้งค่าความละเอียดของสื่อ
MEDIA_RESOLUTION_LOW ตั้งค่าความละเอียดของสื่อเป็นต่ำ (64 โทเค็น)
MEDIA_RESOLUTION_MEDIUM ตั้งค่าความละเอียดของสื่อเป็นปานกลาง (256 โทเค็น)
MEDIA_RESOLUTION_HIGH ตั้งค่าความละเอียดของสื่อเป็นสูง (การจัดเฟรมใหม่แบบซูมที่มีโทเค็น 256 รายการ)

HarmCategory

หมวดหมู่ของการจัดประเภท

หมวดหมู่เหล่านี้ครอบคลุมอันตรายประเภทต่างๆ ที่นักพัฒนาแอปอาจต้องการปรับ

Enum
HARM_CATEGORY_UNSPECIFIED ไม่ได้ระบุหมวดหมู่
HARM_CATEGORY_DEROGATORY PaLM - ความคิดเห็นเชิงลบหรือเป็นอันตรายที่กำหนดเป้าหมายไปยังข้อมูลประจำตัวและ/หรือแอตทริบิวต์ที่ได้รับการคุ้มครอง
HARM_CATEGORY_TOXICITY PaLM - เนื้อหาที่ไม่สุภาพ ไม่เคารพ หรือหยาบคาย
HARM_CATEGORY_VIOLENCE PaLM - อธิบายสถานการณ์ที่แสดงความรุนแรงต่อบุคคลหรือกลุ่มบุคคล หรือคำอธิบายทั่วไปเกี่ยวกับภาพนองเลือด
HARM_CATEGORY_SEXUAL PaLM - มีการอ้างอิงถึงการกระทำทางเพศหรือเนื้อหาลามกอื่นๆ
HARM_CATEGORY_MEDICAL PaLM - โปรโมตคำแนะนำทางการแพทย์ที่ไม่ได้ตรวจสอบ
HARM_CATEGORY_DANGEROUS PaLM - เนื้อหาที่เป็นอันตรายซึ่งส่งเสริม สนับสนุน หรือก่อให้เกิดการกระทําที่เป็นอันตราย
HARM_CATEGORY_HARASSMENT Gemini - เนื้อหาที่แสดงการคุกคาม
HARM_CATEGORY_HATE_SPEECH Gemini - วาจาสร้างความเกลียดชังและเนื้อหา
HARM_CATEGORY_SEXUALLY_EXPLICIT Gemini - เนื้อหาเกี่ยวกับเรื่องเพศอย่างโจ่งแจ้ง
HARM_CATEGORY_DANGEROUS_CONTENT Gemini - เนื้อหาไม่ปลอดภัย
HARM_CATEGORY_CIVIC_INTEGRITY Gemini - เนื้อหาที่อาจใช้เพื่อทำลายความซื่อสัตย์ของพลเมือง

ModalityTokenCount

แสดงข้อมูลการนับโทเค็นสำหรับรูปแบบเดียว

ฟิลด์
modality enum (Modality)

รูปแบบที่เชื่อมโยงกับจำนวนโทเค็นนี้

tokenCount integer

จำนวนโทเค็น

การแสดง JSON
{
  "modality": enum (Modality),
  "tokenCount": integer
}

รูปแบบ

รูปแบบเนื้อหา

Enum
MODALITY_UNSPECIFIED รูปแบบที่ไม่ระบุ
TEXT ข้อความธรรมดา
IMAGE รูปภาพ
VIDEO วิดีโอ
AUDIO เสียง
DOCUMENT เอกสาร เช่น PDF

SafetyRating

คะแนนความปลอดภัยสำหรับเนื้อหา

การจัดประเภทความปลอดภัยประกอบด้วยหมวดหมู่ของอันตรายและระดับความน่าจะเป็นของอันตรายในหมวดหมู่นั้นสำหรับเนื้อหาหนึ่งๆ ระบบจะจัดประเภทเนื้อหาเพื่อความปลอดภัยในหมวดหมู่ความเสียหายต่างๆ และรวมความน่าจะเป็นของการจัดประเภทความเสียหายไว้ที่นี่

ฟิลด์
category enum (HarmCategory)

ต้องระบุ หมวดหมู่ของคะแนนนี้

probability enum (HarmProbability)

ต้องระบุ ความน่าจะเป็นอันตรายสำหรับเนื้อหานี้

blocked boolean

เนื้อหานี้ถูกบล็อกเนื่องจากการจัดประเภทนี้ใช่ไหม

การแสดง JSON
{
  "category": enum (HarmCategory),
  "probability": enum (HarmProbability),
  "blocked": boolean
}

HarmProbability

ความน่าจะเป็นที่เนื้อหาจะเป็นอันตราย

ระบบการจัดประเภทจะให้ความน่าจะเป็นที่เนื้อหาจะไม่ปลอดภัย ซึ่งไม่ได้บ่งบอกถึงความรุนแรงของอันตรายสำหรับเนื้อหาชิ้นหนึ่งๆ

Enum
HARM_PROBABILITY_UNSPECIFIED ไม่ได้ระบุความน่าจะเป็น
NEGLIGIBLE เนื้อหามีโอกาสน้อยมากที่จะไม่ปลอดภัย
LOW เนื้อหามีโอกาสต่ำที่จะไม่ปลอดภัย
MEDIUM เนื้อหามีโอกาสปานกลางที่จะไม่ปลอดภัย
HIGH เนื้อหามีโอกาสสูงที่จะไม่ปลอดภัย

SafetySetting

การตั้งค่าความปลอดภัยซึ่งส่งผลต่อลักษณะการทำงานของการบล็อกเพื่อความปลอดภัย

การส่งการตั้งค่าความปลอดภัยสำหรับหมวดหมู่จะเปลี่ยนความน่าจะเป็นที่อนุญาตซึ่งเนื้อหาจะถูกบล็อก

ฟิลด์
category enum (HarmCategory)

ต้องระบุ หมวดหมู่ของการตั้งค่านี้

threshold enum (HarmBlockThreshold)

ต้องระบุ ควบคุมเกณฑ์ความน่าจะเป็นที่จะบล็อกอันตราย

การแสดง JSON
{
  "category": enum (HarmCategory),
  "threshold": enum (HarmBlockThreshold)
}

HarmBlockThreshold

บล็อกที่และเกินกว่าความน่าจะเป็นที่จะเกิดอันตรายที่ระบุ

Enum
HARM_BLOCK_THRESHOLD_UNSPECIFIED ไม่ได้ระบุเกณฑ์
BLOCK_LOW_AND_ABOVE เนื้อหาที่มีการเปลือยเปล่าเล็กน้อยจะได้รับอนุญาต
BLOCK_MEDIUM_AND_ABOVE เนื้อหาที่มีความเสี่ยง "เล็กน้อย" และ "ต่ำ" จะได้รับอนุญาต
BLOCK_ONLY_HIGH เนื้อหาที่มีระดับความรุนแรงเป็น "เล็กน้อย" "ต่ำ" และ "ปานกลาง" จะได้รับอนุญาต
BLOCK_NONE ระบบจะอนุญาตเนื้อหาทั้งหมด
OFF ปิดตัวกรองความปลอดภัย