以下で発表された新機能の中の1つ「Code Interpreter の Responses API での利用」を試してみます。
●New tools and features in the Responses API | OpenAI
https://openai.com/index/new-tools-and-features-in-the-responses-api/
サクッと試す
公式情報
「Code Interpreter の Responses API での利用」に関する公式ドキュメントは以下です。
●Code Interpreter - OpenAI API
https://platform.openai.com/docs/guides/tools-code-interpreter#usage-notes
シンプルな使い方
一番シンプルな使い方は以下になるようです。
curl を使ったサンプルで、環境変数 OPENAI_API_KEY に APIキーを設定しておけば、以下のコマンドを実行するだけという感じです。
curl https://api.openai.com/v1/responses \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-4.1",
"tools": [{
"type": "code_interpreter",
"container": { "type": "auto" }
}],
"instructions": "You are a personal math tutor. When asked a math question, write and run code to answer the question.",
"input": "I need to solve the equation 3x + 11 = 14. Can you help me?"
}'
実際に試す
上記のサンプルをそのまま実行してみます。以下は、その実行結果の抜粋です。
以下の部分で、計算結果が出ているのを確認できました。
【追記】 Node.js のサンプルが追加されたので試してみた
記事を公開後、Node.js(と Python)のサンプルが追加されたようなので、それも試してみました。
下準備
上記の curl の例と同じく、環境変数 OPENAI_API_KEY に APIキーを設定しておきます。
また、パッケージ「openai」をインストールしておきます。
npm i openai
試すコードと実行結果
試すコードは、公式サンプルそのままです。
import OpenAI from "openai";
const client = new OpenAI();
const resp = await client.responses.create({
model: "gpt-4.1",
tools: [
{
type: "code_interpreter",
container: { type: "auto" },
},
],
instructions:
"You are a personal math tutor. When asked a math question, write and run code to answer the question.",
input: "I need to solve the equation 3x + 11 = 14. Can you help me?",
});
console.log(resp.output_text);
実行結果は以下のとおりで、curl で試した時と同じ結果が得られました。
余談
他に同時に発表された内容を、公式のブログ記事からキャプチャ画像ベースで列挙してみます。