【GPT4o対応】OpenAI API のPythonサンプルコードと出力例

【GPT4o対応】OpenAI API のPythonサンプルコードと出力例
Photo by Fotis Fotopoulos / Unsplash

今回は、OpenAI の API 利用サンプルコードをご紹介します。

OpenAI API は OpenAI純正のAPI のみならず、vLLMなど他の推論エンジンでも OpenAI 準拠のAPIサーバーが公開されており、LLMサービングAPIのデファクトとなりつつありますので、コーディングのお作法をおさえておきましょう。

OpenAI の GPT シリーズのAPIにアクセスするための、シンプルなサンプルコードは以下のようになります。生成結果をストリーミングで逐次受信してみましょう。

サンプルコード:クイックスタート

import asyncio
import os
import traceback

from openai import AsyncOpenAI


async def main() -> None:
    try:
        # モデル名を指定
        # model="gpt-4-turbo" # $10.00/MTok for input ,$30.00/MTok for output
        # model="gpt-4o" # $5.00/MTok for input ,$15.00/MTok for output
        model = "gpt-3.5-turbo-0125"  #

        # 環境変数からAPIキーを取得
        api_key = "your api key"
        
        client = AsyncOpenAI(
            api_key=api_key
        )

        stream = await client.chat.completions.create(
            model=model,
            stream=True,
            messages=[
                {"role": "system", "content": "あなたは誠実な日本語アシスタントです"},
                {"role": "user", "content": "こんにちは"}
            ],
            stream_options={"include_usage": True},  # usage(in,outのトークン数) を出力
        )

        async for chunk in stream:
            print(f"chunk__{chunk}")
    except Exception as e:
        print(f"予期せぬエラーが発生しました: {e}\n{traceback.format_exc()}")
    finally:
        pass


asyncio.run(main())

出力例

