langflow-ai / langflow

Langflow is a low-code app builder for RAG and multi-agent AI applications. It’s Python-based and agnostic to any model, API, or database.
http://www.langflow.org
MIT License
33.97k stars 4.11k forks source link

Unable to import external libraries in langflow using the custom component ImportError: cannot import name 'AnthropicLLM' from 'langchain_community.llms' (C:\Users\SreeshaB\Desktop\langflow\.venv\Lib\site-packages\langchain_community\llms\__init__.py) #3548

Closed ssbaraar closed 2 months ago

dosubot[bot] commented 2 months ago

Hey @ssbaraar! I'm here to help you with any issues you're facing. I can assist you in solving bugs, answering questions, and becoming a contributor. Let's tackle this together!

I found some similar issues and discussions that might help:

  1. Issues that occur when loading custom components (circular import) - This discussion is still open and might provide insights into issues with loading custom components [1].
  2. Error with custom component - Another open discussion that might be relevant [2].
  3. Can't import Spacy in custom components - This open issue provides a solution for importing Spacy in custom components [3].

To import external libraries in Langflow using a custom component, you can include the necessary import statements in your Python script for the custom component. Here is an example:

from langflow.custom import Component
from langflow.inputs import MessageTextInput, IntInput, BoolInput, DropdownInput
from langflow.template import Output
from langflow.schema import Data
import some_external_library  # Import the external library

class DocumentProcessor(Component):
    display_name = "Document Processor"
    description = "This component processes a document"
    icon = "document-icon"

    inputs = [
        MessageTextInput(
            name="document",
            display_name="Document",
            info="Text input for the document content.",
        ),
        DropdownInput(
            name="function",
            display_name="Function",
            options=["Uppercase", "Lowercase", "Titlecase"],
            info="Function to apply to the document content.",
        ),
    ]

    outputs = [
        Output(display_name="Processed Document", name="processed_document", method="process_document"),
    ]

    def process_document(self) -> Data:
        document = self.document
        function = self.function
        page_content = document

        if function == "Uppercase":
            page_content = page_content.upper()
        elif function == "Lowercase":
            page_content = page_content.lower()
        elif function == "Titlecase":
            page_content = page_content.title()

        result = f"Result of {function} function: {page_content}"
        self.status = result
        return Data(data={"result": result})

Make sure the external library is installed in your environment:

pip install some_external_library

After creating your custom component script, follow these steps to load it into Langflow:

  1. Folder Structure: Create a folder for your custom components.

    .
    └── custom_components
        ├── document_processor.py
        └── ...
  2. Load Custom Components: Set the LANGFLOW_COMPONENTS_PATH environment variable to the path of your custom components directory and run the Langflow CLI.

    export LANGFLOW_COMPONENTS_PATH='["/path/to/components"]'
    langflow run

    Alternatively, use the --components-path argument:

    langflow run --components-path /path/to/components

Langflow will print an error message to the console if a component fails to load due to errors in the component's code, including issues with importing external libraries [4].

For more details, you can refer to the Langflow documentation [4].

To continue talking to Dosu, mention @dosu.

ssbaraar commented 2 months ago

from typing import cast

from langflow.base.models.model import LCModelComponent from langflow.field_typing import LanguageModel from langflow.inputs import MessageTextInput from langflow.io import BoolInput, FileInput, FloatInput, IntInput, StrInput

class ChatClaudeComponent(LCModelComponent): display_name = "Claude 3.5 Sonnet" description = "Generate text using Claude 3.5 Sonnet via Anthropic's Vertex SDK." icon = "Claude" name = "ClaudeModel"

inputs = LCModelComponent._base_inputs + [
    FileInput(
        name="credentials",
        display_name="Credentials",
        info="JSON credentials file. Leave empty to fallback to environment variables",
        file_types=["json"],
    ),
    StrInput(name="project_id", display_name="Project ID", value="project_id"),
    StrInput(name="location", display_name="Location", value="Location"),
    IntInput(name="max_tokens", display_name="Max Tokens", value=1024),
    FloatInput(name="temperature", value=0.0, display_name="Temperature"),
    FloatInput(name="top_p", display_name="Top P", value=0.95),
    BoolInput(name="verbose", display_name="Verbose", value=False, advanced=True),
]

