เริ่มต้นใช้งาน Gemini API: Python

ดูใน AI ของ Google เรียกใช้ใน Google Colab ดูซอร์สบน GitHub

การเริ่มต้นอย่างรวดเร็วนี้จะสาธิตวิธีใช้ Python SDK สำหรับ Gemini API ซึ่งช่วยให้คุณเข้าถึงโมเดลภาษาขนาดใหญ่ Gemini ของ Google ได้ ในการเริ่มต้นอย่างรวดเร็วนี้ คุณจะได้เรียนรู้วิธีต่อไปนี้

  1. ตั้งค่าสภาพแวดล้อมในการพัฒนาซอฟต์แวร์และการเข้าถึง API เพื่อใช้ Gemini
  2. สร้างข้อความตอบกลับจากการป้อนข้อความ
  3. สร้างข้อความตอบกลับจากอินพุตหลายรูปแบบ (ข้อความและรูปภาพ)
  4. ใช้ Gemini สำหรับการสนทนาแบบมัลติเทิร์น (แชท)
  5. ใช้การฝังสำหรับโมเดลภาษาขนาดใหญ่

ข้อกำหนดเบื้องต้น

คุณเรียกใช้การเริ่มต้นอย่างรวดเร็วนี้ได้ใน Google Colab ซึ่งจะเรียกใช้สมุดบันทึกนี้โดยตรงในเบราว์เซอร์และไม่จําเป็นต้องกําหนดค่าสภาพแวดล้อมเพิ่มเติม

หรือหากต้องการเริ่มต้นอย่างรวดเร็วนี้ในเครื่อง โปรดตรวจสอบว่าสภาพแวดล้อมในการพัฒนาซอฟต์แวร์ของคุณเป็นไปตามข้อกำหนดต่อไปนี้

  • Python 3.9 ขึ้นไป
  • การติดตั้ง jupyter เพื่อเรียกใช้สมุดบันทึก

ตั้งค่า

ติดตั้ง Python SDK

Python SDK สำหรับ Gemini API จะอยู่ในแพ็กเกจ google-generativeai ติดตั้งทรัพยากร Dependency โดยใช้ PIP:

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

ตั้งค่าคีย์ API

คุณต้องขอรับคีย์ API ก่อน จึงจะใช้ Gemini API ได้ หากยังไม่มี ให้สร้างคีย์ด้วยคลิกเดียวใน Google AI Studio

รับคีย์ API

ใน Colab ให้เพิ่มคีย์ลงในตัวจัดการข้อมูลลับใต้ "🔑" ในแผงด้านซ้าย ตั้งชื่อว่า GOOGLE_API_KEY

เมื่อมีคีย์ API แล้ว ให้ส่งคีย์ดังกล่าวไปยัง 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 แล้ว ใช้ list_models เพื่อดูรุ่น Gemini ที่พร้อมใช้งาน ดังนี้

  • gemini-pro: เพิ่มประสิทธิภาพสำหรับพรอมต์แบบข้อความเท่านั้น
  • gemini-pro-vision: เพิ่มประสิทธิภาพสำหรับพรอมต์แบบข้อความและรูปภาพ
for m in genai.list_models():
  if 'generateContent' in m.supported_generation_methods:
    print(m.name)

แพ็กเกจ genai ยังรองรับโมเดลในตระกูล PaLM แต่มีเพียงรุ่น Gemini เท่านั้นที่รองรับความสามารถแบบหลายโมดัลทั่วไปของเมธอด generateContent

สร้างข้อความจากการป้อนข้อความ

สำหรับพรอมต์แบบข้อความเท่านั้น ให้ใช้โมเดล gemini-pro:

model = genai.GenerativeModel('gemini-pro')

เมธอด 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. การค้นหาสมดุลและความเป็นหนึ่งเดียวกัน: บางคนมีจุดประสงค์ของชีวิตคือการหาความสมดุลและความลงตัวในทุกด้านของชีวิต วิธีนี้อาจต้องมีการจัดการภาระหน้าที่ส่วนบุคคล อาชีพ และสังคม การแสวงหาความสงบสุขและจิตใจภายใน รวมถึงการใช้ชีวิตที่สอดคล้องกับค่านิยมและความเชื่อของบุคคลหนึ่งๆ

