LoginSignup
2
0

GeminiとGPT4の比較(主に速度)

Posted at

はじめに

OpenAIのGPT4とGoogleのGeminiの比較をしてみました。

前提

Mac OS Monterey 12.6.6
Python 3.11.7
openai==1.6.1
google-cloud-aiplatform==1.38.1

実装したコード

Gemini

GoogleのVertex AIでプレビュー版のSDKが公開されていましたので、使ってみました。
(SDKのサンプルが少なかったですが、Streamも実装しました)
https://cloud.google.com/vertex-ai/docs/generative-ai/multimodal/sdk-for-gemini/gemini-sdk-overview-reference?hl=ja

事前準備

pip install --upgrade google-cloud-aiplatform

gcloud init
gcloud auth application-default login

コード

from vertexai.preview.generative_models import GenerativeModel, Content, Part                                                                                                                 
import sys                                                                                                                                                                                    
import time                                                                                                                                                                                   
                                                                                                                                                                                              
from google.cloud.aiplatform_v1beta1.types import (
            content as gapic_content_types,
            )

gemini_pro_model = GenerativeModel("gemini-pro")

def main():
    messages = []
    print("Geminiに質問してください(終了するには 'exit'と入力してください):")

    while True:
        user_input = input("あなた:")
        if user_input.lower() == 'exit':
            break

        if user_input.lower() == 'debug':
            print(messages)

        else:
            start = time.time()

            messages.append({"role": "user", "parts": [gapic_content_types.Part(text=user_input)]})
            response = gemini_pro_model.generate_content(messages, stream=True)

            print("")
            print("Gemini:", end="")

            completion_text = ""

            for event in response:
                try:
                    event_text = event.candidates[0].content.parts[0].text
                    if type(event_text) is str: 
                        print(event_text, end="")
                        sys.stdout.flush()
                        completion_text += event_text
                except Exception as e:
                    print(str(event))
  
            end = time.time()
            messages.append({"role": "model", "parts": [gapic_content_types.Part(text=completion_text)]})
            print("Time: {0} sec".format(end-start))
            print("Words: {0} words".format(len(completion_text)))
            print("Time: {0} sec per 1000 words".format((end-start)*1000/len(completion_text))) 


if __name__ == "__main__":
    main()

GPT4(Azure版)

サンプルはたくさん他の記事であるため、詳細は割愛します。(v1で実装)
streamはOpenAI版と少し挙動が変わります。

事前準備

pip install openai

コード

from openai import AzureOpenAI
import sys
import time

api_key = "[Azure OpenAIのAPIキー]"
azure_endpoint = "[Azure OpenAIのAPIエンドポイント]"
api_version = "2023-03-15-preview"

client = AzureOpenAI(api_key=api_key, azure_endpoint=azure_endpoint, api_version=api_version)
model = "[Azure OpenAIのデプロイしたモデル]"

def main():
    messages = []
    print("ChatGPTに質問してください(終了するには 'exit' と入力してください):")

    while True:
        user_input = input("あなた: ")
        if user_input.lower() == 'exit':
            break

        if user_input.lower() == 'debug':
            print(messages)

        else:
            start = time.time()
            messages.append({"role": "user", "content": user_input})
            response = client.chat.completions.create(
                model=model,
                messages=messages,
                temperature=0,
                stream=True,
            )
            
            completion_text = ""
            print("ChatGPT:", end="")
            
            for event in response:
                event_text = event.choices[0].delta.content
                if type(event_text) is str:
                    print(event_text, end="")
                    sys.stdout.flush()
                    completion_text += event_text
            print("")
            
            end = time.time()
            
            print("Time: {0} sec".format(end-start))
            print("Words: {0} words".format(len(completion_text)))
            print("Time: {0} sec per 1000 words".format((end-start)*1000/len(completion_text)))
            messages.append({"role": "assistant", "content": completion_text})

if __name__ == "__main__":
    main()

GPT4(OpenAI版)

一応、OpenAI版も

事前準備

pip install openai

コード

from openai import OpenAI
import sys
import time

api_key = "[OpenAIのAPIキー]"
client = OpenAI(api_key=api_key)
model = "gpt-4-1106-preview"

def main():
    messages = []
    print("ChatGPTに質問してください(終了するには 'exit' と入力してください):")

    while True:
        user_input = input("あなた: ")
        if user_input.lower() == 'exit':
            break

        if user_input.lower() == 'debug':
            print(messages)

        else:
            start = time.time()

            messages.append({"role": "user", "content": user_input})
            response = client.chat.completions.create(
                model=model,
                messages=messages,
                temperature=0,
                stream=True,
            )

            completion_text = ""
            print("ChatGPT:", end="")
            
            for event in response:
                event_text = event.choices[0].delta.content
                if type(event_text) is str:
                    print(event_text, end="")
                    sys.stdout.flush()
                    completion_text += event_text
            print("")
            
            end = time.time()
            
            print("Time: {0} sec".format(end-start))
            print("Words: {0} words".format(len(completion_text)))
            print("Time: {0} sec 1000 per words".format((end-start)*1000/len(completion_text)))
            messages.append({"role": "assistant", "content": completion_text})

