Gemini API شروع سریع

این شروع سریع به شما نشان می دهد که چگونه کتابخانه های ما را نصب کنید و اولین درخواست 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
  1. برای ایجاد یک پروژه Apps Script جدید، به script.new بروید.
  2. پروژه Untitled را کلیک کنید.
  3. نام پروژه Apps Script را به AI Studio تغییر دهید و روی Rename کلیک کنید.
  4. کلید API خود را تنظیم کنید
    1. در سمت چپ، روی تنظیمات پروژه کلیک کنید نماد تنظیمات پروژه .
    2. در زیر ویژگی های اسکریپت، روی افزودن ویژگی اسکریپت کلیک کنید.
    3. برای Property ، نام کلید را وارد کنید: GEMINI_API_KEY .
    4. برای مقدار ، مقدار کلید API را وارد کنید.
    5. روی ذخیره خصوصیات اسکریپت کلیک کنید.
  5. محتوای فایل Code.gs را با کد زیر جایگزین کنید:

اولین درخواست خود را مطرح کنید

برای ارسال درخواست به Gemini API از متد generateContent استفاده کنید.

from google import genai

client = genai.Client(api_key="YOUR_API_KEY")

response = client.models.generate_content(
    model="gemini-2.0-flash", contents="Explain how AI works in a few words"
)
print(response.text)
import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI({ apiKey: "YOUR_API_KEY" });

async function main() {
  const response = await ai.models.generateContent({
    model: "gemini-2.0-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()
    client, err := genai.NewClient(ctx, &genai.ClientConfig{
        APIKey:  "YOUR_API_KEY",
        Backend: genai.BackendGeminiAPI,
    })
    if err != nil {
        log.Fatal(err)
    }

    result, err := client.Models.GenerateContent(
        ctx,
        "gemini-2.0-flash",
        genai.Text("Explain how AI works in a few words"),
        nil,
    )
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(result.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.0-flash:generateContent?key=${apiKey}`;
  const options = {
    method: 'POST',
    contentType: 'application/json',
    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.0-flash:generateContent?key=$YOUR_API_KEY" \
  -H 'Content-Type: application/json' \
  -X POST \
  -d '{
    "contents": [
      {
        "parts": [
          {
            "text": "Explain how AI works in a few words"
          }
        ]
      }
    ]
  }'

بعدش چی

اکنون که اولین درخواست API خود را انجام دادید، ممکن است بخواهید راهنماهای زیر را که Gemini را در عمل نشان می‌دهند، بررسی کنید: