Gemini API क्विकस्टार्ट

इस शुरुआती लेख में, हमारी लाइब्रेरी इंस्टॉल करने और Gemini API का पहला अनुरोध करने का तरीका बताया गया है.

शुरू करने से पहले

आपके पास Gemini API पासकोड होना चाहिए. अगर आपके पास पहले से कोई खाता नहीं है, तो Google AI Studio में जाकर इसे मुफ़्त में पाया जा सकता है.

Google GenAI SDK इंस्टॉल करना

Python

Python 3.9+ का इस्तेमाल करके, यहां दिए गए pip कमांड का इस्तेमाल करके, google-genai पैकेज इंस्टॉल करें:

pip install -q -U google-genai

JavaScript

Node.js v18+ का इस्तेमाल करके, npm कमांड का इस्तेमाल करके, TypeScript और JavaScript के लिए Google Gen AI SDK टूल इंस्टॉल करें:

npm install @google/genai

शुरू करें

go get कमांड का इस्तेमाल करके, अपनी मॉड्यूल डायरेक्ट्री में google.golang.org/genai इंस्टॉल करें:

go get google.golang.org/genai

Apps Script

  1. नया Apps Script प्रोजेक्ट बनाने के लिए, script.new पर जाएं.
  2. बिना टाइटल वाला प्रोजेक्ट पर क्लिक करें.
  3. Apps Script प्रोजेक्ट का नाम बदलकर AI Studio करें और नाम बदलें पर क्लिक करें.
  4. अपना एपीआई पासकोड सेट करें
    1. बाईं ओर, प्रोजेक्ट सेटिंग प्रोजेक्ट सेटिंग का आइकॉन पर क्लिक करें.
    2. स्क्रिप्ट प्रॉपर्टी में जाकर, स्क्रिप्ट प्रॉपर्टी जोड़ें पर क्लिक करें.
    3. प्रॉपर्टी के लिए, कुंजी का नाम डालें: GEMINI_API_KEY.
    4. वैल्यू के लिए, एपीआई पासकोड की वैल्यू डालें.
    5. स्क्रिप्ट प्रॉपर्टी सेव करें पर क्लिक करें.
  5. Code.gs फ़ाइल के कॉन्टेंट को इस कोड से बदलें:

अपना पहला अनुरोध करना

Gemini API को अनुरोध भेजने के लिए, generateContent तरीके का इस्तेमाल करें.

Python

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)

JavaScript

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())
}

Apps Script

// 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);
}

REST

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"
          }
        ]
      }
    ]
  }'

आगे क्या करना है

आपने अपना पहला एपीआई अनुरोध कर दिया है. अब आपको Gemini के काम करने के तरीके के बारे में बताने वाली इन गाइड को पढ़ना चाहिए: