Node.js 无审查 ChatGPT API:设置步骤
官方 openai 包可直接使用。将其指向新的 base URL,使用模型 ID uncensored,即可拥有支持流式输出、工具调用和 JSON 模式的 Node.js 客户端。
更新于

开始前的准备工作
要进行 无审查 chatgpt api node js 设置,你需要三样东西:Node 18 或更高版本、API 密钥和官方 openai 包。无需安装其他内容。该服务使用 OpenAI 请求格式,因此只要你将 base URL 更改为 https://api.unfilteredchatgpt.top/v1 并将模型更改为 uncensored,用于任何 OpenAI 风格接口的相同 SDK 即可工作。
请明确其定位。它不是 ChatGPT,也不是来自 OpenAI 或其他任何大型厂商的官方 API。它是由独立提供商运行的开放权重模型,经过调优,可在合法成人用途中不拒绝内容。它不会生成涉及未成年人的性内容。
创建一个账户(使用 Google 或邮箱和密码),然后打开 API 密钥页面。那里提供一键 $0.50 试用,有效期 7 天,无需绑卡。将密钥导出为环境变量,使其不留在你的源代码中:
# https://api.unfilteredchatgpt.top/v1 is the base URL, uncensored is the model id
export API_KEY="paste-your-key-here"安装 SDK 并检查密钥
创建一个文件夹,安装包并向 /v1/models 发起一次请求。如果密钥正确,你将获得一个包含 uncensored 模型的 JSON 列表。
# project setup, then call the API once to confirm the key works
npm init -y
npm install openai
curl https://api.unfilteredchatgpt.top/v1/models -H "Authorization: Bearer $API_KEY" # model id: uncensored
使用 ES 模块以便顶层 await 生效。要么将文件命名为 index.mjs,要么在 package.json 中添加 "type": "module"。如果你偏好 CommonJS,请将代码包装在 async 函数中并使用 require("openai")。
你的第一次聊天补全
这是完整的最小化示例。与标准 OpenAI 示例的唯一区别是设置 baseURL 和 model 的两行代码。
// index.mjs (Node 18 or newer)
import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://api.unfilteredchatgpt.top/v1",
apiKey: process.env.API_KEY,
});
const res = await client.chat.completions.create({
model: "uncensored",
messages: [
{ role: "system", content: "You are a blunt, witty co-writer for adult fiction." },
{ role: "user", content: "Describe a rainy border town in four sentences." },
],
temperature: 0.9,
});
console.log(res.choices[0].message.content);
console.log(res.usage);
使用 node index.mjs 运行它。你会得到回复文本以及包含提示词和完成 token 计数的使用对象。系统消息用于设置角色和语气,该模型会紧密遵循它,因此请具体说明你想要的内容。temperature、top_p、stop、seed 和 penalty 参数会直接传递给模型。
上下文窗口为 100,000 个 token,包含提示词和完成部分。max_tokens 是可选的。如果省略它,完成部分可以使用窗口中剩余的任何空间,这对长场景很有用,但如果循环出错则成本高昂,因此在测试代码中设置它。
流式输出到达的 token
对于聊天 UI 或 CLI,你希望在生成时文本就能显示。设置 stream: true 并遍历 chunks。usage 出现在流末尾。
import OpenAI from "openai";
const client = new OpenAI({ baseURL: "https://api.unfilteredchatgpt.top/v1", apiKey: process.env.API_KEY });
const stream = await client.chat.completions.create({
model: "uncensored",
stream: true,
stream_options: { include_usage: true },
messages: [{ role: "user", content: "Tell me a short story about a lighthouse keeper." }],
});
for await (const chunk of stream) {
const delta = chunk.choices[0]?.delta?.content;
if (delta) process.stdout.write(delta);
if (chunk.usage) console.log("\n", chunk.usage);
}
生产代码中有一个细节很重要。流式请求会占用其并发槽位直到流结束。完整密钥允许 8 个并发请求,试用密钥允许 2 个,因此如果你的用户放弃流式输出而不关闭它们,请在客户端中止请求(SDK 支持 AbortController 信号),否则你会比预期更早遇到 concurrency 错误。
工具调用和 JSON 模式
两者都通过标准字段工作。对于工具,传递 tools 和可选的 tool_choice。模型返回 tool_calls,你执行函数,然后使用 role: "tool" 将结果作为消息发送。流式响应也包含 tool_calls。
import OpenAI from "openai";
const client = new OpenAI({ baseURL: "https://api.unfilteredchatgpt.top/v1", apiKey: process.env.API_KEY });
const tools = [{
type: "function",
function: {
name: "roll_dice",
description: "Roll a die with the given number of sides",
parameters: {
type: "object",
properties: { sides: { type: "integer" } },
required: ["sides"],
},
},
}];
const messages = [{ role: "user", content: "Roll a d20 for my attack." }];
const first = await client.chat.completions.create({ model: "uncensored", messages, tools, tool_choice: "auto" });
const msg = first.choices[0].message;
if (msg.tool_calls) {
messages.push(msg);
for (const call of msg.tool_calls) {
const { sides } = JSON.parse(call.function.arguments);
const result = 1 + Math.floor(Math.random() * sides);
messages.push({ role: "tool", tool_call_id: call.id, content: String(result) });
}
const final = await client.chat.completions.create({ model: "uncensored", messages, tools });
console.log(final.choices[0].message.content);
}
对于结构化输出,JSON 模式只需额外一个字段。在提示词中提到 JSON 并描述你想要的结构,因为该标志保证语法而非你的 schema。
import OpenAI from "openai";
const client = new OpenAI({ baseURL: "https://api.unfilteredchatgpt.top/v1", apiKey: process.env.API_KEY });
const res = await client.chat.completions.create({
model: "uncensored",
response_format: { type: "json_object" },
messages: [
{ role: "system", content: "Reply with JSON only: {\"name\": string, \"traits\": string[]}" },
{ role: "user", content: "Invent a cynical ship captain." },
],
});
const character = JSON.parse(res.choices[0].message.content);
console.log(character.name, character.traits);
在实际代码中,始终将 JSON.parse 包裹在 try/catch 中。如果模型在 max_tokens 处截断,JSON 将不完整。
处理错误和速率限制
完整密钥允许 8 个并发请求和每分钟 300 个请求。试用密钥允许 2 个和 60 个。超过限制时,你会收到 HTTP 429 错误,错误代码为 rate_limited(每分钟过多)或 concurrency(同时过多)。临时过载也返回 429,带有 Retry-After: 5 头。所有情况下的修复方法相同:等待并重试,并限制重试次数。
import OpenAI from "openai";
const client = new OpenAI({ baseURL: "https://api.unfilteredchatgpt.top/v1", apiKey: process.env.API_KEY, maxRetries: 0 });
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
async function chat(messages, attempt = 0) {
try {
return await client.chat.completions.create({ model: "uncensored", messages });
} catch (err) {
if (err.status === 429 && attempt < 5) {
const wait = Number(err.headers?.["retry-after"] ?? 5) * 1000;
await sleep(wait + attempt * 500);
return chat(messages, attempt + 1);
}
throw err;
}
}
上述重试会关闭 SDK 的内置重试,以便你控制时机。对于批处理作业,在前面放置一个小队列,例如 p-limit,限制为 6,这在 8 个请求上限下留有缓冲。最大请求体为 8 MB,对于任何文本提示来说都绰绰有余,但粘贴到消息中的 base64 块可能会达到此限制。
失败或被拒绝的请求不会收费,因此错误重试不会额外产生费用。
保留对话历史
API 是无状态的。每个请求必须携带模型应看到的消息,因此聊天机器人需要一个在每轮对话中增长的数组。保持 system 消息在最前面,并在历史记录变长时从最旧的 user 和 assistant 消息开始修剪。
import OpenAI from "openai";
import readline from "node:readline/promises";
const client = new OpenAI({ baseURL: "https://api.unfilteredchatgpt.top/v1", apiKey: process.env.API_KEY });
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
const history = [{ role: "system", content: "You are a dry-humored narrator for an adult text adventure." }];
while (true) {
const text = await rl.question("> ");
if (text.length === 0) break;
history.push({ role: "user", content: text });
const res = await client.chat.completions.create({ model: "uncensored", messages: history, max_tokens: 600 });
const reply = res.choices[0].message.content;
history.push({ role: "assistant", content: reply });
console.log(reply);
while (history.length > 21) history.splice(1, 2); // keep system + last 20
}
rl.close();
因为输入 token 在每次调用时都会计费,40 条消息的历史记录成本远高于 4 条。上下文窗口为 100,000 个 token,所以你很少会触及限制,但你会先注意到账单。长角色扮演会话受益于用简短的运行摘要替换最旧的轮次。
常见问题及解决方法
- 401 或认证错误。 密钥缺失或有拼写错误。检查
process.env.API_KEY是否在运行 Node 的 shell 中设置,因为新的终端标签页不会继承 exports。 - 每次调用都返回 404。 base URL 缺少
/v1,或者路径重复。使用上面显示的确切地址,让 SDK 添加/chat/completions。 - 未找到模型。 模型 ID 为
uncensored,全小写。调用GET /v1/models查看密钥可以使用哪些模型。 - JSON 模式下的 JSON 输出被截断。 输出在
max_tokens处被截断。提高限制或缩短 schema。 - 导致应用挂起的流。 始终消费或中止流。打开的流会占用其并发槽位。
- 成本高于预期。 在每个响应中记录
usage并首先查看输入 token。每轮重新发送历史记录是常见原因。
如果仍有问题,请将响应中的 X-Request-Id 头值发送给支持团队。支持页面 有联系方式。
无 SDK:使用原生 fetch
如果你想要零依赖,Node 18 内置了 fetch。响应头包括用于支持问题的 X-Request-Id 和用于查看剩余额度的 X-Balance-USD。
const res = await fetch("https://api.unfilteredchatgpt.top/v1/chat/completions", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "uncensored",
messages: [{ role: "user", content: "Say hello in one line." }],
}),
});
console.log(res.headers.get("x-request-id"), res.headers.get("x-balance-usd"));
console.log((await res.json()).choices[0].message.content);
其他兼容 OpenAI 的工具以相同方式工作。在任何允许设置自定义端点的客户端中(如 SillyTavern 或 LangChain 的 OpenAI 包装器),输入基础 URL,粘贴你的密钥,并将 uncensored 输入为模型名称。文档页面 有简短版本,聊天机器人 API 文章 展示了如何将这些调用转换为完整的机器人。
发货前的成本检查:输入为每 1M token $0.15,输出为每 1M token $0.50。5,000 输入 token 和 1,500 输出 token 的回复成本为 $0.00075 + $0.00075 = $0.0015,因此 $20 信用额可覆盖约 13,333 次此类请求。定价页面 有充值层级。
更多指南
问答
我可以使用官方的 openai npm 包吗?
可以。将 baseURL 设置为 API 地址并使用模型 ID uncensored。无需其他更改。
我需要哪个 Node 版本?
Node 18 或更高版本,提供内置 fetch 并与当前 openai 包兼容。使用 ES 模块以支持顶层 await。
为什么我会收到 429 错误?
你触发了限制。rate_limited 表示每分钟请求过多,concurrency 表示同时请求过多。等待并重试,并限制并发请求数。
它支持流式输出和函数调用吗?
可以。流式输出通过 SSE 运行,usage 在末尾返回,工具调用遵循 OpenAI 格式,包括流式 tool_calls。
只差一张表单,即可获得密钥
创建账户,复制密钥,更改基础 URL。这就是全部设置。