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

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

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

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

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

Python

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

pip install -q -U google-genai

JavaScript

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

npm install @google/genai

शुरू करें

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

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

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

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

यहां एक उदाहरण दिया गया है, जिसमें Gemini 2.5 Flash मॉडल का इस्तेमाल करके Gemini API को अनुरोध भेजने के लिए, generateContent तरीके का इस्तेमाल किया गया है.

अगर आपने एपीआई पासकोड को एनवायरमेंट वैरिएबल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());
  }
}

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.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
      }
    }
  }'

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.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 को काम करते हुए दिखाया गया है: