ट्यूटोरियल: Gemini API का इस्तेमाल शुरू करना


Google के एआई पर देखें Google Colab में चलाएं GitHub पर सोर्स देखें

इस क्विकस्टार्ट में, Gemini API के लिए Python SDK टूल को इस्तेमाल करने का तरीका बताया गया है. इससे आपको Google के Gemini के लार्ज लैंग्वेज मॉडल का ऐक्सेस मिलता है. इस क्विकस्टार्ट में आपको यह पता चलेगा कि कैसे:

  1. Gemini का इस्तेमाल करने के लिए, अपने डेवलपमेंट एनवायरमेंट और एपीआई का ऐक्सेस सेट अप करें.
  2. टेक्स्ट इनपुट से टेक्स्ट रिस्पॉन्स जनरेट करें.
  3. मल्टीमोडल इनपुट (टेक्स्ट और इमेज) की मदद से टेक्स्ट रिस्पॉन्स जनरेट करें.
  4. कभी-कभी होने वाली बातचीत (चैट) के लिए, Gemini का इस्तेमाल करें.
  5. बड़े लैंग्वेज मॉडल के लिए एम्बेड करने की सुविधा का इस्तेमाल करना.

ज़रूरी शर्तें

इस क्विकस्टार्ट को Google Colab में चलाया जा सकता है. यह notebook, सीधे ब्राउज़र में चलाता है. इसके लिए, एनवायरमेंट के किसी और कॉन्फ़िगरेशन की ज़रूरत नहीं होती.

इसके अलावा, इस क्विकस्टार्ट को स्थानीय तौर पर पूरा करने के लिए, पक्का करें कि आपका डेवलपमेंट एनवायरमेंट इन ज़रूरी शर्तों को पूरा करता हो:

  • Python 3.9 और उसके बाद के वर्शन
  • नोटबुक को चलाने के लिए jupyter को इंस्टॉल किया गया.

सेटअप

Python SDK टूल इंस्टॉल करें

Gemini API के लिए Python SDK, google-generativeai पैकेज में शामिल है. पीआईपी का इस्तेमाल करके, डिपेंडेंसी इंस्टॉल करें:

pip install -q -U google-generativeai

पैकेज इंपोर्ट करें

ज़रूरी पैकेज इंपोर्ट करें.

import pathlib
import textwrap

import google.generativeai as genai

from IPython.display import display
from IPython.display import Markdown


def to_markdown(text):
  text = text.replace('•', '  *')
  return Markdown(textwrap.indent(text, '> ', predicate=lambda _: True))
# Used to securely store your API key
from google.colab import userdata

एपीआई पासकोड सेट अप करना

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

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

Colab में, सीक्रेट मैनेजर में कुंजी जोड़ें. इसके लिए, बाएं पैनल में "GIF" पर जाएं. इसे GOOGLE_API_KEY नाम दें.

एपीआई पासकोड मिलने के बाद, उसे SDK टूल को भेजें. आप इसे दो तरीकों से कर सकते हैं:

  • कुंजी को GOOGLE_API_KEY एनवायरमेंट वैरिएबल में डालें. SDK टूल इसे वहां से अपने-आप चुन लेगा.
  • कुंजी को genai.configure(api_key=...) को पास करें
# Or use `os.getenv('GOOGLE_API_KEY')` to fetch an environment variable.
GOOGLE_API_KEY=userdata.get('GOOGLE_API_KEY')

genai.configure(api_key=GOOGLE_API_KEY)

मॉडल की सूची बनाएं

अब Gemini API का इस्तेमाल किया जा सकता है. Gemini के उपलब्ध मॉडल देखने के लिए, list_models का इस्तेमाल करें:

  • gemini-1.5-flash: हमारा सबसे तेज़ मल्टी-मॉडल मॉडल
  • gemini-1.5-pro: हमारा सबसे बेहतर और स्मार्ट मल्टी-मॉडल मॉडल
for m in genai.list_models():
  if 'generateContent' in m.supported_generation_methods:
    print(m.name)

टेक्स्ट इनपुट से टेक्स्ट जनरेट करें

सिर्फ़ टेक्स्ट वाले प्रॉम्प्ट के लिए, Gemini 1.5 मॉडल या Gemini 1.0 Pro मॉडल का इस्तेमाल करें:

model = genai.GenerativeModel('gemini-1.5-flash')

generate_content तरीके में, कई तरह के मामलों को हैंडल किया जा सकता है. इनमें, मल्टी-टर्न वाली चैट और मल्टीमोडल इनपुट शामिल हैं. यह इस बात पर निर्भर करता है कि मॉडल क्या काम करता है. उपलब्ध मॉडल में सिर्फ़ टेक्स्ट और इमेज को इनपुट के तौर पर और टेक्स्ट को आउटपुट के तौर पर इस्तेमाल किया जा सकता है.

सबसे आसान मामले में, GenerativeModel.generate_content तरीके में प्रॉम्प्ट स्ट्रिंग पास की जा सकती है:

%%time
response = model.generate_content("What is the meaning of life?")
CPU times: user 110 ms, sys: 12.3 ms, total: 123 ms
Wall time: 8.25 s

कुछ मामलों में, आपको सिर्फ़ response.text ऐक्सेसर की ज़रूरत होगी. फ़ॉर्मैट किए गए मार्कडाउन टेक्स्ट को दिखाने के लिए, to_markdown फ़ंक्शन का इस्तेमाल करें:

to_markdown(response.text)

