1. 遇到问题的章节 / Affected Chapter
Chapter7.2
2. 问题类型 / Issue Type
代码错误 / Code Error
3. 具体问题描述 / Problem Description
my_llm.py内的Qwen/Qwen2.5-VL-72B-Instruct用不了,改为deepseek的接口或者其他的就可以运行。当改为Qwen/Qwen3.5-35B-A3B时,报IndexError: list index out of range的错误,问了ai,说是
父类的原始代码
for chunk in response:
content = chunk.choices[0].delta.content or "" # ❌ 直接访问 [0]
没有判断choices[0]为空的情况,重写了think()函数就没有报错了
4. 问题重现材料 / Reproduction Materials
python
import os
from typing import Optional,List,Dict
from openai import OpenAI
from hello_agents import HelloAgentsLLM
class My_LLM(HelloAgentsLLM):
def __init__(
self,
model: Optional[str] = None,
api_key: Optional[str] = None,
base_url: Optional[str] = None,
provider: Optional[str] = "auto",
**kwargs
):
# 检查provider是否为我们想处理的'modelscope'
if provider == "modelscope":
print("正在使用自定义的 ModelScope Provider")
self.provider = "modelscope"
# 解析 ModelScope 的凭证
self.api_key = api_key or os.getenv("MODELSCOPE_API_KEY")
self.base_url = base_url or "https://api-inference.modelscope.cn/v1/"
# 验证凭证是否存在
if not self.api_key:
raise ValueError("ModelScope API key not found. Please set MODELSCOPE_API_KEY environment variable.")
# 设置默认模型和其他参数(使用 ModelScope 推理 API 支持的模型名称)
self.model = model or os.getenv("LLM_MODEL_ID") or "Qwen/Qwen2.5-VL-72B-Instruct"
self.temperature = kwargs.get('temperature', 0.7)
self.max_tokens = kwargs.get('max_tokens')
self.timeout = kwargs.get('timeout', 60)
# 使用获取的参数创建OpenAI客户端实例
self._client = OpenAI(api_key=self.api_key, base_url=self.base_url, timeout=self.timeout)
else:
# 如果不是 modelscope, 则完全使用父类的原始逻辑来处理
super().__init__(model=model, api_key=api_key, base_url=base_url, provider=provider, **kwargs)
报错
❌ 调用LLM API时发生错误: Error code: 400 - {'error': {'message': 'Model id : Qwen/Qwen2.5-VL-72B-Instruct , has no provider supported', 'request_id': '46efcd05-1954-4a79-9b40-217c7838c7c7'}}
修改后
self.model = model or os.getenv("LLM_MODEL_ID") or "Qwen/Qwen3.5-35B-A3B"
有输出但是出现问题
❌ 调用LLM API时发生错误: list index out of range
因为父类源码中没有做非空判断
for chunk in response:
content = chunk.choices[0].delta.content or ""
if content:
print(content, end="", flush=True)
yield content
print() # 在流式输出结束后换行
在my_llm.py中重写think()方法,加上判断,就没有报错了
def think(self, messages: List[Dict[str, str]], temperature: float = 0):
"""
调用大语言模型进行思考,返回流式响应生成器(修复 IndexError)
"""
print(f"🧠 正在调用 {self.model} 模型...")
try:
response = self._client.chat.completions.create(
model=self.model,
messages=messages,
temperature=temperature,
stream=True,
)
for chunk in response:
# 安全检查:确保 choices 不为空
if chunk.choices and len(chunk.choices) > 0:
content = chunk.choices[0].delta.content or ""
if content:
print(content, end="", flush=True)
yield content
print("\n✅ 大语言模型响应成功:")
except Exception as e:
print(f"❌ 调用LLM API时发生错误: {e}")
raise
5. 补充信息 / Additional Information
No response
确认事项 / Verification
1. 遇到问题的章节 / Affected Chapter
Chapter7.2
2. 问题类型 / Issue Type
代码错误 / Code Error
3. 具体问题描述 / Problem Description
my_llm.py内的Qwen/Qwen2.5-VL-72B-Instruct用不了,改为deepseek的接口或者其他的就可以运行。当改为Qwen/Qwen3.5-35B-A3B时,报IndexError: list index out of range的错误,问了ai,说是
父类的原始代码
for chunk in response:
content = chunk.choices[0].delta.content or "" # ❌ 直接访问 [0]
没有判断choices[0]为空的情况,重写了think()函数就没有报错了
4. 问题重现材料 / Reproduction Materials
5. 补充信息 / Additional Information
No response
确认事项 / Verification