前置條件:安裝 OpenAI SDK
首先為您的語言安裝官方的 OpenAI SDK。此函式庫會處理驗證、重試與 JSON 解析,讓您無需從頭撰寫 HTTP 客戶端。我們的 API 使用與原始服務相同的標準介面,意味著您現有的程式碼邏輯保持不變。
您只需要有效的 Python 或 Node.js 環境。SDK 為您處理繁重的任務。
- Python:
pip install openai - Node.js:
npm install openai
配置您的基礎 URL 和 API 金鑰
將您的客戶端指向我們的專用端點。將預設基礎 URL 替換為我們的特定網域,以將流量路由至我們的無審查模型。您還必須提供來自儀表板的 API 金鑰。
此步驟將您的請求與其他供應商隔離。請確保使用下方提供的確切基礎 URL。
from openai import OpenAI
client = OpenAI(base_url="https://api.unrestrictedaigpt.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)import OpenAI from "openai";
const client = new OpenAI({ baseURL: "https://api.unrestrictedaigpt.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);
進行您的第一次 POST /v1/chat/completions 請求
發送標準的聊天完成請求。請使用模型識別碼 uncensored 以確保您獲得正確的模型。API 接受標準參數,例如 temperature 和 max_tokens.
curl https://api.unrestrictedaigpt.com/v1/chat/completions \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "uncensored",
"messages": [{"role": "user", "content": "Run python script to sort list numerically"}]
}'回應將包含生成的文字。如果您需要處理錯誤,請檢查狀態碼。
透過 SSE 處理串流回應
在請求主體中將 stream: true. 這返回一個 Server-Sent Events 串流。您處理每個到達的區塊,這可降低長輸出的感知延遲。
最後一個區塊包含 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 模式
使用 tools 用於啟用函數調用。定義您的函數結構描述,讓模型自行決定何時調用它。您也可以使用 response_format 用於嚴格的資料結構。
兩項功能運作方式與標準 OpenAI API 完全一致。
限制、錯誤與上下文視窗
我們的 API 實施了嚴格的限制以確保穩定性。您擁有 100,000 個 token 的上下文窗口。每次請求的最大輸出為 16,000 個 token。您每分鐘的限制為 300 次請求。401 錯誤表示您的金鑰無效。402 錯誤表示您沒有餘額。429 錯誤表示您達到了速率限制。錯誤和拒絕不會消耗餘額。