ज़िंदगी के मकसद से जुड़ी क्वेरी ने सदियों, संस्कृतियों, और महाद्वीपों के लोगों को हैरान कर दिया है. ऐसा कोई जवाब नहीं है जिसे हर जगह मान्यता मिली हुई हो, लेकिन बहुत से आइडिया पर विचार किए जा चुके हैं. इसके जवाब अक्सर व्यक्तिगत विचारों, मान्यताओं, और ज़िंदगी के अनुभवों पर निर्भर करते हैं.

  1. खुशी और सेहत: बहुत से लोगों का मानना है कि ज़िंदगी का मकसद, खुद का खुशी और सेहत पाना है. इसके लिए, ऐसी चीज़ों की खोज करनी पड़ सकती है जिनसे खुशी मिलती हो, अहम रिश्ते बनाए जा सकें, किसी के शारीरिक और मानसिक स्वास्थ्य की देखभाल की जा सके, और निजी लक्ष्यों और दिलचस्पी को पूरा किया जा सके.

  2. काम का योगदान: कुछ लोग मानते हैं कि ज़िंदगी का मकसद दुनिया के लिए अहम योगदान देना है. इसके लिए, ऐसे पेशे में काम करना पड़ सकता है जिससे दूसरों को फ़ायदा मिलता हो. इसके अलावा, वॉलंटियर या चैरिटेबल गतिविधियों में शामिल होना, कला या साहित्य की कला तैयार करना या आविष्कार करना.

  3. खुद को महसूस करना और खुद का विकास करना: खुद को महसूस करना और खुद का विकास करना, ज़िंदगी का दूसरा लक्ष्य है. इसके लिए, अलग-अलग तरह के कौशल सीखने की ज़रूरत पड़ सकती है. जैसे, नई स्किल सीखना, सीमाओं को पार करना, व्यक्तिगत मुश्किलों का सामना करना, और एक व्यक्ति के तौर पर बेहतर बनना.

  4. नैतिक और नैतिक व्यवहार: कुछ लोगों का मानना है कि जीवन का मकसद नैतिक और नैतिक तरीके से काम करना होता है. इसके लिए लोगों को अपने नैतिक सिद्धांतों का पालन करना पड़ सकता है, मुश्किल होने पर सही काम करना पड़ सकता है, और दुनिया को एक बेहतर जगह बनाने की कोशिश करनी पड़ सकती है.

  5. आध्यात्मिक से जुड़ी ज़रूरतें पूरी करना: कुछ लोगों के लिए, ज़िंदगी का मकसद आध्यात्मिक या धार्मिक आस्था से जुड़ा होता है. इन मकसद के लिए, आपको किसी उच्च शक्ति के साथ जुड़ने, धार्मिक रीति-रिवाजों का पालन करने या आध्यात्मिक शिक्षाओं को अपनाने की ज़रूरत पड़ सकती है.

  6. ज़िंदगी का भरपूर आनंद लेना: कुछ लोगों का मानना है कि ज़िंदगी का मकसद है, ज़िंदगी में मिलने वाली हर चीज़ को अनुभव करना. इनमें सफ़र करना, नई चीज़ें आज़माना, जोखिम भरे काम करना, और नई चीज़ों को अपनाना शामिल हो सकता है.

  7. लेगसी और असर: दूसरे मानते हैं कि ज़िंदगी का मकसद है कि ज़िंदगी में ऐसी विरासत छोड़ें और दुनिया पर असर डालें. इसके लिए, इनमें से कोई अहम उपलब्धि हासिल की जा सकती है, उसे किसी योगदान के लिए याद किया जाता है या दूसरों को प्रेरणा और प्रेरणा दी जाती है.

  8. बैलेंस और आपसी तालमेल की तलाश करना: कुछ लोगों की ज़िंदगी का मकसद यह होता है कि वे अपनी ज़िंदगी के हर पहलू में संतुलन और तालमेल बिठाएं. इसके लिए, निजी, पेशेवर, और सामाजिक जवाबदेहियों के बीच झूठ बोलना पड़ सकता है. साथ ही, इसमें मन से शांति और संतुष्टि की मांग करनी पड़ सकती है. इसके अलावा, अपने मूल्यों और मान्यताओं के मुताबिक ज़िंदगी जीना भी शामिल हो सकता है.

आखिरकार, ज़िंदगी का मतलब एक निजी सफ़र होता है. इसमें, अलग-अलग लोग अपने अनुभवों, विचारों, और आस-पास की दुनिया के साथ बातचीत करके अपने खुद के खास मकसद को खोज सकते हैं.

अगर एपीआई कोई नतीजा नहीं दे पाता, तो GenerateContentResponse.prompt_feedback का इस्तेमाल करें. इससे आपको पता चलेगा कि इसे अनुरोध से जुड़ी सुरक्षा की वजहों से ब्लॉक किया गया है या नहीं.

response.prompt_feedback
safety_ratings {
  category: HARM_CATEGORY_SEXUALLY_EXPLICIT
  probability: NEGLIGIBLE
}
safety_ratings {
  category: HARM_CATEGORY_HATE_SPEECH
  probability: NEGLIGIBLE
}
safety_ratings {
  category: HARM_CATEGORY_HARASSMENT
  probability: NEGLIGIBLE
}
safety_ratings {
  category: HARM_CATEGORY_DANGEROUS_CONTENT
  probability: NEGLIGIBLE
}

Gemini, एक प्रॉम्प्ट के लिए कई जवाब जनरेट कर सकता है. इन जवाबों को candidates कहा जाता है. इनकी समीक्षा करके, जवाब के तौर पर सबसे सही जवाब चुना जा सकता है.

GenerateContentResponse.candidates से जवाब पाने वाले उम्मीदवारों को देखें:

response.candidates
[content {
  parts {
    text: "The query of life\'s purpose has perplexed people across centuries, cultures, and continents. While there is no universally recognized response, many ideas have been put forth, and the response is frequently dependent on individual ideas, beliefs, and life experiences.\n\n1. **Happiness and Well-being:** Many individuals believe that the goal of life is to attain personal happiness and well-being. This might entail locating pursuits that provide joy, establishing significant connections, caring for one\'s physical and mental health, and pursuing personal goals and interests.\n\n2. **Meaningful Contribution:** Some believe that the purpose of life is to make a meaningful contribution to the world. This might entail pursuing a profession that benefits others, engaging in volunteer or charitable activities, generating art or literature, or inventing.\n\n3. **Self-realization and Personal Growth:** The pursuit of self-realization and personal development is another common goal in life. This might entail learning new skills, pushing one\'s boundaries, confronting personal obstacles, and evolving as a person.\n\n4. **Ethical and Moral Behavior:** Some believe that the goal of life is to act ethically and morally. This might entail adhering to one\'s moral principles, doing the right thing even when it is difficult, and attempting to make the world a better place.\n\n5. **Spiritual Fulfillment:** For some, the purpose of life is connected to spiritual or religious beliefs. This might entail seeking a connection with a higher power, practicing religious rituals, or following spiritual teachings.\n\n6. **Experiencing Life to the Fullest:** Some individuals believe that the goal of life is to experience all that it has to offer. This might entail traveling, trying new things, taking risks, and embracing new encounters.\n\n7. **Legacy and Impact:** Others believe that the purpose of life is to leave a lasting legacy and impact on the world. This might entail accomplishing something noteworthy, being remembered for one\'s contributions, or inspiring and motivating others.\n\n8. **Finding Balance and Harmony:** For some, the purpose of life is to find balance and harmony in all aspects of their lives. This might entail juggling personal, professional, and social obligations, seeking inner peace and contentment, and living a life that is in accordance with one\'s values and beliefs.\n\nUltimately, the meaning of life is a personal journey, and different individuals may discover their own unique purpose through their experiences, reflections, and interactions with the world around them."
  }
  role: "model"
}
finish_reason: STOP
index: 0
safety_ratings {
  category: HARM_CATEGORY_SEXUALLY_EXPLICIT
  probability: NEGLIGIBLE
}
safety_ratings {
  category: HARM_CATEGORY_HATE_SPEECH
  probability: NEGLIGIBLE
}
safety_ratings {
  category: HARM_CATEGORY_HARASSMENT
  probability: NEGLIGIBLE
}
safety_ratings {
  category: HARM_CATEGORY_DANGEROUS_CONTENT
  probability: NEGLIGIBLE
}
]

