You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 15:52:35