[问题/Issue] 章节7.2.1:示例代码运行报错
1. 遇到问题的章节 / Affected Chapter
Chapter7.2.1
2. 问题类型 / Issue Type
代码错误 / Code Error
3. 具体问题描述 / Problem Description
示例代码中, 当provider="modelscope", 会进入my_llm的判断分支 if provider == "modelscope" 中,直接创建了OpenAI客户端实例,并没有走父类的__init__流程。所以 _adapter 这个属性压根没有被赋过值,父类那些方法一运行就 AttributeError。
4. 问题重现材料 / Reproduction Materials
在执行到
# 打印响应
print("ModelScope Response:",response_stream)
for chunk in response_stream:
# chunk在my_llm库中已经打印过一遍,这里只需要pass即可
# print(chunk, end="", flush=True)
pass
报错: 正在使用自定义的 ModelScope Provider ModelScope Response: 正在调用 your-model-name 模型... ✅ 大语言模型响应成功: ❌ 调用LLM API时发生错误: 'MyLLM' object has no attribute '_adapter' Traceback (most recent call last): File "/Users/hollies/Documents/projects/hello-agents/code/chapter7/my_main.py", line 19, in for chunk in response_stream: ^^^^^^^^^^^^^^^ File "/Users/hollies/Documents/projects/hello-agents/venv/lib/python3.14/site-packages/hello_agents/core/llm.py", line 106, in think for chunk in self._adapter.stream_invoke(messages, **kwargs): ^^^^^^^^^^^^^ AttributeError: 'MyLLM' object has no attribute '_adapter'
5. 补充信息 / Additional Information
No response
确认事项 / Verification
- 我已阅读过相关章节的文档 / I have read the relevant chapter documentation
- 我已搜索过现有的Issues,确认此问题未被报告 / I have searched existing Issues and confirmed this hasn't been reported
- 我已尝试过基本的故障排除(如重启、重新安装依赖等) / I have tried basic troubleshooting (restart, reinstall dependencies, etc.)
Source: datawhalechina/hello-agents