框架與平台
AutoGen
透過 OpenAI 相容客戶端將 TokenLab 模型與 Microsoft AutoGen 搭配使用
概覽
AutoGen 可以透過 OpenAIChatCompletionClient 使用 TokenLab,這與其用於其他自訂 Chat Completions 端點的路徑相同。
類型: 代理框架 (Agent Framework)
主要路徑: OpenAI 相容的聊天完成 (Chat Completions)
支援信心: 支援自訂端點路徑
安裝
pip install -U autogen-agentchat "autogen-ext[openai]" pyyaml環境變數
export TOKENLAB_API_KEY="sk-your-tokenlab-key"YAML 模型設定
使用 TokenLab 的 /v1 端點作為 base_url:
將以下設定儲存為 tokenlab-model.yaml:
provider: autogen_ext.models.openai.OpenAIChatCompletionClient
config:
model: claude-sonnet-5
base_url: https://api.tokenlab.sh/v1
model_info:
function_calling: true
json_output: true
structured_output: false
vision: false
family: claude載入 YAML 時從環境變數讀取 API Key;YAML 不會自動展開環境變數:
import os
import yaml
from autogen_agentchat.agents import AssistantAgent
from autogen_core.models import ChatCompletionClient
with open("tokenlab-model.yaml", encoding="utf-8") as config_file:
config = yaml.safe_load(config_file)
config["config"]["api_key"] = os.environ["TOKENLAB_API_KEY"]
model_client = ChatCompletionClient.load_component(config)
agent = AssistantAgent("assistant", model_client=model_client)Python 客戶端
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,
"structured_output": False,
"vision": False,
"family": "claude",
},
)
agent = AssistantAgent("assistant", model_client=model_client)傳送第一個請求
import asyncio
async def main():
try:
result = await agent.run(task="Reply with: Connected to TokenLab.")
print(result.messages[-1].content)
finally:
await model_client.close()
asyncio.run(main())端點注意事項
AutoGen 的 OpenAI 擴充功能預期使用 OpenAI 相容的請求格式。若需使用供應商原生功能(例如 Anthropic Messages 或 Gemini generateContent),請使用直接支援這些協定的客戶端來呼叫 TokenLab 的原生端點。