⑧gemini-pro API python ハンズオン
geminiのAPIが公開されました。
チャットレベルでは遜色なくgpt4のように使えるように感じます。
models
/gemini-pro
models
/gemini-pro-vision
とのことでvisionモデルも同時にリリースされました。
必要なもの①モジュール
pip install -q -U google-generativeai
#The Python SDK for the Gemini API
必要なもの②APIキー
APIキーが必要なので上記のサイトで発行して下さい
macの方は.zshrcの中に
export GOOGLE_API_KEY=取得したAPIキー
と記述。windowsの方は環境変数にGOOGLE_API_KEYとして保存
とりあえず実行できるコードを
import pathlib
import textwrap
import os
import google.generativeai as genai
import gradio as gr
import time
# Configure API
GOOGLE_API_KEY = os.getenv('GOOGLE_API_KEY')
genai.configure(api_key=GOOGLE_API_KEY)
# Define the model
model = genai.GenerativeModel('gemini-pro')
# Function to generate content and measure execution time
def generate_content(query):
start_time = time.time() # Start time
response = model.generate_content(query)
end_time = time.time() # End time
# Calculate execution time
execution_time = end_time - start_time
# Format the response and add execution time
formatted_response = textwrap.indent(response.text, '> ', predicate=lambda _: True)
formatted_response += f"\n\n実行時間: {execution_time:.2f} 秒"
return formatted_response
# Create the Gradio interface with Markdown output
iface = gr.Interface(fn=generate_content, inputs="text", outputs="markdown")
# Launch the interface
iface.launch(inbrowser=True)
gradioを使用しています。
pip install gradio
大喜利
パソコンの先生みたいに
GPT4との比較をしてみます
import gradio as gr
import os
import time
from openai import OpenAI
import google.generativeai as genai
# OpenAI Clientの設定
client = OpenAI(api_key=os.environ['OPENAI_API_KEY'])
# Google Gemini APIの設定
GOOGLE_API_KEY = os.getenv('GOOGLE_API_KEY')
genai.configure(api_key=GOOGLE_API_KEY)
gemini_model = genai.GenerativeModel('gemini-pro')
def combined_ask(question):
# Google Geminiモデルからのレスポンスを取得し、テキストファイルに保存
start_time2 = time.time()
response = gemini_model.generate_content(question)
end_time2 = time.time()
gemini_content = response.text
gemini_execution_time = end_time2 - start_time2
gemini_result = f"# [model: gemini-pro]\n\n{gemini_content}\n\n実行時間: {gemini_execution_time:.2f} 秒"
with open("gemini_result.txt", "w") as file:
file.write(gemini_result)
# OpenAIモデルからのレスポンスを取得し、テキストファイルに保存
start_time = time.time()
response = client.chat.completions.create(
model="gpt-4-1106-preview",
messages=[{"role": "system", "content": "Start chat"},
{"role": "user", "content": question}],
temperature=0.7,
)
end_time = time.time()
openai_content = response.choices[0].message.content.strip()
openai_execution_time = end_time - start_time
openai_result = f"# [model: gpt-4-1106-preview]\n\n{openai_content}\n\n実行時間: {openai_execution_time:.2f} 秒"
with open("openai_result.txt", "w") as file:
file.write(openai_result)
# テキストファイルから結果を読み込む
with open("openai_result.txt", "r") as file:
openai_result = file.read()
with open("gemini_result.txt", "r") as file:
gemini_result = file.read()
return openai_result, gemini_result
input_interface = gr.components.Textbox(lines=2, placeholder="User Input Here...")
demo = gr.Interface(
fn=combined_ask,
inputs=input_interface,
outputs=[gr.components.Markdown(label="Response from gpt-4-1106-preview"),
gr.components.Markdown(label="Response from gemini-pro")],
title="gemini-pro and gpt4 response viewer",
description=""
)
demo.launch(inbrowser=True)
雑感
あまり攻めた質問をしてませんが、感じるのはgeminiのレスポンスの速さです(計測を間違えていたらすみません)。精度も高く、今後が楽しみです。