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

# AutoGen

> 了解如何在 Microsoft AutoGen 中配置 OpenAIChatCompletionClient 和自定义 base_url，接入 siflow endpoint，并构建多智能体应用。

[AutoGen](https://github.com/microsoft/autogen) 是 Microsoft 用于构建多智能体 AI 应用的框架。它的 `OpenAIChatCompletionClient` 支持自定义 `base_url`，因此可以让智能体接入 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>"
```

安装 `autogen-agentchat` 和 `autogen-ext[openai]`，然后创建一个接入 siflow endpoint 的客户端。使用 `autogen-ext[openai]` 提供的 `OpenAIChatCompletionClient`，并将该客户端传给 `AssistantAgent`，即可运行多智能体团队。

```python theme={null}
import os
from autogen_ext.models.openai import OpenAIChatCompletionClient
from autogen_core.models import ModelInfo

client = OpenAIChatCompletionClient(
    model="glm-5.2",
    base_url="https://api.siflow.cn/model-api/v1",
    api_key=os.environ["SIFLOW_API_KEY"],
    model_info=ModelInfo(
        vision=False, function_calling=True, json_output=True,
        family="unknown", structured_output=True,
    ),
)
```

| 字段           | 说明                                                                     |
| ------------ | ---------------------------------------------------------------------- |
| `model`      | 模型 ID，例如 `glm-5.2`                                                     |
| `base_url`   | siflow OpenAI-compatible endpoint：`https://api.siflow.cn/model-api/v1` |
| `api_key`    | 从环境变量读取的 API Key                                                       |
| `model_info` | 自定义模型必填。用于声明模型能力，例如 `function_calling`                                 |

自定义模型需要提供 `model_info`。如果没有提供，客户端会抛出 `model_info is required`。对于 `glm-5.2` 这类支持 tool calling 的模型，应设置 `function_calling=True`。

## 验证

发送一条消息。如果返回正常回复，即表示连接成功：

```python theme={null}
import asyncio
from autogen_core.models import UserMessage

async def main():
    r = await client.create(
        [UserMessage(content="In one short sentence, which model are you?", source="user")]
    )
    print(r.content)

asyncio.run(main())
# → I'm GLM, a large language model developed by Z.ai.
```
