部署LLM后端至GCP时遇ImportError:无法从ragas.llms导入LangchainLLM
问题:Docker容器中Ragas导入LangchainLLM失败
在将LLM后端部署到Google Cloud Platform(GCP)过程中,执行以下命令后:
docker build -t meeting-feedback . docker run -d --name meeting-feedback -p 8000:8000 meeting-feedback
容器内出现导入错误。我未直接使用LangChain,但DeepEval依赖LangChain与Ragas,由此引发该问题。
错误信息
ImportError: cannot import name 'LangchainLLM' from 'ragas.llms' (/usr/local/lib/python3.11/site-packages/ragas/llms/__init__.py)
相关配置文件
Dockerfile
# Use the official Python image from the Docker Hub FROM python:3.11 # Avoid Python from buffering stdout and stderr ENV PYTHONUNBUFFERED 1 # Set the working directory in the container to /code WORKDIR /code # Copy the requirements file from your host to /code/ in the container COPY requirements.txt /code/ # Install any dependencies RUN pip install --no-cache-dir --upgrade -r requirements.txt # Copy your application code (both main.py and templates.py) from the host to /code/ in the container COPY meeting-feedback /code/ # Inform Docker that the container listens on port 8000 at runtime EXPOSE 8000 # Command to run the application using Uvicorn. Adjust the module path to match the structure. CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000", "--reload"]
requirements.txt
fastapi openai uvicorn ragas==0.1.7 tiktoken==0.6.0 protobuf==3.20.3 deepeval langchain==0.1.14 firebase_admin==6.5.0 llama_index==0.10.27 langchain-community==0.0.31 pinecone-client[grpc]==3.0.2 llama-index-vector-stores-pinecone==0.1.4
文件结构
/meeting-feedback /meeting-feedback Dockerfile requirements.txt
解决方案
原因分析
Ragas 0.1.7版本适配的是LangChain 0.0.x系列,而你使用的LangChain是0.1.14(新版),两者的API结构存在差异,导致ragas.llms模块中不存在LangchainLLM类,触发导入错误。
修复步骤
- 修改
requirements.txt中的Ragas版本,升级到兼容LangChain 0.1.x的版本,例如:
或者固定兼容版本组合:ragas>=0.2.0ragas==0.2.1 langchain==0.1.14 - 重新构建Docker镜像:
docker build -t meeting-feedback . - 重新运行容器:
docker run -d --name meeting-feedback -p 8000:8000 meeting-feedback
内容的提问来源于stack exchange,提问作者CuriousCat
相关产品推荐
相关产品推荐

