安裝您的 SDK
開始安裝您語言的官方程式庫。我們支援大多數開發者使用的標準 OpenAI SDK 套件。透過終端機安裝套件,即可準備進行下一步。
配置 base URL 和金鑰
將您的客戶端指向我們的端點並設定您的憑證。使用 https://api.openrouterlimits.com/v1 作為您的基礎 URL。取代 YOUR_API_KEY 使用您在「獲取 API 金鑰」頁面生成的金鑰。透過更改 base_url 和 API 金鑰,此設定適用於任何 OpenAI-compatible 客戶端。
傳送您的第一個補全
生成文本的請求。使用模型 ID uncensored 存取我們調整過的開源權重模型。該模型不會拒絕合法的成人或爭議性主題。發送 POST 請求至 /v1/chat/completions 搭配您的訊息。
curl https://api.openrouterlimits.com/v1/chat/completions \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "uncensored",
"messages": [{"role": "user", "content": "Process 5000 records in under 2 seconds"}]
}'
處理串流回應
即時串流接收權杖。設定 stream 以 true 在您的請求主體中。您將收到包含部分文字的伺服器發送事件(SSE)。最後一個區塊包含用於計費的 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 模式
整合函式呼叫或嚴格的 JSON 輸出。使用 tools 參數用於函數呼叫時,模型可輸出結構化的 JSON。設定 response_format 以 {"type": "json_object"} for JSON mode。支援的參數包括 temperature, top_p, stop, seed, presence_penalty,以及 frequency_penalty.
import OpenAI from "openai";
const client = new OpenAI({ baseURL: "https://api.openrouterlimits.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 使用情況
追蹤您的使用量並優雅地處理錯誤。您每支金鑰每分鐘可獲得 300 個請求和 8 個並行請求。上下文視窗為 100,000 個 token。處理錯誤,例如 401(無效金鑰)、402(無額度)和 429(速率限制)。錯誤和拒絕免費。額度永不過期。
from openai import OpenAI
client = OpenAI(base_url="https://api.openrouterlimits.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)