Replies: 5 comments 6 replies
-
Yes, you can create your own Here is a relevant code snippet: from typing import Callable
from langflow.custom import CustomComponent
from langflow.custom.utils import get_function
from langflow.field_typing import Code
class PythonFunctionComponent(CustomComponent):
display_name = "Python Function"
description = "Define a Python function."
icon = "Python"
def build_config(self):
return {
"function_code": {
"display_name": "Code",
"info": "The code for the function.",
"show": True,
},
}
def build(self, function_code: Code) -> Callable:
self.status = function_code
func = get_function(function_code)
return func Additionally, LangFlow provides a more detailed implementation for creating custom Python functions using the from typing import Callable, Optional
from langflow.interface.importing.utils import get_function
from pydantic.v1 import BaseModel, validator
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):
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 = get_function(self.code)
super().__init__(name=name, description=description, func=self.func)
class PythonFunction(Function):
code: str This implementation allows you to define a Python function by providing the function code, which can then be validated and used by other components in LangFlow [1][2]. |
Beta Was this translation helpful? Give feedback.
-
what are langflow.field_types |
Beta Was this translation helpful? Give feedback.
-
can we create a tool which is a python function which will take add 2 numbers, in local and export it as a tool in langflow |
Beta Was this translation helpful? Give feedback.
-
create a tool which uses PythonFunction tool and provide code for this pythonFunction |
Beta Was this translation helpful? Give feedback.
-
can we import langflow PythonFunction directly and just provide code to it to make a new tool? |
Beta Was this translation helpful? Give feedback.
-
can we use the PythonFunction tool of LangFlow and create our own PythonFunction from our local directory and export it to langflow?
the pythonfunction could be called by other components
Beta Was this translation helpful? Give feedback.
All reactions