はじめに
新API
以下の公式の記事に登場している新API の「Responses API」をシンプルに試してみた、という話です。
●New tools for building agents | OpenAI
https://openai.com/index/new-tools-for-building-agents/
記事の中では、こちらの部分などに登場しています。
参照する情報
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
実際に試す
実際に API を試してみます。
利用するライブラリ
今回は、以下の記事を書いた時などにも使った OpenAI の TypeScript・JavaScript用ライブラリを使います。
●GPT-4o と Node.js の process.loadEnvFile() と OpenAI のライブラリを組み合わせる - Qiita
https://qiita.com/youtoy/items/d535f9dd3db95b914fab
試してみる処理
参照記事を見ると、シンプルに使う場合の例が掲載されています。
Chat Completions を使う場合より、少しシンプルに書けるようです。
実装
公式の Node.js のサンプルなども見つつ、以下の内容を試してみました。
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キーを読みこんでいます。
OPENAI_API_KEY=【ご自身の OpenAI の APIキー】
実行結果
上記を実行した結果は以下のとおりです。
Chat Completions との差分
公式サンプルの以下を見ると、差分が分かる例が書かれています。
抜粋すると、以下のような形です。
プロンプトまわり
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);
}
}
出力結果
上記を実行した結果は、以下のとおりで