if __name__ == "__main__":
    main()

結果

日本語

質問:日本で一番高い山と一番低い山について、1000文字程度で説明してください。

Gemini

Geminiに質問してください(終了するには 'exit'と入力してください):
あなた:日本で一番高い山と一番低い山について、1000文字程度で説明してください。

Gemini:**日本最高峰 富士山**
富士山は、静岡県と山梨県にまたがる標高3776mの日本最高峰の山です。1878年にイギリス人宣教師ウォルター・ウェストンが登頂し、世界にその存在が知られるようになりました。富士山は、その美しい円錐形
  フォルムから「霊峰」と呼ばれ、古くから人々に信仰されてきました。富士山には、吉田口、須走口、御殿場口、富士宮口、本栖湖口、御正体表口の6つの登山道があり、毎年多くの登山者が訪れます。富士山
  山頂からは、美しい日の出や夜景を楽しむことができ、国内外から多くの観光客が訪れます。

**日本最低峰 千葉県銚子市の海面**
日本の最低地点は、千葉県銚子市の海面です。標高は0mで、日本の最低点として知られています。銚子市は、千葉県の北東部に位置し、太平洋に面しています。銚子市は、サバやサンマなどの水産物が盛んで、漁
  として知られています。銚子市の海面は、潮の満ち引きによって変化し、満潮時は標高0m、干潮時は標高マイナス1mになります。銚子市には、日本の最低地点を示す標識が設置されており、観光スポットとなっ
  います。Time: 9.617148876190186 sec
Words: 471 words
Time: 20.418575108684045 sec per 1000 words

GPT4(Azure)

ChatGPTに質問してください(終了するには 'exit' と入力してください):
あなた: 日本で一番高い山と一番低い山について、1000文字程度で説明してください。
ChatGPT:日本で一番高い山は富士山で、標高は3776メートルです。富士山はその美しい円錐形の姿から、古来より信仰の対象とされ、芸術作品の題材ともなってきました。また、山頂からの日の出は「ご来光」と
  ばれ、多くの人々に感動を与えています。富士山は山梨県と静岡県にまたがる位置にあり、その周辺は富士箱根伊豆国立公園として保護されています。2013年にはユネスコの世界文化遺産に「富士山-信仰の対
  と芸術の源泉」として登録されました。

一方、日本で一番低い山は「弘法山」で、標高はわずか6メートルです。弘法山は千葉県市川市に位置しており、その名前は空海(弘法大師)に由来しています。空海がこの地を訪れ、法華経を説いたという伝説
  残っており、その功績を称えて山に名前がつけられました。弘法山はその歴史的背景から、地元の人々にとっては信仰の対象となっています。また、弘法山の周辺は公園として整備され、市民の憩いの場となっ
  います。

これら二つの山は、標高の違いだけでなく、その存在意義や背景においても大きな違いを持っています。富士山はその壮大さと美しさから、日本全国、さらには世界中から人々を引きつけています。一方の弘法山
  地元の人々に愛され、その小さな体躯からは思わぬ歴史や文化が感じられます。どちらの山も、日本の自然や文化を象徴する存在と言えるでしょう。
Time: 20.043433904647827 sec
Words: 571 words
Time: 35.10233608519759 sec per 1000 words

GPT4(OpenAI)

ChatGPTに質問してください(終了するには 'exit' と入力してください):
あなた: 日本で一番高い山と一番低い山について、1000文字程度で説明してください。
ChatGPT:日本で一番高い山は富士山(ふじさん)で、標高は約3,776メートルです。富士山は静岡県と山梨県の県境に位置し、日本の象徴的な存在として知られています。約1万年前の火山活動によって形成された
  層火山であり、その美しい円錐形の姿は多くの芸術作品にも描かれています。富士山は2013年にユネスコの世界文化遺産に登録され、その自然の美しさだけでなく、文化的な価値も高く評価されています。毎年
  には多くの登山者が訪れ、山頂からの日の出を見ることが一つの目標とされています。

一方、日本で一番低い山は、香川県にある宮ヶ浜の天狗山(てんぐさん)で、標高はわずか6.1メートルです。天狗山は瀬戸内海に面した小さな山で、山というよりは小高い丘に近い存在です。しかし、日本の測
  法では、周囲を自然に囲まれた地点が独立した山として認められるため、天狗山も「山」としてカウントされています。天狗山はその低さから登山というより散歩やピクニックに適した場所となっており、地元
  人々に親しまれています。

富士山と天狗山は、その高さだけでなく、形成された背景や文化的な意味合いにおいても大きく異なります。富士山は日本の自然を代表する壮大な存在であり、宗教的な信仰の対象としても重要な役割を果たして
  ました。一方で天狗山は、その控えめな姿からは想像もつかないほど、日本の地理的な特徴を象徴するユニークな存在です。どちらの山も日本の多様な自然環境と文化を反映しており、それぞれに価値がありま
  。
