要件#
Node 22以降。WEIGHTSAPI_API_KEYを自分のキーに設定してください。AIアクセスにはサインイン、最低100米ドルの確認済みチャージ1回、承認されたAPIキー、リクエストに十分な利用可能クレジットが必要です。以降の各チャージも最低100米ドルです。残高が少なくてもリクエストをカバーできる場合は使用可能です。クレジットにはトランザクションの検証が必要です。トラフィックを送信する前に、モデルの可用性についてサービス状態を確認してください。
レシピを実行する#
// Node 22+. Save as server.mjs, set WEIGHTSAPI_API_KEY, then node server.mjs.
import http from "node:http";
const base = process.env.WEIGHTSAPI_BASE_URL || "https://weightsapi.com/v1";
http.createServer(async (req, res) => {
if (req.url !== "/stream") {
res.setHeader("Content-Type", "text/html");
return res.end('<button id="run">Run</button><pre id="out"></pre><script>run.onclick=async()=>{out.textContent="";const r=await fetch("/stream");const reader=r.body.getReader();const decoder=new TextDecoder();for(;;){const {value,done}=await reader.read();if(done)break;out.textContent+=decoder.decode(value,{stream:true});}}<\/script>');
}
try {
const upstream = await fetch(base+"/chat/completions", {method:"POST",
headers:{"Content-Type":"application/json",Authorization:"Bearer "+process.env.WEIGHTSAPI_API_KEY},
body:JSON.stringify({model:"Qwen/Qwen3-32B",messages:[{role:"user",content:"Explain SSE."}],stream:true,max_tokens:256})});
res.writeHead(upstream.status, {"Content-Type":upstream.headers.get("content-type")||"text/plain","Cache-Control":"no-store"});
for await (const chunk of upstream.body) res.write(chunk);
res.end();
} catch {res.writeHead(502);res.end("Upstream unavailable");}
}).listen(8080,"127.0.0.1");
本番前#
並行性と再試行を制限し、出力を検証し、ワークフローをユーザーに公開する前にエラーを処理します。