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?"
}'
ערכות ה-SDK של Google GenAI מספקות מאפייני נוחות ישירות באובייקט Interaction שמוחזר, כדי לגשת לתגובה של המודל.
העוזר הנפוץ ביותר הוא interaction.output_text (מחרוזת), שמחזיר את בלוקי הטקסט האחרונים בתשובה של המודל. אם התשובה מחולקת לכמה בלוקים רצופים של 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 import genai
client = genai.Client()
interaction = client.interactions.create(
model="gemini-3.6-flash",
input="Explain how AI works",
generation_config={
"temperature": 1.0
}
)
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: "Explain how AI works",
generation_config: {
temperature: 1.0,
},
});
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": "Explain how AI works",
"generation_config": {
"temperature": 1.0
}
}'
רשימה מלאה של הפרמטרים שאפשר להגדיר והתיאורים שלהם מופיעה במסמכי העזר של 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.6-flash",
input=[
{"type": "text", "text": "Tell me about this instrument"},
{
"type": "image",
"uri": uploaded_file.uri,
"mime_type": uploaded_file.mime_type
}
]
)
print(interaction.output_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.6-flash",
input: [
{type: "text", text: "Tell me about this instrument"},
{
type: "image",
uri: uploadedFile.uri,
mime_type: uploadedFile.mimeType
}
],
});
console.log(interaction.output_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.6-flash",
"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.6-flash",
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.6-flash",
input: "Explain how AI works",
stream: true,
});
for await (const event of stream) {
if (event.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.6-flash",
"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.6-flash",
input