Refactor code and add new functions
This commit is contained in:
parent
ba79225b9c
commit
c24771c79b
1 changed files with 35 additions and 15 deletions
|
|
@ -3,9 +3,11 @@ from pathlib import Path
|
||||||
from typing import TYPE_CHECKING, List
|
from typing import TYPE_CHECKING, List
|
||||||
|
|
||||||
from fastapi import HTTPException
|
from fastapi import HTTPException
|
||||||
from langflow.services.store.schema import StoreComponentCreate
|
from langchain_core.documents import Document
|
||||||
from langflow.services.store.utils import get_lf_version_from_pypi
|
|
||||||
from platformdirs import user_cache_dir
|
from platformdirs import user_cache_dir
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
from langflow.services.store.schema import StoreComponentCreate
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from langflow.services.database.models.flow.model import Flow
|
from langflow.services.database.models.flow.model import Flow
|
||||||
|
|
@ -170,19 +172,37 @@ async def check_langflow_version(component: StoreComponentCreate):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def format_elapsed_time(elapsed_time) -> str:
|
def format_elapsed_time(elapsed_time: float) -> str:
|
||||||
# Format elapsed time to human readable format coming from
|
"""Format elapsed time to a human-readable format coming from perf_counter().
|
||||||
# perf_counter()
|
|
||||||
# If the elapsed time is less than 1 second, return ms
|
- Less than 1 second: returns milliseconds
|
||||||
# If the elapsed time is less than 1 minute, return seconds rounded to 2 decimals
|
- Less than 1 minute: returns seconds rounded to 2 decimals
|
||||||
time_str = ""
|
- 1 minute or more: returns minutes and seconds
|
||||||
|
"""
|
||||||
if elapsed_time < 1:
|
if elapsed_time < 1:
|
||||||
elapsed_time = int(round(elapsed_time * 1000))
|
milliseconds = int(round(elapsed_time * 1000))
|
||||||
time_str = f"{elapsed_time} ms"
|
return f"{milliseconds} ms"
|
||||||
elif elapsed_time < 60:
|
elif elapsed_time < 60:
|
||||||
elapsed_time = round(elapsed_time, 2)
|
seconds = round(elapsed_time, 2)
|
||||||
time_str = f"{elapsed_time} seconds"
|
unit = "second" if seconds == 1 else "seconds"
|
||||||
|
return f"{seconds} {unit}"
|
||||||
else:
|
else:
|
||||||
elapsed_time = round(elapsed_time / 60, 2)
|
minutes = int(elapsed_time // 60)
|
||||||
time_str = f"{elapsed_time} minutes"
|
seconds = round(elapsed_time % 60, 2)
|
||||||
return time_str
|
minutes_unit = "minute" if minutes == 1 else "minutes"
|
||||||
|
seconds_unit = "second" if seconds == 1 else "seconds"
|
||||||
|
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
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue