[问题/Issue] 章节6.2:AutoGen实现代码中“模型客户端配置”对应的函数create_openai_model_client()在接入非标准OpenAI model_id(如AIhubmix平台所提供的)时会报错
documentation
### 1. 遇到问题的章节 / Affected Chapter
Chapter 6.2
### 2. 问题类型 / Issue Type
代码错误 / Code Error
### 3. 具体问题描述 / Problem Description
章节6.2 AutoGen实现代码中“模型客户端配置”对应的函数create_openai_model_client()在接入非标准OpenAI model_id时会出现如下报错:
```bash
model_info is required when model name is not a valid OpenAI model
```
这种情况在使用一些API平台如groq时没有问题,因为model_id是标准的;但是对于AIhubmix等平台(也就是你在资源中所推荐的),model_id是非标准的,比如gpt-4o-free,coding-glm-4.7-free等
为了提升代码鲁棒性,建议解决这一问题。解决方案就是在OpenAIChatCompletionClient函数中加上model_info参数即可,比如:
```python
model_info = ModelInfo(
vision=True, # 是否支持视觉输入
function_calling=True, # 是否支持函数调用
json_output=True, # 是否支持 JSON 格式输出
family="gpt-4o" # 模型家族名称,可选
)
```
希望我的建议能给你们带来帮助,很喜欢你们的教材,加油💪
### 4. 问题重现材料 / Reproduction Materials
原始代码:
```python
def create_openai_model_client():
"""创建并配置 OpenAI 模型客户端"""
return OpenAIChatCompletionClient(
model=os.getenv("LLM_MODEL_ID", "gpt-4o"),
api_key=os.getenv("LLM_API_KEY"),
base_url=os.getenv("LLM_BASE_URL", "https://api.openai.com/v1")
)
```
报错:
```bash
model_info is required when model name is not a valid OpenAI model
```
建议修改代码为:
```python
def create_openai_model_client():
"""创建 OpenAI 模型客户端用于测试"""
return OpenAIChatCompletionClient(
model=os.getenv("LLM_MODEL_ID", "gpt-4o"),
model_info = ModelInfo(
vision=True, # 是否支持视觉输入
function_calling=True, # 是否支持函数调用
json_output=True, # 是否支持 JSON 格式输出
family="gpt-4o" # 模型家族名称,可选
),
api_key=os.getenv("LLM_API_KEY"),
base_url=os.getenv("LLM_BASE_URL", "https://api.openai.com/v1")
)
```
### 5. 补充信息 / Additional Information
_No response_
### 确认事项 / Verification
- [x] 我已阅读过相关章节的文档 / I have read the relevant chapter documentation
- [x] 我已搜索过现有的Issues,确认此问题未被报告 / I have searched existing Issues and confirmed this hasn't been reported
- [x] 我已尝试过基本的故障排除(如重启、重新安装依赖等) / I have tried basic troubleshooting (restart, reinstall dependencies, etc.)
0 条评论