ChatCompletionChunk(id='chatcmpl-9iGtdyZ43HFebZV22QOaZPIzgeStA', choices=[Choice(delta=ChoiceDelta(content='', function_call=None, role='assistant', tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1234567890, model='gpt-3.5-turbo-0125', object='chat.completion.chunk', system_fingerprint=None, usage=None)
ChatCompletionChunk(id='chatcmpl-9iGtdyZ43HFebZV22QOaZPIzgeStA', choices=[Choice(delta=ChoiceDelta(content='こんにちは', function_call=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1234567890, model='gpt-3.5-turbo-0125', object='chat.completion.chunk', system_fingerprint=None, usage=None)
ChatCompletionChunk(id='chatcmpl-9iGtdyZ43HFebZV22QOaZPIzgeStA', choices=[Choice(delta=ChoiceDelta(content='!', function_call=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1234567890, model='gpt-3.5-turbo-0125', object='chat.completion.chunk', system_fingerprint=None, usage=None)
ChatCompletionChunk(id='chatcmpl-9iGtdyZ43HFebZV22QOaZPIzgeStA', choices=[Choice(delta=ChoiceDelta(content='何', function_call=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1234567890, model='gpt-3.5-turbo-0125', object='chat.completion.chunk', system_fingerprint=None, usage=None)
ChatCompletionChunk(id='chatcmpl-9iGtdyZ43HFebZV22QOaZPIzgeStA', choices=[Choice(delta=ChoiceDelta(content='か', function_call=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1234567890, model='gpt-3.5-turbo-0125', object='chat.completion.chunk', system_fingerprint=None, usage=None)
ChatCompletionChunk(id='chatcmpl-9iGtdyZ43HFebZV22QOaZPIzgeStA', choices=[Choice(delta=ChoiceDelta(content='お', function_call=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1234567890, model='gpt-3.5-turbo-0125', object='chat.completion.chunk', system_fingerprint=None, usage=None)
ChatCompletionChunk(id='chatcmpl-9iGtdyZ43HFebZV22QOaZPIzgeStA', choices=[Choice(delta=ChoiceDelta(content='手', function_call=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1234567890, model='gpt-3.5-turbo-0125', object='chat.completion.chunk', system_fingerprint=None, usage=None)
ChatCompletionChunk(id='chatcmpl-9iGtdyZ43HFebZV22QOaZPIzgeStA', choices=[Choice(delta=ChoiceDelta(content='伝', function_call=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1234567890, model='gpt-3.5-turbo-0125', object='chat.completion.chunk', system_fingerprint=None, usage=None)
ChatCompletionChunk(id='chatcmpl-9iGtdyZ43HFebZV22QOaZPIzgeStA', choices=[Choice(delta=ChoiceDelta(content='い', function_call=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1234567890, model='gpt-3.5-turbo-0125', object='chat.completion.chunk', system_fingerprint=None, usage=None)
ChatCompletionChunk(id='chatcmpl-9iGtdyZ43HFebZV22QOaZPIzgeStA', choices=[Choice(delta=ChoiceDelta(content='で', function_call=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1234567890, model='gpt-3.5-turbo-0125', object='chat.completion.chunk', system_fingerprint=None, usage=None)
ChatCompletionChunk(id='chatcmpl-9iGtdyZ43HFebZV22QOaZPIzgeStA', choices=[Choice(delta=ChoiceDelta(content='き', function_call=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1234567890, model='gpt-3.5-turbo-0125', object='chat.completion.chunk', system_fingerprint=None, usage=None)
ChatCompletionChunk(id='chatcmpl-9iGtdyZ43HFebZV22QOaZPIzgeStA', choices=[Choice(delta=ChoiceDelta(content='ます', function_call=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1234567890, model='gpt-3.5-turbo-0125', object='chat.completion.chunk', system_fingerprint=None, usage=None)
ChatCompletionChunk(id='chatcmpl-9iGtdyZ43HFebZV22QOaZPIzgeStA', choices=[Choice(delta=ChoiceDelta(content='か', function_call=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1234567890, model='gpt-3.5-turbo-0125', object='chat.completion.chunk', system_fingerprint=None, usage=None)
ChatCompletionChunk(id='chatcmpl-9iGtdyZ43HFebZV22QOaZPIzgeStA', choices=[Choice(delta=ChoiceDelta(content='?', function_call=None, role=None, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1234567890, model='gpt-3.5-turbo-0125', object='chat.completion.chunk', system_fingerprint=None, usage=None)
ChatCompletionChunk(id='chatcmpl-9iGtdyZ43HFebZV22QOaZPIzgeStA', choices=[Choice(delta=ChoiceDelta(content=None, function_call=None, role=None, tool_calls=None), finish_reason='stop', index=0, logprobs=None)], created=1234567890, model='gpt-3.5-turbo-0125', object='chat.completion.chunk', system_fingerprint=None, usage=None)
ChatCompletionChunk(id='chatcmpl-9iGtdyZ43HFebZV22QOaZPIzgeStA', choices=[], created=1234567890, model='gpt-3.5-turbo-0125', object='chat.completion.chunk', system_fingerprint=None, usage=CompletionUsage(completion_tokens=14, prompt_tokens=31, total_tokens=45))

サンプルコード:ストリーミングされたチャンク内容をパースする

chunkをパースして、中身の各種データを取得してみましょう

import asyncio
import traceback

from openai import AsyncOpenAI


async def main() -> None:
    try:
        model = "gpt-3.5-turbo-0125"
        api_key = "your api key"
        client = AsyncOpenAI(api_key=api_key)

        stream = await client.chat.completions.create(
            model=model,
            stream=True,
            messages=[
                {"role": "system", "content": "あなたは誠実な日本語アシスタントです"},
                {"role": "user", "content": "こんにちは"}
            ],
            stream_options={"include_usage": True},  # usage を出力する
        )

        first_chunk = None
        last_chunk = None
        finish_reason = None
        full_content = ""
        role = None
        created = None
        model = None
        completion_id = None

        async for chunk in stream:

            object_type = chunk.object

            if object_type == "chat.completion.chunk":

                if first_chunk is None:
                    # 初回チャンクのとき
                    first_chunk = chunk

                    model = first_chunk.model
                    created = first_chunk.created
                    completion_id = first_chunk.id

                    if chunk.choices:
                        first_choice = chunk.choices[0]
                        role = first_choice.delta.role

                    # 初回チャンクで取得できる情報
                    print(f"completion_id: {completion_id}")
                    print(f"created: {created}")

                    # 初回チャンクのみで取得できる情報
                    print(f"model: {model}")
                    print(f"role: {role}")

                    print("streaming text:", end="", flush=True)

                last_chunk = chunk

                if chunk.choices:
                    first_choice = chunk.choices[0]

                    if first_choice.delta.content:
                        # 今イテレーションで生成されたテキスト
                        delta_str = first_choice.delta.content

                        print(delta_str, end="", flush=True)  # 生成されたテキストを逐次出力する

                        full_content += delta_str  # 全体テキストに追記

                    if finish_reason is None:
                        finish_reason = first_choice.finish_reason

        print()

        if last_chunk:
            # 最終チャンクのデータを処理

            usage = last_chunk.usage

            print(f"Full Content: {full_content}")
            print(f"Finish Reason: {finish_reason}")

            if usage:
                print(f"ttl tokens: {usage.total_tokens}")
                print(f"num input tokens:: {usage.prompt_tokens}")
                print(f"num output tokens: {usage.completion_tokens}")
            else:
                print("Usage information not available")

    except Exception as e:
        print(f"予期せぬエラーが発生しました: {e}\n{traceback.format_exc()}")
    finally:
        pass


asyncio.run(main())

実行結果

completion_id: chatcmpl-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
created: 123456789
model: gpt-3.5-turbo-0125
role: assistant
streaming text:こんにちは!どのようにお手伝いしましょうか?
Full Content: こんにちは!どのようにお手伝いしましょうか?
Finish Reason: stop
ttl tokens: 51
num input tokens:: 31
num output tokens: 20

Read more

TensorRT 10 × Blackwell 移行ガイド【後編】5D grid_sample をカスタムプラグインで通す

TensorRT 10 × Blackwell 移行ガイド【後編】5D grid_sample をカスタムプラグインで通す

こんにちは! 「TensorRT 10 × Blackwell 移行ガイド」シリーズもいよいよ最終回です。前編では TensorRT 化の基本と「5D(volumetric)の grid_sample はビルドが拒否される」という壁を、中編では「ビルドが通っても正しいとは限らない」沈黙劣化の数々を扱いました。 回テーマ 前編(Part 1)RTX 50 で推論資産が動かない — 基本と最初の壁 中編(Part 2)ビルドが通っても正しいとは限らない — 沈黙劣化 5 連発 後編(Part 3)5D grid_sample をカスタムプラグインで通す(本記事) 後編は、前編で保留にした最大の宿題に挑みます。TensorRT がネイティブに対応していない 5D grid_sample を、

By Qualiteg プロダクト開発部
Luckfox Pico Mを外出先から使う。3,750円・64MBのLinuxボードにWireCanalで公開URLをつけるまで

Luckfox Pico Mを外出先から使う。3,750円・64MBのLinuxボードにWireCanalで公開URLをつけるまで

秋葉原で買った3,750円のLinuxボード Luckfox Pico M(RV1103・64MB・USB-Cのみ)を、Web公開用の受信ポートを追加せずにインターネット側から使えるようにするまで。SDカード書き込み、RNDIS経由のネットワーク、WireCanal無料プランでの公開、busybox initでの常駐とブラウザからのLED操作、再起動復帰(33秒)まで実測手順で解説します。

By Qualiteg プロダクト開発部
Raspberry Pi 5のWebサーバーをインターネットに公開する。ポートは開けない、WireCanalのトンネルで

Raspberry Pi 5のWebサーバーをインターネットに公開する。ポートは開けない、WireCanalのトンネルで

受信ポートを閉じたまま、Raspberry Pi 5のWebサーバーをインターネットに公開します。WireCanalのAgentをワンライナーで入れ、canalを作り、systemdで常駐化。ポート開放もルーター設定も不要で、操作はおよそ5分。再起動後の自動復活まで実測で検収しました。

By Qualiteg プロダクト開発部
Raspberry Pi 5を堅牢化する。SSH鍵認証・UFW・fail2banと、再起動でIPv6が復活する罠

Raspberry Pi 5を堅牢化する。SSH鍵認証・UFW・fail2banと、再起動でIPv6が復活する罠

セットアップ直後のRaspberry Pi 5は、パスワード認証が有効なまま、ファイアウォールなし、更新可能なパッケージ172件と無防備です。SSHの鍵認証化・UFW・fail2ban・自動更新・IPv6無効化を実コマンドと実測検収で解説します。sysctlのIPv6無効化が再起動で復活する現象と恒久対策も実機で確認しました。

By Qualiteg プロダクト開発部