🚀 feat(base.py): add default values for string and prompt type parameters in _build_params method
The unused imports have been removed to improve code cleanliness. The code in the _build_params method has been simplified by removing unnecessary conditions and loops. Default values are now added for string and prompt type parameters to ensure that they have a value even if not explicitly set.
🔧 fix(base.py): remove unused imports and simplify code in _build_params method
This commit is contained in:
parent
966444a88b
commit
cfcdb67ed9
1 changed files with 15 additions and 43 deletions
|
|
@ -1,14 +1,11 @@
|
||||||
from langflow.utils.constants import DIRECT_TYPES
|
|
||||||
from langflow.interface.initialize import loading
|
from langflow.interface.initialize import loading
|
||||||
from langflow.interface.listing import ALL_TYPES_DICT
|
from langflow.interface.listing import ALL_TYPES_DICT
|
||||||
from langflow.utils.logger import logger
|
from langflow.utils.logger import logger
|
||||||
from langflow.utils.util import sync_to_async
|
from langflow.utils.util import sync_to_async
|
||||||
|
|
||||||
|
|
||||||
import contextlib
|
|
||||||
import inspect
|
import inspect
|
||||||
import types
|
import types
|
||||||
import warnings
|
|
||||||
from typing import Any, Dict, List, Optional
|
from typing import Any, Dict, List, Optional
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
|
|
@ -69,6 +66,7 @@ class Vertex:
|
||||||
break
|
break
|
||||||
|
|
||||||
def _build_params(self):
|
def _build_params(self):
|
||||||
|
# sourcery skip: merge-list-append, remove-redundant-if
|
||||||
# Some params are required, some are optional
|
# Some params are required, some are optional
|
||||||
# but most importantly, some params are python base classes
|
# but most importantly, some params are python base classes
|
||||||
# like str and others are LangChain objects like LLMChain, BasePromptTemplate
|
# like str and others are LangChain objects like LLMChain, BasePromptTemplate
|
||||||
|
|
@ -89,8 +87,19 @@ class Vertex:
|
||||||
if isinstance(value, dict)
|
if isinstance(value, dict)
|
||||||
}
|
}
|
||||||
params = {}
|
params = {}
|
||||||
|
|
||||||
|
for edge in self.edges:
|
||||||
|
param_key = edge.target_param
|
||||||
|
if param_key in template_dict:
|
||||||
|
if template_dict[param_key]["list"]:
|
||||||
|
if param_key not in params:
|
||||||
|
params[param_key] = []
|
||||||
|
params[param_key].append(edge.source)
|
||||||
|
else:
|
||||||
|
params[param_key] = edge.source
|
||||||
|
|
||||||
for key, value in template_dict.items():
|
for key, value in template_dict.items():
|
||||||
if key == "_type":
|
if key == "_type" or not value.get("show"):
|
||||||
continue
|
continue
|
||||||
# If the type is not transformable to a python base class
|
# If the type is not transformable to a python base class
|
||||||
# then we need to get the edge that connects to this node
|
# then we need to get the edge that connects to this node
|
||||||
|
|
@ -101,45 +110,8 @@ class Vertex:
|
||||||
file_path = value.get("file_path")
|
file_path = value.get("file_path")
|
||||||
|
|
||||||
params[key] = file_path
|
params[key] = file_path
|
||||||
|
elif value.get("type") in ["str", "prompt"] and params.get(key) is None:
|
||||||
elif value.get("type") not in DIRECT_TYPES:
|
params[key] = value.get("value")
|
||||||
# Get the edge that connects to this node
|
|
||||||
edges = [
|
|
||||||
edge
|
|
||||||
for edge in self.edges
|
|
||||||
if edge.target == self and edge.matched_type in value["type"]
|
|
||||||
]
|
|
||||||
|
|
||||||
# Get the output of the node that the edge connects to
|
|
||||||
# if the value['list'] is True, then there will be more
|
|
||||||
# than one time setting to params[key]
|
|
||||||
# so we need to append to a list if it exists
|
|
||||||
# or create a new list if it doesn't
|
|
||||||
|
|
||||||
if value["required"] and not edges:
|
|
||||||
# If a required parameter is not found, raise an error
|
|
||||||
raise ValueError(
|
|
||||||
f"Required input {key} for module {self.vertex_type} not found"
|
|
||||||
)
|
|
||||||
elif value["list"]:
|
|
||||||
# If this is a list parameter, append all sources to a list
|
|
||||||
params[key] = [edge.source for edge in edges]
|
|
||||||
elif edges:
|
|
||||||
# If a single parameter is found, use its source
|
|
||||||
params[key] = edges[0].source
|
|
||||||
|
|
||||||
elif value["required"] or value.get("value"):
|
|
||||||
# If value does not have value this still passes
|
|
||||||
# but then gives a keyError
|
|
||||||
# so we need to check if value has value
|
|
||||||
new_value = value.get("value")
|
|
||||||
if new_value is None:
|
|
||||||
warnings.warn(f"Value for {key} in {self.vertex_type} is None. ")
|
|
||||||
if value.get("type") == "int":
|
|
||||||
with contextlib.suppress(TypeError, ValueError):
|
|
||||||
new_value = int(new_value) # type: ignore
|
|
||||||
params[key] = new_value
|
|
||||||
|
|
||||||
# Add _type to params
|
# Add _type to params
|
||||||
self.params = params
|
self.params = params
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue