این شروع سریع به شما نشان می دهد که چگونه کتابخانه های ما را نصب کنید و اولین درخواست Gemini API خود را انجام دهید.
شما به یک کلید API Gemini نیاز دارید. اگر قبلاً ندارید، میتوانید آن را به صورت رایگان در Google AI Studio دریافت کنید .
با استفاده از 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"
}
]
}
]
}'
بسیاری از نمونههای کد موجود در این سایت از مدل فلش جمینی 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 را در عمل نشان میدهند، بررسی کنید: