import pathlib
import textwrap
import google.generativeai as genai
# Used to securely store your API keyfrom google.colab import userdata
from IPython.display import display
from IPython.display import Markdown
defto_markdown(text):
text = text.replace('•', ' *')
return Markdown(textwrap.indent(text, '> ', predicate=lambda _: True))
# Or use `os.getenv('GOOGLE_API_KEY')` to fetch an environment variable.
GEMINI_API_KEY=userdata.get('GEMINI_API_KEY')
genai.configure(api_key=GEMINI_API_KEY)
提供されているモデルのリスト。
for m in genai.list_models():
if'generateContent'in m.supported_generation_methods:
print(m.name)
Gemini Pro を試す
model = genai.GenerativeModel('gemini-pro')
response = model.generate_content("ワンピースのチョッパーになりきって、長めの自己紹介をしてください。", stream=True)
for chunk in response:
print(chunk.text)
print("_"*80)
Gemini Pro Vision を試す
次に画像モデルを試してみたいと思います。
model = genai.GenerativeModel('gemini-pro-vision')
適当な画像をロードします。たまたま別タブで開いていた Microsoft の Medprompt の記事にあった画像を使いましょう。
# プロンプトと画像セットで送ると説明が返ってくる
prompt = "Write a summary of what you can learning from the image. Be detailed as much as possible. Also mention which method was the best."
response = model.generate_content([prompt, img])
to_markdown(response.text)