開源專案 · v2.3.1 · MPL-2.0 · NODE.JS >= 22
以單一呼叫契約,統一所有 LLM 供應商。
@ubinity/node-llm 是輕量、零臃腫的 Node.js (>=22) 開源函式庫,將 OpenAI、相容伺服器(DeepSeek / Qwen / Ollama)、Anthropic 與 Gemini / Vertex AI 正規化為單一請求、單一 OpenAI 相容回應與統一錯誤型別(NodeLlmError),同時支援 API Key 與官方訂閱 CLI 呼叫。
$ npm install @ubinity/node-llm const { completion } = require('@ubinity/node-llm');
// Same calling contract for API keys or official subscriptions
const reply = await completion({
model: 'anthropic/claude-haiku-4-5',
auth: 'subscription', // or omit for API key
timeout: 120000,
messages: [
{ role: 'system', content: 'Answer in one concise sentence.' },
{ role: 'user', content: 'What is a vendored dependency?' }
]
});
console.log(reply.auth, reply.choices[0].message.content); 核心設計亮點
同時支援 API Key 與官方訂閱(auth: "subscription")
可透過可選 peer SDK 使用 API Key,亦可透過 Claude Code、Codex、Antigravity CLI 以既有訂閱額度呼叫(自動隔離 API Key 環境變數防誤扣,支援 Windows WSL)。
嚴格契約,拒絕靜默忽略參數
任何不支援的參數皆立即拋出明確的 NodeLlmError ("unsupported"),杜絕線上的隱蔽行為偏差。
內建生產級韌性機制
支援遵循 Retry-After 的指數退避重試、AbortSignal 與逾時控制、LRU 連線複用、Prompt Caching、統一推理 effort 等級與 Tool Calling 標準化。
Supported Model Prefixes & Auth Modes
| Model Prefix | Protocol | Auth Modes |
|---|---|---|
| openai/ · gpt* | openai | API key or Codex CLI (subscription) |
| deepseek/ · qwen/ · dashscope/ | openai | API key |
| ollama/ · custom baseURL | openai | None / optional |
| anthropic/ · claude* | anthropic | API key or Claude Code CLI (subscription) |
| gemini/ · google/ | API key or Antigravity CLI (subscription) | |
| vertex_ai/ | vertex_ai | Google Cloud credentials |
| antigravity/<id> | subscription | Antigravity CLI (subscription only) |
明確的設計邊界(Contract v1)
- 需 Node.js 22 以上版本,各家供應商 SDK 採 optional peerDependencies 按需安裝。
- 每次呼叫回傳單一完整回應;stream: true 與多候選(n > 1)於 v1 契約中明確拒絕。
- 完整 API 參考與更新日誌以 GitHub 儲存庫為唯一準則。
Built with the same deterministic engineering principles behind Ubinity Prompt Vault and Ubinity Ledger .