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

# LangChain

> 了解如何使用 langchain-openai 的 ChatOpenAI 和自定义 base_url，接入 siflow endpoint，并构建 LangChain LLM 应用。

[LangChain](https://github.com/langchain-ai/langchain) 是常用的 LLM 应用开发框架。它的 `langchain-openai` integration 支持自定义 `base_url`，因此 `ChatOpenAI` 可以接入 siflow endpoint。

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

## 配置

在 [API 密钥](https://console.siflow.cn/model-inference/api_keys) 页面创建 API Key，并将其导出为环境变量：

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

使用 `langchain-openai` 中的 `ChatOpenAI` 接入 siflow endpoint：

```python theme={null}
import os
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model="glm-5.2",
    base_url="https://api.siflow.cn/model-api/v1",
    api_key=os.environ["SIFLOW_API_KEY"],
)
```

| 字段         | 说明                                                                                                       |
| ---------- | -------------------------------------------------------------------------------------------------------- |
| `model`    | 模型 ID，例如 `glm-5.2`                                                                                       |
| `base_url` | siflow OpenAI-compatible endpoint：`https://api.siflow.cn/model-api/v1`。LangChain 会追加 `/chat/completions` |
| `api_key`  | 从环境变量读取的 API Key                                                                                         |

请使用 `langchain-openai` 中的 `ChatOpenAI`，不要使用旧的 `OpenAI` completion 类。

如果要使用 LangChain tool/agent 功能，例如 `bind_tools`，请选择原生支持 tool calling 的模型。

## 验证

调用模型。如果返回正常回复，即表示连接成功：

```python theme={null}
print(llm.invoke("In one short sentence, which model are you?").content)
# → I am a GLM large language model developed by Z.ai.
```