डिफ़ॉल्ट रूप से, मॉडल जनरेट करने की पूरी प्रोसेस पूरी होने के बाद रिस्पॉन्स दिखाता है. जवाब जनरेट होते ही उसे स्ट्रीम भी किया जा सकता है. साथ ही, जनरेट होते ही मॉडल, जवाब के कुछ हिस्से दिखाएगा.

जवाबों को स्ट्रीम करने के लिए, GenerativeModel.generate_content(..., stream=True) का इस्तेमाल करें.

%%time
response = model.generate_content("What is the meaning of life?", stream=True)
CPU times: user 102 ms, sys: 25.1 ms, total: 128 ms
Wall time: 7.94 s
for chunk in response:
  print(chunk.text)
  print("_"*80)
The query of life's purpose has perplexed people across centuries, cultures, and
________________________________________________________________________________
 continents. While there is no universally recognized response, many ideas have been put forth, and the response is frequently dependent on individual ideas, beliefs, and life experiences
________________________________________________________________________________
.

1. **Happiness and Well-being:** Many individuals believe that the goal of life is to attain personal happiness and well-being. This might entail locating pursuits that provide joy, establishing significant connections, caring for one's physical and mental health, and pursuing personal goals and aspirations.

2. **Meaning
________________________________________________________________________________
ful Contribution:** Some believe that the purpose of life is to make a meaningful contribution to the world. This might entail pursuing a profession that benefits others, engaging in volunteer or charitable activities, generating art or literature, or inventing.

3. **Self-realization and Personal Growth:** The pursuit of self-realization and personal development is another common goal in life. This might entail learning new skills, exploring one's interests and abilities, overcoming obstacles, and becoming the best version of oneself.

4. **Connection and Relationships:** For many individuals, the purpose of life is found in their relationships with others. This might entail building
________________________________________________________________________________
 strong bonds with family and friends, fostering a sense of community, and contributing to the well-being of those around them.

5. **Spiritual Fulfillment:** For those with religious or spiritual beliefs, the purpose of life may be centered on seeking spiritual fulfillment or enlightenment. This might entail following religious teachings, engaging in spiritual practices, or seeking a deeper understanding of the divine.

6. **Experiencing the Journey:** Some believe that the purpose of life is simply to experience the journey itself, with all its joys and sorrows. This perspective emphasizes embracing the present moment, appreciating life's experiences, and finding meaning in the act of living itself.

7. **Legacy and Impact:** For others, the goal of life is to leave a lasting legacy or impact on the world. This might entail making a significant contribution to a particular field, leaving a positive mark on future generations, or creating something that will be remembered and cherished long after one's lifetime.

Ultimately, the meaning of life is a personal and subjective question, and there is no single, universally accepted answer. It is about discovering what brings you fulfillment, purpose, and meaning in your own life, and living in accordance with those values.
________________________________________________________________________________

स्ट्रीम करते समय, जवाब से जुड़े कुछ एट्रिब्यूट तब तक उपलब्ध नहीं होते, जब तक कि जवाब के सभी हिस्सों को फिर से नहीं दोहराया जाता. इसके बारे में यहां बताया गया है:

response = model.generate_content("What is the meaning of life?", stream=True)

prompt_feedback एट्रिब्यूट काम करता है:

response.prompt_feedback
safety_ratings {
  category: HARM_CATEGORY_SEXUALLY_EXPLICIT
  probability: NEGLIGIBLE
}
safety_ratings {
  category: HARM_CATEGORY_HATE_SPEECH
  probability: NEGLIGIBLE
}
safety_ratings {
  category: HARM_CATEGORY_HARASSMENT
  probability: NEGLIGIBLE
}
safety_ratings {
  category: HARM_CATEGORY_DANGEROUS_CONTENT
  probability: NEGLIGIBLE
}

हालांकि, text जैसे एट्रिब्यूट से ऐसा नहीं होता:

try:
  response.text
except Exception as e:
  print(f'{type(e).__name__}: {e}')
IncompleteIterationError: Please let the response complete iteration before accessing the final accumulated
attributes (or call `response.resolve()`)

इमेज और टेक्स्ट इनपुट से टेक्स्ट जनरेट करें

Gemini में ऐसे कई मॉडल उपलब्ध हैं जो मल्टीमॉडल इनपुट (Gemini 1.5 मॉडल) को हैंडल कर सकते हैं, ताकि टेक्स्ट और इमेज, दोनों इनपुट किए जा सकें. प्रॉम्प्ट के लिए इमेज से जुड़ी ज़रूरी शर्तें ज़रूर देखें.

अगर प्रॉम्प्ट वाले इनपुट में टेक्स्ट और इमेज, दोनों शामिल हैं, तो टेक्स्ट आउटपुट जनरेट करने के लिए, GenerativeModel.generate_content वाले तरीके के साथ Gemini 1.5 का इस्तेमाल करें:

आइए, एक इमेज शामिल करें:

curl -o image.jpg https://t0.gstatic.com/licensed-image?q=tbn:ANd9GcQ_Kevbk21QBRy-PgB4kQpS79brbmmEG7m3VOTShAn4PecDU5H5UxrJxE3Dw1JiaG17V88QIol19-3TM2wCHw
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100  405k  100  405k    0     0  6982k      0 --:--:-- --:--:-- --:--:-- 7106k
import PIL.Image

img = PIL.Image.open('image.jpg')
img

png

Gemini 1.5 मॉडल का इस्तेमाल करें और generate_content की मदद से, इस मॉडल को इमेज पास करें.

model = genai.GenerativeModel('gemini-1.5-flash')
response = model.generate_content(img)

to_markdown(response.text)

ब्राउन राइस, भुनी हुई ब्रोकोली, और शिमला मिर्च के साथ चिकन टेरयाकी मील प्रेप बोल्स.

