AutoGen
AutoGen 是一个用于构建 AI 智能体和多智能体应用的框架。 官方网站:microsoft.github.io/autogen
1.1 使用方法
方式一:OpenAI 兼容端点(推荐,适用于 AutoGen 0.2+)
from autogen_ext.models.openai import OpenAIChatCompletionClient
client = OpenAIChatCompletionClient(
model="minimax/minimax-m2.5",
base_url="https://api.luchentech.com/inference/v1",
api_key="your-luchentech-key",
model_info={
"vision": False,
"function_calling": True,
"json_output": True,
}
)
# 在智能体中使用
from autogen_agentchat import AssistantAgent
agent = AssistantAgent(
name="assistant",
model_client=client,
)
方式二:config_list(旧版)
import autogen
llm_config = {
"config_list": [
{
"model": "minimax/minimax-m2.5",
"api_base": "https://api.luchentech.com/inference/v1",
"api_key": "your-luchentech-key",
"api_type": "openai"
}
]
}
assistant = autogen.AssistantAgent(
name="assistant",
llm_config=llm_config
)
方式三:自定义模型客户端(高级)
from autogen_core.models import ChatCompletionClient, CreateResult, LLMMessage, ModelInfo
class CustomModelClient(ChatCompletionClient):
def __init__(self, api_key, base_url, model, **kwargs):
self.api_key = api_key
self.base_url = base_url
self.model = model
self._model_info = ModelInfo(
vision=False, function_calling=True, json_output=True
)
@property
def model_info(self) -> ModelInfo:
return self._model_info
async def create(self, messages: List[LLMMessage], **kwargs) -> CreateResult:
import requests
headers = {"Authorization": f"Bearer {self.api_key}"}
payload = {"model": self.model, "messages": [{"role": m.role, "content": m.content} for m in messages]}
response = requests.post(f"{self.base_url}/chat/completions", headers=headers, json=payload)
result = response.json()
return CreateResult(
content=result["choices"][0]["message"]["content"],
finish_reason=result["choices"][0].get("finish_reason", "stop"),
usage=result.get("usage", {})
)
client = CustomModelClient(
api_key="your-luchentech-key",
base_url="https://api.luchentech.com/inference/v1",
model="minimax/minimax-m2.5"
)
1.2 故障排除
| 常见问题 | 解决方案 |
|---|---|
| 版本兼容性 | AutoGen 0.2+ 使用 autogen_ext.models.openai |
| model_info 错误 | 根据模型能力设置 vision、function_calling、json_output |
| async/await 错误 | 新版本使用异步接口 |