langflow/src/backend/langflow/interface/tools/custom.py
Gabriel Luiz Freitas Almeida be09849081 🚀 feat(langflow): rename PythonFunction to PythonFunctionTool for better semantics
🚀 feat(langflow): add PythonFunctionToolNode to the frontend node tools
🚀 feat(langflow): add PythonFunctionTool to the custom tools
🚀 feat(langflow): add get_function to importing utils to get the function from code
🚀 feat(langflow): add func parameter to PythonFunctionTool to store the function
🚀 feat(langflow): add name and description parameters to PythonFunctionTool
🚀 feat(langflow): update instantiate_tool to use PythonFunctionTool instead of PythonFunction
🚀 feat(langflow): update constants to use PythonFunctionTool instead of PythonFunction
🚀 feat(langflow): update custom.py to use PythonFunctionTool instead of PythonFunction
🚀 feat(langflow): update loading.py to use get_function and PythonFunctionTool
🚀 feat(langflow): update utils.py to use get_function
🚀 feat(langflow): update test_custom_types.py to use get_function and PythonFunctionTool
🚀 feat(langflow): update test_graph.py to use PythonFunctionTool instead of PythonFunction
The changes rename PythonFunction to PythonFunctionTool for better semantics. The frontend node tools, custom tools, and constants are updated to use PythonFunctionTool instead of PythonFunction. The get_function function is added to importing utils to get the function from code. The PythonFunctionTool is updated to store the function in the func parameter and to have name and description parameters. The instantiate_tool, loading.py, and utils.py are updated to use get_function and PythonFunctionTool. The test_custom_types.py and test_graph.py are updated to use PythonFunctionTool instead of PythonFunction.
2023-05-31 15:41:16 -03:00

41 lines
1,005 B
Python

from typing import Optional
from langflow.interface.importing.utils import get_function
from pydantic import BaseModel, validator
from langflow.utils import validate
from langchain.agents.tools import Tool
class Function(BaseModel):
code: str
imports: Optional[str] = None
# Eval code and store the function
def __init__(self, **data):
super().__init__(**data)
# Validate the function
@validator("code")
def validate_func(cls, v):
try:
validate.eval_function(v)
except Exception as e:
raise e
return v
class PythonFunctionTool(Function, Tool):
"""Python function"""
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)