किसी प्रॉम्प्ट में टेक्स्ट और इमेज, दोनों की जानकारी देने के लिए, स्ट्रिंग और इमेज वाली सूची पास करें:

response = model.generate_content(["Write a short, engaging blog post based on this picture. It should include a description of the meal in the photo and talk about my journey meal prepping.", img], stream=True)
response.resolve()
to_markdown(response.text)

खाना तैयार करने से समय और पैसा बचता है और यह आपको सेहतमंद खाने में भी मदद कर सकता है. यह खाना पौष्टिक और स्वादिष्ट खाने का बहुत अच्छा उदाहरण है, जिसे आसानी से पहले से तैयार किया जा सकता है.

इस खाने में ब्राउन चावल, भुनी हुई सब्ज़ियां, और चिकन टेरियाकी शामिल हैं. ब्राउन राइस एक साबुत अनाज होता है, जिसमें फ़ाइबर और पोषक तत्वों की मात्रा ज़्यादा होती है. भुनी हुई सब्ज़ियां रोज़ाना खाने में विटामिन और मिनरल की मात्रा पाने का बेहतरीन तरीका है. चिकन टेरियाकी, प्रोटीन का एक ऐसा सोर्स है जो फ़्लेवर भी होता है.

यह खाना समय से पहले पकाना आसान है. आसानी से ब्राउन चावल पकाएं, सब्ज़ियां भूनें, और चिकन टेरियाकी पकाएं. इसके बाद, खाने को अलग-अलग कंटेनर में बांटकर रेफ़्रिजरेटर में रखें. जब आप खाने के लिए तैयार हों, तो एक कंटेनर लें और उसे गर्म करें.

यह खाना उन व्यस्त लोगों के लिए एक बढ़िया व्यंजन है जो खाने को पौष्टिक और स्वादिष्ट खाना चाहते हैं. यह उन लोगों के लिए भी स्वादिष्ट है जो अपना वज़न कम करने या सही वज़न बनाए रखने की कोशिश कर रहे हैं.

अगर आपको ऐसे पौष्टिक और स्वादिष्ट भोजन की तलाश है जिसे समय से पहले आसानी से तैयार किया जा सके, तो यह भोजन एक बेहतरीन विकल्प है. इसे आज ही आज़माएं!

चैट पर की गई बातचीत

Gemini की मदद से, फ़्रीफ़ॉर्म तौर पर कई बार बातचीत की जा सकती है. ChatSession क्लास, बातचीत की स्थिति को मैनेज करके इस प्रक्रिया को आसान बनाती है. इसलिए, generate_content के उलट, आपको बातचीत के इतिहास को सूची के तौर पर सेव करने की ज़रूरत नहीं होती.

चैट शुरू करें:

model = genai.GenerativeModel('gemini-1.5-flash')
chat = model.start_chat(history=[])
chat
<google.generativeai.generative_models.ChatSession at 0x7b7b68250100>

ChatSession.send_message वाला तरीका, GenerateContentResponse का वही टाइप दिखाता है जो GenerativeModel.generate_content का है. इसमें आपका मैसेज और चैट के इतिहास में जवाब भी शामिल होता है:

response = chat.send_message("In one sentence, explain how a computer works to a young child.")
to_markdown(response.text)

कंप्यूटर एक बहुत ही स्मार्ट मशीन की तरह होता है, जो हमारे निर्देशों को समझकर उनका पालन कर सकता है. साथ ही, हमारे काम में हमारी मदद कर सकता है और यहां तक कि हमारे साथ गेम भी खेल सकता है!

chat.history
[parts {
   text: "In one sentence, explain how a computer works to a young child."
 }
 role: "user",
 parts {
   text: "A computer is like a very smart machine that can understand and follow our instructions, help us with our work, and even play games with us!"
 }
 role: "model"]

बातचीत जारी रखने के लिए, मैसेज भेजना जारी रखा जा सकता है. चैट को स्ट्रीम करने के लिए, stream=True आर्ग्युमेंट का इस्तेमाल करें:

response = chat.send_message("Okay, how about a more detailed explanation to a high schooler?", stream=True)

for chunk in response:
  print(chunk.text)
  print("_"*80)
A computer works by following instructions, called a program, which tells it what to
________________________________________________________________________________
 do. These instructions are written in a special language that the computer can understand, and they are stored in the computer's memory. The computer's processor
________________________________________________________________________________
, or CPU, reads the instructions from memory and carries them out, performing calculations and making decisions based on the program's logic. The results of these calculations and decisions are then displayed on the computer's screen or stored in memory for later use.

To give you a simple analogy, imagine a computer as a
________________________________________________________________________________
 chef following a recipe. The recipe is like the program, and the chef's actions are like the instructions the computer follows. The chef reads the recipe (the program) and performs actions like gathering ingredients (fetching data from memory), mixing them together (performing calculations), and cooking them (processing data). The final dish (the output) is then presented on a plate (the computer screen).

In summary, a computer works by executing a series of instructions, stored in its memory, to perform calculations, make decisions, and display or store the results.
________________________________________________________________________________

glm.Content ऑब्जेक्ट में glm.Part ऑब्जेक्ट की सूची होती है. हर ऑब्जेक्ट में टेक्स्ट (स्ट्रिंग) या inline_data (glm.Blob) होता है. BLOB में बाइनरी डेटा और mime_type होता है. चैट का इतिहास, ChatSession.history में glm.Content ऑब्जेक्ट की सूची के तौर पर उपलब्ध होता है:

for message in chat.history:
  display(to_markdown(f'**{message.role}**: {message.parts[0].text}'))

user: एक वाक्य में बताओ कि कंप्यूटर छोटे बच्चे के साथ कैसे काम करता है.

model: कंप्यूटर एक बहुत ही स्मार्ट मशीन की तरह होता है. यह हमारे निर्देशों को समझकर उनका पालन कर सकता है, हमारे काम में हमारी मदद कर सकता है, और यहां तक कि हमारे साथ गेम भी खेल सकता है!

user: ठीक है, क्या आप हाई स्कूल में पढ़ने वाले एक बच्चे को, इस बारे में ज़्यादा जानकारी दे सकते हैं?

model: कंप्यूटर निर्देशों का पालन करके काम करता है. प्रोग्राम, यह बताता है कि उसे क्या करना है. ये निर्देश एक खास भाषा में लिखे जाते हैं, जिन्हें कंप्यूटर समझ सकता है और ये कंप्यूटर की मेमोरी में सेव रहते हैं. कंप्यूटर का प्रोसेसर या सीपीयू, मेमोरी से जुड़े निर्देश पढ़ता है और प्रोग्राम के लॉजिक के आधार पर कैलकुलेशन करते हुए और उन्हें प्रोसेस करता है. फिर इन गणनाओं और निर्णयों के परिणामों को कंप्यूटर की स्क्रीन पर दिखाया जाता है या बाद में उपयोग के लिए मेमोरी में संग्रहित किया जाता है.

