مستندات / شروع سریع
شروع سریع
پُل یک درگاه سازگار با OpenAI است. کدی که امروز با SDK رسمی OpenAI نوشتهاید، فقط با عوض کردن آدرس پایه و کلید API روی پُل کار میکند.
۱. ساخت اپ و کلید
- در کنسول وارد شوید و از بخش «اپها و کلیدها» یک اپ بسازید؛ مثلاً
chatbot-prod. - روی «کلید جدید» بزنید و دسترسی
inferenceرا انتخاب کنید. - کلیدی که با
pul_live_یاpul_test_شروع میشود فقط یک بار نمایش داده میشود. آن را در مدیر اسرار پروژهتان ذخیره کنید.
۲. اولین درخواست
curl https://api.pul.example/v1/chat/completions \
-H "Authorization: Bearer $PUL_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model": "gpt-4o-mini", "messages": [{"role": "user", "content": "سلام!"}]}'Python
from openai import OpenAI
client = OpenAI(api_key=os.environ["PUL_API_KEY"], base_url="https://api.pul.example/v1")
r = client.chat.completions.create(model="gpt-4o-mini", messages=[{"role": "user", "content": "سلام!"}])
print(r.choices[0].message.content)Node.js
import OpenAI from "openai";
const client = new OpenAI({ apiKey: process.env.PUL_API_KEY, baseURL: "https://api.pul.example/v1" });
const r = await client.chat.completions.create({ model: "gpt-4o-mini", messages: [{ role: "user", content: "سلام!" }] });
console.log(r.choices[0].message.content);نمونههای کامل در پوشهی examples/ مخزن هستند و با SDKهای رسمی OpenAI آزمایش شدهاند: examples/python/chat.py، examples/node/chat.mjs و examples/curl/chat.sh.
۳. استریم
"stream": true بفرستید. پاسخ به صورت Server-Sent Events میآید و با data: [DONE] تمام میشود. اگر "stream_options": {"include_usage": true} هم بفرستید، پیش از [DONE] یک تکهی جدا با مقدار مصرف توکن دریافت میکنید.
۴. هدرهای پاسخ
| هدر | معنا |
|---|---|
X-Request-ID |
شناسهی یکتای درخواست. هنگام تماس با پشتیبانی آن را بفرستید. |
X-Pol-Provider |
تامینکنندهای که پاسخ داده است |
X-Pol-Model |
مدلی که پاسخ داده است (نام مستعار به مدل اصلی تبدیل میشود) |
X-Pol-Route-Reason |
دلیل انتخاب مسیر: priority، weighted، canary، pinned یا failover |
X-Pol-Attempts |
تعداد تلاشها؛ اگر failover رخ داده باشد بیشتر از ۱ است |
X-RateLimit-Limit-Requests / X-RateLimit-Remaining-Requests |
سقف و مقدار باقیماندهی درخواست در دقیقه |
برای اینکه درخواست حتماً به یک تامینکنندهی مشخص برود، هدر X-Pol-Provider: <نام تامینکننده> را بفرستید.
نمونههای کامل SDK
این فایلها عیناً از پوشهی examples/ مخزن خوانده میشوند و با SDKهای رسمی OpenAI روی درگاه پُل اجرا شدهاند. متغیرهای PUL_API_KEY، PUL_BASE_URL و PUL_MODEL را پیش از اجرا تنظیم کنید.
فایل: examples/python/chat.py
"""Pūl AI with the official OpenAI Python SDK: only base_url and api_key change.
pip install openai
PUL_API_KEY=pul_test_… PUL_BASE_URL=http://localhost:47180/v1 PUL_MODEL=<model> python chat.py
"""
import os
from openai import OpenAI, APIStatusError
client = OpenAI(api_key=os.environ["PUL_API_KEY"], base_url=os.getenv("PUL_BASE_URL", "http://localhost:47180/v1"))
model = os.getenv("PUL_MODEL", "gpt-4o-mini")
print("models:", [m.id for m in client.models.list().data])
raw = client.chat.completions.with_raw_response.create(
model=model, messages=[{"role": "user", "content": "سلام!"}], max_tokens=64
)
completion = raw.parse()
print("reply:", completion.choices[0].message.content)
print("usage:", completion.usage)
print("provider:", raw.headers.get("x-pol-provider"), "request:", raw.headers.get("x-request-id"))
text = ""
for chunk in client.chat.completions.create(
model=model, stream=True, stream_options={"include_usage": True},
messages=[{"role": "user", "content": "Count to five."}],
):
if chunk.choices:
text += chunk.choices[0].delta.content or ""
if chunk.usage:
print("stream usage:", chunk.usage)
print("streamed:", text)
try:
client.chat.completions.create(model="no-such-model", messages=[{"role": "user", "content": "x"}])
except APIStatusError as e:
print("error:", e.status_code, e.body)فایل: examples/node/chat.mjs
// Pūl AI with the official OpenAI Node SDK: only baseURL and apiKey change.
// PUL_API_KEY=pul_test_… PUL_BASE_URL=http://localhost:47180/v1 PUL_MODEL=<model> node chat.mjs
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.PUL_API_KEY,
baseURL: process.env.PUL_BASE_URL ?? "http://localhost:47180/v1",
});
const model = process.env.PUL_MODEL ?? "gpt-4o-mini";
// 1) list models
const models = await client.models.list();
console.log("models:", models.data.map((m) => m.id).join(", "));
// 2) non-streaming completion (+ Pūl response headers)
const { data, response } = await client.chat.completions
.create({ model, messages: [{ role: "user", content: "سلام! یک جمله درباره پُل بگو." }], max_tokens: 64 })
.withResponse();
console.log("reply:", data.choices[0].message.content);
console.log("usage:", data.usage);
console.log("provider:", response.headers.get("x-pol-provider"), "request:", response.headers.get("x-request-id"));
// 3) streaming with usage
const stream = await client.chat.completions.create({
model, stream: true, stream_options: { include_usage: true },
messages: [{ role: "user", content: "Count to five." }],
});
let text = "";
for await (const chunk of stream) {
text += chunk.choices[0]?.delta?.content ?? "";
if (chunk.usage) console.log("stream usage:", chunk.usage);
}
console.log("streamed:", text);
// 4) errors keep the OpenAI shape
try {
await client.chat.completions.create({ model: "no-such-model", messages: [{ role: "user", content: "x" }] });
} catch (e) {
console.log("error:", e.status, e.code, e.type);
}فایل: examples/curl/chat.sh
#!/usr/bin/env bash
# PUL_API_KEY=pul_test_… PUL_MODEL=<model> ./chat.sh
set -euo pipefail
BASE=${PUL_BASE_URL:-http://localhost:47180/v1}
curl -sS "$BASE/models" -H "Authorization: Bearer $PUL_API_KEY"
echo
curl -sS -i "$BASE/chat/completions" -H "Authorization: Bearer $PUL_API_KEY" -H 'Content-Type: application/json' \
-d "{\"model\":\"$PUL_MODEL\",\"messages\":[{\"role\":\"user\",\"content\":\"سلام\"}]}"
echo
curl -sS -N "$BASE/chat/completions" -H "Authorization: Bearer $PUL_API_KEY" -H 'Content-Type: application/json' \
-d "{\"model\":\"$PUL_MODEL\",\"stream\":true,\"stream_options\":{\"include_usage\":true},\"messages\":[{\"role\":\"user\",\"content\":\"hi\"}]}"