安裝標準 OpenAI SDK
此函式庫會自動處理驗證與請求格式化。它能與我們的無審查 LLM API 配合,無需修改。
- Python:
pip install openai - Node.js:
npm install openai
SDK 版本必須足夠新以支援 base_url 參數。這確保了與我們 OpenAI 相容的 API 結構的相容性。
設定您的基礎 URL 和 API 金鑰
將客戶端配置為指向我們的伺服器。使用基礎 URL https://api.unrestrictedaibot.com/v1 以及您的唯一 API 金鑰。此設定允許您使用與其他服務相同的程式碼。
驗證透過 Authorization 標頭。當您提供金鑰時,SDK 會自動處理此標頭注入。
curl https://api.unrestrictedaibot.com/v1/chat/completions \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "uncensored",
"messages": [{"role": "user", "content": "Find all null values in this JSON response"}]
}'
傳送您的第一個聊天補全
向 /v1/chat/completions 使用模型 ID uncensored. 發送使用者訊息並接收文字回應。此模型不會拒絕合法的成人主題或具爭議性的內容。
設定 max_tokens 用於控制輸出長度的參數。若您省略此欄位,預設值為 2,048 個 token。我們的上下文視窗總共支援高達 100,000 個 token。
from openai import OpenAI
client = OpenAI(base_url="https://api.unrestrictedaibot.com/v1", api_key="YOUR_KEY")
resp = client.chat.completions.create(
model="uncensored",
messages=[{"role": "user", "content": "Summarise this thread without softening it."}],
)
print(resp.choices[0].message.content)
啟用串流回應
使用 Server-Sent Events (SSE) 進行實時 token 交付。設置 stream=true 在你的請求中。SDK 會隨著生成進度產生 token。這能降低長回應的感知延遲。
在程式碼中處理串流區塊。最後一個區塊包含總 token 用量。這對於計費透明度和監控消耗量很有用。
stream = client.chat.completions.create(
model="uncensored",
messages=[{"role": "user", "content": "Tell the story in second person."}],
stream=True,
)
for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)
使用 JSON 模式和函式呼叫
請求透過設定 response_format 以 {"type": "json_object"}. 模型回傳可直接解析的有效 JSON。這非常適合建構代理程式或資料管線。
在請求中定義工具以進行函式呼叫。模型可根據使用者提示呼叫函式。使用 tool_choice 用於控制函數的呼叫時機。這能在無需額外基礎設施的情況下增強應用程式的功能。
import OpenAI from "openai";
const client = new OpenAI({ baseURL: "https://api.unrestrictedaibot.com/v1", apiKey: process.env.API_KEY });
const resp = await client.chat.completions.create({
model: "uncensored",
messages: [{ role: "user", content: "Draft a villain monologue for my game." }],
});
console.log(resp.choices[0].message.content);
檢查您的 Token 使用量與限制
透過 API 回應標頭或最終串流區塊監控您的使用情況。您按使用的 token 付費。錯誤和拒絕是免費的。這種透明的計費模式適用於所有請求。
請遵守速率限制:每支金鑰每分鐘 300 次請求和 8 個併發請求。如果您超過這些限制,將收到 429 錯誤。請相應地規劃併發性。