आसान भाषा में कहें, तो मान लीजिए कि कोई कंप्यूटर किसी रेसिपी के मुताबिक काम करता है. रेसिपी, एक प्रोग्राम की तरह है और शेफ़ के काम, कंप्यूटर के निर्देशों की तरह ही होते हैं. शेफ़ रेसिपी (प्रोग्राम) पढ़ता है और सामग्री इकट्ठा करने (मेमोरी से डेटा लाना), उन्हें एक साथ मिलाने (कैलकुलेशन) करने, और उन्हें पकाने (डेटा प्रोसेस करने) जैसे काम करता है. इसके बाद, आखिर में बनने वाले पकवान को प्लेट में (कंप्यूटर स्क्रीन) पर दिखाया जाता है.

सीधे शब्दों में कहें, तो कंप्यूटर निर्देशों की एक सीरीज़ का पालन करके, मेमोरी में सेव होता है. इससे कैलकुलेशन करने, फ़ैसले लेने, और नतीजों को दिखाने या सेव करने जैसे काम किए जाते हैं.

टोकन गिनें

बड़े लैंग्वेज मॉडल में एक कॉन्टेक्स्ट विंडो होती है. कॉन्टेक्स्ट की लंबाई को अक्सर टोकन की संख्या के हिसाब से मापा जाता है. Gemini API की मदद से, किसी भी genai.protos.Content ऑब्जेक्ट के लिए टोकन की संख्या तय की जा सकती है. सबसे सामान्य मामले में, किसी क्वेरी स्ट्रिंग को GenerativeModel.count_tokens तरीके में इस तरह पास किया जा सकता है:

model.count_tokens("What is the meaning of life?")
total_tokens: 7

इसी तरह, अपने ChatSession के लिए, token_count की जांच करें:

model.count_tokens(chat.history)
total_tokens: 501

एम्बेड करने की सुविधा का इस्तेमाल करना

एम्बेड करना एक ऐसी तकनीक है जिसका इस्तेमाल किसी अरे में फ़्लोटिंग पॉइंट नंबर की सूची के रूप में जानकारी को दिखाने के लिए किया जाता है. Gemini की मदद से, टेक्स्ट (शब्द, वाक्य, और टेक्स्ट के ब्लॉक) को वेक्टर के तौर पर दिखाया जा सकता है. इससे, एम्बेड किए गए कॉन्टेंट की तुलना करना और उसमें अंतर करना आसान हो जाता है. उदाहरण के लिए, एक जैसे विषय या भावना ज़ाहिर करने वाले दो टेक्स्ट में एक जैसी एम्बेडिंग होनी चाहिए. इसे कोसाइन (cos) जैसी गणित की तुलना करने की तकनीकों से पहचाना जा सकता है. आपको एम्बेड करने की सुविधा का इस्तेमाल कैसे और क्यों करना चाहिए, इस बारे में ज़्यादा जानने के लिए एम्बेड करने से जुड़ी गाइड देखें.

एम्बेड करने की सुविधा जनरेट करने के लिए, embed_content तरीके का इस्तेमाल करें. इस तरीके से, इन टास्क (task_type) के लिए एम्बेड करने का तरीका मैनेज किया जाता है:

टास्क किस तरह का है कंपनी का ब्यौरा
RETRIEVAL_QUERY तय करता है कि दिया गया टेक्स्ट, खोज/वापस पाने की सेटिंग में क्वेरी है.
RETRIEVAL_DOCUMENT तय करता है कि दिया गया टेक्स्ट, खोज/वापस पाने की सेटिंग में मौजूद दस्तावेज़ है. इस टास्क टाइप का इस्तेमाल करने के लिए, title होना ज़रूरी है.
SEMANTIC_SIMILARITY इससे पता चलता है कि दिए गए टेक्स्ट का इस्तेमाल, सिमैंटिक टेक्स्ट वाली समानता (STS) के लिए किया जाएगा.
वर्गीकरण इससे पता चलता है कि एम्बेड करने की सुविधा का इस्तेमाल, कैटगरी तय करने के लिए किया जाएगा.
क्लस्टरिंग इससे पता चलता है कि एम्बेड करने की सुविधा का इस्तेमाल, क्लस्टर बनाने के लिए किया जाएगा.

यहां दी गई चीज़ें, दस्तावेज़ वापस पाने के लिए एक स्ट्रिंग के लिए एम्बेडिंग जनरेट करती हैं:

result = genai.embed_content(
    model="models/embedding-001",
    content="What is the meaning of life?",
    task_type="retrieval_document",
    title="Embedding of single string")

# 1 input > 1 vector output
print(str(result['embedding'])[:50], '... TRIMMED]')
[-0.003216741, -0.013358698, -0.017649598, -0.0091 ... TRIMMED]

स्ट्रिंग के बैच को मैनेज करने के लिए, content में स्ट्रिंग की सूची पास करें:

result = genai.embed_content(
    model="models/embedding-001",
    content=[
      'What is the meaning of life?',
      'How much wood would a woodchuck chuck?',
      'How does the brain work?'],
    task_type="retrieval_document",
    title="Embedding of list of strings")

# A list of inputs > A list of vectors output
for v in result['embedding']:
  print(str(v)[:50], '... TRIMMED ...')
