基礎 URL 與驗證
API 接受來自 https://api.qwenapipricing.com/v1. 每次呼叫都需要在 Authorization 標頭. 您在註冊頁面產生此金鑰,它用於識別您的預付信用帳戶。
- 使用標準的 HTTP 標頭進行身份驗證。
- 金鑰在您的積分過期或撤銷之前保持有效。
首次請求
發送聊天完成請求以測試連線。這個簡單的呼叫可驗證您的金鑰是否有效,並返回基本回應。
您可以使用此範例來驗證您的金鑰是否有效。
curl https://api.qwenapipricing.com/v1/chat/completions \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "uncensored",
"messages": [{"role": "user", "content": "list the costs for the qwen2.5 models"}]
}'
Python 集成
使用官方 OpenAI Python SDK 與我們的服務互動。將客戶端指向我們的基礎 URL 並提供您的金鑰。
此設定適用於任何期望 OpenAI 端點的標準 Python 程式庫。
from openai import OpenAI
client = OpenAI(base_url="https://api.qwenapipricing.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)
Node.js 集成
Node 開發人員可以使用 openai npm 套件。設定客戶端以我們的 base URL 發送請求。
此方法與現有的 OpenAI 程式碼庫保持相容。
import OpenAI from "openai";
const client = new OpenAI({ baseURL: "https://api.qwenapipricing.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);
串流回應
在請求主體中將 stream: true 在您的請求中。API 透過伺服器發送事件 (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)
速率限制與錯誤
每個金鑰允許每分鐘 300 次請求和 8 個並發連線。無效金鑰會收到 401 錯誤,餘額耗盡時收到 402 錯誤,達到速率限制時收到 429 錯誤。上下文視窗總共支援 100,000 個 token。