Gemini API, कोड चलाने वाला एक टूल उपलब्ध कराता है. इसकी मदद से, मॉडल Python कोड जनरेट और चला सकता है. इसके बाद, मॉडल कोड को चलाने के नतीजों से बार-बार सीख सकता है, जब तक कि वह आखिरी आउटपुट पर नहीं पहुंच जाता. कोड के आधार पर तर्क करने की सुविधा का फ़ायदा पाने वाले ऐप्लिकेशन बनाने के लिए, कोड को लागू करने की सुविधा का इस्तेमाल किया जा सकता है. उदाहरण के लिए, समीकरण हल करने या टेक्स्ट को प्रोसेस करने के लिए, कोड लागू करने का इस्तेमाल किया जा सकता है. ज़्यादा खास काम करने के लिए, कोड को चलाने के माहौल में शामिल लाइब्रेरी का भी इस्तेमाल किया जा सकता है.
Gemini सिर्फ़ Python में कोड को एक्ज़ीक्यूट कर सकता है. Gemini से अब भी किसी दूसरी भाषा में कोड जनरेट करने के लिए कहा जा सकता है. हालांकि, मॉडल कोड को चलाने के लिए, कोड को लागू करने वाले टूल का इस्तेमाल नहीं कर सकता.
कोड को चलाने की सुविधा चालू करना
कोड को लागू करने की सुविधा चालू करने के लिए, मॉडल पर कोड लागू करने वाले टूल को कॉन्फ़िगर करें. इससे, मॉडल को कोड जनरेट और चलाने में मदद मिलती है.
from google import genai
from google.genai import types
client = genai.Client()
response = client.models.generate_content(
model="gemini-2.0-flash",
contents="What is the sum of the first 50 prime numbers? "
"Generate and run code for the calculation, and make sure you get all 50.",
config=types.GenerateContentConfig(
tools=[types.Tool(code_execution=types.ToolCodeExecution)]
),
)
for part in response.candidates[0].content.parts:
if part.text is not None:
print(part.text)
if part.executable_code is not None:
print(part.executable_code.code)
if part.code_execution_result is not None:
print(part.code_execution_result.output)
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({ apiKey: "GOOGLE_API_KEY" });
let response = await ai.models.generateContent({
model: "gemini-2.0-flash",
contents: [
"What is the sum of the first 50 prime numbers? " +
"Generate and run code for the calculation, and make sure you get all 50.",
],
config: {
tools: [{ codeExecution: {} }],
},
});
const parts = response?.candidates?.[0]?.content?.parts || [];
parts.forEach((part) => {
if (part.text) {
console.log(part.text);
}
if (part.executableCode && part.executableCode.code) {
console.log(part.executableCode.code);
}
if (part.codeExecutionResult && part.codeExecutionResult.output) {
console.log(part.codeExecutionResult.output);
}
});
package main
import (
"context"
"fmt"
"os"
"google.golang.org/genai"
)
func main() {
ctx := context.Background()
client, _ := genai.NewClient(ctx, &genai.ClientConfig{
APIKey: os.Getenv("GOOGLE_API_KEY"),
Backend: genai.BackendGeminiAPI,
})
config := &genai.GenerateContentConfig{
Tools: []*genai.Tool{
{CodeExecution: &genai.ToolCodeExecution{}},
},
}
result, _ := client.Models.GenerateContent(
ctx,
"gemini-2.0-flash",
genai.Text("What is the sum of the first 50 prime numbers? " +
"Generate and run code for the calculation, and make sure you get all 50."),
config,
)
fmt.Println(result.Text())
fmt.Println(result.ExecutableCode())
fmt.Println(result.CodeExecutionResult())
}
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=$GOOGLE_API_KEY" \
-H 'Content-Type: application/json' \
-d ' {"tools": [{"code_execution": {}}],
"contents": {
"parts":
{
"text": "What is the sum of the first 50 prime numbers? Generate and run code for the calculation, and make sure you get all 50."
}
},
}'
आउटपुट कुछ ऐसा दिख सकता है, जिसे पढ़ने के लिए फ़ॉर्मैट किया गया है:
Okay, I need to calculate the sum of the first 50 prime numbers. Here's how I'll
approach this:
1. **Generate Prime Numbers:** I'll use an iterative method to find prime
numbers. I'll start with 2 and check if each subsequent number is divisible
by any number between 2 and its square root. If not, it's a prime.
2. **Store Primes:** I'll store the prime numbers in a list until I have 50 of
them.
3. **Calculate the Sum:** Finally, I'll sum the prime numbers in the list.
Here's the Python code to do this:
def is_prime(n):
"""Efficiently checks if a number is prime."""
if n <= 1:
return False
if n <= 3:
return True
if n % 2 == 0 or n % 3 == 0:
return False
i = 5
while i * i <= n:
if n % i == 0 or n % (i + 2) == 0:
return False
i += 6
return True
primes = []
num = 2
while len(primes) < 50:
if is_prime(num):
primes.append(num)
num += 1
sum_of_primes = sum(primes)
print(f'{primes=}')
print(f'{sum_of_primes=}')
primes=[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67,
71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151,
157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229]
sum_of_primes=5117
The sum of the first 50 prime numbers is 5117.
इस आउटपुट में कॉन्टेंट के कई हिस्से शामिल होते हैं. कोड को लागू करने के दौरान, मॉडल ये हिस्से दिखाता है:
text
: मॉडल से जनरेट किया गया इनलाइन टेक्स्टexecutableCode
: मॉडल से जनरेट किया गया कोड, जिसे एक्ज़ीक्यूट करना हैcodeExecutionResult
: चलाए जा सकने वाले कोड का नतीजा
इन हिस्सों के नाम रखने के तरीके, प्रोग्रामिंग भाषा के हिसाब से अलग-अलग होते हैं.
Chat में कोड चलाने की सुविधा का इस्तेमाल करना
कोड को चैट के हिस्से के तौर पर भी चलाया जा सकता है.
from google import genai
from google.genai import types
client = genai.Client()
chat = client.chats.create(
model="gemini-2.0-flash",
config=types.GenerateContentConfig(
tools=[types.Tool(code_execution=types.ToolCodeExecution)]
),
)
response = chat.send_message("I have a math question for you.")
print(response.text)
response = chat.send_message(
"What is the sum of the first 50 prime numbers? "
"Generate and run code for the calculation, and make sure you get all 50."
)
for part in response.candidates[0].content.parts:
if part.text is not None:
print(part.text)
if part.executable_code is not None:
print(part.executable_code.code)
if part.code_execution_result is not None:
print(part.code_execution_result.output)
import {GoogleGenAI} from "@google/genai";
const ai = new GoogleGenAI({ apiKey: "GOOGLE_API_KEY" });
const chat = ai.chats.create({
model: "gemini-2.0-flash",
history: [
{
role: "user",
parts: [{ text: "I have a math question for you:" }],
},
{
role: "model",
parts: [{ text: "Great! I'm ready for your math question. Please ask away." }],
},
],
config: {
tools: [{codeExecution:{}}],
}
});
const response = await chat.sendMessage({
message: "What is the sum of the first 50 prime numbers? " +
"Generate and run code for the calculation, and make sure you get all 50."
});
console.log("Chat response:", response.text);
package main
import (
"context"
"fmt"
"os"
"google.golang.org/genai"
)
func main() {
ctx := context.Background()
client, _ := genai.NewClient(ctx, &genai.ClientConfig{
APIKey: os.Getenv("GOOGLE_API_KEY"),
Backend: genai.BackendGeminiAPI,
})
config := &genai.GenerateContentConfig{
Tools: []*genai.Tool{
{CodeExecution: &genai.ToolCodeExecution{}},
},
}
chat, _ := client.Chats.Create(
ctx,
"gemini-2.0-flash",
config,
nil,
)
result, _ := chat.SendMessage(
ctx,
genai.Part{Text: "What is the sum of the first 50 prime numbers? " +
"Generate and run code for the calculation, and " +
"make sure you get all 50.",
},
)
fmt.Println(result.Text())
fmt.Println(result.ExecutableCode())
fmt.Println(result.CodeExecutionResult())
}
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=$GOOGLE_API_KEY" \
-H 'Content-Type: application/json' \
-d '{"tools": [{"code_execution": {}}],
"contents": [
{
"role": "user",
"parts": [{
"text": "Can you print \"Hello world!\"?"
}]
},{
"role": "model",
"parts": [
{
"text": ""
},
{
"executable_code": {
"language": "PYTHON",
"code": "\nprint(\"hello world!\")\n"
}
},
{
"code_execution_result": {
"outcome": "OUTCOME_OK",
"output": "hello world!\n"
}
},
{
"text": "I have printed \"hello world!\" using the provided python code block. \n"
}
],
},{
"role": "user",
"parts": [{
"text": "What is the sum of the first 50 prime numbers? Generate and run code for the calculation, and make sure you get all 50."
}]
}
]
}'
इनपुट/आउटपुट (I/O)
Gemini 2.0 Flash से, कोड को चलाने की सुविधा में फ़ाइल इनपुट और ग्राफ़ आउटपुट का इस्तेमाल किया जा सकता है. इन इनपुट और आउटपुट सुविधाओं का इस्तेमाल करके, CSV और टेक्स्ट फ़ाइलें अपलोड की जा सकती हैं. साथ ही, इन फ़ाइलों के बारे में सवाल पूछे जा सकते हैं. जवाब के तौर पर, Matplotlib ग्राफ़ जनरेट किए जा सकते हैं. आउटपुट फ़ाइलें, जवाब में इनलाइन इमेज के तौर पर दिखाई जाती हैं.
I/O की कीमत
कोड एक्सीक्यूशन I/O का इस्तेमाल करने पर, आपसे इनपुट टोकन और आउटपुट टोकन के लिए शुल्क लिया जाता है:
इनपुट टोकन:
- उपयोगकर्ता प्रॉम्प्ट
आउटपुट टोकन:
- मॉडल से जनरेट किया गया कोड
- कोड एनवायरमेंट में कोड चलाने का आउटपुट
- मॉडल से जनरेट की गई खास जानकारी
इनपुट/आउटपुट की जानकारी
कोड को लागू करने के I/O के साथ काम करते समय, इन तकनीकी जानकारी के बारे में पता होना चाहिए:
- कोड एनवायरमेंट का रनटाइम ज़्यादा से ज़्यादा 30 सेकंड हो सकता है.
- अगर कोड एनवायरमेंट में कोई गड़बड़ी होती है, तो मॉडल कोड आउटपुट को फिर से जनरेट कर सकता है. ऐसा पांच बार तक हो सकता है.
- फ़ाइल का ज़्यादा से ज़्यादा इनपुट साइज़, मॉडल टोकन विंडो से सीमित होता है. Gemini Flash 2.0 का इस्तेमाल करके, AI Studio में ज़्यादा से ज़्यादा एक करोड़ टोकन की इनपुट फ़ाइल का साइज़ हो सकता है. साथ ही, काम करने वाले इनपुट टाइप की टेक्स्ट फ़ाइलों के लिए, साइज़ करीब 2 एमबी हो सकता है. अगर आपने बहुत बड़ी फ़ाइल अपलोड की है, तो AI Studio उसे भेजने की अनुमति नहीं देगा.
- कोड को टेक्स्ट और CSV फ़ाइलों में सबसे बेहतर तरीके से लागू किया जा सकता है.
- इनपुट फ़ाइल को
part.inlineData
याpart.fileData
(Files API के ज़रिए अपलोड की गई) में पास किया जा सकता है. साथ ही, आउटपुट फ़ाइल को हमेशाpart.inlineData
के तौर पर दिखाया जाता है.
एक बार मुड़ना | दोनों तरफ़ से जानकारी भेजने और पाने की सुविधा (Multimodal Live API) | |
---|---|---|
काम करने वाले मॉडल | Gemini 2.0 के सभी मॉडल | सिर्फ़ फ़्लैश के एक्सपेरिमेंटल मॉडल |
इस्तेमाल किए जा सकने वाले फ़ाइल इनपुट टाइप | .png, .jpeg, .csv, .xml, .cpp, .java, .py, .js, .ts | .png, .jpeg, .csv, .xml, .cpp, .java, .py, .js, .ts |
प्लॉट करने के लिए इस्तेमाल की जा सकने वाली लाइब्रेरी | Matplotlib | Matplotlib |
एक से ज़्यादा टूल इस्तेमाल करना | नहीं | हां |
बिलिंग
Gemini API से कोड को लागू करने की सुविधा चालू करने के लिए, कोई अतिरिक्त शुल्क नहीं लिया जाता. आपके इस्तेमाल किए जा रहे Gemini मॉडल के आधार पर, इनपुट और आउटपुट टोकन की मौजूदा दर के हिसाब से आपसे शुल्क लिया जाएगा.
कोड लागू करने के लिए बिलिंग के बारे में यहां कुछ और बातें बताई गई हैं:
- मॉडल को भेजे गए इनपुट टोकन के लिए, आपसे सिर्फ़ एक बार शुल्क लिया जाता है. साथ ही, मॉडल से मिले फ़ाइनल आउटपुट टोकन के लिए भी आपसे शुल्क लिया जाता है.
- जनरेट किए गए कोड को दिखाने वाले टोकन को आउटपुट टोकन के तौर पर गिना जाता है. जनरेट किए गए कोड में टेक्स्ट और इमेज जैसा मल्टीमोडल आउटपुट शामिल हो सकता है.
- कोड चलाने के नतीजों को भी आउटपुट टोकन के तौर पर गिना जाता है.
बिलिंग मॉडल इस डायग्राम में दिखाया गया है:
- आपके इस्तेमाल किए जा रहे Gemini मॉडल के आधार पर, इनपुट और आउटपुट टोकन की मौजूदा दर के हिसाब से आपसे शुल्क लिया जाता है.
- अगर Gemini आपके सवाल का जवाब जनरेट करते समय, कोड को लागू करता है, तो ओरिजनल प्रॉम्प्ट, जनरेट किया गया कोड, और लागू किए गए कोड का नतीजा, इंटरमीडिएट टोकन के तौर पर लेबल किया जाता है. साथ ही, इन्हें इनपुट टोकन के तौर पर बिल किया जाता है.
- इसके बाद, Gemini खास जानकारी जनरेट करता है और जनरेट किया गया कोड, चलाए गए कोड का नतीजा, और आखिरी खास जानकारी दिखाता है. इन्हें आउटपुट टोकन के तौर पर बिल किया जाता है.
- Gemini API, एपीआई रिस्पॉन्स में इंटरमीडिएट टोकन की संख्या शामिल करता है, ताकि आपको पता चल सके कि शुरुआती प्रॉम्प्ट के अलावा, आपको अतिरिक्त इनपुट टोकन क्यों मिल रहे हैं.
सीमाएं
- मॉडल सिर्फ़ कोड जनरेट और उसे लागू कर सकता है. यह मीडिया फ़ाइलों जैसे अन्य आर्टफ़ैक्ट नहीं दिखा सकता.
- कुछ मामलों में, कोड को लागू करने से मॉडल के आउटपुट के दूसरे हिस्सों में गिरावट आ सकती है. उदाहरण के लिए, कहानी लिखना.
- अलग-अलग मॉडल, कोड को सही तरीके से लागू करने की सुविधा का इस्तेमाल अलग-अलग तरीके से करते हैं.
इस्तेमाल की जा सकने वाली लाइब्रेरी
कोड को चलाने के लिए इस्तेमाल किए जाने वाले एनवायरमेंट में ये लाइब्रेरी शामिल हैं:
- attrs
- शतरंज
- contourpy
- fpdf
- geopandas
- imageio
- jinja2
- joblib
- jsonschema
- jsonschema-specifications
- lxml
- matplotlib
- mpmath
- numpy
- opencv-python
- openpyxl
- पैकेजिंग
- पांडा
- तकिया
- protobuf
- pylatex
- pyparsing
- PyPDF2
- python-dateutil
- python-docx
- python-pptx
- reportlab
- scikit-learn
- scipy
- seaborn
- छह
- striprtf
- sympy
- टेबल में दिखाना
- tensorflow
- toolz
- xlrd
अपनी लाइब्रेरी इंस्टॉल नहीं की जा सकतीं.
आगे क्या करना है
- कोड लागू करने के लिए Colab आज़माएं.
- Gemini API के अन्य टूल के बारे में जानें: