Skip to content

Commit

Permalink
fix use session_id
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoloboschi committed Aug 1, 2024
1 parent 75328f1 commit 3157947
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
17 changes: 10 additions & 7 deletions src/backend/base/langflow/api/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import uuid
import warnings
from typing import TYPE_CHECKING, Optional
from typing import TYPE_CHECKING, Optional, Dict

from fastapi import HTTPException
from sqlmodel import Session
Expand Down Expand Up @@ -122,12 +122,9 @@ def format_elapsed_time(elapsed_time: float) -> str:
return f"{minutes} {minutes_unit}, {seconds} {seconds_unit}"


async def build_graph_from_db_no_cache(flow_id: str, session: Session):
async def build_graph_from_data(flow_id: str, payload: Dict, **kwargs):
"""Build and cache the graph."""
flow: Optional[Flow] = session.get(Flow, flow_id)
if not flow or not flow.data:
raise ValueError("Invalid flow ID")
graph = Graph.from_payload(flow.data, flow_id, flow_name=flow.name, user_id=str(flow.user_id))
graph = Graph.from_payload(payload, flow_id, **kwargs)
for vertex_id in graph._has_session_id_vertices:
vertex = graph.get_vertex(vertex_id)
if vertex is None:
Expand All @@ -139,9 +136,15 @@ async def build_graph_from_db_no_cache(flow_id: str, session: Session):
graph.set_run_id(run_id)
graph.set_run_name()
await graph.initialize_run()

return graph

async def build_graph_from_db_no_cache(flow_id: str, session: Session):
"""Build and cache the graph."""
flow: Optional[Flow] = session.get(Flow, flow_id)
if not flow or not flow.data:
raise ValueError("Invalid flow ID")
return await build_graph_from_data(flow_id, flow.data, flow_name=flow.name, user_id=str(flow.user_id))

async def build_graph_from_db(flow_id: str, session: Session, chat_service: "ChatService"):
graph = await build_graph_from_db_no_cache(flow_id, session)
await chat_service.set_cache(flow_id, graph)
Expand Down
4 changes: 2 additions & 2 deletions src/backend/base/langflow/api/v1/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
format_elapsed_time,
format_exception_message,
get_top_level_vertices,
parse_exception, build_graph_from_db_no_cache,
parse_exception, build_graph_from_db_no_cache, build_graph_from_data,
)
from langflow.api.v1.schemas import (
FlowDataRequest,
Expand Down Expand Up @@ -170,7 +170,7 @@ async def build_graph_and_get_order() -> tuple[list[str], list[str], "Graph"]:
if not data:
graph = await build_graph_from_db_no_cache(flow_id=flow_id_str, session=session)
else:
graph = Graph.from_payload(data.model_dump(), flow_id_str)
graph = await build_graph_from_data(flow_id_str, data.model_dump())
graph.validate_stream()
if stop_component_id or start_component_id:
try:
Expand Down

0 comments on commit 3157947

Please sign in to comment.