Quickstarty
Quickstart: AI Gateway
Jedno API zgodne z OpenAI, Twoje klucze dostawców, budżety i cache.
Dodaj dostawcę (BYOK), utwórz klucz hbai_… i użyj dowolnego SDK OpenAI.
Zmienne w przykładach: $HOSTBASE_TOKEN (token dostępu), $REF (ref projektu). SDK: @hostbase/management (TypeScript) i hostbase_management (Python), pełne API: /docs/api.
1. Dodaj dostawcę i klucz
Klucz dostawcy jest szyfrowany i nigdy nie wraca w API.
curl -X PUT https://host.ulewicz.space/api/v1/projects/$REF/ai-gateway/providers -H "Authorization: Bearer $HOSTBASE_TOKEN" -H "Content-Type: application/json" -d '{"provider":"openai","api_key":"sk-..."}'
curl -X POST https://host.ulewicz.space/api/v1/projects/$REF/ai-gateway/keys -H "Authorization: Bearer $HOSTBASE_TOKEN" -H "Content-Type: application/json" -d '{"name":"backend"}'const key = await hb.createAiKey({ ref, body: { name: "backend" } as never });key = hb.create_ai_key(ref=ref, body={"name": "backend"})2. Wywołaj model
Zmień tylko base URL.
curl https://host.ulewicz.space/api/ai/v1/chat/completions -H "Authorization: Bearer $HBAI_KEY" -H "Content-Type: application/json" \
-d '{"model":"gpt-4o-mini","messages":[{"role":"user","content":"Hello"}]}'import OpenAI from "openai";
const ai = new OpenAI({ baseURL: "https://host.ulewicz.space/api/ai/v1", apiKey: process.env.HBAI_KEY });
const r = await ai.chat.completions.create({ model: "gpt-4o-mini", messages: [{ role: "user", content: "Hello" }] });from openai import OpenAI
ai = OpenAI(base_url="https://host.ulewicz.space/api/ai/v1", api_key=os.environ["HBAI_KEY"])
ai.chat.completions.create(model="gpt-4o-mini", messages=[{"role": "user", "content": "Hello"}])3. Zużycie
Żądania, tokeny, koszt i cache hit.
curl "https://host.ulewicz.space/api/v1/projects/$REF/ai-gateway/usage?range=7d" -H "Authorization: Bearer $HOSTBASE_TOKEN"const usage = await hb.getAiUsage({ ref, query: { range: "7d" } });usage = hb.get_ai_usage(ref=ref, query={"range": "7d"})Dalej: /docs/ai-gateway.