Time: 35.37371206283569 sec
Words: 631 words
Time: 56.05976555124516 sec 1000 per words

英語

Question: Please explain the highest and lowest mountains in the world. About 1000 character.

Gemini

Geminiに質問してください(終了するには 'exit'と入力してください):
あなた:Please explain the highest and lowest mountains in the world. About 1000 character.

Gemini:1. **Mount Everest (Highest)**:
   - Location: Himalayas, Nepal/China border
   - Elevation: 8,848.86 meters (29,031.7 feet)
   - First Ascent: Edmund Hillary and Tenzing Norgay in 1953
   - Significance: Highest mountain above sea level, symbol of mountaineering achievement

2. **Dead Sea (Lowest)**:
   - Location: Border between Israel, Jordan, and the West Bank
   - Elevation: Approximately 430.5 meters (1,412 feet) below sea level
   - Significance: Lowest point on Earth's land surface.Time: 4.5844011306762695 sec
Words: 495 words
Time: 9.261416425608624 sec per 1000 words


GPT4(Azure)

ChatGPTに質問してください(終了するには 'exit' と入力してください):
あなた: Please explain the highest and lowest mountains in the world. About 1000 character.
ChatGPT:The highest mountain in the world is Mount Everest, located in the Himalayas on the border of Nepal and Tibet. It stands at a staggering height of 8,848.86 meters (29,031.7 feet) above sea level, as confirmed by China and Nepal in 2020. Mount Everest is known for its extreme weather conditions and thin air, making it a challenging and dangerous climb. Despite this, it attracts hundreds of climbers from around the world each year, all hoping to reach its summit.

On the other hand, the lowest mountain in the world is Mount Wycheproof in Australia. However, it's more of a hill than a mountain. It stands at a modest height of 43 meters (141 feet) above
 the surrounding plain. Its peak is 148 meters (486 feet) above sea level. Despite its small size, Mount Wycheproof is officially recognized as the world's smallest registered mountain. It's
 located in the small town of Wycheproof, Victoria, and is a popular spot for locals and tourists alike.
Time: 8.869832992553711 sec
Words: 951 words
Time: 9.326848572611683 sec per 1000 words

GPT4(OpenAI)

ChatGPTに質問してください(終了するには 'exit' と入力してください):
あなた: Please explain the highest and lowest mountains in the world. About 1000 character.
ChatGPT:The highest mountain in the world is Mount Everest, which is part of the Himalayas and straddles the border between Nepal and the Tibet Autonomous Region of China. It reaches an astonishing height of 8,848.86 meters (29,031.7 feet) above sea level, as per the 2020 survey by China and Nepal. Everest is known as "Sagarmatha" in Nepal and "Chomolungma" in Tibet. It was first summited on May 29, 1953, by Sir Edmund Hillary from New Zealand and Tenzing Norgay, a Sherpa of Nepal.

In contrast, the lowest mountain in the world is a bit of a misnomer, as the term "mountain" typically implies a certain elevation above the surrounding terrain. However, if we consider the lowest point on Earth's land surface not covered by water, then the shores of the Dead Sea would be a candidate. The Dead Sea is a salt lake bordered by Jordan to the east and Israel and the
 West Bank to the west. Its surface and shores are 432.65 meters (1,419 feet) below sea level, making it the Earth's lowest elevation on land. However, it's not a mountain but rather a depression.

If we strictly talk about the lowest "mountain" in terms of elevation above sea level, it's difficult to define, as there is no universally accepted definition of a minimum height for a hill to be considered a mountain. Some small hills or protuberances might be locally referred to as "mountains" despite having only modest elevations.
Time: 13.48915719985962 sec
Words: 1394 words
Time: 9.676583357144633 sec 1000 per words

まとめ

時間(秒/1000文字あたり)をまとめ。

質問 Gemini GPT 4(Azure) GPT 4(OpenAI)
日本語 20.41 35.10 56.05
英語 9.26 9.32 9.67

全体的に、日本語の方がアウトプットに時間がかかるようです。
英語はどのモデルもアウトプットが早かったが、質問が簡単すぎた気もします。

Geminiは全体的に早いとは思うものの、必ずしもそういうわけではないみたいです。
どのモデルもハルシネーションは発生しているし、情報の精度というのは評価に含めない方が良いかと思っています。
速度以外で、ビジネスで使っていく上でどう評価していいのか正直迷っているのですが・・
RAGの仕組みで与えられた情報をうまくまとめられること、が重要だとは思うのですが。

その他

Azure OpenAIのGPT 4はstreamがまとまって出てくるので、あんまりストリーム感がないです。ただ、全体の速さはそこまで変わらないのかなと思います(あくまで体感)。
もう少しサンプルを増やしたいものの、時間の関係でここまで。

ちなみに、プレビュー版の GPT4 turboも使ってみました。もっと早くなるのかと思いきや、GPT4よりめちゃくちゃ遅いです。(これから早くなっていくのでしょう)

2024年はどのような年になるのか楽しみです。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0