การสร้างข้อความ
Gemini API สามารถสร้างเอาต์พุตข้อความจากอินพุตข้อความ รูปภาพ วิดีโอ และเสียง
ตัวอย่างพื้นฐานมีดังนี้
Python
from google import genai
client = genai.Client()
interaction = client.interactions.create(
model="gemini-3-flash-preview",
input="How does AI work?"
)
print(interaction.steps[-1].content[0].text)
JavaScript
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({});
async function main() {
const interaction = await ai.interactions.create({
model: "gemini-3-flash-preview",
input: "How does AI work?",
});
console.log(interaction.steps.at(-1).content[0].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-flash-preview",
"input": "How does AI work?"
}'
การคิดด้วย Gemini
โมเดล Gemini มักจะเปิดใช้ "การคิด" ไว้โดยค่าเริ่มต้น ซึ่งช่วยให้โมเดลสามารถให้เหตุผลก่อนที่จะตอบสนองต่อ คำขอ
แต่ละโมเดลรองรับการกำหนดค่าการคิดที่แตกต่างกัน ซึ่งช่วยให้คุณควบคุมต้นทุน เวลาในการตอบสนอง และความสามารถได้ ดูรายละเอียดเพิ่มเติมได้ที่ คู่มือการคิด
Python
from google import genai
client = genai.Client()
interaction = client.interactions.create(
model="gemini-3-flash-preview",
input="How does AI work?",
generation_config={
"thinking_level": "low"
}
)
print(interaction.steps[-1].content[0].text)
JavaScript
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({});
async function main() {
const interaction = await ai.interactions.create({
model: "gemini-3-flash-preview",
input: "How does AI work?",
generation_config: {
thinking_level: "low",
},
});
console.log(interaction.steps.at(-1).content[0].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-flash-preview",
"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-flash-preview",
system_instruction="You are a cat. Your name is Neko.",
input="Hello there"
)
print(interaction.steps[-1].content[0].text)
JavaScript
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({});
async function main() {
const interaction = await ai.interactions.create({
model: "gemini-3-flash-preview",
input: "Hello there",
system_instruction: "You are a cat. Your name is Neko.",
});
console.log(interaction.steps.at(-1).content[0].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-flash-preview",
"system_instruction": "You are a cat. Your name is Neko.",
"input": "Hello there"
}'
นอกจากนี้ คุณยังลบล้างพารามิเตอร์การสร้างเริ่มต้น เช่น อุณหภูมิ ได้โดยใช้พารามิเตอร์ generation_config
Python
from google import genai
client = genai.Client()
interaction = client.interactions.create(
model="gemini-3-flash-preview",
input="Explain how AI works",
generation_config={
"temperature": 0.1
}
)
print(interaction.steps[-1].content[0].text)
JavaScript
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({});
async function main() {
const interaction = await ai.interactions.create({
model: "gemini-3-flash-preview",
input: "Explain how AI works",
generation_config: {
temperature: 0.1,
},
});
console.log(interaction.steps.at(-1).content[0].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-flash-preview",
"input": "Explain how AI works",
"generation_config": {
"temperature": 0.1
}
}'
โปรดดูรายการพารามิเตอร์ที่กำหนดค่าได้ทั้งหมดและคำอธิบายในเอกสารอ้างอิงของ Interactions API
อินพุตหลายรูปแบบ
Gemini API รองรับอินพุตหลายรูปแบบ ซึ่งช่วยให้คุณรวมข้อความกับไฟล์สื่อได้ ตัวอย่างต่อไปนี้แสดงวิธีส่งรูปภาพ
Python
from google import genai
client = genai.Client()
uploaded_file = client.files.upload(file="path/to/organ.jpg")
interaction = client.interactions.create(
model="gemini-3-flash-preview",
input=[
{"type": "text", "text": "Tell me about this instrument"},
{
"type": "image",
"uri": uploaded_file.uri,
"mime_type": uploaded_file.mime_type
}
]
)
print(interaction.steps[-1].content[0].text)
JavaScript
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({});
async function main() {
const uploadedFile = await ai.files.upload({
file: "path/to/organ.jpg",
config: { mimeType: "image/jpeg" }
});
const interaction = await ai.interactions.create({
model: "gemini-3-flash-preview",
input: [
{type: "text", text: "Tell me about this instrument"},
{
type: "image",
uri: uploadedFile.uri,
mimeType: uploadedFile.mimeType
}
],
});
console.log(interaction.steps.at(-1).content[0].text);
}
await main();
REST
# First upload the file using the Files API, then use the URI:
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-flash-preview",
"input": [
{"type": "text", "text": "Tell me about this instrument"},
{
"type": "image",
"uri": "YOUR_FILE_URI",
"mime_type": "image/jpeg"
}
]
}'
ดูวิธีการส่งรูปภาพและวิธีการประมวลผลรูปภาพขั้นสูงอื่นๆ ได้ในคู่มือการทำความเข้าใจรูปภาพ นอกจากนี้ API ยังรองรับอินพุตและความเข้าใจเอกสาร วิดีโอ และ เสียงด้วย
การสตรีมคำตอบ
โดยค่าเริ่มต้น โมเดลจะแสดงคำตอบหลังจากกระบวนการสร้างทั้งหมด เสร็จสมบูรณ์แล้วเท่านั้น
หากต้องการให้การโต้ตอบราบรื่นขึ้น ให้ใช้สตรีมมิงเพื่อจัดการกับส่วนต่างๆ ของคำตอบขณะที่ระบบสร้างคำตอบ
Python
from google import genai
client = genai.Client()
stream = client.interactions.create(
model="gemini-3-flash-preview",
input="Explain how AI works",
stream=True
)
for event in stream:
if event.event_type == "step.delta":
if event.delta.type == "text":
print(event.delta.text, end="")
JavaScript
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({});
async function main() {
const stream = await ai.interactions.create({
model: "gemini-3-flash-preview",
input: "Explain how AI works",
stream: true,
});
for await (const event of stream) {
if (event.type === "step.delta") {
if (event.delta.type === "text") {
process.stdout.write(event.delta.text);
}
}
}
}
await main();
REST
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions?alt=sse" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
--no-buffer \
-d '{
"model": "gemini-3-flash-preview",
"input": "Explain how AI works",
"stream": true
}'
การสนทนาหลายรอบ
Interactions API รองรับการสนทนาหลายรอบโดยการเชื่อมโยงการโต้ตอบเข้าด้วยกันโดยใช้ previous_interaction_id แต่ละรอบเป็นการโต้ตอบแยกกัน และ API จะจัดการประวัติการสนทนาโดยอัตโนมัติ
Python
from google import genai
client = genai.Client()
interaction1 = client.interactions.create(
model="gemini-3-flash-preview",
input="I have 2 dogs in my house.",
)
print(interaction1.steps[-1].content[0].text)
interaction2 = client.interactions.create(
model="gemini-3-flash-preview",
input="How many paws are in my house?",
previous_interaction_id=interaction1.id,
)
print(interaction2.steps[-1].content[0].text)
JavaScript
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({});
async function main() {
const interaction1 = await ai.interactions.create({
model: "gemini-3-flash-preview",
input: "I have 2 dogs in my house.",
});
console.log("Response 1:", interaction1.steps.at(-1).content[0].text);
const interaction2 = await ai.interactions.create({
model: "gemini-3-flash-preview",
input: "How many paws are in my house?",
previousInteractionId: interaction1.id,
});
console.log("Response 2:", interaction2.steps.at(-1).content[0].text);
}
await main();
REST
RESPONSE1=$(curl -s -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-flash-preview",
"input": "I have 2 dogs in my house."
}')
INTERACTION_ID=$(echo "$RESPONSE1" | jq -r '.name')
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-flash-preview",
"input": "I have two dogs in my house. How many paws are in my house?",
"previous_interaction_id": "'$INTERACTION_ID'"
}'
นอกจากนี้ คุณยังใช้การสตรีมสำหรับการสนทนาหลายรอบได้ด้วยการรวม previous_interaction_id กับวิธีการสตรีม
Python
from google import genai
client = genai.Client()
interaction1 = client.interactions.create(
model="gemini-3-flash-preview",
input="I have 2 dogs in my house.",
)
print(interaction1.steps[-1].content[0].text)
stream = client.interactions.create(
model="gemini-3-flash-preview",
input="How many paws are in my house?",
previous_interaction_id=interaction1.id,
stream=True
)
for event in stream:
if event.event_type == "step.delta":
if event.delta.type == "text":
print(event.delta.text, end="")
JavaScript
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({});
async function main() {
const interaction1 = await ai.interactions.create({
model: "gemini-3-flash-preview",
input: "I have 2 dogs in my house.",
});
console.log("Response 1:", interaction1.steps.at(-1).content[0].text);
const stream = await ai.interactions.create({
model: "gemini-3-flash-preview",
input: "How many paws are in my house?",
previousInteractionId: interaction1.id,
stream: true,
});
for await (const event of stream) {
if (event.type === "step.delta") {
if (event.delta.type === "text") {
process.stdout.write(event.delta.text);
}
}
}
}
await main();
REST
RESPONSE1=$(curl -s -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-flash-preview",
"input": "I have 2 dogs in my house."
}')
INTERACTION_ID=$(echo "$RESPONSE1" | jq -r '.name')
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions?alt=sse" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
--no-buffer \
-d '{
"model": "gemini-3-flash-preview",
"input": "How many paws are in my house?",
"previous_interaction_id": "'$INTERACTION_ID'",
"stream": true
}'
เคล็ดลับเกี่ยวกับพรอมต์
โปรดดูคู่มือวิศวกรรมพรอมต์ของเราสำหรับคำแนะนำในการใช้ประโยชน์จาก Gemini ให้ได้มากที่สุด
ขั้นตอนถัดไป
- ลองใช้ Gemini ใน Google AI Studio
- ทดลองใช้ เอาต์พุตที่มีโครงสร้างสำหรับ คำตอบที่คล้ายกับ JSON
- สำรวจความสามารถของ Gemini ในการทำความเข้าใจรูปภาพ, วิดีโอ, เสียง และ เอกสาร
- ดูข้อมูลเกี่ยวกับกลยุทธ์การสร้างพรอมต์ไฟล์หลายรูปแบบ