ท้ายที่สุดแล้ว ความหมายของชีวิตคือการเดินทางส่วนบุคคล และแต่ละคนอาจค้นพบวัตถุประสงค์เฉพาะของตนเองผ่านประสบการณ์ การทบทวน และการโต้ตอบกับโลกรอบตัว

หาก API แสดงผลการค้นหาไม่สำเร็จ ให้ใช้ GenerateContentResponse.prompt_feedback เพื่อดูว่า API ถูกบล็อกเนื่องจากข้อกังวลด้านความปลอดภัยเกี่ยวกับข้อความแจ้งหรือไม่

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-pro-vision) ที่ยอมรับทั้งข้อความและรูปภาพ และอินพุต GenerativeModel.generate_content API ออกแบบมาเพื่อจัดการพรอมต์แบบหลายโมดัลและแสดงผลเอาต์พุตข้อความ

ลองใส่รูปภาพ:

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-pro-vision และส่งรูปภาพไปยังโมเดลด้วย generate_content

model = genai.GenerativeModel('gemini-pro-vision')
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-pro')
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 ประวัติการแชทจะแสดงเป็นรายการออบเจ็กต์ glm.Content รายการใน ChatSession.history:

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

ผู้ใช้: ใน 1 ประโยค ให้อธิบายวิธีการทำงานของคอมพิวเตอร์กับเด็กเล็ก

model: คอมพิวเตอร์เป็นเหมือนแมชชีนอัจฉริยะที่สามารถเข้าใจและปฏิบัติตามคำสั่งของเรา ช่วยเราทำงาน หรือแม้แต่เล่นเกมกับเรา!

user: เอาล่ะ เรามาอธิบายรายละเอียดเพิ่มเติมสำหรับนักเรียนมัธยมปลายกันเถอะ

model: คอมพิวเตอร์ทำงานตามคำแนะนำที่เรียกว่าโปรแกรม ซึ่งจะบอกสิ่งที่ต้องทำ คำสั่งเหล่านี้เขียนด้วยภาษาพิเศษที่คอมพิวเตอร์สามารถเข้าใจได้ และเก็บไว้ในหน่วยความจำของคอมพิวเตอร์ หน่วยประมวลผลหรือ CPU ของคอมพิวเตอร์จะอ่านคำสั่งจากหน่วยความจำและนำไปประมวลผล คำนวณและตัดสินใจตามตรรกะของโปรแกรม ผลจากการคำนวณและการตัดสินใจเหล่านี้จะปรากฏบนหน้าจอคอมพิวเตอร์หรือเก็บไว้ในหน่วยความจำเพื่อใช้ในภายหลัง

เพื่อให้เห็นภาพตัวอย่างง่ายๆ ให้สมมติว่ามีคอมพิวเตอร์เป็นเชฟที่ทำตามสูตรอาหาร สูตรอาหารก็เหมือนกับโปรแกรม และการทำงานของเชฟก็เหมือนกับวิธีการที่คอมพิวเตอร์ทำตาม เชฟจะอ่านสูตรอาหาร (โปรแกรม) และดำเนินการต่างๆ เช่น รวบรวมส่วนผสม (ดึงข้อมูลจากหน่วยความจำ) ผสมปนกัน (คำนวณ) และทำอาหาร (ประมวลผลข้อมูล) จากนั้นแสดงอาหารจานสุดท้าย (เอาต์พุต) บนจาน (หน้าจอคอมพิวเตอร์)

กล่าวโดยสรุป คอมพิวเตอร์จะทำงานโดยเรียกใช้ชุดคำสั่งโดยจัดเก็บไว้ในหน่วยความจำเพื่อคำนวณ ตัดสินใจ และแสดงผลหรือจัดเก็บผลลัพธ์

นับโทเค็น

โมเดลภาษาขนาดใหญ่จะมีหน้าต่างบริบท และบ่อยครั้งที่ความยาวของบริบทจะวัดด้วยจำนวนโทเค็น เมื่อใช้ Gemini API คุณจะกำหนดจำนวนโทเค็นต่อออบเจ็กต์ glm.Content ใดก็ได้ ในกรณีที่ง่ายที่สุด คุณสามารถส่งสตริงการค้นหาไปยังเมธอด GenerativeModel.count_tokens ได้ดังนี้

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

