Day 3 Start with the Vertex AI Gemini API and Python SDK

更新於 發佈於 閱讀時間約 21 分鐘
所有內容來自於 Beginner: Introduction to Generative AI Learning Path 這個課程內容,擷取自己想紀錄的內容,詳情請移至Google提供的課程觀看。

今天有兩個作業

  1. 用用看Gemini 1.0 Pro model
  2. 用用看Gemini 1.0 Pro Vision model

準備:進入Google Cloud Console點選左上角的「三」,選Vertex AI > Workbench

Gemini 1.0 Pro model

# load model
model = GenerativeModel("gemini-1.0-pro")
# import 需要的套件
from vertexai.generative_models import (GenerationConfig,
GenerativeModel,
Image,
Part)

1. 輸入一般的問題

responses = model.generate_content("Why is the sky blue?", stream=True)

# responses是一個<generator object _GenerativeModel._generate_content_streaming at 0x7f8439187d80>物件
# 要利用for迴圈print出來

for response in responses:
print(response.text, end="")
The sky appears blue because of a phenomenon called Rayleigh scattering. This occurs when sunlight, which is made up of all the colors of the rainbow, interacts with the Earth's atmosphere. The blue wavelengths of light are scattered more easily by the tiny particles in the atmosphere than the longer wavelengths like red and yellow.

This scattered blue light reaches our eyes from all directions in the sky, making it appear blue. The scattering is most efficient at shorter wavelengths, which is why the sky appears a deeper blue when looking directly upwards, where the light has to travel through more atmosphere.

Here's a little more information about Rayleigh scattering:

* It was first described by Lord Rayleigh in the late 1800s.
* It's why sunsets are often red or orange. As the sun sets, the sunlight has to travel through more atmosphere, and the blue light is scattered away, leaving the longer wavelengths like red and orange to reach our eyes.
* It's also responsible for the blue color of some birds' feathers and the clear blue eyes of some humans.

I hope this explanation helps! Let me know if you have any other questions.

2. Model 參數介紹

    1. temperature: 決定生成的回覆隨機性,越低LLM的回覆就越具確定性,但相對的也比較沒有創意性和開放性。詳細內容可以參考:https://docs.cohere.com/docs/temperature
    2. top_p & top_k: LLM輸出生成的回應前,會先輸出一堆詞彙,這些詞彙會依據品質排名。top_p又名Nucleus Sampling,候選詞彙個數是不固定的,從詞彙的百分比選擇詞彙,例如top_p=0.15,就表示選擇前15%的詞彙作為候選詞彙。而top_k則是從token中選擇k個候選,根據他們的likelihood分數來選擇。不論是top_p或top_k都是值越小的時候生成的內容越固定。詳細的可以參考:https://docs.cohere.com/docs/controlling-generation-with-top-k-top-p
generation_config = GenerationConfig(
temperature=0.9,
top_p=1.0,
top_k=32,
candidate_count=1,
max_output_tokens=8192,
)

responses = model.generate_content(
"Why is the sky blue?",
generation_config=generation_config,
stream=True,
)

for response in responses:
print(response.text, end="")

3. Gemini支援多次對話(Memory)

# 建立對話
chat = model.start_chat()

# 第一輪對話
prompt = """My name is Ned. You are my personal assistant. My favorite movies are Lord of the Rings and Hobbit.
Suggest another movie I might like.
"""
responses = chat.send_message(prompt, stream=True)

# 第二輪對話
prompt2 = "Are my favorite movies based on a book series?"
responses = chat.send_message(prompt2, stream=True)

# 把歷史對話print出來
print(chat.history)
[role: "user"
parts {
text: "My name is Ned. You are my personal assistant. My favorite movies are Lord of the Rings and Hobbit.\n\nSuggest another movie I might like.\n"
}
, role: "model"
parts {
text: "Hi Ned, since you love Lord of the Rings and The Hobbit, both fantasy epics with grand adventures, mythical creatures, and themes of good versus evil, you might enjoy:\n\n* **Willow (1988):** A classic fantasy film with a similar whimsical feel to LOTR, featuring a dwarf warrior on a quest to protect a baby from an evil queen. \n* **The Chronicles of Narnia (series):** Another epic tale about a group of children who discover a magical world.\n\nLet me know what you think! I can offer more suggestions based on what you liked most about LOTR and The Hobbit. Did you enjoy the battles, the friendships, the magical creatures, or something else entirely? \360\237\230\212 \n"
}
, role: "user"
parts {
text: "Are my favorite movies based on a book series?"
}
, role: "model"
parts {
text: "Yes, Ned, both *The Lord of the Rings* and *The Hobbit* are based on book series written by J.R.R. Tolkien! \n\n* **The Hobbit** is a standalone book.\n* **The Lord of the Rings** is a trilogy, consisting of:\n * *The Fellowship of the Ring*\n * *The Two Towers*\n * *The Return of the King*\n\nMany people consider them to be some of the greatest fantasy novels ever written! Have you read any of them? \n"
}
]


