การสร้างข้อความ

Gemini API สามารถสร้างเอาต์พุตข้อความจากอินพุตข้อความ รูปภาพ วิดีโอ และเสียง

ตัวอย่างเบื้องต้นมีดังนี้

Python

from google import genai

client = genai.Client()

interaction = client.interactions.create(
    model="gemini-3.6-flash",
    input="How does AI work?"
)
print(interaction.output_text)

JavaScript

import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI({});

async function main() {
  const interaction = await ai.interactions.create({
    model: "gemini-3.6-flash",
    input: "How does AI work?",
  });
  console.log(interaction.output_text);
}

await main();

REST

curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "gemini-3.6-flash",
    "input": "How does AI work?"
  }'

Google GenAI SDK มีพร็อพเพอร์ตี้ที่สะดวกในออบเจ็กต์ Interaction ที่ส่งกลับมาโดยตรงเพื่อให้เข้าถึงการตอบกลับของโมเดลได้

ตัวช่วยที่ใช้บ่อยที่สุดคือ interaction.output_text (String) ซึ่งจะแสดงผลบล็อกข้อความสุดท้ายในการตอบกลับของโมเดล หากการตอบกลับแยกออกเป็นบล็อก TextContent หลายบล็อกที่ต่อเนื่องกัน ระบบจะรวมบล็อกเหล่านั้นโดยอัตโนมัติ โปรดทราบว่า .output_text จะไม่รวมบล็อกข้อความก่อนหน้าที่คั่นด้วยเนื้อหาที่ไม่ใช่ข้อความ (เช่น ความคิด รูปภาพ เสียง หรือการเรียกใช้เครื่องมือ) สำหรับคำตอบแบบหลายรูปแบบที่ซับซ้อนหรือมีการสลับกัน คุณต้องวนซ้ำ steps ด้วยตนเองแทน ดูข้อมูลเพิ่มเติมเกี่ยวกับพร็อพเพอร์ตี้ที่สะดวกอื่นๆ ของสื่อได้ที่ ภาพรวมของการโต้ตอบ

การคิดด้วย Gemini

โมเดล Gemini มักจะเปิดใช้ "การคิด" ไว้โดยค่าเริ่มต้น ซึ่งช่วยให้โมเดลใช้เหตุผลก่อนที่จะตอบกลับ คำขอได้

โมเดลแต่ละรายการรองรับการกำหนดค่าการคิดที่แตกต่างกัน ซึ่งช่วยให้คุณควบคุมต้นทุน เวลาในการตอบสนอง และความฉลาดได้ ดูรายละเอียดเพิ่มเติมได้ที่ คู่มือการคิด

Python

from google import genai

client = genai.Client()

interaction = client.interactions.create(
    model="gemini-3.6-flash",
    input="How does AI work?",
    generation_config={
        "thinking_level": "low"
    }
)
print(interaction.output_text)

JavaScript

import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI({});

async function main() {
  const interaction = await ai.interactions.create({
    model: "gemini-3.6-flash",
    input: "How does AI work?",
    generation_config: {
      thinking_level: "low",
    },
  });
  console.log(interaction.output_text);
}

await main();

REST

curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "gemini-3.6-flash",
    "input": "How does AI work?",
    "generation_config": {
      "thinking_level": "low"
    }
  }'

คำแนะนำระบบและการกำหนดค่าอื่นๆ

คุณสามารถกำหนดลักษณะการทำงานของโมเดล Gemini ด้วยคำแนะนำระบบได้ ส่งพารามิเตอร์ system_instruction เพื่อกำหนดค่าลักษณะการทำงานของโมเดล

Python

from google import genai

client = genai.Client()

interaction = client.interactions.create(
    model="gemini-3.6-flash",
    system_instruction="You are a cat. Your name is Neko.",
    input="Hello there"
)

print(interaction.output_text)

JavaScript

import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI({});

async function main() {
  const interaction = await ai.interactions.create({
    model: "gemini-3.6-flash",
    input: "Hello there",
    system_instruction: "You are a cat. Your name is Neko.",
  });
  console.log(interaction.output_text);
}

await main();

REST

curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "gemini-3.6-flash",
    "system_instruction": "You are a cat. Your name is Neko.",
    "input": "Hello there"
  }'

นอกจากนี้ คุณยังลบล้างพารามิเตอร์การสร้างเริ่มต้น เช่น อุณหภูมิ โดยใช้พารามิเตอร์ generation_config ได้ด้วย

Python

from google