def build_model(self) -> LanguageModel:
    try:
        from anthropic import AnthropicVertex
        from langchain_community.llms import AnthropicLLM
    except ImportError:
        raise ImportError(
            "Please install the anthropic[vertex] package to use the Claude component."
        )

    if self.credentials:
        from google.oauth2 import service_account

        credentials = service_account.Credentials.from_service_account_file(self.credentials)
        project_id = self.project_id or credentials.project_id
    else:
        project_id = self.project_id
        credentials = None

    client = AnthropicVertex(
        region=self.location,
        project_id=project_id,
        credentials=credentials,
    )

    return cast(
        LanguageModel,
        AnthropicLLM(
            client=client,
            model_name="claude-3-5-sonnet@20240620",
            max_tokens_to_sample=self.max_tokens,
            temperature=self.temperature,
            top_p=self.top_p,
            verbose=self.verbose,
        ),
    )

    Here is my custom component code ,
    Error Building Component

Error building Component Custom Component: Please install the anthropic[vertex] package to use the Claude component.

This is the error iam facing

carlosrcoelho commented 2 months ago

@ssbaraar

Hi,

Could you provide more details about what you want to build with the custom component?

ssbaraar commented 2 months ago

@carlosrcoelho hello ,

I am trying to build a custom component for a language model using the LangFlow framework. The component is designed to interact with the Claude 3.5 Sonnet model via the Anthropic Vertex SDK , as existing vertex ai component is not able to run with credentials for my anthropic SDK , so I have just tried tweaking the code and there was no desired output , hence I went to build a custom component, where I'm facing the above mentioned error of where is says cannot build the " anthropic [vertex] " though I have installed this dependency in the local venv many times trying to uninstall it and reinstalling it again , and also tried my running up the Langflow instance again but everytime was facing the same issue .

Here is code of my custom component :

from typing import cast
from langflow.base.models.model import LCModelComponent
from langflow.field_typing import LanguageModel
from langflow.inputs import MessageTextInput
from langflow.io import BoolInput, FileInput, FloatInput, IntInput, StrInput

class ChatClaudeComponent(LCModelComponent):
    display_name = "Claude 3.5 Sonnet"
    description = "Generate text using Claude 3.5 Sonnet via Anthropic's Vertex SDK."
    icon = "Claude"
    name = "ClaudeModel"

    inputs = LCModelComponent._base_inputs + [
        FileInput(
            name="credentials",
            display_name="Credentials",
            info="JSON credentials file. Leave empty to fallback to environment variables",
            file_types=["json"],
        ),
        StrInput(name="project_id", display_name="Project ID", value="researchdevelopment-424002"),
        StrInput(name="location", display_name="Location", value="europe-west1"),
        IntInput(name="max_tokens", display_name="Max Tokens", value=1024),
        FloatInput(name="temperature", value=0.0, display_name="Temperature"),
        FloatInput(name="top_p", display_name="Top P", value=0.95),
        BoolInput(name="verbose", display_name="Verbose", value=False, advanced=True),
    ]

    def build_model(self) -> LanguageModel:
        try:
            from anthropic import AnthropicVertex
            from langchain.llms import AnthropicLLM
        except ImportError:
            raise ImportError(
                "Please install the anthropic[vertex] package to use the Claude component."
            )

        if self.credentials:
            from google.oauth2 import service_account

            credentials = service_account.Credentials.from_service_account_file(self.credentials)
            project_id = self.project_id or credentials.project_id
        else:
            project_id = self.project_id
            credentials = None

        client = AnthropicVertex(
            region=self.location,
            project_id=project_id,
            credentials=credentials,
        )

        return cast(
            LanguageModel,
            AnthropicLLM(
                client=client,
                model_name="claude-3-5-sonnet@20240620",
                max_tokens_to_sample=self.max_tokens,
                temperature=self.temperature,
                top_p=self.top_p,
                verbose=self.verbose,
            ),
        )
viniciossilva3 commented 2 months ago

@ssbaraar Try this:

from typing import cast
from langflow.base.models.model import LCModelComponent
from langflow.field_typing import LanguageModel
from langflow.inputs import MessageTextInput
from langflow.io import BoolInput, FileInput, FloatInput, IntInput, StrInput
from anthropic import AnthropicVertex

class ChatClaudeComponent(LCModelComponent):
    display_name = "Claude 3.5 Sonnet"
    description = "Generate text using Claude 3.5 Sonnet via Anthropic's Vertex SDK."
    icon = "Claude"
    name = "ClaudeModel"

    inputs = LCModelComponent._base_inputs + [
        FileInput(
            name="credentials",
            display_name="Credentials",
            info="JSON credentials file. Leave empty to fallback to environment variables",
            file_types=["json"],
        ),
        StrInput(name="project_id", display_name="Project ID", value="researchdevelopment-424002"),
        StrInput(name="location", display_name="Location", value="europe-west1"),
        IntInput(name="max_tokens", display_name="Max Tokens", value=1024),
        FloatInput(name="temperature", value=0.0, display_name="Temperature"),
        FloatInput(name="top_p", display_name="Top P", value=0.95),
        BoolInput(name="verbose", display_name="Verbose", value=False, advanced=True),
    ]

    def build_model(self) -> LanguageModel:
        try:
            from anthropic import AnthropicVertex
            # from langchain.llms import AnthropicLLM
            from langchain_anthropic import AnthropicLLM #This is the correct path import

        except ImportError:
            raise ImportError(
                "Please install the anthropic[vertex] package to use the Claude component."
            )

        if self.credentials:
            from google.oauth2 import service_account

            credentials = service_account.Credentials.from_service_account_file(self.credentials)
            project_id = self.project_id or credentials.project_id
        else:
            project_id = self.project_id
            credentials = None

        client = AnthropicVertex(
            region=self.location,
            project_id=project_id,
            credentials=credentials,
        )

        return cast(
            LanguageModel,
            AnthropicLLM(
                client=client,
                model_name="claude-3-5-sonnet@20240620",
                max_tokens_to_sample=self.max_tokens,
                temperature=self.temperature,
                top_p=self.top_p,
                verbose=self.verbose,
            ),
        )
ssbaraar commented 2 months ago

@viniciossilva3 Here is the error , I trying to load the Language Model From the google vertex SDK

Error Building Component

Error building Component Custom Component: 1 validation error for AnthropicLLM root Did not find anthropic_api_key, please add an environment variable ANTHROPIC_API_KEY which contains it, or pass anthropic_api_key as a named parameter. (type=value_error)

ssbaraar commented 2 months ago

Screenshot 2024-08-27 122457 @viniciossilva3 @carlosrcoelho

There is Bug in the Vertex AI component of the Langflow , Without giving the credentials file only the vertex ai is able to run the language model and is generating the output please try to fix that or maybe raise the issue .

This is a security vulnerability that can potentially lead to unauthorized access and misuse of the Vertex AI services.

viniciossilva3 commented 2 months ago

Hi @ssbaraar ! I added a Anthropic API Key field in this example code. You can try this solution:

from typing import cast
from langflow.base.models.model import LCModelComponent
from langflow.field_typing import LanguageModel
from langflow.inputs import MessageTextInput
from langflow.io import BoolInput, FileInput, FloatInput, IntInput, StrInput, SecretStrInput
from anthropic import AnthropicVertex

