इस क्विकस्टार्ट में, लाइब्रेरी इंस्टॉल करने का तरीका बताया गया है. साथ ही, इसमें पहला अनुरोध करने, जवाबों को स्ट्रीम करने, कई बार बातचीत करने की सुविधा बनाने, और टूल इस्तेमाल करने का तरीका बताया गया है.
Gemini API को अनुरोध भेजने के लिए, इन दो तरीकों का इस्तेमाल किया जा सकता है:
- (सुझाया गया) Interactions API एक नया प्रिमिटिव है. इसमें कई चरणों वाले टूल के इस्तेमाल, ऑर्केस्ट्रेशन, और टाइप किए गए एक्ज़ीक्यूशन चरणों के ज़रिए जटिल तर्क वाले फ़्लो के लिए, पहले से मौजूद सहायता शामिल है. आने वाले समय में, मुख्य मॉडल फ़ैमिली के अलावा अन्य नए मॉडल, एजेंट की तरह काम करने वाली नई सुविधाएं, और टूल सिर्फ़ Interactions API पर लॉन्च किए जाएंगे.
generateContentकी मदद से, मॉडल से स्टेटलेस जवाब जनरेट किया जा सकता है. हमारा सुझाव है कि Interactions API का इस्तेमाल करें. हालांकि,generateContentका इस्तेमाल भी किया जा सकता है.
क्विकस्टार्ट के इस वर्शन में, Gemini API को अनुरोध भेजने के लिए Interactions API का इस्तेमाल किया जाता है.
शुरू करने से पहले
Gemini API का इस्तेमाल करने के लिए, आपके पास एक एपीआई पासकोड होना चाहिए. इससे आपके अनुरोधों की पुष्टि की जा सकेगी, सुरक्षा से जुड़ी सीमाएं लागू की जा सकेंगी, और आपके खाते के इस्तेमाल को ट्रैक किया जा सकेगा.
शुरू करने के लिए, AI Studio पर मुफ़्त में एक प्रोजेक्ट बनाएं:
Google GenAI SDK इंस्टॉल करना
Python
Python 3.9 या इसके बाद के वर्शन का इस्तेमाल करके, google-genai पैकेज इंस्टॉल करें. इसके लिए, यहां दी गई pip कमांड का इस्तेमाल करें:
pip install -q -U google-genai
JavaScript
Node.js v18+ का इस्तेमाल करके, TypeScript और JavaScript के लिए Google Gen AI SDK इंस्टॉल करें. इसके लिए, यहां दी गई npm कमांड का इस्तेमाल करें:
npm install @google/genai
टेक्स्ट जनरेट करना
interactions.create तरीके का इस्तेमाल करके, टेक्स्ट वाला जवाब जनरेट करें.
Python
from google import genai
client = genai.Client()
interaction = client.interactions.create(
model="gemini-3.5-flash",
input="Explain how AI works in a few words"
)
print(interaction.output_text)
JavaScript
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({});
async function main() {
const interaction = await ai.interactions.create({
model: "gemini-3.5-flash",
input: "Explain how AI works in a few words",
});
console.log(interaction.output_text);
}
main();
REST
curl -X POST \
"https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-H "Api-Revision: 2026-05-20" \
-d '{
"model": "gemini-3.5-flash",
"input": "Explain how AI works in a few words"
}'
जवाब स्ट्रीम करें
डिफ़ॉल्ट रूप से, मॉडल जवाब सिर्फ़ तब देता है, जब जनरेट करने की पूरी प्रोसेस पूरी हो जाती है. तेज़ी से और ज़्यादा इंटरैक्टिव अनुभव पाने के लिए, जवाब जनरेट होने के साथ-साथ, जवाब के हिस्सों को स्ट्रीम किया जा सकता है.
Python
stream = client.interactions.create(
model="gemini-3.5-flash",
input="Explain how AI works in detail",
stream=True
)
for event in stream:
if event.event_type == "step.delta":
if event.delta.type == "text":
print(event.delta.text, end="", flush=True)
JavaScript
async function main() {
const stream = await ai.interactions.create({
model: "gemini-3.5-flash",
input: "Explain how AI works in detail",
stream: true,
});
for await (const event of stream) {
if (event.event_type === "step.delta") {
if (event.delta.type === "text") {
process.stdout.write(event.delta.text);
}
}
}
}
main();
REST
# Use alt=sse for streaming
curl -X POST \
"https://generativelanguage.googleapis.com/v1beta/interactions?alt=sse" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-H "Api-Revision: 2026-05-20" \
--no-buffer \
-d '{
"model": "gemini-3.5-flash",
"input": "Explain how AI works in detail",
"stream": true
}'
एक से ज़्यादा बार बातचीत करने की सुविधा
Gemini API में, कई बार बातचीत करने की सुविधा पहले से मौजूद होती है.
पिछली बातचीत से मिले id को previous_interaction_id पैरामीटर के तौर पर पास करें. इसके बाद, सर्वर बातचीत के इतिहास को अपने-आप मैनेज करता है.
Python
interaction1 = client.interactions.create(
model="gemini-3.5-flash",
input="I have 2 dogs in my house."
)
print("Response 1:", interaction1.output_text)
interaction2 = client.interactions.create(
model="gemini-3.5-flash",
input="How many paws are in my house?",
previous_interaction_id=interaction1.id
)
print("Response 2:", interaction2.output_text)
JavaScript
async function main() {
const interaction1 = await ai.interactions.create({
model: "gemini-3-flash-preview",
input: "I have 2 dogs in my house.",
});
console.log("Response 1:", interaction1.output_text);
const interaction2 = await ai.interactions.create({
model: "gemini-3-flash-preview",
input: "How many paws are in my house?",
previous_interaction_id: interaction1.id,
});
console.log("Response 2:", interaction2.output_text);
}
main();
REST
# Turn 1: Start the conversation
RESPONSE1=$(curl -s -X POST \
"https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "Api-Revision: 2026-05-20" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
"model": "gemini-3-flash-preview",
"input": "I have 2 dogs in my house."
}')
# Extract the interaction ID
INTERACTION_ID=$(echo "$RESPONSE1" | jq -r '.id')
# Turn 2: Continue the conversation
curl -X POST \
"https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "Api-Revision: 2026-05-20" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-d "{
\"model\": \"gemini-3-flash-preview\",
\"input\": \"How many paws are in my house?\",
\"previous_interaction_id\": \"$INTERACTION_ID\"
}"
टूल इस्तेमाल करना
मॉडल की क्षमताओं को बेहतर बनाएं. इसके लिए, Google Search से मिले नतीजों के आधार पर जवाब जनरेट करें, ताकि रीयल-टाइम में वेब कॉन्टेंट ऐक्सेस किया जा सके. मॉडल यह अपने-आप तय करता है कि कब खोजना है, क्वेरी कब पूरी करनी है, और उद्धरणों के साथ जवाब कब जनरेट करना है.
यहां दिए गए उदाहरण में, Google Search को चालू करने का तरीका बताया गया है:
Python
interaction = client.interactions.create(
model="gemini-3-flash-preview",
input="Who won the euro 2024?",
tools=[{"type": "google_search"}]
)
print(interaction.output_text)
for step in interaction.steps:
if step.type == "model_output":
for content_block in step.content:
if content_block.type == "text" and content_block.annotations:
print("\nCitations:")
for annotation in content_block.annotations:
if annotation.type == "url_citation":
print(f" - [{annotation.title}]({annotation.url})")
JavaScript
async function main() {
const interaction = await ai.interactions.create({
model: "gemini-3-flash-preview",
input: "Who won the euro 2024?",
tools: [{ type: "google_search" }]
});
console.log(interaction.output_text);
for (const step of interaction.steps) {
if (step.type === 'model_output') {
for (const contentBlock of step.content) {
if (contentBlock.type === 'text' && contentBlock.annotations) {
console.log("\nCitations:");
for (const annotation of contentBlock.annotations) {
if (annotation.type === 'url_citation') {
console.log(` - [${annotation.title}](${annotation.url})`);
}
}
}
}
}
}
}
main();
REST
curl -X POST \
"https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "Api-Revision: 2026-05-20" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gemini-3-flash-preview",
"input": "Who won the euro 2024?",
"tools": [{"type": "google_search"}]
}'
Gemini API में पहले से मौजूद अन्य टूल भी काम करते हैं:
- कोड को एक्ज़ीक्यूट करना: इस सुविधा की मदद से, मॉडल को गणित की मुश्किल समस्याओं को हल करने के लिए Python कोड लिखने और उसे चलाने की अनुमति मिलती है.
- यूआरएल कॉन्टेक्स्ट: इसकी मदद से, जवाबों को उन वेब पेज यूआरएल के हिसाब से तैयार किया जा सकता है जिन्हें आपने उपलब्ध कराया है.
- फ़ाइल खोज: इसकी मदद से, फ़ाइलें अपलोड की जा सकती हैं. साथ ही, सिमैंटिक सर्च का इस्तेमाल करके, उनके कॉन्टेंट में जवाबों की पुष्टि की जा सकती है.
- Google Maps: इसकी मदद से, जगह की जानकारी के डेटा के आधार पर जवाब पाए जा सकते हैं. साथ ही, जगहों, दिशा-निर्देशों, और मैप को खोजा जा सकता है.
- कंप्यूटर का इस्तेमाल करना: इसकी मदद से मॉडल, वर्चुअल कंप्यूटर स्क्रीन, कीबोर्ड, और माउस के साथ इंटरैक्ट करके टास्क पूरे कर सकता है.
कस्टम फ़ंक्शन कॉल करना
अपने कस्टम टूल और एपीआई से मॉडल कनेक्ट करने के लिए, फ़ंक्शन कॉलिंग का इस्तेमाल करें. मॉडल यह तय करता है कि आपके फ़ंक्शन को कब कॉल करना है. साथ ही, यह आपके ऐप्लिकेशन को लागू करने के लिए, आर्ग्युमेंट के साथ function_call चरण दिखाता है.
इस उदाहरण में, तापमान का एक मॉक फ़ंक्शन तय किया गया है. साथ ही, यह जांच की गई है कि मॉडल इसे कॉल करना चाहता है या नहीं.
Python
import json
weather_function = {
"type": "function",
"name": "get_current_temperature",
"description": "Gets the current temperature for a given location.",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city name, e.g. San Francisco",
},
},
"required": ["location"],
},
}
interaction = client.interactions.create(
model="gemini-3-flash-preview",
input="What's the temperature in London?",
tools=[weather_function],
)
fc_step = None
for step in interaction.steps:
if step.type == "function_call":
fc_step = step
break
if fc_step:
print(f"Model requested function: {fc_step.name} with args {fc_step.arguments}")
mock_result = {"temperature": "15C", "condition": "Cloudy"}
final_interaction = client.interactions.create(
model="gemini-3-flash-preview",
input=[
{
"type": "function_result",
"name": fc_step.name,
"call_id": fc_step.id,
"result": [{"type": "text", "text": json.dumps(mock_result)}],
}
],
tools=[weather_function],
previous_interaction_id=interaction.id,
)
print("Final Response:", final_interaction.output_text)
JavaScript
async function main() {
const weatherFunction = {
type: 'function',
name: 'get_current_temperature',
description: 'Gets the current temperature for a given location.',
parameters: {
type: 'object',
properties: {
location: {
type: 'string',
description: 'The city name, e.g. San Francisco',
},
},
required: ['location'],
},
};
const interaction = await ai.interactions.create({
model: 'gemini-3-flash-preview',
input: "What's the temperature in London?",
tools: [weatherFunction],
});
const fcStep = interaction.steps.find(s => s.type === 'function_call');
if (fcStep) {
console.log(`Model requested function: ${fcStep.name}`);
const mockResult = { temperature: "15C", condition: "Cloudy" };
const finalInteraction = await ai.interactions.create({
model: 'gemini-3-flash-preview',
input: [{
type: 'function_result',
name: fcStep.name,
call_id: fcStep.id,
result: [{ type: 'text', text: JSON.stringify(mockResult) }]
}],
tools: [weatherFunction],
previous_interaction_id: interaction.id,
});
console.log("Final Response:", finalInteraction.output_text);
}
}
main();
REST
curl -X POST \
"https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "Api-Revision: 2026-05-20" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gemini-3-flash-preview",
"input": "What'\''s the temperature in London?",
"tools": [{
"type": "function",
"name": "get_current_temperature",
"description": "Gets the current temperature for a given location.",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string", "description": "The city name"}
},
"required": ["location"]
}
}]
}'
आगे क्या करना है
अब आपने Gemini API का इस्तेमाल शुरू कर दिया है. ज़्यादा बेहतर ऐप्लिकेशन बनाने के लिए, यहाँ दी गई गाइड पढ़ें: