इस शुरुआती लेख में, हमारी लाइब्रेरी इंस्टॉल करने और 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
Java
अगर Maven का इस्तेमाल किया जा रहा है, तो अपनी डिपेंडेंसी में ये जोड़कर, google-genai इंस्टॉल किया जा सकता है:
<dependencies>
<dependency>
<groupId>com.google.genai</groupId>
<artifactId>google-genai</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
Apps Script
- नया Apps Script प्रोजेक्ट बनाने के लिए, script.new पर जाएं.
- बिना टाइटल वाला प्रोजेक्ट पर क्लिक करें.
- Apps Script प्रोजेक्ट का नाम बदलकर AI Studio करें और नाम बदलें पर क्लिक करें.
- अपना एपीआई पासकोड सेट करें
- बाईं ओर, प्रोजेक्ट सेटिंग
पर क्लिक करें.
- स्क्रिप्ट प्रॉपर्टी में जाकर, स्क्रिप्ट प्रॉपर्टी जोड़ें पर क्लिक करें.
- प्रॉपर्टी के लिए, कुंजी का नाम डालें:
GEMINI_API_KEY
. - वैल्यू के लिए, एपीआई पासकोड की वैल्यू डालें.
- स्क्रिप्ट प्रॉपर्टी सेव करें पर क्लिक करें.
- बाईं ओर, प्रोजेक्ट सेटिंग
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 के काम करने के तरीके के बारे में बताने वाली ये गाइड देखनी चाहिए: