Refactor graph building and caching
This commit is contained in:
parent
aadb4c7d87
commit
5cef5b868a
1 changed files with 54 additions and 32 deletions
|
|
@ -22,7 +22,6 @@ from langflow.api.v1.schemas import (
|
||||||
VerticesOrderResponse,
|
VerticesOrderResponse,
|
||||||
)
|
)
|
||||||
from langflow.graph.graph.base import Graph
|
from langflow.graph.graph.base import Graph
|
||||||
from langflow.graph.vertex.base import StatelessVertex
|
|
||||||
from langflow.processing.process import process_tweaks_on_graph
|
from langflow.processing.process import process_tweaks_on_graph
|
||||||
from langflow.services.auth.utils import (
|
from langflow.services.auth.utils import (
|
||||||
get_current_active_user,
|
get_current_active_user,
|
||||||
|
|
@ -275,6 +274,25 @@ async def try_running_celery_task(vertex, user_id):
|
||||||
return vertex
|
return vertex
|
||||||
|
|
||||||
|
|
||||||
|
def build_and_cache_graph(
|
||||||
|
flow_id: str,
|
||||||
|
session: Session,
|
||||||
|
chat_service: "ChatService",
|
||||||
|
graph: Optional[Graph] = None,
|
||||||
|
):
|
||||||
|
"""Build and cache the graph."""
|
||||||
|
flow: Flow = session.get(Flow, flow_id)
|
||||||
|
if not flow or not flow.data:
|
||||||
|
raise ValueError("Invalid flow ID")
|
||||||
|
other_graph = Graph.from_payload(flow.data)
|
||||||
|
if graph is None:
|
||||||
|
graph = other_graph
|
||||||
|
else:
|
||||||
|
graph = graph.update(other_graph)
|
||||||
|
chat_service.set_cache(flow_id, graph)
|
||||||
|
return graph
|
||||||
|
|
||||||
|
|
||||||
@router.get("/build/{flow_id}/vertices", response_model=VerticesOrderResponse)
|
@router.get("/build/{flow_id}/vertices", response_model=VerticesOrderResponse)
|
||||||
async def get_vertices(
|
async def get_vertices(
|
||||||
flow_id: str,
|
flow_id: str,
|
||||||
|
|
@ -288,17 +306,7 @@ async def get_vertices(
|
||||||
graph = None
|
graph = None
|
||||||
if cache := chat_service.get_cache(flow_id):
|
if cache := chat_service.get_cache(flow_id):
|
||||||
graph: Graph = cache.get("result")
|
graph: Graph = cache.get("result")
|
||||||
|
graph = build_and_cache_graph(flow_id, session, chat_service, graph)
|
||||||
flow: Flow = session.get(Flow, flow_id)
|
|
||||||
if not flow or not flow.data:
|
|
||||||
raise ValueError("Invalid flow ID")
|
|
||||||
other_graph = Graph.from_payload(flow.data)
|
|
||||||
if graph is None:
|
|
||||||
graph = other_graph
|
|
||||||
else:
|
|
||||||
graph = graph.update(other_graph)
|
|
||||||
chat_service.set_cache(flow_id, graph)
|
|
||||||
|
|
||||||
if component_id:
|
if component_id:
|
||||||
try:
|
try:
|
||||||
vertices = graph.sort_up_to_vertex(component_id)
|
vertices = graph.sort_up_to_vertex(component_id)
|
||||||
|
|
@ -332,34 +340,48 @@ async def build_vertex(
|
||||||
"""Build a vertex instead of the entire graph."""
|
"""Build a vertex instead of the entire graph."""
|
||||||
try:
|
try:
|
||||||
cache = chat_service.get_cache(flow_id)
|
cache = chat_service.get_cache(flow_id)
|
||||||
graph = cache.get("result")
|
if not cache:
|
||||||
|
# If there's no cache
|
||||||
|
logger.warning(
|
||||||
|
f"No cache found for {flow_id}. Building graph starting at {vertex_id}"
|
||||||
|
)
|
||||||
|
graph = build_and_cache_graph(
|
||||||
|
flow_id=flow_id, session=get_session(), chat_service=chat_service
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
graph = cache.get("result")
|
||||||
result_dict = {}
|
result_dict = {}
|
||||||
duration = ""
|
duration = ""
|
||||||
start_time = time.perf_counter()
|
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 isinstance(graph, Graph):
|
|
||||||
raise ValueError("Invalid graph")
|
|
||||||
if not (vertex := graph.get_vertex(vertex_id)):
|
if not (vertex := graph.get_vertex(vertex_id)):
|
||||||
raise ValueError("Invalid vertex")
|
raise ValueError(f"Invalid vertex {vertex_id}")
|
||||||
try:
|
try:
|
||||||
if isinstance(vertex, StatelessVertex) or not vertex._built:
|
if not vertex.pinned or not vertex._built:
|
||||||
await vertex.build(user_id=current_user.id)
|
await vertex.build(user_id=current_user.id)
|
||||||
params = vertex._built_object_repr()
|
params = vertex._built_object_repr()
|
||||||
valid = True
|
valid = True
|
||||||
result_dict = vertex.get_built_result()
|
result_dict = vertex.get_built_result()
|
||||||
# We need to set the artifacts to pass information
|
# We need to set the artifacts to pass information
|
||||||
# to the frontend
|
# to the frontend
|
||||||
vertex.set_artifacts()
|
vertex.set_artifacts()
|
||||||
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(
|
result_dict = ResultDict(
|
||||||
results=result_dict,
|
results=result_dict,
|
||||||
artifacts=artifacts,
|
artifacts=artifacts,
|
||||||
duration=duration,
|
duration=duration,
|
||||||
timedelta=timedelta,
|
timedelta=timedelta,
|
||||||
)
|
)
|
||||||
|
vertex.set_result(result_dict)
|
||||||
|
elif vertex.result is not None:
|
||||||
|
params = vertex._built_object_repr()
|
||||||
|
valid = True
|
||||||
|
result_dict = vertex.result
|
||||||
|
else:
|
||||||
|
raise ValueError(f"No result found for vertex {vertex_id}")
|
||||||
chat_service.set_cache(flow_id, graph)
|
chat_service.set_cache(flow_id, graph)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
params = str(exc)
|
params = str(exc)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue