-
is there a way in which a python function can be sent as a tool to a custom component to perform a simple task. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 7 replies
-
Hey @SVyusti! 👋 I'm here to help you with any questions or issues you have. Let's tackle this together! To send a Python function as a tool to a custom component and create an input for a custom component that is provided by connecting another component to it, you can use the
Here is an example: from langflow.interface.importing.utils import get_function
from langflow.utils import validate
from langchain.agents.tools import Tool
class Function(BaseModel):
code: str
function: Optional[Callable] = None
imports: Optional[str] = None
def __init__(self, **data):
super().__init__(**data)
@validator("code")
def validate_func(cls, v):
try:
validate.eval_function(v)
except Exception as e:
raise e
return v
def get_function(self):
"""Get the function"""
function_name = validate.extract_function_name(self.code)
return validate.create_function(self.code, function_name)
class PythonFunctionTool(Function, Tool):
name: str = "Custom Tool"
description: str
code: str
def __init__(self, name: str, description: str, code: str):
self.name = name
self.description = description
self.code = code
self.func = self.get_function()
super().__init__(name=name, description=description, func=self.func)
# Example usage
function_code = """
def my_function(x):
return x * 2
"""
component = PythonFunctionTool(name="Multiplier", description="Multiplies input by 2", code=function_code)
# Now you can use the function
result = component.func(5)
print(result) # Output: 10 To create an input for a custom component that is provided by connecting another component to it, you would typically use the framework's capabilities to connect components and pass data between them. This part is specific to how Langflow handles component connections and data flow [1]. Langflow handles the connection and data flow between different components in the UI by using a combination of React hooks, state management, and component composition. Specifically, it uses the For more detailed examples and documentation on creating and connecting custom components, you can refer to the "docs/docs/administration/custom-component.mdx" file in the Langflow repository [3]. |
Beta Was this translation helpful? Give feedback.
-
how can we call this function in another component? |
Beta Was this translation helpful? Give feedback.
-
second component requires first component as an input , how to modify the first component to pass the function |
Beta Was this translation helpful? Give feedback.
-
both should be langflow custom component |
Beta Was this translation helpful? Give feedback.
To modify two Langflow custom components so that the first component can pass a function to the second component, you can follow these steps:
Here is an example of how you can achieve this:
First Component (FunctionProviderComponent)