1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Node.js】OpenAI Vision APIを使ってみた

Last updated at Posted at 2023-11-08

11月7日早朝のOpenAI DevDayで、たくさんのアップデートがあり、とりあえずOpenAI Vision APIを使ってみました。

ターミナル画面で以下のコマンドを入力

$ mkdir visionAPI
$ cd visionAPI
$ npm init -y

環境変数を使うため dotenvをインストール

$ npm install --save dotenv

同じフォルダに .envを作る

$ touch .env

.env に環境変数を入力

.env
OPENAI_API_KEY="OpenAIのAPI"

パッケージをインストール

$ npm install openai

実行プログラム app.js を作成

$ touch app.js

app.js に以下のコードを入力

app.js
require("dotenv").config();

const OpenAI = require('openai');

const openai = new OpenAI({
    apiKey: process.env.OPENAI_API_KEY,
  });

async function main() {

    const response =  await openai.chat.completions.create({
        model: "gpt-4-vision-preview",
        messages: [
            {
                role: "user",
                content: [
                    {type: "text", text: "この画像には何が写っていますか?"},
                    {
                     type: "image_url",
                     image_url: "画像ファイルのURL",
                    },
                ],
            }    
        ],
        max_tokens:300,
    });
     
    console.log(response.choices[0]);

}

main();




試しに、自分のブログから
この画像のURLをいれてみます。

実行すると、

node app.js
{
  message: {
    role: 'assistant',
    content: 'この画像には、三本の異なる種類の日本酒がテーブルの上に置かれています。左から、黒いラベルに赤い文字がありますね、「勝駒(かちぐら)」と書かれているようです。真ん中のボトルは「大七」とラベルにあり、右の緑のボトルは「国稀 CLASSIC」の文字が見えます。また、日本酒を飲むための三つの小さなおちょこも並んでいます。このような設定は日本の居酒屋やレストランにてよく見られます。背景には和風のインテリアが見て取れ、日本の飲食文化が感じられる場面です。'
  },
  finish_details: { type: 'stop', stop: '<|fim_suffix|>' },
  index: 0
}

さすがにラベルまでは読めなかったですが、すごい精度ですね。
1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?