ในทำนองเดียวกัน คุณอาจตรวจสอบ token_count สำหรับ ChatSession ได้ดังนี้

model.count_tokens(chat.history)
total_tokens: 501

ใช้การฝัง

การฝังเป็นเทคนิคที่ใช้ในการแสดงข้อมูลเป็นรายการเลขทศนิยมในอาร์เรย์ คุณสามารถใช้ Gemini เพื่อแสดงข้อความ (คำ ประโยค และบล็อกข้อความ) ในรูปแบบเวกเตอร์ ซึ่งช่วยในการเปรียบเทียบและความแตกต่างของการฝังได้ง่ายขึ้น ตัวอย่างเช่น ข้อความ 2 ข้อความที่มีเรื่องหรือความรู้สึกคล้ายกันควรมีการฝังที่คล้ายกัน ซึ่งสามารถระบุได้โดยใช้เทคนิคการเปรียบเทียบทางคณิตศาสตร์ เช่น ความคล้ายคลึงกันของโคไซน์ สำหรับข้อมูลเพิ่มเติมเกี่ยวกับวิธีและเหตุผลที่คุณควรใช้การฝัง โปรดดูที่คำแนะนำในการฝัง

ใช้เมธอด 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 จะยอมรับสตริงหรือรายการสตริงอย่างง่าย แต่ฟังก์ชันนี้สร้างขึ้นจากประเภท glm.Content (เช่น GenerativeModel.generate_content) ออบเจ็กต์ glm.Content เป็นหน่วยหลักของการสนทนาใน API

แม้ว่าออบเจ็กต์ glm.Content จะเป็นแบบหลายโมดัล แต่เมธอด embed_content รองรับเฉพาะการฝังข้อความเท่านั้น การออกแบบนี้จะช่วยให้ API มีความเป็นไปได้ที่จะขยายไปยังการฝังในหลายโมดัล

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 ...

ในทำนองเดียวกัน ประวัติการแชทจะมีรายการออบเจ็กต์ glm.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...

Use Case ขั้นสูง

ส่วนต่อไปนี้จะกล่าวถึงกรณีการใช้งานขั้นสูงและรายละเอียดในระดับที่ต่ำกว่าของ Python SDK สำหรับ Gemini API

การตั้งค่าความปลอดภัย

อาร์กิวเมนต์ 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 เพื่อให้คุณส่งพรอมต์ไปยัง API ได้อย่างง่ายดาย ส่วนนี้นำเสนอข้อมูลที่พิมพ์อย่างสมบูรณ์ซึ่งเทียบเท่ากับตัวอย่างก่อนหน้านี้ คุณจึงเข้าใจรายละเอียดในระดับต่ำลงเกี่ยวกับวิธีที่ SDK เข้ารหัสข้อความได้ดีขึ้น

ส่วนพื้นฐานของ Python SDK คือไลบรารีของไคลเอ็นต์ google.ai.generativelanguage ดังนี้

import google.ai.generativelanguage as glm

SDK จะพยายามแปลงข้อความเป็นออบเจ็กต์ glm.Content ซึ่งมีรายการออบเจ็กต์ glm.Part รายการที่ประกอบด้วยรายการต่อไปนี้

  1. a text (สตริง)
  2. inline_data (glm.Blob) โดยที่ Blob มีไบนารี data และ mime_type

คุณยังส่งผ่านคลาสเหล่านี้เป็นพจนานุกรมที่คล้ายกันได้

ดังนั้น ค่าที่พิมพ์แบบเต็มซึ่งเทียบเท่ากับตัวอย่างก่อนหน้านี้คือ

