บทแนะนำ: เริ่มต้นใช้งาน Gemini API


ดูใน 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 ติดตั้งการอ้างอิงโดยใช้ 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-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 รองรับ Use Case ที่หลากหลาย รวมถึงการแชทแบบหลายรอบและการป้อนข้อมูลแบบหลายโมดัล ทั้งนี้ขึ้นอยู่กับการรองรับของโมเดลที่ใช้ โมเดลที่มีอยู่รองรับเฉพาะข้อความและรูปภาพเป็นอินพุต และรองรับข้อความเป็นเอาต์พุตเท่านั้น

ในกรณีที่ง่ายที่สุด คุณสามารถส่งสตริงข้อความแจ้งไปยังเมธอด 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 1.5 และ Gemini 1.0 Pro Vision) คุณจึงป้อนได้ทั้งข้อความและรูปภาพ โปรดอ่านข้อกำหนดเกี่ยวกับรูปภาพสำหรับพรอมต์

เมื่ออินพุตพรอมต์มีทั้งข้อความและรูปภาพ ให้ใช้โมเดล Gemini 1.5 หรือโมเดล Gemini 1.0 Pro Vision โดยใช้เมธอด GenerativeModel.generate_content เพื่อสร้างเอาต์พุตข้อความ ดังนี้

ตัวอย่างรูปภาพมีดังนี้

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

ออบเจ็กต์ genai.protos.Content มีรายการออบเจ็กต์ genai.protos.Part ซึ่งแต่ละรายการมีข้อความ (สตริง) หรือ inline_data (genai.protos.Blob) โดยที่ BLOB มีข้อมูลไบนารีและ mime_type ประวัติการแชทจะแสดงเป็นรายการออบเจ็กต์ genai.protos.Content รายการใน ChatSession.history:

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

user: อธิบายวิธีการทำงานของคอมพิวเตอร์แก่เด็กเล็กในประโยคเดียว

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

user: อ้อ แล้วถ้าจะให้อธิบายโดยละเอียดสำหรับนักเรียนมัธยมปลายล่ะ

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

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

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

นับโทเค็น

โมเดลภาษาขนาดใหญ่จะมีหน้าต่างบริบท และมักจะวัดความยาวของบริบทด้วยจำนวนโทเค็น เมื่อใช้ Gemini API คุณจะกำหนดจำนวนโทเค็นต่อออบเจ็กต์ genai.protos.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 จะยอมรับสตริงแบบง่ายหรือรายการสตริง แต่จริงๆ แล้วสร้างขึ้นจากประเภท genai.protos.Content (เช่น GenerativeModel.generate_content) ออบเจ็กต์ genai.protos.Content เป็นหน่วยหลักของการสนทนาใน API

แม้ว่าออบเจ็กต์ genai.protos.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 ...

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

Use Case ขั้นสูง

ส่วนต่อไปนี้จะกล่าวถึง 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 ดังนี้

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

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

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

ดังนั้น ตัวอย่างที่สมบูรณ์ซึ่งเทียบเท่ากับตัวอย่างก่อนหน้านี้คือ:

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 เป็นเพียง Wrapper ที่เกี่ยวข้องกับ 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)

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

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

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

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

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

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

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

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

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-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 ที่พร้อมใช้งาน