Gemini API ให้สิทธิ์เข้าถึง Gemma ที่โฮสต์เป็น API การเขียนโปรแกรมที่คุณใช้ได้ ในการพัฒนาแอปพลิเคชันหรือการสร้างต้นแบบ API นี้เป็นทางเลือกที่สะดวก แทนการตั้งค่าอินสแตนซ์ Gemma ในเครื่องและบริการเว็บของคุณเองเพื่อจัดการ งาน Generative AI
รุ่นที่รองรับ
Gemini API รองรับโมเดล Gemma 4 ต่อไปนี้
gemma-4-31b-itgemma-4-26b-a4b-it
ตัวอย่างต่อไปนี้แสดงวิธีใช้ Gemma กับ Gemini API
Python
from google import genai
client = genai.Client()
response = client.models.generate_content(
model="gemma-4-26b-a4b-it",
contents="Roses are red...",
)
print(response.text)
JavaScript
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI();
const response = await ai.models.generateContent({
model: "gemma-4-26b-a4b-it",
contents: "Roses are red...",
});
console.log(response.text);
REST
curl "https://generativelanguage.googleapis.com/v1beta/models/gemma-4-26b-a4b-it:generateContent" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [{
"parts":[{"text": "Roses are red..."}]
}]
}'
คุณเข้าถึง Gemini API ได้ในหลายแพลตฟอร์ม เช่น อุปกรณ์เคลื่อนที่ เว็บ และบริการระบบคลาวด์ รวมถึงใช้ได้กับภาษาโปรแกรมหลายภาษา ดูข้อมูลเพิ่มเติมเกี่ยวกับ แพ็กเกจ SDK ของ Gemini API ได้ที่หน้า การดาวน์โหลด SDK ของ Gemini API ดูข้อมูลเบื้องต้นเกี่ยวกับ Gemini API ได้ที่การเริ่มต้นใช้งาน Gemini API
กำลังคิด
Gemma 4 ใช้ "กระบวนการคิด" ภายในที่เพิ่มประสิทธิภาพการให้เหตุผลแบบหลายขั้นตอน ซึ่งให้ประสิทธิภาพที่เหนือกว่าในโดเมนที่ต้องใช้ตรรกะสูง เช่น การเขียนโค้ดอัลกอริทึมและการพิสูจน์ทางคณิตศาสตร์ขั้นสูง
แม้ว่า Gemma 4 จะรองรับการเปิดหรือปิดฟีเจอร์นี้อย่างเคร่งครัด แต่คุณก็เปิดใช้ฟีเจอร์นี้
ใน API ได้โดยตั้งค่าระดับการคิดเป็น "high"
ตัวอย่างต่อไปนี้แสดงวิธีเปิดใช้งานกระบวนการคิด
Python
from google import genai
from google.genai import types
client = genai.Client()
response = client.models.generate_content(
model="gemma-4-26b-a4b-it",
contents="What is the water formula?",
config=types.GenerateContentConfig(
thinking_config=types.ThinkingConfig(thinking_level="high")
),
)
print(response.text)
JavaScript
import { GoogleGenAI, ThinkingLevel } from "@google/genai";
const ai = new GoogleGenAI();
const response = await ai.models.generateContent({
model: "gemma-4-26b-a4b-it",
contents: "What is the water formula?",
config: {
thinkingConfig: {
thinkingLevel: ThinkingLevel.HIGH,
},
},
});
console.log(response.text);
REST
curl "https://generativelanguage.googleapis.com/v1beta/models/gemma-4-26b-a4b-it:generateContent" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [{
"parts":[{"text": "What is the water formula?"}]
}],
"generationConfig": {
"thinkingConfig": {
"thinkingLevel": "high"
}
}
}'
ดูข้อมูลเพิ่มเติมเกี่ยวกับ Thinking
- แนวคิดเกี่ยวกับ Gemini API (ข้อมูลเบื้องต้นทั่วไป)
- Gemma Thinking (ความสามารถเฉพาะของ Gemma)
การทำความเข้าใจรูปภาพ
โมเดล Gemma 4 สามารถประมวลผลรูปภาพ ซึ่งช่วยให้นักพัฒนาแอปสามารถใช้กรณีการใช้งานที่ล้ำสมัยมากมาย ซึ่งในอดีตจะต้องใช้โมเดลเฉพาะโดเมน
ตัวอย่างต่อไปนี้แสดงวิธีใช้ข้อมูลรูปภาพของ Gemma กับ Gemini API
Python
from google import genai
client = genai.Client()
my_file = client.files.upload(file="path/to/sample.jpg")
response = client.models.generate_content(
model="gemma-4-26b-a4b-it",
contents=[my_file, "Caption this image."],
)
print(response.text)
JavaScript
import {
GoogleGenAI,
createUserContent,
createPartFromUri,
} from "@google/genai";
const ai = new GoogleGenAI();
const myfile = await ai.files.upload({
file: "path/to/sample.jpg",
config: { mimeType: "image/jpeg" },
});
const response = await ai.models.generateContent({
model: "gemma-4-26b-a4b-it",
contents: createUserContent([
createPartFromUri(myfile.uri, myfile.mimeType),
"Caption this image.",
]),
});
console.log(response.text);
```
REST
IMAGE_PATH="cats-and-dogs.jpg"
MIME_TYPE=$(file -b --mime-type "${IMAGE_PATH}")
NUM_BYTES=$(wc -c < "${IMAGE_PATH}")
DISPLAY_NAME=IMAGE
tmp_header_file=upload-header.tmp
# Initial resumable request defining metadata.
curl "https://generativelanguage.googleapis.com/upload/v1beta/files" \
-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 "@${IMAGE_PATH}" 2> /dev/null > file_info.json
file_uri=$(jq -r ".file.uri" file_info.json)
echo file_uri=$file_uri
# Now generate content using that file
curl "https://generativelanguage.googleapis.com/v1beta/models/gemma-4-26b-a4b-it:generateContent" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [{
"parts":[
{"file_data":{"mime_type": "'"${MIME_TYPE}"'", "file_uri": "'"${file_uri}"'"}},
{"text": "Caption this image."}]
}]
}' 2> /dev/null > response.json
cat response.json
echo
jq -r ".candidates[].content.parts[].text" response.json
ดูข้อมูลเพิ่มเติมเกี่ยวกับความเข้าใจรูปภาพ
- การทำความเข้าใจรูปภาพของ Gemini API (ข้อมูลเบื้องต้นทั่วไป)
- การทำความเข้าใจรูปภาพของ Gemma (ความสามารถเฉพาะของ Gemma)
คำสั่งของระบบ
คุณส่งคำสั่งของระบบเพื่อกำหนดลักษณะการทำงานของโมเดลได้โดยทำดังนี้
Python
from google import genai
from google.genai import types
client = genai.Client()
response = client.models.generate_content(
model="gemma-4-26b-a4b-it",
config=types.GenerateContentConfig(
system_instruction="You are a wise Kyoto tea master. Speak calmly and poetically, using nature metaphors. Keep answers under 3 sentences."
),
contents="What is the purpose of the tea ceremony?"
)
print(response.text)
JavaScript
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI();
const response = await ai.models.generateContent({
model: "gemma-4-26b-a4b-it",
contents: "What is the purpose of the tea ceremony?",
config: {
systemInstruction: "You are a wise Kyoto tea master. Speak calmly and poetically, using nature metaphors. Keep answers under 3 sentences."
}
});
console.log(response.text);
REST
curl "https://generativelanguage.googleapis.com/v1beta/models/gemma-4-26b-a4b-it:generateContent" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [{
"parts":[{"text": "What is the purpose of the tea ceremony?"}]
}],
"systemInstruction": {
"parts": [{"text": "You are a wise Kyoto tea master. Speak calmly and poetically, using nature metaphors. Keep answers under 3 sentences."}]
}
}'
การสนทนาแบบหลายรอบ
SDK มีอินเทอร์เฟซแชทที่ติดตามประวัติการสนทนา โดยอัตโนมัติ
Python
from google import genai
client = genai.Client()
chat = client.chats.create(model="gemma-4-26b-a4b-it")
response = chat.send_message("What are the three most famous castles in Japan?")
print(response.text)
response = chat.send_message("Which one should I visit in spring for cherry blossoms?")
print(response.text)
JavaScript
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI();
const chat = ai.chats.create({ model: "gemma-4-26b-a4b-it" });
let response = await chat.sendMessage({ message: "What are the three most famous castles in Japan?" });
console.log(response.text);
response = await chat.sendMessage({ message: "Which one should I visit in spring for cherry blossoms?" });
console.log(response.text);
REST
curl "https://generativelanguage.googleapis.com/v1beta/models/gemma-4-26b-a4b-it:generateContent" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [
{
"role": "user",
"parts": [{ "text": "What are the three most famous castles in Japan?" }]
},
{
"role": "model",
"parts": [{ "text": "Himeji Castle, Matsumoto Castle, and Kumamoto Castle are often considered the top three." }]
},
{
"role": "user",
"parts": [{ "text": "Which one should I visit in spring for cherry blossoms?" }]
}
]
}'
การเรียกใช้ฟังก์ชัน
กำหนดเครื่องมือเป็นการประกาศฟังก์ชัน โมเดลจะตัดสินใจว่าจะเรียกใช้ฟังก์ชันเมื่อใด ดังนี้
Python
from google import genai
from google.genai import types
# Define the function declaration
get_weather = {
"name": "get_weather",
"description": "Get current weather for a given location.",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City and state, e.g. 'San Francisco, CA'",
},
},
"required": ["location"],
},
}
client = genai.Client()
tools = types.Tool(function_declarations=[get_weather])
config = types.GenerateContentConfig(tools=[tools])
response = client.models.generate_content(
model="gemma-4-26b-a4b-it",
contents="Should I bring an umbrella to Kyoto today?",
config=config,
)
# The model returns a function call instead of text
if response.function_calls:
for fc in response.function_calls:
print(f"Function to call: {fc.name}")
print(f"ID: {fc.id}")
print(f"Arguments: {fc.args}")
else:
print("No function call found in the response.")
print(response.text)
JavaScript
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI();
const get_weather = {
name: "get_weather",
description: "Get current weather for a given location.",
parameters: {
type: "object",
properties: {
location: {
type: "string",
description: "City and state, e.g. 'San Francisco, CA'",
},
},
required: ["location"],
},
};
const response = await ai.models.generateContent({
model: "gemma-4-26b-a4b-it",
contents: "Should I bring an umbrella to Kyoto today?",
config: {
tools: [{ functionDeclarations: [get_weather] }]
}
});
if (response.functionCalls) {
for (const fc of response.functionCalls) {
console.log(`Function to call: ${fc.name}`);
console.log(`Arguments: ${JSON.stringify(fc.args)}`);
}
} else {
console.log("No function call found in the response.");
console.log(response.text);
}
REST
curl "https://generativelanguage.googleapis.com/v1beta/models/gemma-4-26b-a4b-it:generateContent" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [{
"parts":[{"text": "Should I bring an umbrella to Kyoto today?"}]
}],
"tools": [{
"functionDeclarations": [{
"name": "get_weather",
"description": "Get current weather for a given location.",
"parameters": {
"type": "OBJECT",
"properties": {
"location": {
"type": "STRING",
"description": "City and state, e.g. 'San Francisco, CA'"
}
},
"required": ["location"]
}
}]
}]
}'
Google Search
อ้างอิงคำตอบของ Ground Gemma 4 จากข้อมูลเว็บแบบเรียลไทม์ด้วย Google Search โดยทำดังนี้
Python
from google import genai
from google.genai import types
client = genai.Client()
response = client.models.generate_content(
model="gemma-4-26b-a4b-it",
contents="What are the dates for cherry blossom season in Tokyo this year?",
config=types.GenerateContentConfig(
tools=[{"google_search":{}}]
),
)
print(response.text)
# Access grounding metadata for citations
for chunk in response.candidates[0].grounding_metadata.grounding_chunks:
print(f"Source: {chunk.web.title} — {chunk.web.uri}")
JavaScript
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI();
const response = await ai.models.generateContent({
model: "gemma-4-26b-a4b-it",
contents: "What are the dates for cherry blossom season in Tokyo this year?",
config: {
tools: [{ googleSearch: {} }]
}
});
console.log(response.text);
if (response.candidates?.[0]?.groundingMetadata?.groundingChunks) {
for (const chunk of response.candidates[0].groundingMetadata.groundingChunks) {
if (chunk.web) {
console.log(`Source: ${chunk.web.title} — ${chunk.web.uri}`);
}
}
}
REST
curl "https://generativelanguage.googleapis.com/v1beta/models/gemma-4-26b-a4b-it:generateContent" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [{
"parts":[{"text": "What are the dates for cherry blossom season in Tokyo this year?"}]
}],
"tools": [{"googleSearch": {}}]
}'