Open1

さくっとOpenAI Vision APIを触った

オレミ_微経験エンジニアオレミ_微経験エンジニア

クイックスタートのサンプルからコードを引用
https://platform.openai.com/docs/guides/vision

from openai import OpenAI
from dotenv import load_dotenv
import os

load_dotenv()


client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))

response = client.chat.completions.create(
    model="gpt-4-vision-preview",
    messages=[
        {
            "role": "user",
            "content": [
                {"type": "text", "text": "What’s in this image?"},
                {
                    "type": "image_url",
                    "image_url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg",
                },
            ],
        }
    ],
    max_tokens=300,
)

print(response.choices[0])


テスト画像

結果

he image shows a wooden boardwalk extending through a lush green landscape. The boardwalk appears to be a walkway for visitors to enjoy the natural surroundings without impacting the grassy habitat. The tall grasses on either side of the path are vibrant and suggest a healthy, undisturbed ecosystem. In the background, you can see some trees and shrubs dotting the horizon under a blue sky adorned with wispy clouds. The scene is peaceful and suggests a place where one can go for a quiet walk and enjoy nature.

和訳

画像は、緑豊かな風景の中に伸びる木製の遊歩道。この遊歩道は、草地の生息環境に影響を与えることなく、自然環境を楽しむための遊歩道のようだ。遊歩道の両脇に生えている背の高い草は生き生きとしており、健全で乱されていない生態系を思わせる。背景には、うっすらと雲が浮かぶ青空の下、地平線に点在する木々や低木が見える。この光景は、静かな散歩をしながら自然を満喫できる場所であることを示唆している。
# old
import openai

openai.api_key = os.environ['OPENAI_API_KEY']

# new
from openai import OpenAI

client = OpenAI(
  api_key=os.environ['OPENAI_API_KEY'],  # this is also the default, it can be omitted
)