読み方のヒント

矢印ボタン(または ← → キー)でページを移動できます。

第2節 画像を渡して読ませる

2.1 画像を入力に入れる

Responses APIでは、input の内容に、文と画像を並べて入れられます。手元の画像は、base64という文字の形にして渡します。

def describe_photo(client, path):
    with open(path, "rb") as f:
        data = base64.b64encode(f.read()).decode()
    response = client.responses.create(
        model=model_main(),
        input=[{
            "role": "user",
            "content": [
                {"type": "input_text", "text": "写っている湯のみの数と、欠けや割れが見えるかを答えてください。見えないものは推測しないでください。"},
                {"type": "input_image", "image_url": f"data:image/jpeg;base64,{data}"},
            ],
        }],
    )
    return response.output_text
内容の種類入れるもの
input_text頼む文
input_image画像。data: で始まる文字か、公開されている画像のURL

6 / 20