MacBook Air上ChatOllama调用Llama3失败 VSCode报错求助
问题:Ollama+Llama3在VSCode中调用报错(终端正常)
环境信息
- 设备:8GB内存MacBook Air
- 已部署Ollama服务,运行地址
http://localhost:11434 - 终端可正常与Llama3交互,但VSCode中通过LangChain调用时抛出异常
错误日志
Checkpoint 1 Checkpoint 2 checkpoint 3 <class 'list'> <class 'langchain_core.messages.human.HumanMessage'> Input to invoke: [HumanMessage(content='Are you ready?', additional_kwargs={}, response_metadata={})] Traceback (most recent call last): File "/Users/sandeephugar/VSCProjects/purplekicks/manager/llmtest.py", line 156, in <module> response = llm.invoke(test_message) File "/Users/sandeephugar/VSCProjects/purplekicks/env/lib/python3.9/site-packages/langchain_core/language_models/chat_models.py", line 286, in invoke self.generate_prompt( File "/Users/sandeephugar/VSCProjects/purplekicks/env/lib/python3.9/site-packages/langchain_core/language_models/chat_models.py", line 786, in generate_prompt return self.generate(prompt_messages, stop=stop, callbacks=callbacks, **kwargs) File "/Users/sandeephugar/VSCProjects/purplekicks/env/lib/python3.9/site-packages/langchain_core/language_models/chat_models.py", line 643, in generate raise e File "/Users/sandeephugar/VSCProjects/purplekicks/env/lib/python3.9/site-packages/langchain_core/language_models/chat_models.py", line 633, in generate self._generate_with_cache( File "/Users/sandeephugar/VSCProjects/purplekicks/env/lib/python3.9/site-packages/langchain_core/language_models/chat_models.py", line 851, in _generate_with_cache result = self._generate( File "/Users/sandeephugar/VSCProjects/purplekicks/env/lib/python3.9/site-packages/langchain_ollama/chat_models.py", line 644, in _generate final_chunk = self._chat_stream_with_aggregation( File "/Users/sandeephugar/VSCProjects/purplekicks/env/lib/python3.9/site-packages/langchain_ollama/chat_models.py", line 558, in _chat_stream_with_aggregation tool_calls=_get_tool_calls_from_response(stream_resp), File "/Users/sandeephugar/VSCProjects/purplekicks/env/lib/python3.9/site-packages/langchain_ollama/chat_models.py", line 70, in _get_tool_calls_from_response for tc in response["message"]["tool_calls"]: TypeError: 'NoneType' object is not iterable
测试代码
from langchain_ollama import ChatOllama from langchain_core.messages import AIMessage, HumanMessage # Initialize the LLM (Ollama) llm = ChatOllama(model="llama3") print("Checkpoint 1") # Check if the model is accessible if not llm: raise ValueError("LLM instance is not initialized properly.") print("Checkpoint 2") # Test a simple input test_message = [HumanMessage(content="Are you ready?")] print("checkpoint 3") print(type(test_message)) # Should print <class 'list'> print(type(test_message[0])) print("Input to invoke:", test_message) # Invoke the model and get the response response = llm.invoke(test_message) print(response.content) # Check if the response is not None if response is None: raise ValueError("The model did not respond. Ensure the Ollama server is running and the model is loaded.") print("LLM is ready and responding.") print("Checkpoint 4") # Iterate over the response list and print each message content for msg in response: print(msg.content) print("Checkpoint 6")
已尝试方案
- 调试并处理错误
- 修改消息格式为元组列表形式:
test_message = [( "system", "You are a helpful assistant here to chat.", ), ("human", "hello, nice to meet you!"),]
解决方案
1. 升级langchain-ollama依赖包
该错误是旧版本langchain-ollama的已知bug,当模型未返回工具调用结果时,代码会尝试遍历None值。升级到最新版本可修复此问题:
pip install --upgrade langchain-ollama
2. 修正代码中的遍历错误
原代码最后for msg in response:会报错,因为llm.invoke()返回的是单个AIMessage对象,而非列表。修正后的代码片段:
# Invoke the model and get the response response = llm.invoke(test_message) print(response.content) print("LLM is ready and responding.") print("Checkpoint 4") # 直接打印响应内容,无需遍历 print("Response content:", response.content) print("Checkpoint 6")
3. 可选:显式指定文本格式(若升级后仍有问题)
初始化ChatOllama时显式设置输出格式为文本,避免默认的工具调用格式处理:
llm = ChatOllama(model="llama3", format="text")
内容的提问来源于stack exchange,提问作者cooldude
相关产品推荐
相关产品推荐

