يوضّح لك دليل البدء السريع هذا كيفية تثبيت المكتبات وتقديم طلبك الأول باستخدام Gemini API.
قبل البدء
يجب توفّر مفتاح واجهة برمجة التطبيقات في Gemini. إذا لم يكن لديك حساب، يمكنك الحصول عليه مجانًا في Google AI Studio.
تثبيت حزمة تطوير البرامج (SDK) لتكنولوجيات الذكاء الاصطناعي التوليدي من Google
Python
باستخدام Python 3.9 والإصدارات الأحدث، ثبِّت
حزمة google-genai
باستخدام
أمر pip التالي:
pip install -q -U google-genai
JavaScript
باستخدام الإصدار 18 من Node.js والإصدارات الأحدث، ثبِّت حزمة تطوير البرامج (SDK) لميزة "الذكاء الاصطناعي التوليدي من Google" لـ TypeScript وJavaScript باستخدام volgendأمر npm:
npm install @google/genai
انتقال
ثبِّت google.golang.org/genai في دليل الوحدة باستخدام أمر go get:
go get google.golang.org/genai
Java
إذا كنت تستخدم Maven، يمكنك تثبيت google-genai من خلال إضافة ما يلي إلى التبعيات:
<dependencies>
<dependency>
<groupId>com.google.genai</groupId>
<artifactId>google-genai</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
برمجة تطبيقات
- لإنشاء مشروع جديد في Apps Script، انتقِل إلى script.new.
- انقر على مشروع بلا عنوان.
- أعِد تسمية مشروع Apps Script AI Studio وانقر على إعادة التسمية.
- ضبط مفتاح واجهة برمجة التطبيقات
- على يمين الصفحة، انقر على إعدادات المشروع
.
- ضمن خصائص النصوص البرمجية، انقر على إضافة خاصية نص برمجي.
- بالنسبة إلى الموقع، أدخِل اسم المفتاح:
GEMINI_API_KEY
. - في حقل القيمة، أدخِل قيمة مفتاح واجهة برمجة التطبيقات.
- انقر على حفظ مواقع النصوص البرمجية.
- على يمين الصفحة، انقر على إعدادات المشروع
- استبدِل محتويات ملف
Code.gs
بالرمز التالي:
تقديم طلبك الأول
في ما يلي مثال يستخدم الأسلوب
generateContent
لإرسال طلب إلى Gemini API باستخدام نموذج Gemini 2.5 Flash.
إذا ضبطت مفتاح واجهة برمجة التطبيقات على المتغيّر البيئي GEMINI_API_KEY
، سيلتقطه العميل تلقائيًا عند استخدام مكتبات Gemini API.
بخلاف ذلك، عليك تمرير مفتاح واجهة برمجة التطبيقات كأحد
المَعلمات عند إعداد العميل.
تجدر الإشارة إلى أنّ جميع عيّنات الرموز البرمجية في مستندات Gemini API تفترض أنّك ضبطت
متغيّر البيئة GEMINI_API_KEY
.
Python
from google import genai
# The client gets the API key from the environment variable `GEMINI_API_KEY`.
client = genai.Client()
response = client.models.generate_content(
model="gemini-2.5-flash", contents="Explain how AI works in a few words"
)
print(response.text)
JavaScript
import { GoogleGenAI } from "@google/genai";
// The client gets the API key from the environment variable `GEMINI_API_KEY`.
const ai = new GoogleGenAI({});
async function main() {
const response = await ai.models.generateContent({
model: "gemini-2.5-flash",
contents: "Explain how AI works in a few words",
});
console.log(response.text);
}
main();
انتقال
package main
import (
"context"
"fmt"
"log"
"google.golang.org/genai"
)
func main() {
ctx := context.Background()
// The client gets the API key from the environment variable `GEMINI_API_KEY`.
client, err := genai.NewClient(ctx, nil)
if err != nil {
log.Fatal(err)
}
result, err := client.Models.GenerateContent(
ctx,
"gemini-2.5-flash",
genai.Text("Explain how AI works in a few words"),
nil,
)
if err != nil {
log.Fatal(err)
}
fmt.Println(result.Text())
}
Java
package com.example;
import com.google.genai.Client;
import com.google.genai.types.GenerateContentResponse;
public class GenerateTextFromTextInput {
public static void main(String[] args) {
// The client gets the API key from the environment variable `GEMINI_API_KEY`.
Client client = new Client();
GenerateContentResponse response =
client.models.generateContent(
"gemini-2.5-flash",
"Explain how AI works in a few words",
null);
System.out.println(response.text());
}
}
برمجة تطبيقات
// See https://developers.google.com/apps-script/guides/properties
// for instructions on how to set the API key.
const apiKey = PropertiesService.getScriptProperties().getProperty('GEMINI_API_KEY');
function main() {
const payload = {
contents: [
{
parts: [
{ text: 'Explain how AI works in a few words' },
],
},
],
};
const url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent';
const options = {
method: 'POST',
contentType: 'application/json',
headers: {
'x-goog-api-key': apiKey,
},
payload: JSON.stringify(payload)
};
const response = UrlFetchApp.fetch(url, options);
const data = JSON.parse(response);
const content = data['candidates'][0]['content']['parts'][0]['text'];
console.log(content);
}
REST
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [
{
"parts": [
{
"text": "Explain how AI works in a few words"
}
]
}
]
}'
تكون ميزة "التفكير" مفعّلة تلقائيًا في العديد من نماذج الرموز البرمجية.
تستخدِم العديد من نماذج الرموز البرمجية على هذا الموقع الإلكتروني نموذج Gemini 2.5 Flash، الذي تم تفعيل ميزة "التفكير" فيه تلقائيًا لتحسين جودة الردود. يُرجى العِلم أنّ هذا قد يؤدي إلى زيادة وقت الاستجابة واستخدام الرموز المميّزة. إذا كنت تمنح الأولوية للسرعة أو كنت تريد تقليل التكاليف، يمكنك إيقاف هذه الميزة من خلال ضبط ميزانية التفكير على القيمة صفر، كما هو موضّح في الأمثلة أدناه. لمزيد من التفاصيل، يُرجى الاطّلاع على دليل التفكير.
Python
from google import genai
from google.genai import types
client = genai.Client()
response = client.models.generate_content(
model="gemini-2.5-flash",
contents="Explain how AI works in a few words",
config=types.GenerateContentConfig(
thinking_config=types.ThinkingConfig(thinking_budget=0) # Disables thinking
),
)
print(response.text)
JavaScript
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({});
async function main() {
const response = await ai.models.generateContent({
model: "gemini-2.5-flash",
contents: "Explain how AI works in a few words",
config: {
thinkingConfig: {
thinkingBudget: 0, // Disables thinking
},
}
});
console.log(response.text);
}
await main();
انتقال
package main
import (
"context"
"fmt"
"os"
"google.golang.org/genai"
)
func main() {
ctx := context.Background()
client, err := genai.NewClient(ctx, nil)
if err != nil {
log.Fatal(err)
}
result, _ := client.Models.GenerateContent(
ctx,
"gemini-2.5-flash",
genai.Text("Explain how AI works in a few words"),
&genai.GenerateContentConfig{
ThinkingConfig: &genai.ThinkingConfig{
ThinkingBudget: int32(0), // Disables thinking
},
}
)
fmt.Println(result.Text())
}
REST
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [
{
"parts": [
{
"text": "Explain how AI works in a few words"
}
]
}
]
"generationConfig": {
"thinkingConfig": {
"thinkingBudget": 0
}
}
}'
برمجة تطبيقات
// See https://developers.google.com/apps-script/guides/properties
// for instructions on how to set the API key.
const apiKey = PropertiesService.getScriptProperties().getProperty('GEMINI_API_KEY');
function main() {
const payload = {
contents: [
{
parts: [
{ text: 'Explain how AI works in a few words' },
],
},
],
};
const url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent';
const options = {
method: 'POST',
contentType: 'application/json',
headers: {
'x-goog-api-key': apiKey,
},
payload: JSON.stringify(payload)
};
const response = UrlFetchApp.fetch(url, options);
const data = JSON.parse(response);
const content = data['candidates'][0]['content']['parts'][0]['text'];
console.log(content);
}
الخطوات التالية
بعد أن أرسلت طلبك الأول إلى واجهة برمجة التطبيقات، ننصحك باستكشاف المراجع التالية التي توضّح كيفية استخدام Gemini: