# To install the Python SDK, use this CLI command:
# pip install google-generativeai
import google.generativeai as genai
from google.generativeai import GenerativeModel
API_KEY=""
genai.configure(api_key=API_KEY)
# To install the Python SDK, use this CLI command:
# pip install google-cloud-aiplatform
import vertexai
from vertexai.generative_models
import GenerativeModel, Image
PROJECT_ID = ""
REGION = "" # e.g. us-central1
vertexai.init(project=PROJECT_ID, location=REGION)
透過文字提示生成文字的程式碼範例
Google AI
Vertex AI
model = GenerativeModel('gemini-1.5-flash')
response = model.generate_content('The opposite of hot is')
print(response.text) # The opposite of hot is cold.
model = GenerativeModel('gemini-1.5-flash')
response = model.generate_content('The opposite of hot is')
print(response.text) # The opposite of hot is cold.
從文字和圖片生成文字的程式碼範例
Google AI
Vertex AI
import PIL.Image
multimodal_model = GenerativeModel('gemini-1.5-flash')
image = PIL.Image.open('image.jpg')
response = multimodal_model.generate_content(['What is this picture?', image])
print(response.text) # A cat is shown in this picture.
multimodal_model = GenerativeModel("gemini-1.5-flash")
image = Image.load_from_file("image.jpg")
response = multimodal_model.generate_content(["What is shown in this image?", image])
print(response.text) # A cat is shown in this picture.
生成多輪聊天的程式碼範例
Google AI
Vertex AI
model = GenerativeModel('gemini-1.5-flash')
chat = model.start_chat()
print(chat.send_message("How are you?").text)
print(chat.send_message("What can you do?").text)
model = GenerativeModel("gemini-1.5-flash")
chat = model.start_chat()
print(chat.send_message("How are you?").text)
print(chat.send_message("What can you do?").text)