> ## Documentation Index
> Fetch the complete documentation index at: https://docs.siflow.cn/llms.txt
> Use this file to discover all available pages before exploring further.

# 首次调用 API

> 学习如何创建算秩 Model Inference API 密钥，并通过 REST 请求或 OpenAI 兼容 Python SDK 完成首次模型调用。

您可以创建 API 密钥，通过 REST 或 OpenAI 兼容 SDK 发起调用。

## 1. 创建 API 密钥

1. 进入 [API 密钥](https://console.siflow.cn/model-inference/api_keys) 页面，在右上角单击**创建 API 密钥**。
2. 为密钥设置一个便于识别的名称，并复制生成的 API Key。

建议将 API Key 保存在环境变量中使用：

```bash theme={null}
export API_KEY="YOUR_API_KEY"
```

列表中每个密钥都会显示创建时间、最近使用时间和速率限制等信息。关于费用额度与速率限制的更多信息，请参见 [API 密钥](/model-inference/usage/api-keys)。

## 2. 调用模型

### 通过 REST 调用

您可以通过标准 HTTP 请求调用模型。以下是一个流式 Chat Completions 示例：

```bash theme={null}
curl -N https://api.siflow.cn/model-api/chat/completions \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "Qwen/Qwen3-32B",
    "stream": true,
    "messages": [
      {"role": "user", "content": "What new opportunities might reasoning models bring to the market?"}
    ]
  }'
```

以上示例会直接输出原始流式响应。由于该 API 使用 Server-Sent Events（SSE），您会看到多行 `data: {...}` JSON。如果您只想查看拼接后的文本内容，可以配合 `jq` 处理响应：

<Tip>
  该脚本需要在您的机器上安装 `jq`，例如在 macOS 上使用 `brew install jq`，在 Debian/Ubuntu 上使用 `apt-get install jq`。
</Tip>

```bash theme={null}
curl -N https://api.siflow.cn/model-api/chat/completions \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "Qwen/Qwen3-32B",
    "stream": true,
    "messages": [{"role": "user", "content": "What new opportunities might reasoning models bring to the market?"}]
  }' 2>/dev/null | while IFS= read -r line; do
  if [[ "$line" == data:* ]]; then
    json="${line#data: }"
    if [[ "$json" != "[DONE]" ]]; then
      echo -n "$(echo "$json" | jq -r '.choices[0].delta.content // empty')"
    fi
  fi
done
echo
```

如果您希望在单个响应中接收完整结果，请将 `stream` 设置为 `false`。对于其他任务（如文生图），请参阅文档或模型的详情页面，以获取相应的 API 和参数。

### 通过 OpenAI 兼容的 Python SDK 调用

平台兼容官方 OpenAI Python SDK。请先安装 Python 3.7.1+，然后执行：

```bash theme={null}
pip install --upgrade openai
```

示例如下（支持 `reasoning` 字段的流式输出）：

```python theme={null}
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.getenv("API_KEY") or "YOUR_API_KEY",
    base_url="https://api.siflow.cn/model-api"
)

response = client.chat.completions.create(
    model="Qwen/Qwen3-32B",
    messages=[
        {"role": "user", "content": "What new opportunities might reasoning models bring to the market?"}
    ],
    stream=True
)

for chunk in response:
    if not chunk.choices:
        continue
    delta = chunk.choices[0].delta
    if getattr(delta, "content", None):
        print(delta.content, end="", flush=True)
    if getattr(delta, "reasoning", None):
        print(delta.reasoning, end="", flush=True)
```

如果您不需要流式输出，请移除 `stream=True`，改为读取 `response.choices[0].message.content`。请根据业务场景，从 [模型广场](https://console.siflow.cn/model-inference/models) 中选择合适的模型，并合理设置参数。

## 3. 查看用量

在控制台的 [首页](https://console.siflow.cn/model-inference/home)，您可以查看用量数据，包括 token 消耗、消费金额、Top Models、Top API Keys 和成员用量等信息，快速了解主要调用来源；其中成员用量仅管理员可见。如需查看使用详情和指标数据，可参考 [查看使用详情](/model-inference/usage/view-usage) 和 [指标](/model-inference/usage/view-metrics)。
