คำตอบสั้น
ฉันจะเรียก LLMwithCrypto จาก Node.js ได้อย่างไร?
ติดตั้งแพ็กเกจทางการด้วย npm install openai จากนั้นสร้าง client ด้วย baseURL: "https://llmwithcrypto.com/api/v1" และ apiKey: process.env.LLMWITHCRYPTO_API_KEY LLMwithCrypto ตอบกลับ client.chat.completions.create() ในรูปแบบ OpenAI สำหรับ 12 open-weight model พร้อม streaming และ tool calls ในโมเดลที่รองรับ ตามที่ระบุไว้ ณ วันที่ 23 ตุลาคม 2026
สิ่งที่คุณต้องมี
| Node.js | 18 ขึ้นไป, ES modules (.mjs หรือ "type": "module") |
|---|---|
| แพ็กเกจ | openai (ทางการ) |
| Base URL | https://llmwithcrypto.com/api/v1 |
| Key | ในตัวแปรสภาพแวดล้อม LLMWITHCRYPTO_API_KEY ไม่ใช่ในโค้ดหรือเบราว์เซอร์ |
ฉันจะส่งหนึ่งคำขอได้อย่างไร?
สร้าง client ครั้งเดียวตอนเริ่มต้นแล้วใช้ซ้ำทุกที่ เก็บ key ไว้บนเซิร์ฟเวอร์: key ที่ฝังอยู่ในโค้ดเบราว์เซอร์สามารถถูกอ่านได้โดยใครก็ตามที่เปิดหน้าเว็บ ดังนั้นเรียก API จาก backend ของคุณและส่งเฉพาะคำตอบไปยังเบราว์เซอร์
import OpenAI from 'openai';
const client = new OpenAI({
baseURL: 'https://llmwithcrypto.com/api/v1',
apiKey: process.env.LLMWITHCRYPTO_API_KEY,
});
const resp = await client.chat.completions.create({
model: 'deepseek-v4-pro',
messages: [
{ role: 'system', content: 'Answer in two sentences.' },
{ role: 'user', content: 'What is a context window?' },
],
max_tokens: 200,
});
console.log(resp.choices[0].message.content);
console.log(resp.usage.prompt_tokens, 'in,', resp.usage.completion_tokens, 'out');
ฉันจะรันชุดงานโดยไม่ชนกับ rate limit ได้อย่างไร?
รันคำขอจำนวนคงที่ในแต่ละครั้ง helper mapLimit เล็ก ๆ ด้านล่างนี้เก็บสี่คำขอให้ทำงานพร้อมกันและคืนผลลัพธ์ตามลำดับ ไม่ต้องใช้แพ็กเกจเพิ่มเติม ทุกบัญชีมีขีดจำกัดคำขอและ token ต่อนาที ซึ่งแสดงในแดชบอร์ด; แจ้งฝ่ายสนับสนุนและขีดจำกัดนั้นสามารถเพิ่มได้
import OpenAI from 'openai';
const client = new OpenAI({
baseURL: 'https://llmwithcrypto.com/api/v1',
apiKey: process.env.LLMWITHCRYPTO_API_KEY,
});
async function translate(text) {
const resp = await client.chat.completions.create({
model: 'qwen3-235b-a22b-instruct-2507',
messages: [
{ role: 'system', content: 'Translate into Spanish. Reply with the translation only.' },
{ role: 'user', content: text },
],
max_tokens: 200,
});
return resp.choices[0].message.content;
}
// at most 4 requests in flight
async function mapLimit(items, limit, fn) {
const out = new Array(items.length);
let next = 0;
async function worker() {
while (next < items.length) {
const i = next++;
out[i] = await fn(items[i]);
}
}
await Promise.all(Array.from({ length: limit }, worker));
return out;
}
const lines = ['Your order has shipped.', 'Your payment was received.', 'Your key was revoked.'];
console.log(await mapLimit(lines, 4, translate));
ฉันจะสตรีมคำตอบได้อย่างไร?
ส่ง stream: true และใช้ for await กับผลลัพธ์ แต่ละ chunk มีข้อความบางส่วนใน choices[0].delta.content; เมื่อใช้ stream_options: { include_usage: true } chunk สุดท้ายจะมีจำนวน token
import OpenAI from 'openai';
const client = new OpenAI({
baseURL: 'https://llmwithcrypto.com/api/v1',
apiKey: process.env.LLMWITHCRYPTO_API_KEY,
});
const stream = await client.chat.completions.create({
model: 'deepseek-v4.1-flash',
messages: [{ role: 'user', content: 'Write a haiku about latency.' }],
stream: true,
stream_options: { include_usage: true },
max_tokens: 120,
});
for await (const chunk of stream) {
const text = chunk.choices[0]?.delta?.content;
if (text) process.stdout.write(text);
if (chunk.usage) console.log(`\n${chunk.usage.prompt_tokens} in, ${chunk.usage.completion_tokens} out`);
}
ฉันจะดูเครดิตที่เหลือได้อย่างไร?
endpoint เครดิตไม่ได้เป็นส่วนหนึ่งของ OpenAI SDK ดังนั้นเรียกด้วย fetch ฟรี; เรียกก่อนชุดงานขนาดใหญ่เพื่อให้แน่ใจว่าเครดิตจะครอบคลุม
const res = await fetch('https://llmwithcrypto.com/api/v1/credits', {
headers: { Authorization: `Bearer ${process.env.LLMWITHCRYPTO_API_KEY}` },
});
const credits = await res.json();
console.log('credit left:', credits.balance_usd, 'USD');
ฉันจะจัดการกับข้อผิดพลาดได้อย่างไร?
SDK จะ throw OpenAI.APIError สำหรับทุก HTTP error โดยมี status ใน err.status และ code ของ body ใน err.code ทดสอบโค้ด ไม่ใช่ข้อความ: insufficient_balance หมายถึงเติมเครดิต model_not_found หมายถึง id ผิด rate_limit_exceeded หมายถึงรอแล้วลองใหม่
SDK ลองใหม่บางข้อผิดพลาดสองครั้งพร้อมหยุดพักอยู่แล้ว (maxRetries) สำหรับชุดงานยาว ให้คงค่าเริ่มต้นนั้นไว้ จำกัด concurrency ตามข้างบน และบันทึกความล้มเหลวที่คุณยอมแพ้ หน้า errors แสดงรายการทุก status และรายการใดที่คุ้มค่าลองใหม่ การเรียกที่ล้มเหลวไม่มีค่าใช้จ่าย: คุณจ่ายต่อ token จากยอดคงเหลือ: input และ output tokens ถูกนับด้วย tokenizer ของโมเดลนั้นเองและคิดตามราคาของโมเดลนั้นต่อ 1M tokens การเรียกที่จบด้วย error ไม่มีค่าใช้จ่าย
ฉันจะย้ายแอป OpenAI ที่มีอยู่ได้อย่างไร?
เปลี่ยนตัวเลือกของ client, baseURL และ apiKey และแทนที่ชื่อโมเดล gpt-… ทุกตัวด้วย id จาก หน้า models prompt, โค้ดสตรีมมิ่ง และการแยกวิเคราะห์ choices และ usage คงเดิมไว้ รันเทสต์บนโมเดลใหม่ก่อนสลับทราฟฟิก: tool calling และ JSON output เป็นไปตามที่แต่ละโมเดลรองรับ
Node.js: คำถามที่พบบ่อย
ฉันเรียก API จากเบราว์เซอร์ได้ไหม?
ในทางเทคนิคได้ แต่ห้ามใส่ key ของคุณในหน้านั้น เรียกจากเซิร์ฟเวอร์ของคุณ หรือจาก serverless function ที่เก็บ key ไว้
Vercel AI SDK ใช้ได้ไหม?
ไลบรารีใดก็ตามที่รับ base URL ที่เข้ากันได้กับ OpenAI จะใช้ได้ API รองรับรูปแบบ OpenAI chat completions: ชี้ OpenAI SDK, LangChain หรือเครื่องมือที่เข้ากันได้กับ OpenAI ใด ๆ ไปที่ base URL ของเราพร้อม API key ของคุณ แล้วเปลี่ยนชื่อโมเดล
จะเกิดอะไรขึ้นเมื่อเครดิตหมด?
การเรียกจะคืน 402 insufficient_balance และไม่มีค่าใช้จ่าย หน้า errors แสดงวิธีตอบสนอง