[0.0040260437, 0.004124458, -0.014209415, -0.00183 ... TRIMMED ...
[-0.004049845, -0.0075574904, -0.0073463684, -0.03 ... TRIMMED ...
[0.025310587, -0.0080734305, -0.029902633, 0.01160 ... TRIMMED ...

genai.embed_content फ़ंक्शन, स्ट्रिंग या स्ट्रिंग की सूचियों को स्वीकार करता है, लेकिन असल में यह genai.protos.Content टाइप (जैसे, GenerativeModel.generate_content) के आस-पास बनाया जाता है. glm.Content ऑब्जेक्ट, एपीआई में बातचीत की मुख्य इकाइयां होते हैं.

genai.protos.Content ऑब्जेक्ट मल्टीमॉडल है, लेकिन embed_content तरीके में सिर्फ़ टेक्स्ट एम्बेड करने की सुविधा काम करती है. यह डिज़ाइन, एपीआई को मल्टीमोडल एम्बेड करने की सुविधा का इस्तेमाल करने की संभावना बताता है.

response.candidates[0].content
parts {
  text: "A computer works by following instructions, called a program, which tells it what to do. These instructions are written in a special language that the computer can understand, and they are stored in the computer\'s memory. The computer\'s processor, or CPU, reads the instructions from memory and carries them out, performing calculations and making decisions based on the program\'s logic. The results of these calculations and decisions are then displayed on the computer\'s screen or stored in memory for later use.\n\nTo give you a simple analogy, imagine a computer as a chef following a recipe. The recipe is like the program, and the chef\'s actions are like the instructions the computer follows. The chef reads the recipe (the program) and performs actions like gathering ingredients (fetching data from memory), mixing them together (performing calculations), and cooking them (processing data). The final dish (the output) is then presented on a plate (the computer screen).\n\nIn summary, a computer works by executing a series of instructions, stored in its memory, to perform calculations, make decisions, and display or store the results."
}
role: "model"
result = genai.embed_content(
    model = 'models/embedding-001',
    content = response.candidates[0].content)

# 1 input > 1 vector output
print(str(result['embedding'])[:50], '... TRIMMED ...')
[-0.013921871, -0.03504407, -0.0051786783, 0.03113 ... TRIMMED ...

इसी तरह, चैट के इतिहास में genai.protos.Content ऑब्जेक्ट की सूची होती है, जिसे सीधे embed_content फ़ंक्शन में पास किया जा सकता है:

chat.history
[parts {
   text: "In one sentence, explain how a computer works to a young child."
 }
 role: "user",
 parts {
   text: "A computer is like a very smart machine that can understand and follow our instructions, help us with our work, and even play games with us!"
 }
 role: "model",
 parts {
   text: "Okay, how about a more detailed explanation to a high schooler?"
 }
 role: "user",
 parts {
   text: "A computer works by following instructions, called a program, which tells it what to do. These instructions are written in a special language that the computer can understand, and they are stored in the computer\'s memory. The computer\'s processor, or CPU, reads the instructions from memory and carries them out, performing calculations and making decisions based on the program\'s logic. The results of these calculations and decisions are then displayed on the computer\'s screen or stored in memory for later use.\n\nTo give you a simple analogy, imagine a computer as a chef following a recipe. The recipe is like the program, and the chef\'s actions are like the instructions the computer follows. The chef reads the recipe (the program) and performs actions like gathering ingredients (fetching data from memory), mixing them together (performing calculations), and cooking them (processing data). The final dish (the output) is then presented on a plate (the computer screen).\n\nIn summary, a computer works by executing a series of instructions, stored in its memory, to perform calculations, make decisions, and display or store the results."
 }
 role: "model"]
result = genai.embed_content(
    model = 'models/embedding-001',
    content = chat.history)

# 1 input > 1 vector output
for i,v in enumerate(result['embedding']):
  print(str(v)[:50], '... TRIMMED...')
[-0.014632266, -0.042202696, -0.015757175, 0.01548 ... TRIMMED...
[-0.010979066, -0.024494737, 0.0092659835, 0.00803 ... TRIMMED...
[-0.010055617, -0.07208932, -0.00011750793, -0.023 ... TRIMMED...
[-0.013921871, -0.03504407, -0.0051786783, 0.03113 ... TRIMMED...

बेहतर इस्तेमाल के उदाहरण

यहां दिए गए सेक्शन में, Gemini API के लिए Python SDK टूल के बेहतर इस्तेमाल और उसके निचले लेवल की जानकारी के बारे में बताया गया है.

सुरक्षा सेटिंग

safety_settings आर्ग्युमेंट की मदद से, यह कॉन्फ़िगर किया जा सकता है कि मॉडल, प्रॉम्प्ट और जवाब, दोनों में कौनसी चीज़ें ब्लॉक करता है और किन चीज़ों की अनुमति देता है. सुरक्षा सेटिंग, डिफ़ॉल्ट रूप से ऐसे कॉन्टेंट को ब्लॉक कर देती हैं जिसमें मीडियम और/या इस बात की संभावना ज़्यादा होती है कि वे सभी डाइमेंशन में असुरक्षित हों. सुरक्षा की सेटिंग के बारे में ज़्यादा जानें.

कोई सवाल डालें और मॉडल को डिफ़ॉल्ट सुरक्षा सेटिंग के साथ चलाएं. इससे कोई प्रॉम्प्ट नहीं दिखेगा:

response = model.generate_content('[Questionable prompt here]')
response.candidates
[content {
  parts {
    text: "I\'m sorry, but this prompt involves a sensitive topic and I\'m not allowed to generate responses that are potentially harmful or inappropriate."
  }
  role: "model"
}
finish_reason: STOP
index: 0
safety_ratings {
  category: HARM_CATEGORY_SEXUALLY_EXPLICIT
  probability: NEGLIGIBLE
}
safety_ratings {
  category: HARM_CATEGORY_HATE_SPEECH
  probability: NEGLIGIBLE
}
safety_ratings {
  category: HARM_CATEGORY_HARASSMENT
  probability: NEGLIGIBLE
}
safety_ratings {
  category: HARM_CATEGORY_DANGEROUS_CONTENT
  probability: NEGLIGIBLE
}
]

prompt_feedback से आपको पता चलेगा कि किस सुरक्षा फ़िल्टर ने प्रॉम्प्ट को ब्लॉक किया है:

response.prompt_feedback
safety_ratings {
  category: HARM_CATEGORY_SEXUALLY_EXPLICIT
  probability: NEGLIGIBLE
}
safety_ratings {
  category: HARM_CATEGORY_HATE_SPEECH
  probability: NEGLIGIBLE
}
safety_ratings {
  category: HARM_CATEGORY_HARASSMENT
  probability: NEGLIGIBLE
}
safety_ratings {
  category: HARM_CATEGORY_DANGEROUS_CONTENT
  probability: NEGLIGIBLE
}

अब कॉन्फ़िगर की गई नई सुरक्षा सेटिंग से मॉडल को यही प्रॉम्प्ट दें. ऐसा करने पर आपको जवाब मिल सकता है.

response = model.generate_content('[Questionable prompt here]',
                                  safety_settings={'HARASSMENT':'block_none'})
response.text

यह भी ध्यान रखें कि हर उम्मीदवार के पास अपना safety_ratings होता है. ऐसा तब होता है, जब प्रॉम्प्ट पास हो जाता है, लेकिन सुरक्षा जांच में हर उम्मीदवार के जवाब फ़ेल हो जाते हैं.

मैसेज को कोड में बदलें

पिछले सेक्शन में, SDK टूल का इस्तेमाल किया गया था, ताकि एपीआई पर प्रॉम्प्ट आसानी से भेजे जा सकें. इस सेक्शन में, पिछले उदाहरण की तरह पूरी तरह टाइप की गई जानकारी दी गई है. इससे मैसेज को कोड में बदलने के तरीके के बारे में निचले लेवल की जानकारी को बेहतर तरीके से समझने में मदद मिलती है.

SDK टूल आपके मैसेज को genai.protos.Content ऑब्जेक्ट में बदलने की कोशिश करता है. इसमें genai.protos.Part ऑब्जेक्ट की एक सूची होती है. हर ऑब्जेक्ट में, इनमें से कोई एक ऑब्जेक्ट होता है:

  1. a text (स्ट्रिंग)
  2. inline_data (genai.protos.Blob), जहां एक BLOB में बाइनरी data और एक mime_type होते हैं.
  3. या अन्य तरह का डेटा शामिल नहीं करते.

इनमें से किसी भी क्लास को समकक्ष शब्दकोश के रूप में भी पास किया जा सकता है.

इसलिए, पिछले उदाहरण की तरह पूरी तरह टाइप किया गया तरीका यह है:

model = genai.GenerativeModel('gemini-1.5-flash')
response = model.generate_content(
    genai.protos.Content(
        parts = [
            genai.protos.Part(text="Write a short, engaging blog post based on this picture."),
            genai.protos.Part(
                inline_data=genai.protos.Blob(
                    mime_type='image/jpeg',
                    data=pathlib.Path('image.jpg').read_bytes()
                )
            ),
        ],
    ),
    stream=True)
response.resolve()

to_markdown(response.text[:100] + "... [TRIMMED] ...")

खाना तैयार करने से समय और पैसा बचता है और यह आपको सेहतमंद खाने में भी मदद कर सकता है. इसके हिसाब से ... [TRIMMED] ...

कई मोड़ वाली बातचीत

हालांकि, पहले दिखाई गई genai.ChatSession क्लास, इस्तेमाल के कई उदाहरणों को हैंडल कर सकती है, लेकिन इससे कुछ अनुमान लगाया जा सकता है. अगर चैट पर लागू होने वाली यह सुविधा, आपके इस्तेमाल के उदाहरण के हिसाब से सही नहीं है, तो याद रखें कि genai.ChatSession, GenerativeModel.generate_content के लिए सिर्फ़ एक रैपर है. एक अनुरोध के अलावा, यह बहु-मोड़ बातचीत भी मैनेज कर सकता है.

अलग-अलग मैसेज genai.protos.Content ऑब्जेक्ट या काम करने वाले डिक्शनरी हैं, जैसा कि पिछले सेक्शन में बताया गया है. शब्दकोश के तौर पर, मैसेज के लिए role और parts कुंजियों की ज़रूरत होती है. किसी बातचीत में मौजूद role या तो user हो सकता है, जो प्रॉम्प्ट देता है या model हो सकता है जिससे जवाब मिलते हैं.

genai.protos.Content ऑब्जेक्ट की सूची पास करें और इसे एक से ज़्यादा बार की जाने वाली चैट के तौर पर माना जाएगा:

model = genai.GenerativeModel('gemini-1.5-flash')

messages = [
    {'role':'user',
     'parts': ["Briefly explain how a computer works to a young child."]}
]
response = model.generate_content(messages)

to_markdown(response.text)

कंप्यूटर को एक ऐसे स्मार्ट दोस्त के तौर पर मानें जो कई कामों में आपकी मदद कर सकता है. जिस तरह आपके पास सोचने और सीखने के लिए दिमाग होता है, उसी तरह कंप्यूटर में भी दिमाग होता है. इसे प्रोसेसर कहा जाता है. यह किसी कंप्यूटर के बॉस की तरह है, जो उसे बताता है कि क्या करना है.

कंप्यूटर के अंदर, मेमोरी नाम की एक खास जगह होती है, जो एक बड़े स्टोरेज बॉक्स की तरह होती है. यह वे सभी चीज़ें याद रखता है जो आपने इसे करने के लिए कहा है, जैसे गेम खोलना या वीडियो चलाना.

जब कीबोर्ड पर बटन दबाएं या माउस से स्क्रीन पर मौजूद चीज़ों पर क्लिक करें, तो इसका मतलब है कि कंप्यूटर पर मैसेज भेजे जा रहे हैं. ये मैसेज खास तारों से होकर, प्रोसेसर में पहुंचते हैं. इन तारों को केबल कहा जाता है.

प्रोसेसर मैसेज पढ़ता है और कंप्यूटर को यह बताता है कि क्या करना है. यह प्रोग्राम खोल सकता है, आपको तस्वीरें दिखा सकता है या संगीत भी चला सकता है.

स्क्रीन पर आपको दिखाई देने वाली सभी चीज़ें ग्राफ़िक्स कार्ड द्वारा निर्मित होती हैं, जो कंप्यूटर के अंदर किसी जादुई कलाकार की तरह काम करता है. यह प्रोसेसर के निर्देशों का पालन करके, उन्हें रंग-बिरंगी तस्वीरों और वीडियो में बदल देता है.

आपके पसंदीदा गेम, वीडियो या तस्वीरें सेव करने के लिए, कंप्यूटर एक खास स्टोरेज स्पेस का इस्तेमाल करता है. इस स्टोरेज को हार्ड ड्राइव कहा जाता है. यह एक विशाल लाइब्रेरी की तरह है, जहां कंप्यूटर आपकी सभी कीमती चीज़ों को सुरक्षित रख सकता है.

साथ ही, दोस्तों के साथ गेम खेलने या मज़ेदार वीडियो देखने के लिए इंटरनेट से कनेक्ट करने के लिए, कंप्यूटर नेटवर्क कार्ड का इस्तेमाल करता है. इसकी मदद से, इंटरनेट केबल या वाई-फ़ाई सिग्नल के ज़रिए मैसेज भेजे और पाए जाते हैं.

इसलिए, जिस तरह आपका दिमाग कुछ सीखने और खेलने में आपकी मदद करता है, उसी तरह कंप्यूटर का प्रोसेसर, मेमोरी, ग्राफ़िक्स कार्ड, हार्ड ड्राइव, और नेटवर्क कार्ड सभी मिलकर आपके कंप्यूटर को सुपर-स्मार्ट दोस्त बनाते हैं. इससे आपको शानदार काम करने में मदद मिलती है!

बातचीत जारी रखने के लिए, जवाब के साथ दूसरा मैसेज जोड़ें.

messages.append({'role':'model',
                 'parts':[response.text]})

messages.append({'role':'user',
                 'parts':["Okay, how about a more detailed explanation to a high school student?"]})

response = model.generate_content(messages)

to_markdown(response.text)

मूल रूप से, कंप्यूटर एक ऐसी मशीन है जिसे निर्देशों का सेट लागू करने के लिए प्रोग्राम किया जा सकता है. इसमें कई ज़रूरी कॉम्पोनेंट शामिल होते हैं, जो जानकारी को प्रोसेस करने, सेव करने, और दिखाने के लिए एक साथ काम करते हैं:

1. प्रोसेसर (सीपीयू): - कंप्यूटर का दिमाग. - निर्देशों को लागू करता है और कैलकुलेशन करता है. - गति को गीगार्ट्ज़ (गीगाहर्ट्ज़) में मापा गया है. - आम तौर पर, गीगाहर्ट्ज़ की स्पीड ज़्यादा होने का मतलब है कि प्रोसेसिंग ज़्यादा तेज़ हो गई है.

2. मेमोरी (रैम): - प्रोसेस किए जा रहे डेटा के लिए अस्थायी स्टोरेज. - प्रोग्राम के चलने के दौरान निर्देश और डेटा होल्ड करता है. - गीगाबाइट (जीबी) में मापा गया. - ज़्यादा जीबी रैम की मदद से, एक साथ कई प्रोग्राम चलाए जा सकते हैं.

3. स्टोरेज (एचडीडी/एसएसडी): - डेटा के लिए स्थायी स्टोरेज. - ऑपरेटिंग सिस्टम, प्रोग्राम, और उपयोगकर्ता की फ़ाइलें सेव करता है. - गीगाबाइट (जीबी) या टेराबाइट (टीबी) में मापा जाता है. - हार्ड डिस्क ड्राइव (एचडीडी) परंपरागत, धीमी, और सस्ती होती हैं. - सॉलिड स्टेट ड्राइव (एसएसडी), नई, तेज़, और ज़्यादा महंगी हैं.

4. ग्राफ़िक कार्ड (जीपीयू): - यह इमेज को प्रोसेस करता है और दिखाता है. - गेमिंग, वीडियो एडिटिंग, और ज़्यादा ग्राफ़िक वाले अन्य कामों के लिए ज़रूरी है. - वीडियो की रैम (वीआरएएम) और क्लॉक स्पीड में मापा जाता है.

5. मदरबोर्ड: - यह सभी कॉम्पोनेंट को कनेक्ट करता है. - ताकत और बातचीत के रास्ते उपलब्ध कराता है.

6. इनपुट/आउटपुट (I/O) डिवाइस: - उपयोगकर्ता को कंप्यूटर से इंटरैक्ट करने की अनुमति दें. - उदाहरण: कीबोर्ड, माउस, मॉनिटर, प्रिंटर.

7. ऑपरेटिंग सिस्टम (ओएस): - कंप्यूटर के संसाधनों को मैनेज करने वाला सॉफ़्टवेयर. - यूज़र इंटरफ़ेस और बुनियादी फ़ंक्शन उपलब्ध कराता है. - उदाहरण: Windows, macOS, Linux.

जब कंप्यूटर पर कोई प्रोग्राम चलाया जाता है, तो ये चीज़ें होती हैं:

  1. प्रोग्राम के निर्देश, स्टोरेज से मेमोरी में लोड किए जाते हैं.
  2. प्रोसेसर, मेमोरी में मौजूद निर्देश पढ़ता है और उन्हें एक-एक करके एक्ज़ीक्यूट करता है.
  3. अगर निर्देश में कैलकुलेशन शामिल हैं, तो प्रोसेसर उन्हें अपने अरिथमेटिक लॉजिक यूनिट (एएलयू) का इस्तेमाल करके पूरा करता है.
  4. अगर निर्देश में डेटा शामिल है, तो प्रोसेसर मेमोरी में लिखता या पढ़ता है.
  5. कैलकुलेशन या डेटा में बदलाव के नतीजे, मेमोरी में सेव किए जाते हैं.
  6. अगर प्रोग्राम को स्क्रीन पर कुछ दिखाने की ज़रूरत है, तो यह ज़रूरी डेटा ग्राफ़िक कार्ड को भेज देता है.
  7. ग्राफ़िक्स कार्ड डेटा को प्रोसेस करके, उसे मॉनिटर पर भेजता है. यहां पर वह दिखता है.

यह प्रोसेस तब तक जारी रहती है, जब तक कि प्रोग्राम अपना टास्क पूरा नहीं कर लेता या उपयोगकर्ता उसे बंद नहीं कर देता.

जनरेशन कॉन्फ़िगरेशन

generation_config आर्ग्युमेंट की मदद से, जनरेशन पैरामीटर में बदलाव किया जा सकता है. मॉडल को भेजे जाने वाले हर प्रॉम्प्ट में पैरामीटर की वैल्यू शामिल होती हैं. इनसे यह कंट्रोल किया जाता है कि मॉडल, रिस्पॉन्स कैसे जनरेट करता है.

model = genai.GenerativeModel('gemini-1.5-flash')
response = model.generate_content(
    'Tell me a story about a magic backpack.',
    generation_config=genai.types.GenerationConfig(
        # Only one candidate for now.
        candidate_count=1,
        stop_sequences=['x'],
        max_output_tokens=20,
        temperature=1.0)
)
text = response.text

if response.candidates[0].finish_reason.name == "MAX_TOKENS":
    text += '...'

to_markdown(text)

किसी समय की बात है, हरी-भरी पहाड़ियों के बीच एक छोटे से शहर में एक युवा लड़की रहती थी जिसका नाम था...

आगे क्या करना है

  • प्रॉम्प्ट डिज़ाइन, प्रॉम्प्ट बनाने की प्रोसेस है, जो लैंग्वेज मॉडल से मनचाहा जवाब देती है. अच्छी तरह से स्ट्रक्चर किए गए प्रॉम्प्ट लिखना, किसी लैंग्वेज मॉडल से सटीक और अच्छी क्वालिटी वाले जवाब पाने का एक अहम हिस्सा है. प्रॉम्प्ट लिखने के सबसे सही तरीकों के बारे में जानें.
  • अलग-अलग कामों को पूरा करने के लिए, Gemini में मॉडल के कई वैरिएशन मिलते हैं. जैसे, इनपुट का टाइप और जटिलता, चैट या डायलॉग की भाषा के अन्य टास्क को लागू करना, और साइज़ से जुड़ी शर्तें. Gemini के उपलब्ध मॉडल के बारे में जानें.