Gemini 1.0 Pro Vision model

# load model
multimodal_model = GenerativeModel("gemini-1.0-pro-vision")

讀取圖片&影片的方式,下面有範例

# 從local讀取圖片
image = Image.load_from_file("image.jpg")
# 從gcs讀取圖片
image = Part.from_uri(gcs_uri, mime_type="image/jpeg")
# 直接從URL讀取圖片
image = load_image_from_url(image_url) # convert to bytes
# 讀取影片
video = Part.from_uri(video_uri, mime_type="video/mp4")



Example 1. 利用local圖片產出文字

# Download an image from Google Cloud Storage
! gsutil cp "gs://cloud-samples-data/generative-ai/image/320px-Felis_catus-cat_on_snow.jpg" ./image.jpg

# Load from local file
image = Image.load_from_file("image.jpg")

# Prepare contents
prompt = "Describe this image?"
contents = [image, prompt]

responses = multimodal_model.generate_content(contents, stream=True)

print("-------Prompt--------")
print_multimodal_prompt(contents)

print("\n-------Response--------")
for response in responses:
print(response.text, end="")
raw-image



Example 2. 從gcs讀取圖片

# Load image from Cloud Storage URI
gcs_uri = "gs://cloud-samples-data/generative-ai/image/boats.jpeg"

# Prepare contents
image = Part.from_uri(gcs_uri, mime_type="image/jpeg")
prompt = "Describe the scene?"
contents = [image, prompt]

responses = multimodal_model.generate_content(contents, stream=True)

print("-------Prompt--------")
print_multimodal_prompt(contents)

print("\n-------Response--------")
for response in responses:
print(response.text, end="")
raw-image



Example 3. 從URL讀取圖片

# Load image from Cloud Storage URI
image_url = (
"https://storage.googleapis.com/cloud-samples-data/generative-ai/image/boats.jpeg"
)
image = load_image_from_url(image_url) # convert to bytes

# Prepare contents
prompt = "Describe the scene?"
contents = [image, prompt]

responses = multimodal_model.generate_content(contents, stream=True)

print("-------Prompt--------")
print_multimodal_prompt(contents)

print("\n-------Response--------")
for response in responses:
print(response.text, end="")
raw-image



Example 4. 多個圖片+few-shot prompting

# Load images from Cloud Storage URI
image1_url = "https://storage.googleapis.com/github-repo/img/gemini/intro/landmark1.jpg"
image2_url = "https://storage.googleapis.com/github-repo/img/gemini/intro/landmark2.jpg"
image3_url = "https://storage.googleapis.com/github-repo/img/gemini/intro/landmark3.jpg"
image1 = load_image_from_url(image1_url)
image2 = load_image_from_url(image2_url)
image3 = load_image_from_url(image3_url)

# Prepare prompts
prompt1 = """{"city": "London", "Landmark:", "Big Ben"}"""
prompt2 = """{"city": "Paris", "Landmark:", "Eiffel Tower"}"""

# Prepare contents
contents = [image1, prompt1, image2, prompt2, image3]

responses = multimodal_model.generate_content(contents, stream=True)

print("-------Prompt--------")
print_multimodal_prompt(contents)

print("\n-------Response--------")
for response in responses:
print(response.text, end="")



Example 5 讀取影片並要求回覆格式為json

prompt = """
Answer the following questions using the video only:
What is the profession of the main person?
What are the main features of the phone highlighted?
Which city was this recorded in?
Provide the answer JSON.
"""

video = Part.from_uri(video_uri, mime_type="video/mp4")
contents = [prompt, video]

responses = multimodal_model.generate_content(contents, stream=True)

for response in responses:
print(response.text, end="")
```json
{
"person": {
"profession": "photographer"
},
"phone": {
"features": [
"Video Boost",
"Night Sight"
]
},
"city": "Tokyo"
}
```


附上課程github

https://github.com/GoogleCloudPlatform/generative-ai/blob/main/gemini/getting-started/intro_gemini_python.ipynb



