> ## 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.

# LiteLLM

> 了解如何使用 LiteLLM SDK 或 proxy server 的 OpenAI-compatible provider，通过自定义 api_base 接入 siflow endpoint。

[LiteLLM](https://github.com/BerriAI/litellm) 是一个 Python SDK 和 proxy server，可通过统一的 OpenAI-style interface 调用多种 LLM API。它的 OpenAI-compatible provider（`openai/<model>` + 自定义 `api_base`）可以接入 siflow endpoint。

本文默认模型为 `glm-5.2`；完整模型列表和价格请查看 [模型广场](https://console.siflow.cn/model-inference/models)。

## 配置

在 [API 密钥](https://console.siflow.cn/model-inference/api_keys) 页面创建 API Key，并将其导出为环境变量。请不要把 key 写入源码：

```bash theme={null}
export SIFLOW_API_KEY="<Your API Key>"
```

### SDK

SDK 调用时，在模型名前加 `openai/` 前缀，并设置 `api_base`：

```python theme={null}
import os
from litellm import completion

response = completion(
    model="openai/glm-5.2",
    api_base="https://api.siflow.cn/model-api/v1",
    api_key=os.environ["SIFLOW_API_KEY"],
    messages=[{"role": "user", "content": "In one short sentence, which model are you?"}],
)
print(response.choices[0].message.content)
```

### Proxy

LiteLLM proxy server 也使用同样的路由方式，在 `config.yaml` 中配置：

```yaml theme={null}
model_list:
  - model_name: glm-5.2
    litellm_params:
      model: openai/glm-5.2
      api_base: https://api.siflow.cn/model-api/v1
      api_key: os.environ/SIFLOW_API_KEY
```

| 字段         | 说明                                                                                                     |
| ---------- | ------------------------------------------------------------------------------------------------------ |
| `model`    | `openai/<id>`。`openai/` 前缀会选择 OpenAI-compatible 路由；使用模型 ID，例如 `openai/glm-5.2`                         |
| `api_base` | siflow OpenAI-compatible endpoint：`https://api.siflow.cn/model-api/v1`。LiteLLM 会追加 `/chat/completions` |
| `api_key`  | 创建的 API Key。SDK 调用中通过 `api_key=` 传入；proxy 配置中使用 `os.environ/SIFLOW_API_KEY`                            |

自定义 OpenAI-compatible endpoint 需要使用 `openai/` 前缀。否则 LiteLLM 会尝试从裸模型 ID 推断 provider，并且不会路由到 `api_base`。

如果要使用 LiteLLM 的 function-calling 功能，请选择原生支持 tool calling 的模型。

## 验证

导出 `SIFLOW_API_KEY` 后运行上面的 SDK 示例。如果返回正常回复，即表示连接成功：

```text theme={null}
$ python siflow_litellm.py
I am GLM, a large language model developed by Z.ai.
```

对于 proxy，运行：

```bash theme={null}
litellm --config config.yaml
```

然后使用 `model: glm-5.2` 调用 `http://localhost:4000/v1/chat/completions`。

LiteLLM 内置的模型数据库没有列出模型 ID，因此可能提示未知 context size 或 price。可以显式传入 `max_tokens`，或通过 `litellm.register_model({...})` 注册模型。这不会影响对话响应。