class ChatClaudeComponent(LCModelComponent):
    display_name = "Claude 3.5 Sonnet"
    description = "Generate text using Claude 3.5 Sonnet via Anthropic's Vertex SDK."
    icon = "Claude"
    name = "ClaudeModel"

    inputs = LCModelComponent._base_inputs + [
        SecretStrInput(name="api_key", display_name="Anthropic API Key", info="Input your Anthropic API Key", value=""),
        FileInput(
            name="credentials",
            display_name="Credentials",
            info="JSON credentials file. Leave empty to fallback to environment variables",
            file_types=["json"],
        ),
        StrInput(name="project_id", display_name="Project ID", value="researchdevelopment-424002"),
        StrInput(name="location", display_name="Location", value="europe-west1"),
        IntInput(name="max_tokens", display_name="Max Tokens", value=1024),
        FloatInput(name="temperature", value=0.0, display_name="Temperature"),
        FloatInput(name="top_p", display_name="Top P", value=0.95),
        BoolInput(name="verbose", display_name="Verbose", value=False, advanced=True),
    ]

    def build_model(self) -> LanguageModel:
        try:
            from anthropic import AnthropicVertex
            # from langchain.llms import AnthropicLLM
            from langchain_anthropic import AnthropicLLM #This is the correct path import

        except ImportError:
            raise ImportError(
                "Please install the anthropic[vertex] package to use the Claude component."
            )

        if self.credentials:
            from google.oauth2 import service_account

            credentials = service_account.Credentials.from_service_account_file(self.credentials)
            project_id = self.project_id or credentials.project_id
        else:
            project_id = self.project_id
            credentials = None

        client = AnthropicVertex(
            region=self.location,
            project_id=project_id,
            credentials=credentials,
        )

        return cast(
            LanguageModel,
            AnthropicLLM(
                anthropic_api_key= self.api_key,
                client=client,
                model_name="claude-3-5-sonnet@20240620",
                max_tokens_to_sample=self.max_tokens,
                temperature=self.temperature,
                top_p=self.top_p,
                verbose=self.verbose,
            ),
        )
ssbaraar commented 2 months ago

Facing the same issue @viniciossilva3

Im trying to load the language model from a vertex ai SDK end point

The format of that would be something like this :

{
     "type": "service_account",
     "project_id": "researchdevelopment-412302",
     "private_key_id": "62a8e1237a12344c550aaa2cee7ff5b8490123ad",
     "private_key": "-----BEGIN PRIVATE KEY-----\n4nYAtxqW+w4/vA93pq/FlDYltKAODYHAYD4uSLBzngFngDnD6Y/Q+IW+izZMnySG\neFU3h3mBLvB/iU+9kteysdzcFLRVGOJxOS9cDFI5v38eQOn9iN4iFfCgu+SQoFml\nY9ugcxBiw4Ghfa5eFEZDG2tbO44HB/tvE95A394/9QKBgQDj0R073RBGImXm5F+W\n-----END PRIVATE KEY-----\n",
     "client_email": "123414412308-compute@developer.gserviceaccount.com",
     "client_id": "123462248247656962802",
     "auth_uri": "https://accounts.google.com/o/oauth2/auth",
     "token_uri": "https://oauth2.googleapis.com/token",
     "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
     "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/791234413808-compute%40developer.gserviceaccount.com",
     "universe_domain": "googleapis.com"
 }
viniciossilva3 commented 2 months ago

Hello @ssbaraar! I tried to replicate a similar scenario here, please check if this code solves your issue. Please message back if you had any success or any other issues. Screenshot from 2024-08-27 17-16-39

from typing import cast
from langflow.base.models.model import LCModelComponent
from langflow.field_typing import LanguageModel
from langflow.io import BoolInput, FileInput, FloatInput, IntInput, StrInput, SecretStrInput

class ChatClaudeComponent(LCModelComponent):
    display_name = "Claude 3.5 Sonnet"
    description = "Generate text using Claude 3.5 Sonnet via Anthropic's Vertex SDK."
    icon = "chat"
    name = "ClaudeModel"

    inputs = LCModelComponent._base_inputs + [
        SecretStrInput(name="api_key", display_name="Anthropic API Key", required=True),
        FileInput(
            name="credentials",
            display_name="Credentials",
            info="JSON credentials file. Leave empty to fallback to environment variables",
            file_types=["json"],
        ),
        StrInput(name="project_id", display_name="Project ID", value="researchdevelopment-424002"),
        StrInput(name="location", display_name="Location", value="europe-west1"),
        IntInput(name="max_tokens", display_name="Max Tokens", value=1024),
        FloatInput(name="temperature", value=0.0, display_name="Temperature"),
        FloatInput(name="top_p", display_name="Top P", value=0.95),
        BoolInput(name="verbose", display_name="Verbose", value=False, advanced=True),
    ]

    def build_model(self) -> LanguageModel:
        try:
            from anthropic import AnthropicVertex
            from langchain_anthropic import AnthropicLLM
        except ImportError:
            raise ImportError(
                "Please install the anthropic[vertex] package to use the Claude component."
            )

        if self.credentials:
            import json
            from google.oauth2 import service_account

            # Carregar as credenciais do arquivo JSON
            with open(self.credentials, 'r') as file:
                credentials_dict = json.load(file)

            # Criar credenciais a partir do objeto JSON
            credentials = service_account.Credentials.from_service_account_info(credentials_dict)
            project_id = self.project_id or credentials.project_id
        else:
            project_id = self.project_id
            credentials = None

        client = AnthropicVertex(
            region=self.location,
            project_id=project_id,
            credentials=credentials,
        )

        # Usar a chave privada do JSON para a configuração
        anthropic_api_key = self.api_key

        return cast(
            LanguageModel,
            AnthropicLLM(
                anthropic_api_key=anthropic_api_key,
                client=client,
                model_name="claude-3-5-sonnet@20240620",
                max_tokens_to_sample=self.max_tokens,
                temperature=self.temperature,
                top_p=self.top_p,
                verbose=self.verbose,
            ),
        )
ssbaraar commented 2 months ago

Hey @viniciossilva3 , thank you for that code , I have a doubt , in the api placeholder what value did you use is it the llm api key or the credentials.json ? Cause I only have the credentials.json I don't need any external api key

ssbaraar commented 2 months ago

I have shared the api end point json , how it exactly looks like , you can check there

ssbaraar commented 2 months ago

@viniciossilva3 i have tried out your code and it asks for the api key while dont have any api key instead i only have the google vertex ai sdk end point , which allows me to use multiple models

here is the error of your code for me ( as i dont have any anthropic api key )

Oops! Looks like you missed something
Custom Component is missing Anthropic API Key.

Iam trying to use the claude 3.5 sonnet model using my google vertex ai api end point which looks like this , if you have any idea how to load this model using this json , please help me

{
    "type": "service_account",
    "project_id": "researchdevelopment-412302",
    "private_key_id": "62a8e1237a12344c550aaa2cee7ff5b8490123ad",
    "private_key": "-----BEGIN PRIVATE KEY-----\n4nYAtxqW+w4/vA93pq/FlDYltKAODYHAYD4uSLBzngFngDnD6Y/Q+IW+izZMnySG\neFU3h3mBLvB/iU+9kteysdzcFLRVGOJxOS9cDFI5v38eQOn9iN4iFfCgu+SQoFml\nY9ugcxBiw4Ghfa5eFEZDG2tbO44HB/tvE95A394/9QKBgQDj0R073RBGImXm5F+W\n-----END PRIVATE KEY-----\n",
    "client_email": "123414412308-compute@developer.gserviceaccount.com",
    "client_id": "123462248247656962802",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://oauth2.googleapis.com/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/791234413808-compute%40developer.gserviceaccount.com",
    "universe_domain": "googleapis.com"
}
viniciossilva3 commented 2 months ago

Hi @ssbaraar, I'm still learning about components in Langflow. I was able to get the content generation working, but I haven't figured out how to output the model's response yet. If you'd like, I can share the code or you can download the component from the Langflow STORE (name: Chat Claude 3.5 Sonnet ONLY). I'll be sure to study the documentation and other components to improve the component's functionality in the future. I hope this can help you!

Flow Test

1 - Create a Google Cloud project 2 - Enable the VERTEX AI API 3 - Configure IAM Roles these roles include:Vertex AI User or Vertex AI Administrator, and Service Account User 4 - Enable Sonnet on Cloud : Claude 3.5 Sonnet 5 - Add the component in your FLOW 6 - Add the credentials file with your credentials 7 - Try this component.

from typing import cast, Any
from langflow.base.models.model import LCModelComponent
from langflow.field_typing import LanguageModel, Text, BaseChatModel
from langflow.inputs import MessageTextInput, FileInput, StrInput, IntInput, FloatInput, DropdownInput, BoolInput
from langflow.template import Output
from langflow.schema.message import Message
import json

class ChatClaudeComponent(LCModelComponent):
    display_name = "Chat Claude 3.5 Sonnet - Vertex SDK"
    description = "A custom model component that interacts with the Claude 3 API via Anthropic's Vertex SDK."
    documentation: str = "https://console.cloud.google.com/vertex-ai/publishers/anthropic/model-garden/claude-3-5-sonnet"
    icon = "bot"
    name = "ChatClaudeComponent"

    inputs = [
        MessageTextInput(name="input_value", display_name="Input"),
        MessageTextInput(name="system_message", display_name="System"),
        FileInput(
            name="credentials",
            display_name="Credentials",
            info="JSON credentials file. Leave empty to fallback to environment variables.",
            file_types=["json"],
        ),
        StrInput(
            name="input_model",
            display_name="Model Name",
            info="Input the Model Name",
            value="claude-3-5-sonnet@20240620",
        ),
        StrInput(name="location", display_name="Location", value="us-east5"),
        IntInput(name="max_tokens", display_name="Max Tokens", value=1024),
        FloatInput(name="temperature", value=0.0, display_name="Temperature"),
        FloatInput(name="top_p", display_name="Top P", value=0.95),
        BoolInput(name="stream", display_name="Stream", value=False, advanced=True),
        BoolInput(name="verbose", display_name="Verbose", value=False, advanced=True),
    ]

    outputs = [
        Output(display_name="Text", name="generated_text_output", method="build_model"),
        # Output(display_name="Model", name="model_output", method="get_model"),
    ]

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.generated_text: str = ""  # Explicitly defined as a string
        self.model = None

    def build_model(self)  -> Message:
        try:
            from google.cloud import aiplatform
            from google.oauth2 import service_account
            from anthropic import AnthropicVertex
        except ImportError as e:
            if "anthropic" in str(e):
                raise ImportError(
                    "The 'anthropic' library is not installed. Please install it with `python -m pip install -U 'anthropic[vertex]'`.",
                )
            elif "google" in str(e):
                raise ImportError(
                    "One or more Google Cloud libraries are not installed. Please verify if you have installed the 'google-cloud-aiplatform' package.",
                )

        user_input = self.input_value
        system_input = self.system_message
        model =  self.input_model
        region =  self.location
        max_tokens = self.max_tokens
        temperature =  self.temperature
        top_p =  self.top_p
        stream = self.stream

        # Load credentials from file if provided
        if self.credentials:
            # Create credentials from the JSON object
            credentials = service_account.Credentials.from_service_account_file(
                self.credentials,
                scopes=["https://www.googleapis.com/auth/cloud-platform"]
            )
            project_id = credentials.project_id
        else:
            raise ValueError("Credentials not provided.")

        # Instantiate the AnthropicVertex client
        client = AnthropicVertex(
            project_id=project_id,
            region=region,
            credentials=credentials
        )
        self.model = client

        # Sends a chat request to Claude 3.5 and returns the response message
        response = client.messages.create(
            system=system_input,
            max_tokens=max_tokens,
            messages=[
                {
                    "role": "user",
                    "content": user_input,
                }
            ],
            model=model,
            top_p=top_p,
            stream=stream,
            temperature=temperature
        )

        # Save the response
        response_to_json = json.loads(response.model_dump_json(indent=2))
        generated_text = response_to_json["content"][0]["text"]
        print(generated_text)

        self.generated_text = generated_text
        self.status = self.generated_text
        return self.generated_text

    def get_model(self) -> LanguageModel:
        return cast(LanguageModel, self.build_model())

    def process(self) -> dict:
        return {
            "generated_text_output": self.generated_text,
            # "model_output": self.model_output,
        }
ssbaraar commented 2 months ago

@viniciossilva3 hello ,

Appreciate the approach , im also learning to code some really great efficient components, thankyou for the code I'll understand the code and try to enhance it further 😀

viniciossilva3 commented 2 months ago

Hello @ssbaraar! I hope you can do what you want. If you need anything, you can use the Langflow helper on ChatGPT: Langflow Assistant If you want, you can close the topic, if you have any other questions you can ask on the Langflow community discord too.

ssbaraar commented 2 months ago

Yeah sure @viniciossilva3 , Thank you for the support 🙏🏻 can we connect on LinkedIn, if it's possible ?

viniciossilva3 commented 2 months ago

Hi @ssbaraar Sure, you can click the link in my github profile.

ssbaraar commented 2 months ago

Thank you @viniciossilva3 , Learned a lot , hoping to build some really great and complex flows and agents and leverage the use case of langflow 😀