留言
avatar-img
留言分享你的想法!
avatar-img
LLM 筆記
0會員
3內容數
工程師的小筆記
你可能也想看
Thumbnail
每年4月、5月都是最多稅要繳的月份,當然大部份的人都是有機會繳到「綜合所得稅」,只是相當相當多人還不知道,原來繳給政府的稅!可以透過一些有活動的銀行信用卡或電子支付來繳,從繳費中賺一點點小確幸!就是賺個1%~2%大家也是很開心的,因為你們把沒回饋變成有回饋,就是用卡的最高境界 所得稅線上申報
Thumbnail
每年4月、5月都是最多稅要繳的月份,當然大部份的人都是有機會繳到「綜合所得稅」,只是相當相當多人還不知道,原來繳給政府的稅!可以透過一些有活動的銀行信用卡或電子支付來繳,從繳費中賺一點點小確幸!就是賺個1%~2%大家也是很開心的,因為你們把沒回饋變成有回饋,就是用卡的最高境界 所得稅線上申報
Thumbnail
全球科技產業的焦點,AKA 全村的希望 NVIDIA,於五月底正式發布了他們在今年 2025 第一季的財報 (輝達內部財務年度為 2026 Q1,實際日曆期間為今年二到四月),交出了打敗了市場預期的成績單。然而,在銷售持續高速成長的同時,川普政府加大對於中國的晶片管制......
Thumbnail
全球科技產業的焦點,AKA 全村的希望 NVIDIA,於五月底正式發布了他們在今年 2025 第一季的財報 (輝達內部財務年度為 2026 Q1,實際日曆期間為今年二到四月),交出了打敗了市場預期的成績單。然而,在銷售持續高速成長的同時,川普政府加大對於中國的晶片管制......
Thumbnail
重點摘要: 6 月繼續維持基準利率不變,強調維持高利率主因為關稅 點陣圖表現略為鷹派,收斂 2026、2027 年降息預期 SEP 連續 2 季下修 GDP、上修通膨預測值 --- 1.繼續維持利率不變,強調需要維持高利率是因為關稅: 聯準會 (Fed) 召開 6 月利率會議
Thumbnail
重點摘要: 6 月繼續維持基準利率不變,強調維持高利率主因為關稅 點陣圖表現略為鷹派,收斂 2026、2027 年降息預期 SEP 連續 2 季下修 GDP、上修通膨預測值 --- 1.繼續維持利率不變,強調需要維持高利率是因為關稅: 聯準會 (Fed) 召開 6 月利率會議
Thumbnail
我想要一天分享一點「LLM從底層堆疊的技術」,並且每篇文章長度控制在三分鐘以內,讓大家不會壓力太大,但是又能夠每天成長一點。 回顧 AI說書 - 從0開始 - 129 中說,Bidirectional Encoder Representations from Transformers (BER
Thumbnail
我想要一天分享一點「LLM從底層堆疊的技術」,並且每篇文章長度控制在三分鐘以內,讓大家不會壓力太大,但是又能夠每天成長一點。 回顧 AI說書 - 從0開始 - 129 中說,Bidirectional Encoder Representations from Transformers (BER
Thumbnail
我想要一天分享一點「LLM從底層堆疊的技術」,並且每篇文章長度控制在三分鐘以內,讓大家不會壓力太大,但是又能夠每天成長一點。 回顧 AI說書 - 從0開始 - 39 至 AI說書 - 從0開始 - 69 的第二章內容,我們拿 Encoder 出來看: 幾點注意如下: BERT 模型使用 M
Thumbnail
我想要一天分享一點「LLM從底層堆疊的技術」,並且每篇文章長度控制在三分鐘以內,讓大家不會壓力太大,但是又能夠每天成長一點。 回顧 AI說書 - 從0開始 - 39 至 AI說書 - 從0開始 - 69 的第二章內容,我們拿 Encoder 出來看: 幾點注意如下: BERT 模型使用 M
Thumbnail
我想要一天分享一點「LLM從底層堆疊的技術」,並且每篇文章長度控制在三分鐘以內,讓大家不會壓力太大,但是又能夠每天成長一點。 現在我們來看看 Google Gemini (https://gemini.google.com/ to start a dialog) 的能力: 回答如下:
Thumbnail
我想要一天分享一點「LLM從底層堆疊的技術」,並且每篇文章長度控制在三分鐘以內,讓大家不會壓力太大,但是又能夠每天成長一點。 現在我們來看看 Google Gemini (https://gemini.google.com/ to start a dialog) 的能力: 回答如下:
Thumbnail
我想要一天分享一點「LLM從底層堆疊的技術」,並且每篇文章長度控制在三分鐘以內,讓大家不會壓力太大,但是又能夠每天成長一點。 我們已經在 AI說書 - 從0開始 - 114 建立了 Transformer 模型,並在 AI說書 - 從0開始 - 115 載入權重並執行 Tokenizing,現
Thumbnail
我想要一天分享一點「LLM從底層堆疊的技術」,並且每篇文章長度控制在三分鐘以內,讓大家不會壓力太大,但是又能夠每天成長一點。 我們已經在 AI說書 - 從0開始 - 114 建立了 Transformer 模型,並在 AI說書 - 從0開始 - 115 載入權重並執行 Tokenizing,現
Thumbnail
我想要一天分享一點「LLM從底層堆疊的技術」,並且每篇文章長度控制在三分鐘以內,讓大家不會壓力太大,但是又能夠每天成長一點。 我們已經在 AI說書 - 從0開始 - 114 建立了 Transformer 模型。 現在我們來載入預訓練權重,預訓練的權重包含 Transformer 的智慧
Thumbnail
我想要一天分享一點「LLM從底層堆疊的技術」,並且每篇文章長度控制在三分鐘以內,讓大家不會壓力太大,但是又能夠每天成長一點。 我們已經在 AI說書 - 從0開始 - 114 建立了 Transformer 模型。 現在我們來載入預訓練權重,預訓練的權重包含 Transformer 的智慧
Thumbnail
我想要一天分享一點「LLM從底層堆疊的技術」,並且每篇文章長度控制在三分鐘以內,讓大家不會壓力太大,但是又能夠每天成長一點。 我們已經在 AI說書 - 從0開始 - 113 中安裝了 Google Trax,現在來建立 Transformer 模型: model = trax,models.
Thumbnail
我想要一天分享一點「LLM從底層堆疊的技術」,並且每篇文章長度控制在三分鐘以內,讓大家不會壓力太大,但是又能夠每天成長一點。 我們已經在 AI說書 - 從0開始 - 113 中安裝了 Google Trax,現在來建立 Transformer 模型: model = trax,models.
Thumbnail
我想要一天分享一點「LLM從底層堆疊的技術」,並且每篇文章長度控制在三分鐘以內,讓大家不會壓力太大,但是又能夠每天成長一點。 延續 AI說書 - 從0開始 - 86 提及 SuperGLUE 任務清單,當中會包含以下欄位: 名稱 (Name):經過微調的預訓練模型的下游任務的名稱 標識符
Thumbnail
我想要一天分享一點「LLM從底層堆疊的技術」,並且每篇文章長度控制在三分鐘以內,讓大家不會壓力太大,但是又能夠每天成長一點。 延續 AI說書 - 從0開始 - 86 提及 SuperGLUE 任務清單,當中會包含以下欄位: 名稱 (Name):經過微調的預訓練模型的下游任務的名稱 標識符
Thumbnail
我想要一天分享一點「LLM從底層堆疊的技術」,並且每篇文章長度控制在三分鐘以內,讓大家不會壓力太大,但是又能夠每天成長一點。 Transformer 可以透過繼承預訓練模型 (Pretrained Model) 來微調 (Fine-Tune) 以執行下游任務。 Pretrained Mo
Thumbnail
我想要一天分享一點「LLM從底層堆疊的技術」,並且每篇文章長度控制在三分鐘以內,讓大家不會壓力太大,但是又能夠每天成長一點。 Transformer 可以透過繼承預訓練模型 (Pretrained Model) 來微調 (Fine-Tune) 以執行下游任務。 Pretrained Mo
Thumbnail
我想要一天分享一點「LLM從底層堆疊的技術」,並且每篇文章長度控制在三分鐘以內,讓大家不會壓力太大,但是又能夠每天成長一點。 我們已經在AI說書 - 從0開始 - 18中,介紹了OpenAI的GPT模型如何利用Inference的Pipeline產生Token。 完整Pipeline可能
Thumbnail
我想要一天分享一點「LLM從底層堆疊的技術」,並且每篇文章長度控制在三分鐘以內,讓大家不會壓力太大,但是又能夠每天成長一點。 我們已經在AI說書 - 從0開始 - 18中,介紹了OpenAI的GPT模型如何利用Inference的Pipeline產生Token。 完整Pipeline可能
追蹤感興趣的內容從 Google News 追蹤更多 vocus 的最新精選內容追蹤 Google News