model = genai.GenerativeModel('gemini-pro-vision')
response = model.generate_content(
    glm.Content(
        parts = [
            glm.Part(text="Write a short, engaging blog post based on this picture."),
            glm.Part(
                inline_data=glm.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 เป็นเพียง Wrapper ที่เกี่ยวข้องกับ GenerativeModel.generate_content นอกจากคำขอเดี่ยวๆ แล้ว ยังสามารถจัดการการสนทนาแบบหลายมุมมองได้

ข้อความแต่ละรายการเป็นออบเจ็กต์ glm.Content รายการหรือพจนานุกรมที่เข้ากันได้ ตามที่เห็นในส่วนก่อนหน้านี้ ข้อความต้องใช้คีย์ role และ parts เนื่องจากเป็นพจนานุกรม role ในการสนทนาอาจเป็น user ซึ่งแสดงพรอมต์ หรือ model ซึ่งระบุคำตอบ

ส่งรายการวัตถุ glm.Content รายการ และจะถือว่าเป็นการแชทแบบมัลติเทิร์น:

model = genai.GenerativeModel('gemini-pro')

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

to_markdown(response.text)

ลองจินตนาการว่าคอมพิวเตอร์เป็นเพื่อนที่ชาญฉลาดที่ช่วยเหลือคุณได้หลายอย่าง เช่นเดียวกับที่คุณมีสมองในการคิดและเรียนรู้ คอมพิวเตอร์ก็มีสมองเช่นกัน ที่เรียกว่า "โปรเซสเซอร์" เหมือนกับเจ้านายของคอมพิวเตอร์ คอยบอกให้คอมพิวเตอร์รู้ว่าต้องทำอะไรบ้าง

ภายในคอมพิวเตอร์จะมีสถานที่พิเศษที่เรียกว่า หน่วยความจำ ซึ่งเป็นเหมือนกล่องเก็บของขนาดใหญ่ แอปจะจดจำทุกสิ่งที่คุณบอกให้ทำ เช่น เปิดเกมหรือเล่นวิดีโอ

เมื่อคุณกดปุ่มบนแป้นพิมพ์หรือคลิกสิ่งต่างๆ บนหน้าจอด้วยเมาส์ จะเป็นการส่งข้อความไปยังคอมพิวเตอร์นั้น ข้อความเหล่านี้จะเดินทางผ่านสายพิเศษที่เรียกว่าสายเคเบิลไปยังตัวประมวลผล

โปรเซสเซอร์จะอ่านข้อความและบอกคอมพิวเตอร์ว่าต้องทำอะไรบ้าง สามารถเปิดโปรแกรม แสดงรูปภาพ หรือแม้กระทั่งเล่นเพลงให้คุณ

ทุกอย่างที่คุณเห็นบนหน้าจอจะสร้างขึ้นโดยการ์ดกราฟิก คล้ายๆ กับศิลปินเวทมนตร์ในคอมพิวเตอร์ ซึ่งจะนำคำสั่งของโปรเซสเซอร์มาเปลี่ยนเป็นรูปภาพและวิดีโอสีสันสดใส

คอมพิวเตอร์จะใช้พื้นที่เก็บข้อมูลพิเศษที่เรียกว่าฮาร์ดไดรฟ์เพื่อบันทึกเกม วิดีโอ หรือรูปภาพที่คุณชื่นชอบ เหมือนกับห้องสมุดขนาดใหญ่ที่คอมพิวเตอร์สามารถเก็บรักษาสิ่งที่มีค่าของคุณทั้งหมดให้ปลอดภัย

และเมื่อคุณต้องการเชื่อมต่ออินเทอร์เน็ตเพื่อเล่นเกมกับเพื่อนหรือดูวิดีโอตลกๆ คอมพิวเตอร์จะใช้สิ่งที่เรียกว่าการ์ดเครือข่ายเพื่อส่งและรับข้อความผ่านสายอินเทอร์เน็ตหรือสัญญาณ Wi-Fi

เช่นเดียวกับที่สมองของคุณช่วยให้คุณเรียนรู้และเล่น โปรเซสเซอร์ของคอมพิวเตอร์ หน่วยความจำ การ์ดกราฟิก ฮาร์ดไดรฟ์ และการ์ดเครือข่ายจะทำงานร่วมกันเพื่อทำให้คอมพิวเตอร์ของคุณเป็นเพื่อนอัจฉริยะที่ช่วยคุณทำสิ่งที่น่าอัศจรรย์ได้!

หากต้องการสนทนาต่อ ให้เพิ่มคําตอบและข้อความอื่น

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. โปรเซสเซอร์ (CPU): - สมองของคอมพิวเตอร์ - ดำเนินการคำสั่งและคำนวณ - ความเร็วที่มีหน่วยเป็นกิกะเฮิรตซ์ (GHz) - โดยทั่วไป GHz ที่มากกว่าหมายถึงการประมวลผลที่เร็วขึ้น

2. หน่วยความจำ (RAM): - พื้นที่เก็บข้อมูลชั่วคราวสำหรับการประมวลผลข้อมูล - เก็บคำสั่งและข้อมูลขณะโปรแกรมทำงานอยู่ - วัดเป็นกิกะไบต์ (GB) - RAM ที่มากขึ้นจะช่วยให้โปรแกรมทำงานพร้อมกันได้มากขึ้น

3. พื้นที่เก็บข้อมูล (HDD/SSD): - พื้นที่เก็บข้อมูลถาวรสำหรับข้อมูล - จัดเก็บระบบปฏิบัติการ โปรแกรม และไฟล์ของผู้ใช้ - วัดเป็นกิกะไบต์ (GB) หรือเทราไบต์ (TB) - ฮาร์ดดิสก์ไดรฟ์ (HDD) เป็นแบบดั้งเดิม ช้ากว่าและถูกกว่า - Solid State ไดรฟ์ (SSD) เป็นรุ่นใหม่กว่า เร็วกว่า และแพงกว่า

4. การ์ดแสดงผล (GPU): - ประมวลผลและแสดงรูปภาพ - จำเป็นสำหรับการเล่นเกม การตัดต่อวิดีโอ และงานอื่นๆ ที่เน้นกราฟิก - วัดใน RAM วิดีโอ (VRAM) และความเร็วของนาฬิกา

5. แผงวงจรหลัก: - เชื่อมต่อคอมโพเนนต์ทั้งหมด - ให้เส้นทางการสื่อสารและพลังงาน

6. อุปกรณ์อินพุต/เอาต์พุต (I/O): - อนุญาตให้ผู้ใช้โต้ตอบกับคอมพิวเตอร์ - ตัวอย่างเช่น แป้นพิมพ์ เมาส์ จอภาพ เครื่องพิมพ์

7. ระบบปฏิบัติการ (OS): - ซอฟต์แวร์ที่จัดการทรัพยากรของคอมพิวเตอร์ - มีอินเทอร์เฟซผู้ใช้และฟังก์ชันการทำงานพื้นฐาน - เช่น Windows, macOS, Linux

เมื่อคุณเรียกใช้โปรแกรมในคอมพิวเตอร์ สิ่งต่อไปนี้จะเกิดขึ้น

  1. ระบบจะโหลดวิธีการสำหรับโปรแกรมจากพื้นที่เก็บข้อมูลลงในหน่วยความจำ
  2. โปรเซสเซอร์จะอ่านวิธีการจากหน่วยความจำและดำเนินการทีละรายการ
  3. หากคำสั่งมีการคำนวณ ตัวประมวลผลจะดำเนินการโดยใช้หน่วยตรรกะทางคณิตศาสตร์ (ALU)
  4. หากคำสั่งเกี่ยวข้องกับข้อมูล โปรเซสเซอร์จะอ่านหรือเขียนข้อมูลลงในหน่วยความจำ
  5. ผลของการคำนวณหรือการจัดการข้อมูลจะจัดเก็บไว้ในหน่วยความจำ
  6. หากโปรแกรมต้องแสดงบางอย่างบนหน้าจอ โปรแกรมจะส่งข้อมูลที่จำเป็นไปยังการ์ดแสดงผล
  7. การ์ดแสดงผลจะประมวลผลข้อมูลและส่งไปยังจอภาพ ซึ่งจะแสดงข้อมูลดังกล่าว

กระบวนการนี้จะดําเนินต่อไปจนกว่าโปรแกรมจะทํางานเสร็จสมบูรณ์หรือผู้ใช้สิ้นสุดการใช้งาน

การกำหนดค่าการสร้าง

อาร์กิวเมนต์ generation_config ช่วยให้คุณแก้ไขพารามิเตอร์การสร้างได้ ทุกพรอมต์ที่คุณส่งไปยังโมเดลจะมีค่าพารามิเตอร์ที่ควบคุมวิธีที่โมเดลสร้างคำตอบ

model = genai.GenerativeModel('gemini-pro')
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 ที่มีให้ใช้งาน
  • Gemini มีตัวเลือกในการขอเพิ่มขีดจำกัดอัตราคำขอ ขีดจำกัดอัตราสำหรับรุ่น Gemini-Pro คือ 60 คำขอต่อนาที (RPM)