Node.jsからGitHub Modelsを使って各種モデルにアクセスする方法のメモです。
アクセスの仕方
基本はAPIキーがあればGitHubのサイトにアクセスする必要はそんなにないので最初だけの手順です
マーケットプレイスにアクセスする。
色々選べっぞ。o3も使えるみてぇだな。
モデルのプレビュー画面に移動します。ここで試しにAPI経由の会話などをできますが、ここでは右上のUse This Models
を選択
使い方のチュートリアルが表示されます。
まずはAPIキー(Developer Keyって書いてますね)を取得します。
APIキーの取得(GitHubのアクセストークン)
https://tech.kentem.jp/entry/2024/12/16/090000
こちらの記事を参考にしました。
トークン取得の管理画面にいき、Generate new token
を選択します。
classicじゃない方ですね。
任意の名前(for GitHub Modelsみたいな)名前をつける
特に設定など無しでそのまま作成します。
github_pat_xxxxxxxxxxxxx
のようなAPIキーが作成されます。
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で使うのは良きですね。
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が消滅
などですね。
すごい微妙でした。
o4-miniも試したけど、GPT-4oの方がこの辺優秀に見えてしまった。
色々選べっぞ。o3も使えるみてぇだな。
が好きだったんで記事にもしれっと採用