Gemini API की मदद से Gemma को चलाना

Gemini API, Gemma को होस्ट किए गए ऐक्सेस के तौर पर उपलब्ध कराता है. यह एक प्रोग्रामिंग एपीआई है, जिसका इस्तेमाल ऐप्लिकेशन डेवलपमेंट या प्रोटोटाइपिंग में किया जा सकता है. यह एपीआई, Gemma के लोकल इंस्टेंस और जनरेटिव एआई से जुड़े टास्क मैनेज करने के लिए वेब सेवा को सेट अप करने का एक आसान विकल्प है.

काम करने वाले मॉडल

Gemini API, Gemma 4 के इन मॉडल के साथ काम करता है:

  • gemma-4-31b-it
  • gemma-4-26b-a4b-it

इस उदाहरण में, Gemini API के साथ Gemma का इस्तेमाल करने का तरीका दिखाया गया है:

Python

from google import genai

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

response = client.models.generate_content(
    model="gemma-4-31b-it",
    contents="Roses are red...",
)

print(response.text)

Node.js

import { GoogleGenAI } from "@google/genai";

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

const response = await ai.models.generateContent({
  model: "gemma-4-31b-it",
  contents: "Roses are red...",
});
console.log(response.text);

REST

curl "https://generativelanguage.googleapis.com/v1beta/models/gemma-4-31b-it:generateContent?key=YOUR_API_KEY" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
  "contents": [{
    "parts":[{"text": "Roses are red..."}]
    }]
   }'

एपीआई पासकोड पाएं

Gemini API को मोबाइल, वेब, और क्लाउड सेवाओं जैसे कई प्लैटफ़ॉर्म पर ऐक्सेस किया जा सकता है. साथ ही, इसे कई प्रोग्रामिंग भाषाओं के साथ इस्तेमाल किया जा सकता है. Gemini API SDK पैकेज के बारे में ज़्यादा जानने के लिए, Gemini API के एसडीके डाउनलोड पेज पर जाएं. Gemini API के बारे में सामान्य जानकारी के लिए, Gemini API क्विकस्टार्ट देखें.

सूझ-बूझ वाला मॉडल

Gemma 4, "सोचने की प्रोसेस" का इस्तेमाल करता है. इससे, यह कई चरणों में तर्क देने की क्षमता को ऑप्टिमाइज़ करता है. साथ ही, यह लॉजिकल डोमेन में बेहतर परफ़ॉर्म करता है. जैसे, एल्गोरिथम कोडिंग और गणित के मुश्किल सवालों को हल करना.

Gemma 4 में, इस सुविधा को चालू या बंद करने का विकल्प होता है. हालांकि, एपीआई में इसे चालू करने के लिए, थिंकिंग लेवल को "high" पर सेट करना होता है.

यहां दिए गए उदाहरण में, सोचने की प्रोसेस को चालू करने का तरीका बताया गया है:

Python

from google import genai

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

response = client.models.generate_content(
    model="gemma-4-31b-it",
    contents="What is the water formula?",
    config=types.GenerateContentConfig(
        thinking_config=types.ThinkingConfig(thinking_level="high")
    ),
)

print(response.text)

Node.js

import { GoogleGenAI } from "@google/genai";

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

const response = await ai.models.generateContent({
  model: "gemma-4-31b-it",
  contents: "What is the water formula?",
  config: {
    thinkingConfig: {
      thinkingLevel: ThinkingLevel.HIGH,
    },
  },
});
console.log(response.text);

REST

curl "https://generativelanguage.googleapis.com/v1beta/models/gemma-4-31b-it:generateContent?key=YOUR_API_KEY" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
  "contents": [{
    "parts":[{"text": "What is the water formula?"}]
    }],
    "generationConfig": {
      "thinkingConfig": {
            "thinkingLevel": "high"
      }
    }
   }'

सोचने के बारे में ज़्यादा जानें:

इमेज की बारीक़ी से पहचान

Gemma 4 मॉडल, इमेज को प्रोसेस कर सकते हैं. इससे डेवलपर को कई नई सुविधाएं मिलती हैं. पहले इन सुविधाओं के लिए, डोमेन के हिसाब से मॉडल की ज़रूरत होती थी.

इस उदाहरण में, Gemini API के साथ Gemma की इमेज इनपुट का इस्तेमाल करने का तरीका दिखाया गया है:

Python

from google import genai

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

my_file = client.files.upload(file="path/to/sample.jpg")

response = client.models.generate_content(
    model="gemma-4-31b-it",
    contents=[my_file, "Caption this image."],
)

print(response.text)

Node.js

import {
  GoogleGenAI,
  createUserContent,
  createPartFromUri,
} from "@google/genai";

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

const myfile = await ai.files.upload({
  file: "path/to/sample.jpg",
  config: { mimeType: "image/jpeg" },
});

const response = await ai.models.generateContent({
  model: "gemma-4-31b-it",
  contents: createUserContent([
    createPartFromUri(myfile.uri, myfile.mimeType),
    "Caption this image.",
  ]),
});
console.log(response.text);
 ```

REST

IMAGE_PATH="cats-and-dogs.jpg"
MIME_TYPE=$(file -b --mime-type "${IMAGE_PATH}")
NUM_BYTES=$(wc -c < "${IMAGE_PATH}")
DISPLAY_NAME=IMAGE

tmp_header_file=upload-header.tmp

# Initial resumable request defining metadata.
# The upload url is in the response headers dump them to a file.
curl "https://generativelanguage.googleapis.com/upload/v1beta/files?key=YOUR_API_KEY" \
  -D upload-header.tmp \
  -H "X-Goog-Upload-Protocol: resumable" \
  -H "X-Goog-Upload-Command: start" \
  -H "X-Goog-Upload-Header-Content-Length: ${NUM_BYTES}" \
  -H "X-Goog-Upload-Header-Content-Type: ${MIME_TYPE}" \
  -H "Content-Type: application/json" \
  -d "{'file': {'display_name': '${DISPLAY_NAME}'}}" 2> /dev/null

upload_url=$(grep -i "x-goog-upload-url: " "${tmp_header_file}" | cut -d" " -f2 | tr -d "\r")
rm "${tmp_header_file}"

# Upload the actual bytes.
curl "${upload_url}" \
  -H "Content-Length: ${NUM_BYTES}" \
  -H "X-Goog-Upload-Offset: 0" \
  -H "X-Goog-Upload-Command: upload, finalize" \
  --data-binary "@${IMAGE_PATH}" 2> /dev/null > file_info.json

file_uri=$(jq -r ".file.uri" file_info.json)
echo file_uri=$file_uri

# Now generate content using that file
curl "https://generativelanguage.googleapis.com/v1beta/models/gemma-4-31b-it:generateContent?key=YOUR_API_KEY" \
    -H 'Content-Type: application/json' \
    -X POST \
    -d '{
      "contents": [{
        "parts":[
          {"file_data":{"mime_type": "'"${MIME_TYPE}"'", "file_uri": "'"${file_uri}"'"}},
          {"text": "Caption this image."}]
        }]
      }' 2> /dev/null > response.json

cat response.json
echo

jq -r ".candidates[].content.parts[].text" response.json

इमेज को समझने की सुविधा के बारे में ज़्यादा जानें: