Move ResultData to graph
This commit is contained in:
parent
b7e52f62be
commit
9beadd70f1
2 changed files with 30 additions and 17 deletions
|
|
@ -3,9 +3,7 @@ from pathlib import Path
|
||||||
from typing import TYPE_CHECKING, List, Optional
|
from typing import TYPE_CHECKING, List, Optional
|
||||||
|
|
||||||
from fastapi import HTTPException
|
from fastapi import HTTPException
|
||||||
from langchain_core.documents import Document
|
|
||||||
from platformdirs import user_cache_dir
|
from platformdirs import user_cache_dir
|
||||||
from pydantic import BaseModel
|
|
||||||
from sqlmodel import Session
|
from sqlmodel import Session
|
||||||
|
|
||||||
from langflow.graph.graph.base import Graph
|
from langflow.graph.graph.base import Graph
|
||||||
|
|
@ -217,20 +215,6 @@ def format_elapsed_time(elapsed_time: float) -> str:
|
||||||
return f"{minutes} {minutes_unit}, {seconds} {seconds_unit}"
|
return f"{minutes} {minutes_unit}, {seconds} {seconds_unit}"
|
||||||
|
|
||||||
|
|
||||||
def serialize_field(value):
|
|
||||||
"""Unified serialization function for handling both BaseModel and Document types,
|
|
||||||
including handling lists of these types."""
|
|
||||||
if isinstance(value, (list, tuple)):
|
|
||||||
return [serialize_field(v) for v in value]
|
|
||||||
elif isinstance(value, Document):
|
|
||||||
return value.to_json()
|
|
||||||
elif isinstance(value, BaseModel):
|
|
||||||
return value.model_dump()
|
|
||||||
elif isinstance(value, str):
|
|
||||||
return {"result": value}
|
|
||||||
return value
|
|
||||||
|
|
||||||
|
|
||||||
def build_and_cache_graph(
|
def build_and_cache_graph(
|
||||||
flow_id: str,
|
flow_id: str,
|
||||||
session: Session,
|
session: Session,
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,37 @@
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
from typing import Any, Optional
|
||||||
|
from langflow.graph.utils import serialize_field
|
||||||
|
|
||||||
|
from pydantic import BaseModel, Field, field_serializer
|
||||||
|
|
||||||
|
|
||||||
class InterfaceComponentTypes(Enum):
|
class ResultData(BaseModel):
|
||||||
|
results: Optional[Any] = Field(default_factory=dict)
|
||||||
|
artifacts: Optional[Any] = Field(default_factory=dict)
|
||||||
|
timedelta: Optional[float] = None
|
||||||
|
duration: Optional[str] = None
|
||||||
|
|
||||||
|
@field_serializer("results")
|
||||||
|
def serialize_results(self, value):
|
||||||
|
if isinstance(value, dict):
|
||||||
|
return {key: serialize_field(val) for key, val in value.items()}
|
||||||
|
return serialize_field(value)
|
||||||
|
|
||||||
|
|
||||||
|
class InterfaceComponentTypes(str, Enum):
|
||||||
# ChatInput and ChatOutput are the only ones that are
|
# ChatInput and ChatOutput are the only ones that are
|
||||||
# power components
|
# power components
|
||||||
ChatInput = "ChatInput"
|
ChatInput = "ChatInput"
|
||||||
ChatOutput = "ChatOutput"
|
ChatOutput = "ChatOutput"
|
||||||
|
TextInput = "TextInput"
|
||||||
|
TextOutput = "TextOutput"
|
||||||
|
|
||||||
|
|
||||||
|
INPUT_COMPONENTS = [
|
||||||
|
InterfaceComponentTypes.ChatInput,
|
||||||
|
InterfaceComponentTypes.TextInput,
|
||||||
|
]
|
||||||
|
OUTPUT_COMPONENTS = [
|
||||||
|
InterfaceComponentTypes.ChatOutput,
|
||||||
|
InterfaceComponentTypes.TextOutput,
|
||||||
|
]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue