chore: refactor and add components integration tests (#3607)

* improve inegration tests

* add fixes

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
Nicolò Boschi 2024-09-02 15:21:47 +02:00 • committed by GitHub
commit 96872f3aa5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
32 changed files with 523 additions and 131 deletions

View file

@ -263,7 +263,7 @@ class Component(CustomComponent):
for input_ in inputs:
if input_.name is None:
raise ValueError("Input name cannot be None.")
self._inputs[input_.name] = input_
self._inputs[input_.name] = deepcopy(input_)
def validate(self, params: dict):
"""
@ -496,6 +496,8 @@ class Component(CustomComponent):
#! works and then update this later
field_config = self.get_template_config(self)
frontend_node = ComponentFrontendNode.from_inputs(**field_config)
for key, value in self._inputs.items():
frontend_node.set_field_load_from_db_in_template(key, False)
self._map_parameters_on_frontend_node(frontend_node)
frontend_node_dict = frontend_node.to_dict(keep_name=False)
@ -532,7 +534,9 @@ class Component(CustomComponent):
"data": {
"node": frontend_node.to_dict(keep_name=False),
"type": self.name or self.__class__.__name__,
}
"id": self._id,
},
"id": self._id,
}
return data

View file

@ -202,33 +202,35 @@ class Graph:
self._edges = self._graph_data["edges"]
self.initialize()
def add_component(self, _id: str, component: "Component"):
if _id in self.vertex_map:
return
def add_component(self, component: "Component", component_id: Optional[str] = None) -> str:
component_id = component_id or component._id
if component_id in self.vertex_map:
return component_id
component._id = component_id
if component_id in self.vertex_map:
raise ValueError(f"Component ID {component_id} already exists")
frontend_node = component.to_frontend_node()
frontend_node["data"]["id"] = _id
frontend_node["id"] = _id
self._vertices.append(frontend_node)
vertex = self._create_vertex(frontend_node)
vertex.add_component_instance(component)
self.vertices.append(vertex)
self.vertex_map[_id] = vertex
self._add_vertex(vertex)
if component._edges:
for edge in component._edges:
self._add_edge(edge)
if component._components:
for _component in component._components:
self.add_component(_component._id, _component)
self.add_component(_component)
return component_id
def _set_start_and_end(self, start: "Component", end: "Component"):
if not hasattr(start, "to_frontend_node"):
raise TypeError(f"start must be a Component. Got {type(start)}")
if not hasattr(end, "to_frontend_node"):
raise TypeError(f"end must be a Component. Got {type(end)}")
self.add_component(start._id, start)
self.add_component(end._id, end)
self.add_component(start, start._id)
self.add_component(end, end._id)
def add_component_edge(self, source_id: str, output_input_tuple: tuple[str, str], target_id: str):
source_vertex = self.get_vertex(source_id)
@ -242,6 +244,18 @@ class Graph:
raise ValueError(f"Source vertex {source_id} does not have a custom component.")
if target_vertex._custom_component is None:
raise ValueError(f"Target vertex {target_id} does not have a custom component.")
try:
input_field = target_vertex.get_input(input_name)
input_types = input_field.input_types
input_field_type = str(input_field.field_type)
except ValueError:
input_field = target_vertex.data.get("node", {}).get("template", {}).get(input_name)
if not input_field:
raise ValueError(f"Input field {input_name} not found in target vertex {target_id}")
input_types = input_field.get("input_types", [])
input_field_type = input_field.get("type", "")
edge_data: EdgeData = {
"source": source_id,
"target": target_id,
@ -256,8 +270,8 @@ class Graph:
"targetHandle": {
"fieldName": input_name,
"id": target_vertex.id,
"inputTypes": target_vertex.get_input(input_name).input_types,
"type": str(target_vertex.get_input(input_name).field_type),
"inputTypes": input_types,
"type": input_field_type,
},
},
}
@ -1397,7 +1411,7 @@ class Graph:
tasks.append(task)
vertex_task_run_count[vertex_id] = vertex_task_run_count.get(vertex_id, 0) + 1
logger.debug(f"Running layer {layer_index} with {len(tasks)} tasks")
logger.debug(f"Running layer {layer_index} with {len(tasks)} tasks, {current_batch}")
try:
next_runnable_vertices = await self._execute_tasks(tasks, lock=lock)
except Exception as e:
@ -1463,6 +1477,8 @@ class Graph:
# This could usually happen with input vertices like ChatInput
self.run_manager.remove_vertex_from_runnables(v.id)
logger.debug(f"Vertex {v.id}, result: {v._built_result}, object: {v._built_object}")
for v in vertices:
next_runnable_vertices = await self.get_next_runnable_vertices(lock, vertex=v, cache=False)
results.extend(next_runnable_vertices)

View file

@ -112,8 +112,8 @@ def update_params_with_load_from_db_fields(
try:
key = custom_component.variables(params[field], field)
except ValueError as e:
# check if "User id is not set" is in the error message
if "User id is not set" in str(e) and not fallback_to_env_vars:
# check if "User id is not set" is in the error message, this is an internal bug
if "User id is not set" in str(e):
raise e
logger.debug(str(e))
if fallback_to_env_vars and key is None:

View file

@ -25,7 +25,6 @@ from langflow.initial_setup.setup import (
create_or_update_starter_projects,
initialize_super_user_if_needed,
load_flows_from_directory,
download_nltk_resources,
)
from langflow.interface.types import get_and_cache_all_types_dict
from langflow.interface.utils import setup_llm_caching
@ -182,7 +181,7 @@ def create_app():
FastAPIInstrumentor.instrument_app(app)
# Get necessary NLTK packages
download_nltk_resources()
# download_nltk_resources()
return app

View file

@ -184,3 +184,9 @@ class FrontendNode(BaseModel):
if field.name == field_name:
field.value = value
break
def set_field_load_from_db_in_template(self, field_name, value):
for field in self.template.fields:
if field.name == field_name and hasattr(field, "load_from_db"):
field.load_from_db = value
break