2
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?

Node.js で OpenAI の 新API(Responses API)をシンプルに試す

Last updated at Posted at 2025-03-14

はじめに

新API

以下の公式の記事に登場している新API の「Responses API」をシンプルに試してみた、という話です。

●New tools for building agents | OpenAI
 https://openai.com/index/new-tools-for-building-agents/

2025-03-14_23-40-49.jpg

記事の中では、こちらの部分などに登場しています。

2025-03-14_23-42-12.jpg

参照する情報

API の情報を追いかけてみていたところ以下が読みやすかったので、こちらも参照しつつ進めてみます。

●OpenAI Responses API と Agents SDK リリースのまとめ
 https://zenn.dev/openaidevs/articles/616681c2e7dc39

掲載されているサンプルは Python ですが、Node.js の処理に読みかえたり、以下の公式サンプルを併用しつつ活用していきます。

●openai/openai-node: Official JavaScript / TypeScript library for the OpenAI API
 https://github.com/openai/openai-node/tree/master

2025-03-15_00-34-45.jpg

実際に試す

実際に API を試してみます。

利用するライブラリ

今回は、以下の記事を書いた時などにも使った OpenAI の TypeScript・JavaScript用ライブラリを使います。

●GPT-4o と Node.js の process.loadEnvFile() と OpenAI のライブラリを組み合わせる - Qiita
 https://qiita.com/youtoy/items/d535f9dd3db95b914fab

2025-03-15_00-13-34.jpg

試してみる処理

参照記事を見ると、シンプルに使う場合の例が掲載されています。

2025-03-15_00-16-44.jpg

Chat Completions を使う場合より、少しシンプルに書けるようです。

実装

公式の Node.js のサンプルなども見つつ、以下の内容を試してみました。

app.mjs
import OpenAI from "openai";

process.loadEnvFile("./development.env");
const client = new OpenAI();

const response = await client.responses.create({
  model: "gpt-4o",
  input: "あなたは誰?",
});

console.log(response.output_text);

APIキーは、過去の記事でも使った Node.js の process.loadEnvFile() で設定してみました。具体的には以下のファイルを用意して、ここから APIキーを読みこんでいます。

development.env
OPENAI_API_KEY=【ご自身の OpenAI の APIキー】

実行結果

上記を実行した結果は以下のとおりです。

2025-03-15_00-25-28.jpg

Chat Completions との差分

公式サンプルの以下を見ると、差分が分かる例が書かれています。

2025-03-15_00-34-45.jpg

抜粋すると、以下のような形です。

プロンプトまわり

const completion = await client.chat.completions.create({
  model: 'gpt-4o',
  messages: [
    { role: 'developer', content: 'Talk like a pirate.' },
    { role: 'user', content: 'Are semicolons optional in JavaScript?' },
  ],
});

 ↓

const response = await client.responses.create({
  model: 'gpt-4o',
  instructions: 'You are a coding assistant that talks like a pirate',
  input: 'Are semicolons optional in JavaScript?',
});

出力の取得

console.log(completion.choices[0].message.content);

 ↓

console.log(response.output_text);

すっきりと書けるようになっているのが分かります。

さらにお試し

さらにストリーミングによる出力を試してみます。

実装

以下のような実装で試してみました。

import OpenAI from "openai";

process.loadEnvFile("./development.env");
const client = new OpenAI();

const stream = await client.responses.create({
  model: "gpt-4o",
  input: "あなたは誰?",
  stream: true,
});

for await (const event of stream) {
  if (event.type === "response.output_text.delta") {
    console.log(event.delta);
  }
}

出力結果

上記を実行した結果は、以下のとおりで

2025-03-15_01-09-48.jpg

2
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
2
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?