Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How am I supposed to install new pip packages? #4594

Closed
espogian opened this issue Nov 14, 2024 · 3 comments
Closed

How am I supposed to install new pip packages? #4594

espogian opened this issue Nov 14, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@espogian
Copy link

Bug Description

I'm using langflow with Docker Compose. I have tried to create a custom model which leverages the Python module transformers, but the front-end is always throwing back an error of module not found, even after installation through pip install transformers.

Reproduction

  1. Create the following Dockerfile:
FROM langflowai/langflow:latest

RUN pip install transformers

CMD ["python", "-m", "langflow", "run", "--host", "0.0.0.0", "--port", "7860"]
  1. Create the following docker-compose.yml file:
services:
  langflow:
    build: .
    container_name: langflow
    ports:
      - 7860:7860
  1. Run docker compose up -d --build
  2. Open http://localhost:7860
  3. Try to create a new Custom Component with the following code (it is sufficient that the module imports tranformers):
# Import necessary modules
from langflow.custom import Component
from langflow.io import MessageTextInput, Output
from langflow.schema import Data

# Import transformers and LlamaTokenizer
from transformers import LlamaTokenizer

class CustomComponent(Component):
    display_name = "LLaMA Token Counter Component"
    description = "Counts the total tokens in the input text using LLaMA tokenization."
    documentation: str = "http://docs.langflow.org/components/custom"
    icon = "code"
    name = "LlamaTokenCounterComponent"

    # Define the input and output fields
    inputs = [
        MessageTextInput(
            name="input_text",
            display_name="Input Text",
            info="Enter the text for which you want to count tokens",
            value="",
            tool_mode=True,
        ),
    ]

    outputs = [
        Output(display_name="Token Count", name="token_count", method="count_tokens"),
    ]

    # Initialize the tokenizer for the LLaMA model
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # Replace the model path below with the specific LLaMA model path you are using
        self.tokenizer = LlamaTokenizer.from_pretrained("meta-llama/Llama-2-7b-chat-hf")

    # Method to count tokens in the input text
    def count_tokens(self) -> Data:
        # Get the input text from the component
        input_text = self.input_text

        # Tokenize the input text
        tokens = self.tokenizer.encode(input_text)

        # Calculate the token count
        token_count = len(tokens)

        # Prepare and return the output data
        data = Data(value=token_count)
        self.status = data
        return data

Expected behavior

Langflow web interface throws back the following error:

Error building Component: Module transformers not found. Please install it and try again
Traceback (most recent call last):
  File "/app/.venv/lib/python3.12/site-packages/langflow/utils/validate.py", line 231, in prepare_global_scope
    imported_module = importlib.import_module(node.module)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)

Who can help?

No response

Operating System

macOS Sequoia 15.1

Langflow Version

1.1.0

Python Version

3.12

Screenshot

No response

Flow File

No response

@espogian espogian added the bug Something isn't working label Nov 14, 2024
Copy link

dosubot bot commented Nov 14, 2024

Hey @espogian! I'm here to help you out with any bugs, questions, or contributions you have for Langflow. Let's get to the bottom of this together.

To install new pip packages in a Langflow Docker setup, you should modify the pyproject.toml file to include your desired packages and then rebuild the Docker image. This approach ensures that the packages are installed in the correct environment used by Langflow. Here's how you can do it:

  1. Edit pyproject.toml: Add the transformers package under the [tool.poetry.dependencies] section.

    Example modification:

    [tool.poetry.dependencies]
    python = "^3.12"
    transformers = "^4.0.0"
  2. Rebuild the Docker Image: Use the Dockerfile to rebuild the image, which will install the specified packages using Poetry.

    Run the following command to rebuild the Docker image:

    docker build -t your-langflow-image .
  3. Update your Dockerfile: Ensure your Dockerfile uses the updated image.

    Example Dockerfile:

    FROM your-langflow-image
    
    CMD ["python", "-m", "langflow", "run", "--host", "0.0.0.0", "--port", "7860"]
  4. Update your docker-compose.yml: Make sure it builds from the updated Dockerfile.

    Example docker-compose.yml:

    services:
      langflow:
        build: .
        container_name: langflow
        ports:
          - 7860:7860
  5. Rebuild and Run: Use Docker Compose to rebuild and run the container.

    docker compose up -d --build

This method ensures that the transformers package is included in the Docker image and available in the Langflow environment.

To continue talking to Dosu, mention @dosu.

@mataide
Copy link

mataide commented Nov 14, 2024

There is a simple way:

FROM langflowai/langflow:v1.0.19.post2

# Install pip in the virtual environment
RUN /app/.venv/bin/python -m ensurepip --upgrade

# Install the unstructured-ingest module using the pip from the virtual environment
RUN /app/.venv/bin/python -m pip install unstructured-ingest

# Set the environment variable to use the virtual environment’s Python by default
ENV PATH="/app/.venv/bin:$PATH"

CMD ["python", "-m", "langflow", "run", "--host", "0.0.0.0", "--port", "7860"]

@espogian
Copy link
Author

Thank you, this Dockerfile solved my issue:

FROM langflowai/langflow:latest

RUN /app/.venv/bin/python -m ensurepip --upgrade

RUN /app/.venv/bin/python -m pip install transformers sentencepiece

ENV PATH="/app/.venv/bin:$PATH"

CMD ["python", "-m", "langflow", "run", "--host", "0.0.0.0", "--port", "7860"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants