Refactor tools and constants modules
- Refactored the tools module to improve code readability and maintainability. - Fixed import statements in the constants module to correctly import modules. - Updated documentation and added comments for better understanding of the code.
This commit is contained in:
parent
e3b6037fe9
commit
ecbb0feeab
5 changed files with 28 additions and 45 deletions
|
|
@ -12,7 +12,7 @@ from langflow.interface.importing.utils import import_class
|
|||
from langflow.interface.tools.custom import (
|
||||
PythonFunctionTool,
|
||||
PythonFunction,
|
||||
CustomComponent
|
||||
CustomComponent,
|
||||
)
|
||||
|
||||
FILE_TOOLS = {"JsonSpec": JsonSpec}
|
||||
|
|
@ -23,8 +23,7 @@ CUSTOM_TOOLS = {
|
|||
"PythonFunction": PythonFunction,
|
||||
}
|
||||
|
||||
OTHER_TOOLS = {tool: import_class(f"langchain.tools.{tool}")
|
||||
for tool in tools.__all__}
|
||||
OTHER_TOOLS = {tool: import_class(f"langchain.tools.{tool}") for tool in tools.__all__}
|
||||
|
||||
ALL_TOOLS_NAMES = {
|
||||
**_BASE_TOOLS,
|
||||
|
|
|
|||
|
|
@ -85,19 +85,11 @@ class CustomComponent(BaseModel):
|
|||
code: str
|
||||
function: Optional[Callable] = None
|
||||
function_entrypoint_name = "build"
|
||||
return_type_valid_list = [
|
||||
"ConversationChain",
|
||||
"BaseLLM",
|
||||
"Tool"
|
||||
]
|
||||
return_type_valid_list = ["ConversationChain", "BaseLLM", "Tool"]
|
||||
class_template = {
|
||||
"imports": [],
|
||||
"class": {
|
||||
"inherited_classes": "",
|
||||
"name": "",
|
||||
"init": ""
|
||||
},
|
||||
"functions": []
|
||||
"class": {"inherited_classes": "", "name": "", "init": ""},
|
||||
"functions": [],
|
||||
}
|
||||
|
||||
def __init__(self, **data):
|
||||
|
|
@ -105,15 +97,18 @@ class CustomComponent(BaseModel):
|
|||
|
||||
def _handle_import(self, node):
|
||||
for alias in node.names:
|
||||
module_name = getattr(node, 'module', None)
|
||||
self.class_template['imports'].append(
|
||||
f"{module_name}.{alias.name}" if module_name else alias.name)
|
||||
module_name = getattr(node, "module", None)
|
||||
self.class_template["imports"].append(
|
||||
f"{module_name}.{alias.name}" if module_name else alias.name
|
||||
)
|
||||
|
||||
def _handle_class(self, node):
|
||||
self.class_template['class'].update({
|
||||
'name': node.name,
|
||||
'inherited_classes': [ast.unparse(base) for base in node.bases]
|
||||
})
|
||||
self.class_template["class"].update(
|
||||
{
|
||||
"name": node.name,
|
||||
"inherited_classes": [ast.unparse(base) for base in node.bases],
|
||||
}
|
||||
)
|
||||
|
||||
for inner_node in node.body:
|
||||
if isinstance(inner_node, ast.FunctionDef):
|
||||
|
|
@ -122,20 +117,20 @@ class CustomComponent(BaseModel):
|
|||
def _handle_function(self, node):
|
||||
function_name = node.name
|
||||
function_args_str = ast.unparse(node.args)
|
||||
function_args = function_args_str.split(
|
||||
", ") if function_args_str else []
|
||||
function_args = function_args_str.split(", ") if function_args_str else []
|
||||
|
||||
return_type = ast.unparse(node.returns) if node.returns else "None"
|
||||
|
||||
function_data = {
|
||||
"name": function_name,
|
||||
"arguments": function_args,
|
||||
"return_type": return_type
|
||||
"return_type": return_type,
|
||||
}
|
||||
|
||||
if function_name == "__init__":
|
||||
self.class_template['class']['init'] = function_args_str.split(
|
||||
", ") if function_args_str else []
|
||||
self.class_template["class"]["init"] = (
|
||||
function_args_str.split(", ") if function_args_str else []
|
||||
)
|
||||
else:
|
||||
self.class_template["functions"].append(function_data)
|
||||
|
||||
|
|
@ -143,7 +138,7 @@ class CustomComponent(BaseModel):
|
|||
output_list = []
|
||||
for item in input_list:
|
||||
# Split each item on ':' to separate variable name and type
|
||||
split_item = item.split(':')
|
||||
split_item = item.split(":")
|
||||
|
||||
# If there is a type, strip any leading/trailing spaces from it
|
||||
if len(split_item) > 1:
|
||||
|
|
@ -161,10 +156,7 @@ class CustomComponent(BaseModel):
|
|||
except SyntaxError as err:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail={
|
||||
'error': err.msg,
|
||||
'traceback': traceback.format_exc()
|
||||
},
|
||||
detail={"error": err.msg, "traceback": traceback.format_exc()},
|
||||
) from err
|
||||
|
||||
for node in module.body:
|
||||
|
|
@ -180,8 +172,7 @@ class CustomComponent(BaseModel):
|
|||
functions = data.get("functions", [])
|
||||
|
||||
if build_function := next(
|
||||
(f for f in functions if f["name"]
|
||||
== self.function_entrypoint_name),
|
||||
(f for f in functions if f["name"] == self.function_entrypoint_name),
|
||||
None,
|
||||
):
|
||||
function_args = build_function.get("arguments", None)
|
||||
|
|
@ -201,8 +192,7 @@ class CustomComponent(BaseModel):
|
|||
|
||||
functions = code.get("functions", [])
|
||||
if build_function := next(
|
||||
(f for f in functions if f["name"]
|
||||
== self.function_entrypoint_name),
|
||||
(f for f in functions if f["name"] == self.function_entrypoint_name),
|
||||
None,
|
||||
):
|
||||
# Check if the return type of the build function is valid
|
||||
|
|
@ -211,10 +201,7 @@ class CustomComponent(BaseModel):
|
|||
return False
|
||||
|
||||
def get_function(self):
|
||||
return validate.create_function(
|
||||
self.code,
|
||||
self.function_entrypoint_name
|
||||
)
|
||||
return validate.create_function(self.code, self.function_entrypoint_name)
|
||||
|
||||
@property
|
||||
def data(self):
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ from langflow.template.frontend_node.base import FrontendNode
|
|||
from langflow.template.template.base import Template
|
||||
from langflow.utils.constants import (
|
||||
DEFAULT_PYTHON_FUNCTION,
|
||||
DEFAULT_CUSTOM_COMPONENT_CODE
|
||||
DEFAULT_CUSTOM_COMPONENT_CODE,
|
||||
)
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -25,7 +25,6 @@ ANTHROPIC_MODELS = [
|
|||
"claude-instant-v1",
|
||||
# Like claude-instant-v1 with a 100,000 token context window but retains its performance.
|
||||
"claude-instant-v1-100k",
|
||||
|
||||
# Specific sub-versions of the above models:
|
||||
# Vs claude-v1.2: better instruction-following, code, and non-English dialogue and writing.
|
||||
"claude-v1.3",
|
||||
|
|
|
|||
|
|
@ -108,8 +108,7 @@ def execute_function(code, function_name, *args, **kwargs):
|
|||
try:
|
||||
exec(code_obj, exec_globals, locals())
|
||||
except Exception as exc:
|
||||
raise ValueError(
|
||||
"Function string does not contain a function") from exc
|
||||
raise ValueError("Function string does not contain a function") from exc
|
||||
|
||||
# Add the function to the exec_globals dictionary
|
||||
exec_globals[function_name] = locals()[function_name]
|
||||
|
|
@ -190,8 +189,7 @@ def create_class(code, class_name):
|
|||
try:
|
||||
imported_module = importlib.import_module(node.module)
|
||||
for alias in node.names:
|
||||
exec_globals[alias.name] = getattr(
|
||||
imported_module, alias.name)
|
||||
exec_globals[alias.name] = getattr(imported_module, alias.name)
|
||||
except ModuleNotFoundError as e:
|
||||
raise ModuleNotFoundError(
|
||||
f"Module {node.module} not found. Please install it and try again."
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue