این شروع سریع به شما نشان می دهد که چگونه کتابخانه های ما را نصب کنید و اولین درخواست Gemini API خود را انجام دهید.
قبل از شروع
شما به یک کلید Gemini API نیاز دارید. اگر قبلاً ندارید، میتوانید آن را به صورت رایگان در Google AI Studio دریافت کنید .
Google GenAI SDK را نصب کنید
پایتون
با استفاده از Python 3.9+ ، بسته google-genai
را با استفاده از دستور pip زیر نصب کنید:
pip install -q -U google-genai
جاوا اسکریپت
با استفاده از Node.js v18+ ، Google Gen AI SDK را برای TypeScript و JavaScript با استفاده از دستور npm زیر نصب کنید:
npm install @google/genai
برو
با استفاده از دستور go get google.golang.org/genai را در فهرست ماژول خود نصب کنید:
go get google.golang.org/genai
جاوا
اگر از 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 بروید.
- پروژه Untitled را کلیک کنید.
- نام پروژه Apps Script را به AI Studio تغییر دهید و روی Rename کلیک کنید.
- کلید API خود را تنظیم کنید
- در سمت چپ، روی تنظیمات پروژه کلیک کنید
.
- در زیر ویژگی های اسکریپت، روی افزودن ویژگی اسکریپت کلیک کنید.
- برای Property ، نام کلید را وارد کنید:
GEMINI_API_KEY
. - برای مقدار ، مقدار کلید API را وارد کنید.
- روی ذخیره خصوصیات اسکریپت کلیک کنید.
- در سمت چپ، روی تنظیمات پروژه کلیک کنید
- محتوای فایل
Code.gs
را با کد زیر جایگزین کنید:
اولین درخواست خود را مطرح کنید
در اینجا یک مثال است که از متد generateContent
برای ارسال درخواست به Gemini API با استفاده از مدل Gemini 2.5 Flash استفاده می کند.
اگر کلید API خود را بهعنوان متغیر محیطی GEMINI_API_KEY
تنظیم کنید، هنگام استفاده از کتابخانههای Gemini API، بهطور خودکار توسط مشتری دریافت میشود. در غیر این صورت باید کلید API خود را به عنوان یک آرگومان در هنگام مقداردهی اولیه مشتری ارسال کنید .
توجه داشته باشید که همه نمونههای کد موجود در اسناد API Gemini فرض میکنند که شما متغیر محیطی GEMINI_API_KEY
را تنظیم کردهاید.
پایتون
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)
جاوا اسکریپت
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())
}
جاوا
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);
}
استراحت
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"
}
]
}
]
}'
"Thinking" به طور پیش فرض در بسیاری از نمونه های کد ما روشن است
بسیاری از نمونههای کد موجود در این سایت از مدل فلش جمینی 2.5 استفاده میکنند که قابلیت «تفکر» را بهطور پیشفرض فعال کرده است تا کیفیت پاسخگویی را افزایش دهد. باید توجه داشته باشید که این ممکن است زمان پاسخگویی و استفاده از توکن را افزایش دهد. اگر سرعت را در اولویت قرار میدهید یا میخواهید هزینهها را به حداقل برسانید، میتوانید این ویژگی را با صفر کردن بودجه فکری، همانطور که در مثالهای زیر نشان داده شده است، غیرفعال کنید. برای جزئیات بیشتر، راهنمای تفکر را ببینید.
پایتون
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)
جاوا اسکریپت
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())
}
استراحت
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);
}
بعدش چی
اکنون که اولین درخواست API خود را انجام دادید، ممکن است بخواهید راهنماهای زیر را که Gemini را در عمل نشان میدهند، بررسی کنید: