在VS Code本地虚拟环境测试Ollama自定义Prompt遇导入错误
解决LangChain导入Ollama的ImportError问题
问题描述
在VS Code的.ipynb文件中,使用本地虚拟环境.venv(Python 3.11.9)测试Ollama自定义Prompt时,出现以下错误:
ImportError: cannot import name 'Ollama' from 'langchain.llms'
用户使用的代码:
from langchain.llms import Ollama ollama = Ollama(base_url= "http://localhost:11434", model="PremPagal") print("who are you?")
自定义Prompt内容:
FROM llama3 # set the temperature to 1 [higher is more creative, lower is more coherent] PARAMETER temperature 1 # set the system message SYSTEM """ You are PremPagal, your task is to suggest weekend activities in Bengaluru. As your user the following details: - Age - Profession - Type of activities (Note: give four options based on age and profession) """
解决方案
1. 调整导入路径
LangChain版本更新后,Ollama的集成已迁移到langchain_community包中,修改导入代码为:
from langchain_community.llms import Ollama ollama = Ollama(base_url= "http://localhost:11434", model="PremPagal") print("who are you?")
2. 确保安装必要依赖
如果未安装langchain_community,在虚拟环境中执行以下命令:
pip install langchain langchain_community
若已安装旧版LangChain,先卸载再重新安装:
pip uninstall langchain -y pip install langchain langchain_community
3. 确认虚拟环境配置
在VS Code中,通过右下角的Python解释器选择器,确保当前.ipynb文件使用的是.venv虚拟环境,避免依赖包安装到全局环境导致导入失败。
内容的提问来源于stack exchange,提问作者Abhishek Gupta
相关产品推荐
相关产品推荐

