Closed3

OpenAIのChatCompletionモデルのレスポンス性能を計測・比較してみた

kun432kun432

各モデルのレスポンス性能を計測比較してみた

!pip install --upgrade openai
from google.colab import userdata
import os

os.environ["OPENAI_API_KEY"] = userdata.get('OPENAI_API_KEY')
import time
from functools import wraps
import statistics
from openai import OpenAI

def timing_decorator(repeats=1):
    def decorator(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            times = []
            results = []
            for _ in range(repeats):
                start_time = time.time()
                result = func(*args, **kwargs)
                results.append(result)
                end_time = time.time()
                times.append(end_time - start_time)
            avg_time = round(sum(times) / len(times),2)
            max_time = round(max(times),2)
            min_time = round(min(times),2)
            median_time = round(statistics.median(times),2)
            timing_info = {
                'avg': avg_time,
                'max': max_time,
                'min': min_time,
                'med': median_time,
                'times': [round(t, 2) for t in times]
            }
            return results[0] if repeats == 1 else results, timing_info
        return wrapper
    return decorator

@timing_decorator(repeats=10)
def openai_completion(client, model, messages):
    completion = client.chat.completions.create(
        model=model,
        messages=messages
    )
    return completion.choices[0].message.content

models = ["gpt-3.5-turbo", "gpt-3.5-turbo-1106", "gpt-4", "gpt-4-1106-preview"]

max_model_length = max(len(model) for model in models)
format_string = "{:<20} : {:>10} {:>10} {:>10} {:>10}"

client = OpenAI()

# OpenAIの公式ドキュメントのサンプルプロンプト
messages = [
    {"role": "system", "content": "You are a poetic assistant, skilled in explaining complex programming concepts with creative flair."},
    {"role": "user", "content": "Compose a poem that explains the concept of recursion in programming."}
]

print(format_string.format("Model", "AVG", "MIN", "MAX", "MED"))
for model in models:
    res = openai_completion(client, model, messages)[1]
    print(format_string.format(model, res["avg"], res["min"], res["max"], res["med"]))

10回実施した結果(秒)

モデル 平均 最小 最大 中間
gpt-3.5-turbo 7.91 6.57 9.46 7.68
gpt-3.5-turbo-1106 1.55 1.28 1.89 1.54
gpt-4 25.11 15.67 32.15 26.09
gpt-4-1106-preview 6.55 4.36 9.06 6.47

kun432kun432

変なゴミが入ってたのでちょっと修正した。

kun432kun432

ちなみに日本語の場合

messages = [
    {"role": "system", "content": "あなたは詩的なアシスタントで、複雑なプログラミングのコンセプトをクリエイティブなセンスで説明することに長けている。"},
    {"role": "user", "content": "プログラミングにおける再帰の概念を説明する詩を作ってください。日本語で。"}
]
モデル 平均 最小 最大 中間
gpt-3.5-turbo 8.42 6.53 11.17 8.55
gpt-3.5-turbo-1106 1.48 0.79 2.45 1.45
gpt-4 30.62 19.35 41.29 27.91
gpt-4-1106-preview 7.69 5.52 11.06 7.25

gpt-3.5-turbo-1106だけなぜか英語よりもちょっと速くなってるけども(2回やったけど同じだった)、やっぱり日本語は英語に比べると全体的に遅くなると思って良さそう。

このスクラップは6ヶ月前にクローズされました