5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【開発者向け】無料でGPT-4o APIキーを取得する方法

Last updated at Posted at 2025-02-26

はじめに

OpenAI APIの有料版の使い方は広く知られていますが、開発者がテスト目的で使える無料のGPT-4o APIキーを取得する方法をご存知でしょうか?今回は、GitHubを通じて無料でGPT-4oを体験できる方法をご紹介します。

APIキーの取得手順

1. GitHubマーケットプレイスにアクセス

まずは下記のリンクにアクセスしてください:
https://github.com/marketplace/models/azure-openai/gpt-4o/playground

2. APIキーの発行

1. 画面右上の「Get API key」ボタンをクリックします

スクリーンショット 2025-02-20 20.52.53.png

2. 表示されたダイアログで「Get developer key」ボタンをクリックします

スクリーンショット 2025-02-20 20.54.22.png

3. 遷移先のページで「Generate new token」ボタンを押してトークンを発行します

スクリーンショット 2025-02-20 20.42.37.png

4. 発行されたトークンをコピーし、安全なスペースに保存しましょう

実装手順

1. 環境設定

まずはNode.jsをインストールしてください。

2. プロジェクトの初期化

以下の内容でpackage.jsonを作成します:

{
  "type": "module",
  "dependencies": {
    "openai": "latest"
  }
}

3. 依存関係のインストール

VSCodeでフォルダを開き、ターミナルで以下のコマンドを実行します:

npm install

4. 環境変数の設定

.envファイルを作成し、以下の内容を記入してください。

# 忘れないように、YOUR_TOKENを置き換えてください。
GITHUB_TOKEN=YOUR_TOKEN

5. サンプルコードの実装

以下のコードをsample.jsとして保存します:

import OpenAI from "openai";
import dotenv from "dotenv";

dotenv.config();

const token = process.env["GITHUB_TOKEN"];
const endpoint = "https://models.inference.ai.azure.com";
const modelName = "gpt-4o";

export async function main() {
  const client = new OpenAI({ baseURL: endpoint, apiKey: token });

  const response = await client.chat.completions.create({
    messages: [
      { role:"system", content: "You are a helpful assistant." },
      { role:"user", content: "What is the capital of France?" }
    ],
    temperature: 1.0,
    top_p: 1.0,
    max_tokens: 1000,
    model: modelName
  });

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

main().catch((err) => {
  console.error("The sample encountered an error:", err);
});

6. 実行

ターミナルで以下のコマンドを実行します:

node sample.js

実行結果がターミナルに表示され、GPT-4oによって生成された回答を確認できます。

スクリーンショット 2025-02-20 21.05.52.png

7. その他のサンプルコード実装

先のdialogの「Run a basic code sample」セクションには、以下のような様々なサンプルコードが用意されています。それぞれの実装例を自分で見てみましょう。

  • Run a multi-turn conversation
  • Stream the output
  • Chat with an image input
  • Identify and invoke tools

8. レート制限

こちらのページで確認しましょう
https://docs.github.com/en/github-models/prototyping-with-ai-models#rate-limits

さいごに

この手順を従うことで、開発者は無料でGPT-4oのAPIを試すことができます。本番環境では正規のOpenAI APIの利用をお勧めしますが、初めてAPIを試したい初心者は、ぜひ遊んでみてください!

OpenAI Documentation
OpenAIのAPI利用登録とAPIキー発行方法(動画)

5
5
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
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?