langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
89.65k stars 14.17k forks source link

ModuleNotFoundError: No module named 'langchain_community' #21880

Open KaiQiang-Liu1994 opened 2 months ago

KaiQiang-Liu1994 commented 2 months ago

Checked other resources

Example Code

----part0导入所需要的类

import os import requests from PIL import Image from transformers import BlipProcessor, BlipForConditionalGeneration from langchain.tools import BaseTool from langchain import OpenAI from langchain.agents import initialize_agent,AgentType

----part1 初始化图像字母生成

hf_model = "Salesforce/blip-image-caption-large" processor = BlipProcessor.from_pretrained(hf_model) model = BlipForConditionalGeneration.from_pretrained(hf_model)

--定义图像字母生成工具类

class ImageCapTool(BaseTool): name = "Image captioner" description = "为图片创作说明文案"

def _run(self,url:str):
    image = Image.open(requests.get(url,stream=True).raw).convert("RGB")
    inputs = processor(image,return_tensors="pt")
    out = model.generate(**inputs,max_new_tokens=20)
    caption = processor.decode(out[0],skip_special_tokens=True)
    return caption

def _arun(self,query:str):
    raise NotImplementedError("This tool does not support async")

os.environ["OPENAI_API_KEY"] = "" llm = OpenAI(temperature=0.2) tools = [ImageCapTool] agent = initialize_agent( agent = AgentType.ZERO_SHOT_REACT_DESCRIPTION, tools=tools, llm=llm, verbose = True, )

image_url = "https://image.baidu.com/search/detail?ct=503316480&z=undefined&tn=baiduimagedetail&ipn=d&word=%E7%8E%AB%E7%91%B0&step_word=&lid=8350970360390223282&ie=utf-8&in=&cl=2&lm=-1&st=undefined&hd=undefined&latest=undefined&copyright=undefined&cs=1485018591,1347421720&os=1568910280,55160396&simid=3419305429,75243099&pn=6&rn=1&di=7355526631391232001&ln=1941&fr=&fmq=1716115722875_R&fm=&ic=undefined&s=undefined&se=&sme=&tab=0&width=undefined&height=undefined&face=undefined&is=0,0&istype=0&ist=&jit=&bdtype=0&spn=0&pi=0&gsm=1e&objurl=https%3A%2F%2Fs2.best-wallpaper.net%2Fwallpaper%2Fiphone%2F1911%2FOne-red-rose-petals-black-background_iphone_640x1136.jpg&rpstart=0&rpnum=0&adpicid=0&nojc=undefined&dyTabStr=MCwxLDMsMiw2LDQsNSw4LDcsOQ%3D%3D" agent.invoke(input=f'{image_url}\n请创作合适的中文推广文案')

Error Message and Stack Trace (if applicable)

D:\miniconda\envs\llm\python.exe D:\langchain_code\langchain0519\demo02.py Traceback (most recent call last): File "D:\langchain_code\langchain0519\demo02.py", line 7, in from langchain import OpenAI File "D:\miniconda\envs\llm\Lib\site-packages\langchain__init.py", line 189, in getattr__ from langchain_community.llms import OpenAI ModuleNotFoundError: No module named 'langchain_community'

Description

D:\miniconda\envs\llm\python.exe D:\langchain_code\langchain0519\demo02.py Traceback (most recent call last): File "D:\langchain_code\langchain0519\demo02.py", line 7, in from langchain import OpenAI File "D:\miniconda\envs\llm\Lib\site-packages\langchain__init.py", line 189, in getattr__ from langchain_community.llms import OpenAI ModuleNotFoundError: No module named 'langchain_community'

System Info

D:\miniconda\envs\llm\python.exe D:\langchain_code\langchain0519\demo02.py Traceback (most recent call last): File "D:\langchain_code\langchain0519\demo02.py", line 7, in from langchain import OpenAI File "D:\miniconda\envs\llm\Lib\site-packages\langchain__init.py", line 189, in getattr__ from langchain_community.llms import OpenAI ModuleNotFoundError: No module named 'langchain_community'

kavyabhargava commented 2 months ago

Facing same issue

laishzh commented 2 months ago

You can try this command to install langchain_community manually. pip install langchain-community.

Reason: langchain-community is not automatically installed in langchain v0.2.

kavyabhargava commented 2 months ago

Tried that too. Still getting error: ModuleNotFoundError Traceback (most recent call last) Cell In[4], line 1 ----> 1 from langchain.chat_models import ChatOpenAI

File ~/opt/anaconda3/lib/python3.9/site-packages/langchain/chat_models/init.py:27, in getattr(name) 26 def getattr(name: str) -> None: ---> 27 from langchain_community import chat_models 29 # If not in interactive env, raise warning. 30 if not is_interactive_env():

ModuleNotFoundError: No module named 'langchain_community'

Vitalis001 commented 2 months ago

@kavyabhargava try this command to install. pip install langchain-community --trusted-host mirrors.cloud.tencent.com

arc-ch commented 1 month ago

@kavyabhargava try this command to install. pip install langchain-community --trusted-host mirrors.cloud.tencent.com

facing the same issue even after doing this

Thoams0211 commented 1 month ago

You can try this command to install langchain_community manually. pip install langchain-community.

Reason: langchain-community is not automatically installed in langchain v0.2.

It is usful

panuoksala commented 1 month ago

If you are using conda, you need to add conda-forge as channel into conda. image

Then you can install langchain-community package and it should start to work.

maurice6411 commented 1 month ago

You can try this command to install langchain_community manually. pip install langchain-community.

Reason: langchain-community is not automatically installed in langchain v0.2.

Lifesaver

Achbi commented 1 month ago

you can try pip install langchain-community and if you are using openai key than pip install langchain_openai and instead of from langchain import OpenAI use from langchain_openai import OpenAI

9050350 commented 1 month ago

Facing same issue

janaMLdevop commented 3 weeks ago

from langchain_community.llms.ollama import Ollama (try this command it will work)