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

OpenAI の gpt-oss-120b を Vercel AI Gateway の API で試す【Node.js】

Last updated at Posted at 2025-08-11

はじめに

この記事の内容は、以下の記事や X のポストに書いている「OpenAI のオープンなモデル gpt-oss」の話です。

●gpt-oss-120b(OpenAI のオープンなモデル)を OpenAI公式のプレイグラウンド(ログイン不要でブラウザで使えるもの)で試す - Qiita
 https://qiita.com/youtoy/items/ad997c8c901ab5ab9acc

●【Node.js】 Hugging Face の「How to use OpenAI’s GPT OSS」に書かれている内容を試してみた(OpenAI の gpt-oss-120b を API で使う) - Qiita
 https://qiita.com/youtoy/items/8a6fed0448a9a2cfb180

上記では、自宅の Mac mini(M4、ユニファイドメモリ 24GB)でのローカルLLM や、モデルのホスティングサービスを使った API利用を試したものでした。
※ 20b のほうは、モデルサイズ的に自宅の Mac mini で扱えたものの、120b のほうはモデルサイズがその Mac mini で扱えるサイズではなかったので、クラウドサービス経由での利用としました

今回は 120b のほうのモデルを API で扱う話にはなるのですが、上記とは別のホスティング元を利用してみた話になります。

今回の内容

今回は、Vercel 公式の X のポストや記事に書かれている、「Vercel AI Gateway」で OpenAI の「gpt-oss(120b のほう)」を扱ってみます。

●gpt-oss-20b and gpt-oss-120b are now supported in Vercel AI Gateway - Vercel
 https://vercel.com/changelog/gpt-oss-20b-and-gpt-oss-120b-are-now-supported-in-vercel-ai-gateway

image.png

公式記事の内容

上記の公式記事のほうの内容を見ると、Node.js用のサンプルコードが掲載されています。

import { streamText } from 'ai'

const result = streamText({
  model: "openai/gpt-oss-120b", // or openai/gpt-oss-20b
  prompt: "Generate an ansi animation of sutro tower" 
})

Vercel の AI SDK のパッケージが使われているサンプルとなっていて、モデル指定のところが gpt-oss になっています(それと、APIキーが特定の名前の環境変数に設定されている前提になっているようです)。

さっそく試す

さっそく試してみます。以下の「AI Gateway」のページを見てみると、APIキーを取得できるページのリンクや、公式ドキュメントのリンクが掲載されています。

●AI Gateway
 https://vercel.com/ai-gateway

image.png

AI Gateway を使うためのセットアップについては、以下の公式ドキュメントに書いてありました。

●AI Gateway
 https://vercel.com/docs/ai-gateway

image.png

APIキーの取得

AI Gateway の APIキーを取得します。そのために、Vercel のダッシュボードの「AI Gateway」のタブ( https://vercel.com/【Vercelの自身のプロジェクト名】/~/ai/api-keys )を開きます。

image.png

さらに、その「AI Gateway」のページの左メニューから「API Keys」という項目を選びます。ここで APIキーを作成するのですが、1つキーを作成した後の状態は以下のようになります。

image.png

パッケージのインストールと APIキーの設定

以下の AI SDK のパッケージを、npmコマンドでインストールします。

●ai - npm
 https://www.npmjs.com/package/ai

コマンドは以下のとおりです。

npm i ai

それと、「AI_GATEWAY_API_KEY」という名前の環境変数に AI Gateway の APIキーをセットしておきます。

今回用のコードと実行結果

今回は、とりあえずストリーミングレスポンスを使ったコードを、以下のような内容で作ってみました。

import { streamText } from "ai";

const message = "なぜ空は青い?";

const result = streamText({
  model: "openai/gpt-oss-120b",
  prompt: message,
});

let fullResponse = "";
process.stdout.write("回答: ");
for await (const delta of result.textStream) {
  fullResponse += delta;
  process.stdout.write(delta);
}
process.stdout.write("\n\n");

実行結果

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

応答が返ってくるまでの時間も短く、また応答が返ってきてからの出力のスピードも良い感じの結果となりました。

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