跳转到内容

Models

快速开始:你的第一次 API 调用

读完这一页,你会拥有一个 Grafilab API 密钥、一个模型 id、终端里的一次 JSON 补全、 同一次调用的流式版本,以及用量面板里那条显示花费的记录。

  • 一个在资料中设置了所在国家/地区的 Grafilab 账号 —— 这决定了账号的计费货币。参 见创建账号。
  • 非零的额度余额,或者你选的模型上有免费额度。参见 额度与账单。
  • curl,或 Python 3.9+,或 Node.js 18+。
  1. 创建一个 API 密钥。

    在控制台打开 API,点击 Generate New Key。输入一个名字 —— quickstart 就行 —— 然后按 Enter。 API Key Created 对话框只会显示一次完整密钥:复制它,然后点击 I’ve saved my key。之后表格里只显示密钥的后四位,需要再看时可以用 Show Key 切换。

    把密钥放进环境变量,这样它就不会落进你的代码里:

    终端窗口
    export GRAFILAB_API_KEY="sk-grafilab-..."
  2. 选一个模型。

    The model catalog is live, so list it rather than copying ids from a page. Every entry’s id is the exact string to use in any tool or SDK.

    Terminal window
    curl https://llm.grafilab.ai/v1/models \
    -H "Authorization: Bearer $GRAFILAB_API_KEY" | jq -r '.data[].id'

    The same ids appear as cards in the Playground, with context size and prices.

  3. 发一次 chat completion。

    base URL 是 https://llm.grafilab.ai/v1。任何 OpenAI 兼容的客户端都能用 —— 你只需要改 base URL 和密钥。

    The examples on this page use grafilab/qwen3.6-flash, the sample model from the API reference. Replace it with any id returned by GET /v1/models.

    终端窗口
    curl https://llm.grafilab.ai/v1/chat/completions \
    -H "Authorization: Bearer $GRAFILAB_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
    "model": "grafilab/qwen3.6-flash",
    "messages": [{"role": "user", "content": "Say hello in five words."}]
    }'

    你会拿回一个标准的 chat completion,这里做了省略:

    {
    "id": "chatcmpl-...",
    "object": "chat.completion",
    "model": "grafilab/qwen3.6-flash",
    "choices": [
    {
    "index": 0,
    "message": { "role": "assistant", "content": "Hello, good to meet you today." },
    "finish_reason": "stop"
    }
    ],
    "usage": { "prompt_tokens": 13, "completion_tokens": 8, "total_tokens": 21 }
    }
  4. 把同一次调用改成流式。

    设 stream: true,模型一边写你一边收到 server-sent events。再加上 stream_options: {"include_usage": true},结束标记前的最后一帧数据就会带上 token 计数 —— 没有它,流式调用不会向客户端报告任何用量。

    终端窗口
    curl -N https://llm.grafilab.ai/v1/chat/completions \
    -H "Authorization: Bearer $GRAFILAB_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
    "model": "grafilab/qwen3.6-flash",
    "messages": [{"role": "user", "content": "Say hello in five words."}],
    "stream": true,
    "stream_options": {"include_usage": true}
    }'

    原始 SSE 流以 data: [DONE] 结束。

  5. 看看它花了多少钱。

    回到 API,找到这个密钥所在的行,点击 View Usage。控制台会打开筛选到该 密钥的 Inference API Usage,因为每个请求 都归属到为它鉴权的那个密钥。看你发起调用那天的 Total cost 和 Token breakdown 卡片 —— Input、Cached、Output。

    那个页面下方的脚注写着 All dates in UTC · data may lag up to 30 min:喂给这 些数字的汇总任务每 30 分钟重算一次今天和昨天,所以你刚刚发出的调用可能还没出 现。

下面三条都成立时,你就完成了:

  • 非流式调用返回了非空的 choices[0].message.content。
  • 流式调用逐段打印出文本,并以一个 usage 对象收尾。
  • 大约 30 分钟内,该密钥的 Inference API Usage 显示今天有非零的 Total cost 和一份 token 明细。

文本调用在交付之后按次计费,所以还没产出内容就失败的调用不收费。

  • 401 invalid_api_key —— 密钥不对、已经轮换过,或者粘贴时带上了空格或换行。 到 API 重新复制一次;如果你已经没有那个值了,就轮换密钥并使用新值。
  • 404 model_not_found —— id 拼错了、模型仅限邀请,或者它在你的账号货币下没 有定价。重新跑一次 GET /v1/models,从响应里原样复制一个 id。
  • 429 insufficient_quota —— 这是余额问题,不是吞吐限制。到 Billing 充 值。
  • 403 billing_currency_unset —— 你的账号还没有计费货币。在资料里设置所在国 家/地区,然后重试。
  • 连接被拒绝,或 URL 本身返回 404 —— 检查 base URL。OpenAI 接口是 https://llm.grafilab.ai/v1,客户端会自己追加 /chat/completions;Anthropic 客户端用裸的源站 https://llm.grafilab.ai,并自己追加 /v1/messages。