框架與平台
AutoGen
透過 OpenAI 相容客戶端使用 TokenLab 模型與 Microsoft AutoGen
概覽
AutoGen 可以透過 OpenAIChatCompletionClient 呼叫 TokenLab。請在客戶端中設定 TokenLab 的 Base URL、API key 以及 model ID。
安裝
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 的原生端點。