يوضّح لك هذا الدليل السريع كيفية تثبيت المكتبات وإجراء طلبك الأول، وبث الردود، وإنشاء محادثات متعدّدة الأدوار، واستخدام الأدوات.
يمكنك استخدام طريقتَين لإرسال طلب إلى Gemini API:
- (يُنصح به) واجهة Interactions API هي عنصر أساسي جديد يتضمّن دعمًا مدمجًا لاستخدام الأدوات المتعدّدة الخطوات، والتنسيق، وتدفقات الاستدلال المعقّدة من خلال خطوات التنفيذ المكتوبة. في المستقبل، سيتم إطلاق الطُرز الجديدة التي تتجاوز عائلة النماذج الأساسية، بالإضافة إلى إمكانات بالذكاء الاصطناعي الوكيل والأدوات الجديدة، حصريًا على Interactions API.
- توفّر
generateContentطريقة لإنشاء ردّ بدون حالة من نموذج. مع أنّنا ننصح باستخدام Interactions API، إلا أنّgenerateContentمتوافق تمامًا.
يستخدم هذا الإصدار من دليل التشغيل السريع Interactions API لإرسال طلب إلى Gemini API.
قبل البدء
لاستخدام Gemini API، يجب أن يكون لديك مفتاح واجهة برمجة تطبيقات للمصادقة على طلباتك وفرض حدود الأمان وتتبُّع الاستخدام في حسابك.
يمكنك إنشاء واحد مجانًا في AI Studio للبدء:
تثبيت حزمة تطوير البرامج (SDK) من Google GenAI
Python
باستخدام Python 3.9 أو إصدار أحدث، ثبِّت حزمة
google-genai
باستخدام
أمر pip التالي:
pip install -q -U google-genai
JavaScript
باستخدام Node.js الإصدار 18 أو إصدار أحدث، ثبِّت حزمة تطوير البرامج (SDK) من Google للذكاء الاصطناعي التوليدي المتوافقة مع TypeScript وJavaScript باستخدام أمر npm التالي:
npm install @google/genai
إنشاء نص
استخدِم طريقة interactions.create
لإنشاء ردّ نصي.
Python
from google import genai
client = genai.Client()
interaction = client.interactions.create(
model="gemini-3.5-flash",
input="Explain how AI works in a few words"
)
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.5-flash",
input: "Explain how AI works in a few words",
});
console.log(interaction.output_text);
}
main();
REST
curl -X POST \
"https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-H "Api-Revision: 2026-05-20" \
-d '{
"model": "gemini-3.5-flash",
"input": "Explain how AI works in a few words"
}'
عرض الردود تدريجيًا
تعرض النماذج تلقائيًا استجابة بعد اكتمال عملية الإنشاء بأكملها. للحصول على تجربة أسرع وأكثر تفاعلية، يمكنك بث أجزاء الرد أثناء إنشائها.
Python
stream = client.interactions.create(
model="gemini-3.5-flash",
input="Explain how AI works in detail",
stream=True
)
for event in stream:
if event.event_type == "step.delta":
if event.delta.type == "text":
print(event.delta.text, end="", flush=True)
JavaScript
async function main() {
const stream = await ai.interactions.create({
model: "gemini-3.5-flash",
input: "Explain how AI works in detail",
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);
}
}
}
}
main();
REST
# Use alt=sse for streaming
curl -X POST \
"https://generativelanguage.googleapis.com/v1beta/interactions?alt=sse" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-H "Api-Revision: 2026-05-20" \
--no-buffer \
-d '{
"model": "gemini-3.5-flash",
"input": "Explain how AI works in detail",
"stream": true
}'
المحادثات المتعدّدة الجولات
تتضمّن Gemini API إمكانات مدمجة لتصميم محادثات متعدّدة الأدوار.
ما عليك سوى تمرير id الذي تم عرضه من التفاعل السابق كالمَعلمة previous_interaction_id، وسيتولّى الخادم تلقائيًا إدارة سجلّ المحادثات.
Python
interaction1 = client.interactions.create(
model="gemini-3.5-flash",
input="I have 2 dogs in my house."
)
print("Response 1:", interaction1.output_text)
interaction2 = client.interactions.create(
model="gemini-3.5-flash",
input="How many paws are in my house?",
previous_interaction_id=interaction1.id
)
print("Response 2:", interaction2.output_text)
JavaScript
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.output_text);
const interaction2 = await ai.interactions.create({
model: "gemini-3-flash-preview",
input: "How many paws are in my house?",
previous_interaction_id: interaction1.id,
});
console.log("Response 2:", interaction2.output_text);
}
main();
REST
# Turn 1: Start the conversation
RESPONSE1=$(curl -s -X POST \
"https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "Api-Revision: 2026-05-20" \
-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."
}')
# Extract the interaction ID
INTERACTION_ID=$(echo "$RESPONSE1" | jq -r '.id')
# Turn 2: Continue the conversation
curl -X POST \
"https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "Api-Revision: 2026-05-20" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-d "{
\"model\": \"gemini-3-flash-preview\",
\"input\": \"How many paws are in my house?\",
\"previous_interaction_id\": \"$INTERACTION_ID\"
}"
استخدام الأدوات
يمكنك توسيع نطاق إمكانات النموذج من خلال تحديد مصدر الردود باستخدام "بحث Google" للوصول إلى محتوى الويب في الوقت الفعلي. يقرّر النموذج تلقائيًا متى يبدأ البحث، وينفّذ طلبات البحث، ويصوغ ردًا يتضمّن اقتباسات.
يوضّح المثال التالي كيفية تفعيل "بحث Google":
Python
interaction = client.interactions.create(
model="gemini-3-flash-preview",
input="Who won the euro 2024?",
tools=[{"type": "google_search"}]
)
print(interaction.output_text)
for step in interaction.steps:
if step.type == "model_output":
for content_block in step.content:
if content_block.type == "text" and content_block.annotations:
print("\nCitations:")
for annotation in content_block.annotations:
if annotation.type == "url_citation":
print(f" - [{annotation.title}]({annotation.url})")
JavaScript
async function main() {
const interaction = await ai.interactions.create({
model: "gemini-3-flash-preview",
input: "Who won the euro 2024?",
tools: [{ type: "google_search" }]
});
console.log(interaction.output_text);
for (const step of interaction.steps) {
if (step.type === 'model_output') {
for (const contentBlock of step.content) {
if (contentBlock.type === 'text' && contentBlock.annotations) {
console.log("\nCitations:");
for (const annotation of contentBlock.annotations) {
if (annotation.type === 'url_citation') {
console.log(` - [${annotation.title}](${annotation.url})`);
}
}
}
}
}
}
}
main();
REST
curl -X POST \
"https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "Api-Revision: 2026-05-20" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gemini-3-flash-preview",
"input": "Who won the euro 2024?",
"tools": [{"type": "google_search"}]
}'
تتيح Gemini API أيضًا استخدام أدوات مضمّنة أخرى:
- تنفيذ الرمز البرمجي: يتيح للنموذج كتابة رموز Python البرمجية وتشغيلها لحلّ المسائل الرياضية المعقّدة.
- سياق عنوان URL: يتيح لك تحديد سياق الردود في عناوين URL معيّنة لصفحات الويب تقدّمها.
- البحث عن الملفات: تتيح لك هذه الميزة تحميل الملفات وتحديد مصدر الردود من محتواها باستخدام البحث الدلالي.
- خرائط Google: تتيح لك تحديد الموقع الجغرافي في الردود والبحث عن الأماكن والاتجاهات والخرائط.
- استخدام الكمبيوتر: تتيح هذه الميزة للنموذج التفاعل مع شاشة ولوحة مفاتيح وفأرة افتراضية على الكمبيوتر لتنفيذ المهام.
استدعاء دوال مخصّصة
استخدِم ميزة استدعاء الدوال
لربط النماذج بأدواتك وواجهات برمجة التطبيقات المخصّصة. يحدّد النموذج وقت استدعاء الدالة ويعرض خطوة function_call مع وسيطات لتنفيذها في تطبيقك.
يعرّف هذا المثال دالة وهمية لدرجة الحرارة ويتحقّق مما إذا كان النموذج يريد استدعاءها.
Python
import json
weather_function = {
"type": "function",
"name": "get_current_temperature",
"description": "Gets the current temperature for a given location.",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city name, e.g. San Francisco",
},
},
"required": ["location"],
},
}
interaction = client.interactions.create(
model="gemini-3-flash-preview",
input="What's the temperature in London?",
tools=[weather_function],
)
fc_step = None
for step in interaction.steps:
if step.type == "function_call":
fc_step = step
break
if fc_step:
print(f"Model requested function: {fc_step.name} with args {fc_step.arguments}")
mock_result = {"temperature": "15C", "condition": "Cloudy"}
final_interaction = client.interactions.create(
model="gemini-3-flash-preview",
input=[
{
"type": "function_result",
"name": fc_step.name,
"call_id": fc_step.id,
"result": [{"type": "text", "text": json.dumps(mock_result)}],
}
],
tools=[weather_function],
previous_interaction_id=interaction.id,
)
print("Final Response:", final_interaction.output_text)
JavaScript
async function main() {
const weatherFunction = {
type: 'function',
name: 'get_current_temperature',
description: 'Gets the current temperature for a given location.',
parameters: {
type: 'object',
properties: {
location: {
type: 'string',
description: 'The city name, e.g. San Francisco',
},
},
required: ['location'],
},
};
const interaction = await ai.interactions.create({
model: 'gemini-3-flash-preview',
input: "What's the temperature in London?",
tools: [weatherFunction],
});
const fcStep = interaction.steps.find(s => s.type === 'function_call');
if (fcStep) {
console.log(`Model requested function: ${fcStep.name}`);
const mockResult = { temperature: "15C", condition: "Cloudy" };
const finalInteraction = await ai.interactions.create({
model: 'gemini-3-flash-preview',
input: [{
type: 'function_result',
name: fcStep.name,
call_id: fcStep.id,
result: [{ type: 'text', text: JSON.stringify(mockResult) }]
}],
tools: [weatherFunction],
previous_interaction_id: interaction.id,
});
console.log("Final Response:", finalInteraction.output_text);
}
}
main();
REST
curl -X POST \
"https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "Api-Revision: 2026-05-20" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gemini-3-flash-preview",
"input": "What'\''s the temperature in London?",
"tools": [{
"type": "function",
"name": "get_current_temperature",
"description": "Gets the current temperature for a given location.",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string", "description": "The city name"}
},
"required": ["location"]
}
}]
}'
الخطوات التالية
بعد أن بدأت استخدام واجهة Gemini API، يمكنك الاطّلاع على الأدلة التالية لإنشاء تطبيقات أكثر تقدّمًا: