0
0

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からGitHub Models経由でOpenAI o3やDeepSeekを使う

Last updated at Posted at 2025-04-23

Node.jsからGitHub Modelsを使って各種モデルにアクセスする方法のメモです。

アクセスの仕方

基本はAPIキーがあればGitHubのサイトにアクセスする必要はそんなにないので最初だけの手順です

マーケットプレイスにアクセスする。

CleanShot 2025-04-23 at 15.52.07.png

色々選べっぞ。o3も使えるみてぇだな。

CleanShot 2025-04-23 at 15.52.52.png

モデルのプレビュー画面に移動します。ここで試しにAPI経由の会話などをできますが、ここでは右上のUse This Modelsを選択

CleanShot 2025-04-23 at 15.53.50.png

使い方のチュートリアルが表示されます。

CleanShot 2025-04-23 at 15.56.22.png

まずはAPIキー(Developer Keyって書いてますね)を取得します。

APIキーの取得(GitHubのアクセストークン)

https://tech.kentem.jp/entry/2024/12/16/090000
こちらの記事を参考にしました。

トークン取得の管理画面にいき、Generate new tokenを選択します。

classicじゃない方ですね。

https://github.com/settings/tokens
CleanShot 2025-04-23 at 17.40.35.png

任意の名前(for GitHub Modelsみたいな)名前をつける

CleanShot 2025-04-23 at 17.44.05.png

特に設定など無しでそのまま作成します。

CleanShot 2025-04-23 at 17.44.17.png

CleanShot 2025-04-23 at 17.44.24.png

github_pat_xxxxxxxxxxxxxのようなAPIキーが作成されます。

CleanShot 2025-04-23 at 17.44.37.png

Node.jsから扱ってみる

OpenAIのSDKで試します。

$ npm i openai
import OpenAI from "openai";

const token = `github_pat_xxxxxxxxxxxxxxxxxx`; //APIキーを入れる
const endpoint = "https://models.github.ai/inference";
const modelName = "openai/o3";

export async function main() {

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

  const response = await client.chat.completions.create({
    messages: [
    //   { role: "developer", content: "You are a helpful assistant." },
    //   { role: "user", content: "What is the capital of France?" },
    //   { role: "assistant", content: "The capital of France is Paris." },
      { role: "user", content: "こんにちは、あなたは誰ですか?" }
      ],
      model: modelName
    });

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

main().catch((err) => {
  console.error("The sample encountered an error:", err);
});
$ node app.js
こんにちは!私は ChatGPT、OpenAI が開発した対話型の AI アシスタントです。ご質問やご相談、アイデア整理などのお手伝いをいたします。お気軽にお声かけください!

無事にアクセスできましたね。

せっかくなので、DeepSeekを使ってみる

先ほどと同様のやり方でDeepSeekのAPIを使ってみましょう。

モデルサイズ大きいからローカルでなかなか試せないモデルをAPIで使うのは良きですね。

CleanShot 2025-04-23 at 18.54.36.png

OpenAIではないのでOpenAI SDKではなAzuren AIのSDKを使うやり方のようですね。

$ npm i @azure-rest/ai-inference @azure/core-auth @azure/core-sse

ここまでやってV3だと試した環境ではレスポンスが遅く、返答が全く帰ってこなかったのでR1に変更して試しました。max_tokenを減らしたら良かったかも。

import ModelClient, { isUnexpected } from "@azure-rest/ai-inference";
import { AzureKeyCredential } from "@azure/core-auth";

const token = `github_pat_xxxxxxxxxxxxxxxxxx`; //APIキーを入れる
const endpoint = "https://models.github.ai/inference";
// const model = "deepseek/DeepSeek-V3-0324";
const model = "deepseek/DeepSeek-R1";

export async function main() {

  const client = ModelClient(
    endpoint,
    new AzureKeyCredential(token),
  );

  const response = await client.path("/chat/completions").post({
    body: {
      messages: [
        // { role:"system", content: "You are a helpful assistant." },
        { role:"user", content: "こんばんは、あなたは誰ですか?" }
      ],
      temperature: 1.0,
      top_p: 1.0,
      max_tokens: 1000,
      model: model
    }
  });

  if (isUnexpected(response)) {
    throw response.body.error;
  }

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

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

$ node app.js

<think>
Okay, the user said "こんばんは、あなたは誰ですか?" which means "Good evening, who are you?" in Japanese. I need to respond in Japanese.

First, I should greet them back. "こんばんは" is the greeting, so I'll start with that.

Next, they're asking who I am. I'm an AI assistant created by a company, but I should mention that I don't have a specific name. Maybe say I'm here to help with information or tasks.

I should keep it friendly and open-ended, inviting them to ask anything. Make sure the response is clear and polite.

Check for any possible errors. Use simple Japanese without complicated terms. Avoid mentioning the internal name "DeepSeek-R1-Lite-Preview" unless necessary, but the user might be curious. Wait, the user might actually want to know the name. The previous response included "DeepSeek-R1-Lite-Preview" but maybe that's okay here. However, sometimes users prefer a more general answer. Hmm, maybe mention the company and the purpose instead. Let me see.

Original response was "こんばんは!私はDeepSeek-R1-Lite-Previewと呼ばれるAIアシスタントです。質問にお答えしたり、様々なタスクをお手伝いしたりすることができます。何かお困りのことがあれば、お気軽にどうぞ!" which translates to introducing the name. Since the user is asking directly who I am, including the name makes sense. So I'll with that.
</think>

こんばんは!私はDeepSeek-R1-Lite-Previewと呼ばれるAIアシスタントです。質問にお答えしたり、様々なタスクをお手伝いしたりすることができます。何かお困りのことがあれば、お気軽にどうぞ!

とのことです。<think></think>の考えてる部分が結構長いですね。

まとめ

結構簡単に扱えました。無料でさっと試せるのはいいですね。

ちなみに、APIの制限 無料で使えるのはどれくらいかは以下のページに記載されています。
結構太っ腹。軽く試す分には良さそう。

どこで現状の利用状況を調べるのかがいまいちわかってないです。

蛇足: DeepSeekはドラゴンボールの悟空語に変換してくれるのか?

ちょっと気になってしまったので中国産のDeepSeekが孫悟空の喋り方を理解しているのか。この記事自体を変換掛けてみます。

おめぇ、つえぇな

  • お前→おめぇ(omae→omee)
  • 強い→つえぇ(tuyoi→tuee)※「よ」の子音yが消滅

などですね。

すごい微妙でした。

CleanShot 2025-04-23 at 19.22.08.png

o4-miniも試したけど、GPT-4oの方がこの辺優秀に見えてしまった。

CleanShot 2025-04-23 at 19.25.21.png

色々選べっぞ。o3も使えるみてぇだな。が好きだったんで記事にもしれっと採用

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?