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

# AutoGen

> 透過 OpenAI 相容客戶端將 TokenLab 模型與 Microsoft AutoGen 搭配使用

## 概覽

AutoGen 可以透過 `OpenAIChatCompletionClient` 使用 TokenLab，這與其用於其他自訂 Chat Completions 端點的路徑相同。

<Note>
  **類型**: 代理框架 (Agent Framework)

  **主要路徑**: OpenAI 相容的聊天完成 (Chat Completions)

  **支援信心**: 支援自訂端點路徑
</Note>

## 安裝

```bash theme={null}
pip install -U autogen-agentchat "autogen-ext[openai]" pyyaml
```

## 環境變數

```bash theme={null}
export TOKENLAB_API_KEY="sk-your-tokenlab-key"
```

## YAML 模型設定

使用 TokenLab 的 `/v1` 端點作為 `base_url`：

```yaml theme={null}
provider: autogen_ext.models.openai.OpenAIChatCompletionClient
config:
  model: claude-sonnet-5
  base_url: https://api.tokenlab.sh/v1
  api_key: ${TOKENLAB_API_KEY}
  model_info:
    function_calling: true
    json_output: true
    vision: false
    family: claude
```

## Python 客戶端

```python theme={null}
import os

from autogen_agentchat.agents import AssistantAgent
from autogen_ext.models.openai import OpenAIChatCompletionClient

model_client = OpenAIChatCompletionClient(
    model="claude-sonnet-5",
    base_url="https://api.tokenlab.sh/v1",
    api_key=os.environ["TOKENLAB_API_KEY"],
    model_info={
        "function_calling": True,
        "json_output": True,
        "vision": False,
        "family": "claude",
    },
)

agent = AssistantAgent("assistant", model_client=model_client)
```

## 端點注意事項

AutoGen 的 OpenAI 擴充功能預期使用 OpenAI 相容的請求格式。若需使用供應商原生功能（例如 Anthropic Messages 或 Gemini `generateContent`），請使用直接支援這些協定的客戶端來呼叫 TokenLab 的原生端點。
