Merge branch 'cz/promise-each' of github.com:logspace-ai/langflow into cz/promise-each

This commit is contained in:
cristhianzl 2024-02-20 19:53:04 -03:00
commit 11092c5955
5 changed files with 38 additions and 24 deletions

View file

@ -20,7 +20,7 @@ from langflow.api.v1.schemas import (
BuildStatus, BuildStatus,
BuiltResponse, BuiltResponse,
InitResponse, InitResponse,
ResultDict, ResultData,
StreamData, StreamData,
VertexBuildResponse, VertexBuildResponse,
VerticesOrderResponse, VerticesOrderResponse,
@ -322,6 +322,7 @@ async def build_vertex(
inputs: dict = Body(None), inputs: dict = Body(None),
): ):
"""Build a vertex instead of the entire graph.""" """Build a vertex instead of the entire graph."""
start_time = time.perf_counter()
try: try:
cache = chat_service.get_cache(flow_id) cache = chat_service.get_cache(flow_id)
if not cache: if not cache:
@ -336,7 +337,6 @@ async def build_vertex(
graph = cache.get("result") graph = cache.get("result")
result_dict = {} result_dict = {}
duration = "" duration = ""
start_time = time.perf_counter()
if tweaks: if tweaks:
graph = process_tweaks_on_graph(graph, tweaks) graph = process_tweaks_on_graph(graph, tweaks)
if not (vertex := graph.get_vertex(vertex_id)): if not (vertex := graph.get_vertex(vertex_id)):
@ -351,13 +351,9 @@ async def build_vertex(
# to the frontend # to the frontend
vertex.set_artifacts() vertex.set_artifacts()
artifacts = vertex.artifacts artifacts = vertex.artifacts
timedelta = time.perf_counter() - start_time result_dict = ResultData(
duration = format_elapsed_time(timedelta)
result_dict = ResultDict(
results=result_dict, results=result_dict,
artifacts=artifacts, artifacts=artifacts,
duration=duration,
timedelta=timedelta,
) )
vertex.set_result(result_dict) vertex.set_result(result_dict)
elif vertex.result is not None: elif vertex.result is not None:
@ -370,7 +366,7 @@ async def build_vertex(
except Exception as exc: except Exception as exc:
params = str(exc) params = str(exc)
valid = False valid = False
result_dict = ResultDict(results={}) result_dict = ResultData(results={})
artifacts = {} artifacts = {}
# If there's an error building the vertex # If there's an error building the vertex
# we need to clear the cache # we need to clear the cache
@ -383,6 +379,12 @@ async def build_vertex(
data=result_dict, data=result_dict,
artifacts=artifacts, artifacts=artifacts,
) )
timedelta = time.perf_counter() - start_time
duration = format_elapsed_time(timedelta)
result_dict.duration = duration
result_dict.timedelta = timedelta
return VertexBuildResponse( return VertexBuildResponse(
valid=valid, valid=valid,
params=params, params=params,

View file

@ -222,7 +222,7 @@ class VerticesOrderResponse(BaseModel):
ids: List[List[str]] ids: List[List[str]]
class ResultDict(BaseModel): class ResultData(BaseModel):
results: Optional[Any] = Field(default_factory=dict) results: Optional[Any] = Field(default_factory=dict)
artifacts: Optional[Any] = Field(default_factory=dict) artifacts: Optional[Any] = Field(default_factory=dict)
timedelta: Optional[float] = None timedelta: Optional[float] = None
@ -240,7 +240,7 @@ class VertexBuildResponse(BaseModel):
valid: bool valid: bool
params: Optional[str] params: Optional[str]
"""JSON string of the params.""" """JSON string of the params."""
data: ResultDict data: ResultData
"""Mapping of vertex ids to result dict containing the param name and result value.""" """Mapping of vertex ids to result dict containing the param name and result value."""
timestamp: Optional[datetime] = Field(default_factory=datetime.utcnow) timestamp: Optional[datetime] = Field(default_factory=datetime.utcnow)
"""Timestamp of the build.""" """Timestamp of the build."""

View file

@ -14,7 +14,7 @@ from langflow.utils.util import sync_to_async
from loguru import logger from loguru import logger
if TYPE_CHECKING: if TYPE_CHECKING:
from langflow.api.v1.schemas import ResultDict from langflow.api.v1.schemas import ResultData
from langflow.graph.edge.base import ContractEdge from langflow.graph.edge.base import ContractEdge
from langflow.graph.graph.base import Graph from langflow.graph.graph.base import Graph
@ -50,7 +50,7 @@ class Vertex:
self.parent_is_top_level = False self.parent_is_top_level = False
self.layer = None self.layer = None
self.should_run = True self.should_run = True
self.result: Optional["ResultDict"] = None self.result: Optional["ResultData"] = None
try: try:
self.is_interface_component = InterfaceComponentTypes(self.vertex_type) self.is_interface_component = InterfaceComponentTypes(self.vertex_type)
except ValueError: except ValueError:
@ -81,7 +81,7 @@ class Vertex:
) )
return edge_results return edge_results
def set_result(self, result: "ResultDict") -> None: def set_result(self, result: "ResultData") -> None:
self.result = result self.result = result
def get_built_result(self): def get_built_result(self):

View file

@ -1,13 +1,12 @@
from typing import TYPE_CHECKING, Any, Dict, Optional, Type from typing import TYPE_CHECKING, Any, Dict, Optional, Type
import duckdb import duckdb
from langflow.services.deps import get_monitor_service
from loguru import logger from loguru import logger
from pydantic import BaseModel from pydantic import BaseModel
from langflow.services.deps import get_monitor_service
if TYPE_CHECKING: if TYPE_CHECKING:
from langflow.api.v1.schemas import ResultDict from langflow.api.v1.schemas import ResultData
INDEX_KEY = "index" INDEX_KEY = "index"
@ -44,7 +43,9 @@ def model_to_sql_column_definitions(model: Type[BaseModel]) -> dict:
return columns return columns
def drop_and_create_table_if_schema_mismatch(db_path: str, table_name: str, model: Type[BaseModel]): def drop_and_create_table_if_schema_mismatch(
db_path: str, table_name: str, model: Type[BaseModel]
):
with duckdb.connect(db_path) as conn: with duckdb.connect(db_path) as conn:
# Get the current schema from the database # Get the current schema from the database
try: try:
@ -64,8 +65,12 @@ def drop_and_create_table_if_schema_mismatch(db_path: str, table_name: str, mode
conn.execute(f"CREATE SEQUENCE seq_{table_name} START 1;") conn.execute(f"CREATE SEQUENCE seq_{table_name} START 1;")
except duckdb.CatalogException: except duckdb.CatalogException:
pass pass
desired_schema[INDEX_KEY] = f"INTEGER PRIMARY KEY DEFAULT NEXTVAL('seq_{table_name}')" desired_schema[INDEX_KEY] = (
columns_sql = ", ".join(f"{name} {data_type}" for name, data_type in desired_schema.items()) f"INTEGER PRIMARY KEY DEFAULT NEXTVAL('seq_{table_name}')"
)
columns_sql = ", ".join(
f"{name} {data_type}" for name, data_type in desired_schema.items()
)
create_table_sql = f"CREATE TABLE {table_name} ({columns_sql})" create_table_sql = f"CREATE TABLE {table_name} ({columns_sql})"
conn.execute(create_table_sql) conn.execute(create_table_sql)
@ -133,7 +138,7 @@ async def log_vertex_build(
vertex_id: str, vertex_id: str,
valid: bool, valid: bool,
params: Any, params: Any,
data: "ResultDict", data: "ResultData",
artifacts: Optional[dict] = None, artifacts: Optional[dict] = None,
): ):
try: try:

View file

@ -3,7 +3,7 @@ from typing import Callable
import socketio import socketio
from langflow.api.utils import format_elapsed_time from langflow.api.utils import format_elapsed_time
from langflow.api.v1.schemas import ResultDict, VertexBuildResponse from langflow.api.v1.schemas import ResultData, VertexBuildResponse
from langflow.graph.graph.base import Graph from langflow.graph.graph.base import Graph
from langflow.graph.vertex.base import StatelessVertex from langflow.graph.vertex.base import StatelessVertex
from langflow.services.database.models.flow.model import Flow from langflow.services.database.models.flow.model import Flow
@ -73,11 +73,16 @@ async def build_vertex(
artifacts = vertex.artifacts artifacts = vertex.artifacts
timedelta = time.perf_counter() - start_time timedelta = time.perf_counter() - start_time
duration = format_elapsed_time(timedelta) duration = format_elapsed_time(timedelta)
result_dict = ResultDict(results=result_dict, artifacts=artifacts, duration=duration, timedelta=timedelta) result_dict = ResultData(
results=result_dict,
artifacts=artifacts,
duration=duration,
timedelta=timedelta,
)
except Exception as exc: except Exception as exc:
params = str(exc) params = str(exc)
valid = False valid = False
result_dict = ResultDict(results={}) result_dict = ResultData(results={})
artifacts = {} artifacts = {}
set_cache(flow_id, graph) set_cache(flow_id, graph)
await log_vertex_build( await log_vertex_build(
@ -90,7 +95,9 @@ async def build_vertex(
) )
# Emit the vertex build response # Emit the vertex build response
response = VertexBuildResponse(valid=valid, params=params, id=vertex.id, data=result_dict) response = VertexBuildResponse(
valid=valid, params=params, id=vertex.id, data=result_dict
)
await sio.emit("vertex_build", data=response.model_dump(), to=sid) await sio.emit("vertex_build", data=response.model_dump(), to=sid)
except Exception as exc: except Exception as exc: