ref: Add some postponed annotations evaluations (#3990)

Add some postponed annotations evaluations
This commit is contained in:
Christophe Bornet 2024-10-02 16:27:11 +02:00 • committed by GitHub
commit 426719607b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
38 changed files with 176 additions and 101 deletions

View file

@ -1,3 +1,5 @@
from __future__ import annotations
import io import io
import json import json
import re import re
@ -123,7 +125,7 @@ def read_flows(
*, *,
current_user: User = Depends(get_current_active_user), current_user: User = Depends(get_current_active_user),
session: Session = Depends(get_session), session: Session = Depends(get_session),
settings_service: "SettingsService" = Depends(get_settings_service), settings_service: SettingsService = Depends(get_settings_service),
remove_example_flows: bool = False, remove_example_flows: bool = False,
components_only: bool = False, components_only: bool = False,
): ):
@ -184,7 +186,7 @@ def read_flow(
session: Session = Depends(get_session), session: Session = Depends(get_session),
flow_id: UUID, flow_id: UUID,
current_user: User = Depends(get_current_active_user), current_user: User = Depends(get_current_active_user),
settings_service: "SettingsService" = Depends(get_settings_service), settings_service: SettingsService = Depends(get_settings_service),
): ):
"""Read a flow.""" """Read a flow."""
auth_settings = settings_service.auth_settings auth_settings = settings_service.auth_settings

View file

@ -1,3 +1,5 @@
from __future__ import annotations
from fastapi import APIRouter, Depends, HTTPException, Request, Response, status from fastapi import APIRouter, Depends, HTTPException, Request, Response, status
from fastapi.security import OAuth2PasswordRequestForm from fastapi.security import OAuth2PasswordRequestForm
from sqlmodel import Session from sqlmodel import Session
@ -127,7 +129,7 @@ async def auto_login(
async def refresh_token( async def refresh_token(
request: Request, request: Request,
response: Response, response: Response,
settings_service: "SettingsService" = Depends(get_settings_service), settings_service: SettingsService = Depends(get_settings_service),
db: Session = Depends(get_session), db: Session = Depends(get_session),
): ):
auth_settings = settings_service.auth_settings auth_settings = settings_service.auth_settings

View file

@ -1,3 +1,5 @@
from __future__ import annotations
import warnings import warnings
from typing import Any from typing import Any
@ -18,7 +20,7 @@ class FlowTool(BaseTool):
graph: Graph | None = None graph: Graph | None = None
flow_id: str | None = None flow_id: str | None = None
user_id: str | None = None user_id: str | None = None
inputs: list["Vertex"] = [] inputs: list[Vertex] = []
get_final_results_only: bool = True get_final_results_only: bool = True
@property @property

View file

@ -1,3 +1,5 @@
from __future__ import annotations
from typing import TYPE_CHECKING, Any, cast from typing import TYPE_CHECKING, Any, cast
from loguru import logger from loguru import logger
@ -10,7 +12,7 @@ if TYPE_CHECKING:
class Edge: class Edge:
def __init__(self, source: "Vertex", target: "Vertex", edge: EdgeData): def __init__(self, source: Vertex, target: Vertex, edge: EdgeData):
self.source_id: str = source.id if source else "" self.source_id: str = source.id if source else ""
self.target_id: str = target.id if target else "" self.target_id: str = target.id if target else ""
self.valid_handles: bool = False self.valid_handles: bool = False
@ -207,13 +209,13 @@ class Edge:
class CycleEdge(Edge): class CycleEdge(Edge):
def __init__(self, source: "Vertex", target: "Vertex", raw_edge: EdgeData): def __init__(self, source: Vertex, target: Vertex, raw_edge: EdgeData):
super().__init__(source, target, raw_edge) super().__init__(source, target, raw_edge)
self.is_fulfilled = False # Whether the contract has been fulfilled. self.is_fulfilled = False # Whether the contract has been fulfilled.
self.result: Any = None self.result: Any = None
self.is_cycle = True self.is_cycle = True
async def honor(self, source: "Vertex", target: "Vertex") -> None: async def honor(self, source: Vertex, target: Vertex) -> None:
""" """
Fulfills the contract by setting the result of the source vertex to the target vertex's parameter. Fulfills the contract by setting the result of the source vertex to the target vertex's parameter.
If the edge is runnable, the source vertex is run with the message text and the target vertex's If the edge is runnable, the source vertex is run with the message text and the target vertex's
@ -238,7 +240,7 @@ class CycleEdge(Edge):
target.params[self.target_param] = self.result target.params[self.target_param] = self.result
self.is_fulfilled = True self.is_fulfilled = True
async def get_result_from_source(self, source: "Vertex", target: "Vertex"): async def get_result_from_source(self, source: Vertex, target: Vertex):
# Fulfill the contract if it has not been fulfilled. # Fulfill the contract if it has not been fulfilled.
if not self.is_fulfilled: if not self.is_fulfilled:
await self.honor(source, target) await self.honor(source, target)

View file

@ -1,3 +1,5 @@
from __future__ import annotations
from typing import TYPE_CHECKING, NamedTuple, Protocol from typing import TYPE_CHECKING, NamedTuple, Protocol
from typing_extensions import NotRequired, TypedDict from typing_extensions import NotRequired, TypedDict
@ -32,11 +34,11 @@ class GraphDump(TypedDict, total=False):
class VertexBuildResult(NamedTuple): class VertexBuildResult(NamedTuple):
result_dict: "ResultData" result_dict: ResultData
params: str params: str
valid: bool valid: bool
artifacts: dict artifacts: dict
vertex: "Vertex" vertex: Vertex
class OutputConfigDict(TypedDict): class OutputConfigDict(TypedDict):

View file

@ -1,7 +1,9 @@
from __future__ import annotations
import json import json
from collections.abc import Generator from collections.abc import Generator
from enum import Enum from enum import Enum
from typing import TYPE_CHECKING, Any, Optional from typing import TYPE_CHECKING, Any
from uuid import UUID from uuid import UUID
from langchain_core.documents import Document from langchain_core.documents import Document
@ -118,7 +120,7 @@ def post_process_raw(raw, artifact_type: str):
return raw return raw
def _vertex_to_primitive_dict(target: "Vertex") -> dict: def _vertex_to_primitive_dict(target: Vertex) -> dict:
""" """
Cleans the parameters of the target vertex. Cleans the parameters of the target vertex.
""" """
@ -134,7 +136,7 @@ def _vertex_to_primitive_dict(target: "Vertex") -> dict:
async def log_transaction( async def log_transaction(
flow_id: str | UUID, source: "Vertex", status, target: Optional["Vertex"] = None, error=None flow_id: str | UUID, source: Vertex, status, target: Vertex | None = None, error=None
) -> None: ) -> None:
try: try:
if not get_settings_service().settings.transactions_storage_enabled: if not get_settings_service().settings.transactions_storage_enabled:
@ -162,7 +164,7 @@ def log_vertex_build(
vertex_id: str, vertex_id: str,
valid: bool, valid: bool,
params: Any, params: Any,
data: "ResultDataResponse", data: ResultDataResponse,
artifacts: dict | None = None, artifacts: dict | None = None,
): ):
try: try:

View file

@ -1,3 +1,5 @@
from __future__ import annotations
import ast import ast
import asyncio import asyncio
import inspect import inspect
@ -6,7 +8,7 @@ import traceback
import types import types
from collections.abc import AsyncIterator, Callable, Iterator, Mapping from collections.abc import AsyncIterator, Callable, Iterator, Mapping
from enum import Enum from enum import Enum
from typing import TYPE_CHECKING, Any, Optional from typing import TYPE_CHECKING, Any
import pandas as pd import pandas as pd
from loguru import logger from loguru import logger
@ -46,7 +48,7 @@ class Vertex:
def __init__( def __init__(
self, self,
data: NodeData, data: NodeData,
graph: "Graph", graph: Graph,
base_type: str | None = None, base_type: str | None = None,
is_task: bool = False, is_task: bool = False,
params: dict | None = None, params: dict | None = None,
@ -108,7 +110,7 @@ class Vertex:
def to_data(self): def to_data(self):
return self._data return self._data
def add_component_instance(self, component_instance: "Component"): def add_component_instance(self, component_instance: Component):
component_instance.set_vertex(self) component_instance.set_vertex(self)
self._custom_component = component_instance self._custom_component = component_instance
@ -165,15 +167,15 @@ class Vertex:
pass pass
@property @property
def edges(self) -> list["CycleEdge"]: def edges(self) -> list[CycleEdge]:
return self.graph.get_vertex_edges(self.id) return self.graph.get_vertex_edges(self.id)
@property @property
def outgoing_edges(self) -> list["CycleEdge"]: def outgoing_edges(self) -> list[CycleEdge]:
return [edge for edge in self.edges if edge.source_id == self.id] return [edge for edge in self.edges if edge.source_id == self.id]
@property @property
def incoming_edges(self) -> list["CycleEdge"]: def incoming_edges(self) -> list[CycleEdge]:
return [edge for edge in self.edges if edge.target_id == self.id] return [edge for edge in self.edges if edge.target_id == self.id]
@property @property
@ -181,11 +183,11 @@ class Vertex:
return {edge.source_handle.name for edge in self.edges} return {edge.source_handle.name for edge in self.edges}
@property @property
def predecessors(self) -> list["Vertex"]: def predecessors(self) -> list[Vertex]:
return self.graph.get_predecessors(self) return self.graph.get_predecessors(self)
@property @property
def successors(self) -> list["Vertex"]: def successors(self) -> list[Vertex]:
return self.graph.get_successors(self) return self.graph.get_successors(self)
@property @property
@ -268,7 +270,7 @@ class Vertex:
return AsyncResult(self.task_id) return AsyncResult(self.task_id)
def _set_params_from_normal_edge(self, params: dict, edge: "Edge", template_dict: dict): def _set_params_from_normal_edge(self, params: dict, edge: Edge, template_dict: dict):
param_key = edge.target_param param_key = edge.target_param
# If the param_key is in the template_dict and the edge.target_id is the current node # If the param_key is in the template_dict and the edge.target_id is the current node
@ -570,7 +572,7 @@ class Vertex:
async def _build_dict_and_update_params( async def _build_dict_and_update_params(
self, self,
key, key,
vertices_dict: dict[str, "Vertex"], vertices_dict: dict[str, Vertex],
): ):
""" """
Iterates over a dictionary of vertices, builds each and updates the params dictionary. Iterates over a dictionary of vertices, builds each and updates the params dictionary.
@ -594,7 +596,7 @@ class Vertex:
""" """
return all(self._is_vertex(vertex) for vertex in value) return all(self._is_vertex(vertex) for vertex in value)
async def get_result(self, requester: "Vertex", target_handle_name: str | None = None) -> Any: async def get_result(self, requester: Vertex, target_handle_name: str | None = None) -> Any:
""" """
Retrieves the result of the vertex. Retrieves the result of the vertex.
@ -606,7 +608,7 @@ class Vertex:
async with self._lock: async with self._lock:
return await self._get_result(requester, target_handle_name) return await self._get_result(requester, target_handle_name)
async def _get_result(self, requester: "Vertex", target_handle_name: str | None = None) -> Any: async def _get_result(self, requester: Vertex, target_handle_name: str | None = None) -> Any:
""" """
Retrieves the result of the built component. Retrieves the result of the built component.
@ -627,7 +629,7 @@ class Vertex:
asyncio.create_task(log_transaction(str(flow_id), source=self, target=requester, status="success")) asyncio.create_task(log_transaction(str(flow_id), source=self, target=requester, status="success"))
return result return result
async def _build_vertex_and_update_params(self, key, vertex: "Vertex"): async def _build_vertex_and_update_params(self, key, vertex: Vertex):
""" """
Builds a given vertex and updates the params dictionary accordingly. Builds a given vertex and updates the params dictionary accordingly.
""" """
@ -641,7 +643,7 @@ class Vertex:
async def _build_list_of_vertices_and_update_params( async def _build_list_of_vertices_and_update_params(
self, self,
key, key,
vertices: list["Vertex"], vertices: list[Vertex],
): ):
""" """
Iterates over a list of vertices, builds each and updates the params dictionary. Iterates over a list of vertices, builds each and updates the params dictionary.
@ -711,7 +713,7 @@ class Vertex:
msg = f"Error building Component {self.display_name}: \n\n{exc}" msg = f"Error building Component {self.display_name}: \n\n{exc}"
raise ComponentBuildException(msg, tb) from exc raise ComponentBuildException(msg, tb) from exc
def _update_built_object_and_artifacts(self, result: Any | tuple[Any, dict] | tuple["Component", Any, dict]): def _update_built_object_and_artifacts(self, result: Any | tuple[Any, dict] | tuple[Component, Any, dict]):
""" """
Updates the built object and its artifacts. Updates the built object and its artifacts.
""" """
@ -769,7 +771,7 @@ class Vertex:
user_id=None, user_id=None,
inputs: dict[str, Any] | None = None, inputs: dict[str, Any] | None = None,
files: list[str] | None = None, files: list[str] | None = None,
requester: Optional["Vertex"] = None, requester: Vertex | None = None,
event_manager: EventManager | None = None, event_manager: EventManager | None = None,
**kwargs, **kwargs,
) -> Any: ) -> Any:
@ -809,7 +811,7 @@ class Vertex:
return await self.get_requester_result(requester) return await self.get_requester_result(requester)
async def get_requester_result(self, requester: Optional["Vertex"]): async def get_requester_result(self, requester: Vertex | None):
# If the requester is None, this means that # If the requester is None, this means that
# the Vertex is the root of the graph # the Vertex is the root of the graph
if requester is None: if requester is None:
@ -824,7 +826,7 @@ class Vertex:
else await requester_edge.get_result_from_source(source=self, target=requester) else await requester_edge.get_result_from_source(source=self, target=requester)
) )
def add_edge(self, edge: "CycleEdge") -> None: def add_edge(self, edge: CycleEdge) -> None:
if edge not in self.edges: if edge not in self.edges:
self.edges.append(edge) self.edges.append(edge)

View file

@ -1,3 +1,5 @@
from __future__ import annotations
import asyncio import asyncio
import contextlib import contextlib
import json import json
@ -75,7 +77,7 @@ class ComponentVertex(Vertex):
for key, value in self._built_object.items(): for key, value in self._built_object.items():
self.add_result(key, value) self.add_result(key, value)
def get_edge_with_target(self, target_id: str) -> Generator["CycleEdge", None, None]: def get_edge_with_target(self, target_id: str) -> Generator[CycleEdge, None, None]:
""" """
Get the edge with the target id. Get the edge with the target id.
@ -89,7 +91,7 @@ class ComponentVertex(Vertex):
if edge.target_id == target_id: if edge.target_id == target_id:
yield edge yield edge
async def _get_result(self, requester: "Vertex", target_handle_name: str | None = None) -> Any: async def _get_result(self, requester: Vertex, target_handle_name: str | None = None) -> Any:
""" """
Retrieves the result of the built component. Retrieves the result of the built component.

View file

@ -1,10 +1,12 @@
from __future__ import annotations
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
if TYPE_CHECKING: if TYPE_CHECKING:
from langflow.graph.vertex.base import Vertex from langflow.graph.vertex.base import Vertex
def build_clean_params(target: "Vertex") -> dict: def build_clean_params(target: Vertex) -> dict:
""" """
Cleans the parameters of the target vertex. Cleans the parameters of the target vertex.
""" """

View file

@ -1,3 +1,5 @@
from __future__ import annotations
from collections.abc import Awaitable, Callable from collections.abc import Awaitable, Callable
from typing import TYPE_CHECKING, Any, cast from typing import TYPE_CHECKING, Any, cast
from uuid import UUID from uuid import UUID
@ -42,7 +44,7 @@ def list_flows(*, user_id: str | None = None) -> list[Data]:
async def load_flow( async def load_flow(
user_id: str, flow_id: str | None = None, flow_name: str | None = None, tweaks: dict | None = None user_id: str, flow_id: str | None = None, flow_name: str | None = None, tweaks: dict | None = None
) -> "Graph": ) -> Graph:
from langflow.graph.graph.base import Graph from langflow.graph.graph.base import Graph
from langflow.processing.process import process_tweaks from langflow.processing.process import process_tweaks
@ -120,7 +122,7 @@ async def run_flow(
def generate_function_for_flow( def generate_function_for_flow(
inputs: list["Vertex"], flow_id: str, user_id: str | UUID | None inputs: list[Vertex], flow_id: str, user_id: str | UUID | None
) -> Callable[..., Awaitable[Any]]: ) -> Callable[..., Awaitable[Any]]:
""" """
Generate a dynamic flow function based on the given inputs and flow ID. Generate a dynamic flow function based on the given inputs and flow ID.
@ -196,7 +198,7 @@ async def flow_function({func_args}):
def build_function_and_schema( def build_function_and_schema(
flow_data: Data, graph: "Graph", user_id: str | UUID | None flow_data: Data, graph: Graph, user_id: str | UUID | None
) -> tuple[Callable[..., Awaitable[Any]], type[BaseModel]]: ) -> tuple[Callable[..., Awaitable[Any]], type[BaseModel]]:
""" """
Builds a dynamic function and schema for a given flow. Builds a dynamic function and schema for a given flow.
@ -215,7 +217,7 @@ def build_function_and_schema(
return dynamic_flow_function, schema return dynamic_flow_function, schema
def get_flow_inputs(graph: "Graph") -> list["Vertex"]: def get_flow_inputs(graph: Graph) -> list[Vertex]:
""" """
Retrieves the flow inputs from the given graph. Retrieves the flow inputs from the given graph.
@ -232,7 +234,7 @@ def get_flow_inputs(graph: "Graph") -> list["Vertex"]:
return inputs return inputs
def build_schema_from_inputs(name: str, inputs: list["Vertex"]) -> type[BaseModel]: def build_schema_from_inputs(name: str, inputs: list[Vertex]) -> type[BaseModel]:
""" """
Builds a schema from the given inputs. Builds a schema from the given inputs.
@ -253,7 +255,7 @@ def build_schema_from_inputs(name: str, inputs: list["Vertex"]) -> type[BaseMode
return create_model(name, **fields) # type: ignore return create_model(name, **fields) # type: ignore
def get_arg_names(inputs: list["Vertex"]) -> list[dict[str, str]]: def get_arg_names(inputs: list[Vertex]) -> list[dict[str, str]]:
""" """
Returns a list of dictionaries containing the component name and its corresponding argument name. Returns a list of dictionaries containing the component name and its corresponding argument name.

View file

@ -1,3 +1,5 @@
from __future__ import annotations
import asyncio import asyncio
import json import json
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
@ -59,8 +61,8 @@ all_types_dict_cache = None
async def get_and_cache_all_types_dict( async def get_and_cache_all_types_dict(
settings_service: "SettingsService", settings_service: SettingsService,
cache_service: "CacheService", cache_service: CacheService,
force_refresh: bool = False, force_refresh: bool = False,
lock: asyncio.Lock | None = None, lock: asyncio.Lock | None = None,
): ):

View file

@ -1,4 +1,6 @@
from typing import TYPE_CHECKING, Union from __future__ import annotations
from typing import TYPE_CHECKING
from langchain_core.callbacks import BaseCallbackHandler from langchain_core.callbacks import BaseCallbackHandler
from loguru import logger from loguru import logger
@ -34,7 +36,7 @@ def get_langfuse_callback(trace_id):
return None return None
def flush_langfuse_callback_if_present(callbacks: list[Union[BaseCallbackHandler, "CallbackHandler"]]): def flush_langfuse_callback_if_present(callbacks: list[BaseCallbackHandler | CallbackHandler]):
""" """
If langfuse callback is present, run callback.langfuse.flush() If langfuse callback is present, run callback.langfuse.flush()
""" """

View file

@ -1,4 +1,6 @@
from typing import TYPE_CHECKING, Any, Union, cast from __future__ import annotations
from typing import TYPE_CHECKING, Any, cast
from loguru import logger from loguru import logger
from pydantic import BaseModel from pydantic import BaseModel
@ -20,11 +22,11 @@ class Result(BaseModel):
async def run_graph_internal( async def run_graph_internal(
graph: "Graph", graph: Graph,
flow_id: str, flow_id: str,
stream: bool = False, stream: bool = False,
session_id: str | None = None, session_id: str | None = None,
inputs: list["InputValueRequest"] | None = None, inputs: list[InputValueRequest] | None = None,
outputs: list[str] | None = None, outputs: list[str] | None = None,
) -> tuple[list[RunOutputs], str]: ) -> tuple[list[RunOutputs], str]:
"""Run the graph and generate the result""" """Run the graph and generate the result"""
@ -56,7 +58,7 @@ async def run_graph_internal(
def run_graph( def run_graph(
graph: "Graph", graph: Graph,
input_value: str, input_value: str,
input_type: str, input_type: str,
output_type: str, output_type: str,
@ -109,7 +111,7 @@ def run_graph(
def validate_input( def validate_input(
graph_data: dict[str, Any], tweaks: Union["Tweaks", dict[str, str | dict[str, Any]]] graph_data: dict[str, Any], tweaks: Tweaks | dict[str, str | dict[str, Any]]
) -> list[dict[str, Any]]: ) -> list[dict[str, Any]]:
if not isinstance(graph_data, dict) or not isinstance(tweaks, dict): if not isinstance(graph_data, dict) or not isinstance(tweaks, dict):
msg = "graph_data and tweaks should be dictionaries" msg = "graph_data and tweaks should be dictionaries"
@ -151,7 +153,7 @@ def apply_tweaks_on_vertex(vertex: Vertex, node_tweaks: dict[str, Any]) -> None:
def process_tweaks( def process_tweaks(
graph_data: dict[str, Any], tweaks: Union["Tweaks", dict[str, dict[str, Any]]], stream: bool = False graph_data: dict[str, Any], tweaks: Tweaks | dict[str, dict[str, Any]], stream: bool = False
) -> dict[str, Any]: ) -> dict[str, Any]:
""" """
This function is used to tweak the graph data using the node id and the tweaks dict. This function is used to tweak the graph data using the node id and the tweaks dict.

View file

@ -1,3 +1,5 @@
from __future__ import annotations
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from langflow.services.base import Service from langflow.services.base import Service
@ -9,5 +11,5 @@ if TYPE_CHECKING:
class AuthService(Service): class AuthService(Service):
name = "auth_service" name = "auth_service"
def __init__(self, settings_service: "SettingsService"): def __init__(self, settings_service: SettingsService):
self.settings_service = settings_service self.settings_service = settings_service

View file

@ -1,3 +1,5 @@
from __future__ import annotations
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from langflow.logging.logger import logger from langflow.logging.logger import logger
@ -13,7 +15,7 @@ class CacheServiceFactory(ServiceFactory):
def __init__(self): def __init__(self):
super().__init__(CacheService) super().__init__(CacheService)
def create(self, settings_service: "SettingsService"): def create(self, settings_service: SettingsService):
# Here you would have logic to create and configure a CacheService # Here you would have logic to create and configure a CacheService
# based on the settings_service # based on the settings_service

View file

@ -1,3 +1,5 @@
from __future__ import annotations
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from langflow.services.database.service import DatabaseService from langflow.services.database.service import DatabaseService
@ -11,7 +13,7 @@ class DatabaseServiceFactory(ServiceFactory):
def __init__(self): def __init__(self):
super().__init__(DatabaseService) super().__init__(DatabaseService)
def create(self, settings_service: "SettingsService"): def create(self, settings_service: SettingsService):
# Here you would have logic to create and configure a DatabaseService # Here you would have logic to create and configure a DatabaseService
if not settings_service.settings.database_url: if not settings_service.settings.database_url:
msg = "No database URL provided" msg = "No database URL provided"

View file

@ -1,3 +1,5 @@
from __future__ import annotations
import time import time
from contextlib import contextmanager from contextlib import contextmanager
from datetime import datetime from datetime import datetime
@ -32,7 +34,7 @@ if TYPE_CHECKING:
class DatabaseService(Service): class DatabaseService(Service):
name = "database_service" name = "database_service"
def __init__(self, settings_service: "SettingsService"): def __init__(self, settings_service: SettingsService):
self.settings_service = settings_service self.settings_service = settings_service
if settings_service.settings.database_url is None: if settings_service.settings.database_url is None:
msg = "No database URL provided" msg = "No database URL provided"
@ -45,7 +47,7 @@ class DatabaseService(Service):
self.alembic_cfg_path = langflow_dir / "alembic.ini" self.alembic_cfg_path = langflow_dir / "alembic.ini"
self.engine = self._create_engine() self.engine = self._create_engine()
def _create_engine(self) -> "Engine": def _create_engine(self) -> Engine:
"""Create the engine for the database.""" """Create the engine for the database."""
if self.settings_service.settings.database_url and self.settings_service.settings.database_url.startswith( if self.settings_service.settings.database_url and self.settings_service.settings.database_url.startswith(
"sqlite" "sqlite"

View file

@ -1,3 +1,5 @@
from __future__ import annotations
from collections.abc import Generator from collections.abc import Generator
from contextlib import contextmanager from contextlib import contextmanager
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
@ -45,7 +47,7 @@ def get_service(service_type: ServiceType, default=None):
return service_manager.get(service_type, default) # type: ignore return service_manager.get(service_type, default) # type: ignore
def get_telemetry_service() -> "TelemetryService": def get_telemetry_service() -> TelemetryService:
""" """
Retrieves the TelemetryService instance from the service manager. Retrieves the TelemetryService instance from the service manager.
@ -57,7 +59,7 @@ def get_telemetry_service() -> "TelemetryService":
return get_service(ServiceType.TELEMETRY_SERVICE, TelemetryServiceFactory()) # type: ignore return get_service(ServiceType.TELEMETRY_SERVICE, TelemetryServiceFactory()) # type: ignore
def get_tracing_service() -> "TracingService": def get_tracing_service() -> TracingService:
""" """
Retrieves the TracingService instance from the service manager. Retrieves the TracingService instance from the service manager.
@ -69,7 +71,7 @@ def get_tracing_service() -> "TracingService":
return get_service(ServiceType.TRACING_SERVICE, TracingServiceFactory()) # type: ignore return get_service(ServiceType.TRACING_SERVICE, TracingServiceFactory()) # type: ignore
def get_state_service() -> "StateService": def get_state_service() -> StateService:
""" """
Retrieves the StateService instance from the service manager. Retrieves the StateService instance from the service manager.
@ -81,7 +83,7 @@ def get_state_service() -> "StateService":
return get_service(ServiceType.STATE_SERVICE, StateServiceFactory()) # type: ignore return get_service(ServiceType.STATE_SERVICE, StateServiceFactory()) # type: ignore
def get_socket_service() -> "SocketIOService": def get_socket_service() -> SocketIOService:
""" """
Get the SocketIOService instance from the service manager. Get the SocketIOService instance from the service manager.
@ -91,7 +93,7 @@ def get_socket_service() -> "SocketIOService":
return get_service(ServiceType.SOCKETIO_SERVICE) # type: ignore return get_service(ServiceType.SOCKETIO_SERVICE) # type: ignore
def get_storage_service() -> "StorageService": def get_storage_service() -> StorageService:
""" """
Retrieves the storage service instance. Retrieves the storage service instance.
@ -103,7 +105,7 @@ def get_storage_service() -> "StorageService":
return get_service(ServiceType.STORAGE_SERVICE, default=StorageServiceFactory()) # type: ignore return get_service(ServiceType.STORAGE_SERVICE, default=StorageServiceFactory()) # type: ignore
def get_variable_service() -> "VariableService": def get_variable_service() -> VariableService:
""" """
Retrieves the VariableService instance from the service manager. Retrieves the VariableService instance from the service manager.
@ -116,7 +118,7 @@ def get_variable_service() -> "VariableService":
return get_service(ServiceType.VARIABLE_SERVICE, VariableServiceFactory()) # type: ignore return get_service(ServiceType.VARIABLE_SERVICE, VariableServiceFactory()) # type: ignore
def get_plugins_service() -> "PluginService": def get_plugins_service() -> PluginService:
""" """
Get the PluginService instance from the service manager. Get the PluginService instance from the service manager.
@ -126,7 +128,7 @@ def get_plugins_service() -> "PluginService":
return get_service(ServiceType.PLUGIN_SERVICE) # type: ignore return get_service(ServiceType.PLUGIN_SERVICE) # type: ignore
def get_settings_service() -> "SettingsService": def get_settings_service() -> SettingsService:
""" """
Retrieves the SettingsService instance. Retrieves the SettingsService instance.
@ -143,7 +145,7 @@ def get_settings_service() -> "SettingsService":
return get_service(ServiceType.SETTINGS_SERVICE, SettingsServiceFactory()) # type: ignore return get_service(ServiceType.SETTINGS_SERVICE, SettingsServiceFactory()) # type: ignore
def get_db_service() -> "DatabaseService": def get_db_service() -> DatabaseService:
""" """
Retrieves the DatabaseService instance from the service manager. Retrieves the DatabaseService instance from the service manager.
@ -156,7 +158,7 @@ def get_db_service() -> "DatabaseService":
return get_service(ServiceType.DATABASE_SERVICE, DatabaseServiceFactory()) # type: ignore return get_service(ServiceType.DATABASE_SERVICE, DatabaseServiceFactory()) # type: ignore
def get_session() -> Generator["Session", None, None]: def get_session() -> Generator[Session, None, None]:
""" """
Retrieves a session from the database service. Retrieves a session from the database service.
@ -169,7 +171,7 @@ def get_session() -> Generator["Session", None, None]:
@contextmanager @contextmanager
def session_scope() -> Generator["Session", None, None]: def session_scope() -> Generator[Session, None, None]:
""" """
Context manager for managing a session scope. Context manager for managing a session scope.
@ -195,7 +197,7 @@ def session_scope() -> Generator["Session", None, None]:
raise raise
def get_cache_service() -> "CacheService": def get_cache_service() -> CacheService:
""" """
Retrieves the cache service from the service manager. Retrieves the cache service from the service manager.
@ -207,7 +209,7 @@ def get_cache_service() -> "CacheService":
return get_service(ServiceType.CACHE_SERVICE, CacheServiceFactory()) # type: ignore return get_service(ServiceType.CACHE_SERVICE, CacheServiceFactory()) # type: ignore
def get_session_service() -> "SessionService": def get_session_service() -> SessionService:
""" """
Retrieves the session service from the service manager. Retrieves the session service from the service manager.
@ -219,7 +221,7 @@ def get_session_service() -> "SessionService":
return get_service(ServiceType.SESSION_SERVICE, SessionServiceFactory()) # type: ignore return get_service(ServiceType.SESSION_SERVICE, SessionServiceFactory()) # type: ignore
def get_task_service() -> "TaskService": def get_task_service() -> TaskService:
""" """
Retrieves the TaskService instance from the service manager. Retrieves the TaskService instance from the service manager.
@ -232,7 +234,7 @@ def get_task_service() -> "TaskService":
return get_service(ServiceType.TASK_SERVICE, TaskServiceFactory()) # type: ignore return get_service(ServiceType.TASK_SERVICE, TaskServiceFactory()) # type: ignore
def get_chat_service() -> "ChatService": def get_chat_service() -> ChatService:
""" """
Get the chat service instance. Get the chat service instance.
@ -242,7 +244,7 @@ def get_chat_service() -> "ChatService":
return get_service(ServiceType.CHAT_SERVICE) # type: ignore return get_service(ServiceType.CHAT_SERVICE) # type: ignore
def get_store_service() -> "StoreService": def get_store_service() -> StoreService:
""" """
Retrieves the StoreService instance from the service manager. Retrieves the StoreService instance from the service manager.

View file

@ -1,3 +1,5 @@
from __future__ import annotations
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from langflow.services.factory import ServiceFactory from langflow.services.factory import ServiceFactory
@ -11,5 +13,5 @@ class PluginServiceFactory(ServiceFactory):
def __init__(self): def __init__(self):
super().__init__(PluginService) super().__init__(PluginService)
def create(self, settings_service: "SettingsService"): def create(self, settings_service: SettingsService):
return PluginService(settings_service) return PluginService(settings_service)

View file

@ -1,4 +1,6 @@
from typing import TYPE_CHECKING, Optional from __future__ import annotations
from typing import TYPE_CHECKING
from loguru import logger from loguru import logger
@ -10,7 +12,7 @@ if TYPE_CHECKING:
class LangfuseInstance: class LangfuseInstance:
_instance: Optional["Langfuse"] = None _instance: Langfuse | None = None
@classmethod @classmethod
def get(cls): def get(cls):

View file

@ -1,3 +1,5 @@
from __future__ import annotations
import importlib import importlib
import inspect import inspect
import os import os
@ -15,7 +17,7 @@ if TYPE_CHECKING:
class PluginService(Service): class PluginService(Service):
name = "plugin_service" name = "plugin_service"
def __init__(self, settings_service: "SettingsService"): def __init__(self, settings_service: SettingsService):
self.plugins: dict[str, BasePlugin] = {} self.plugins: dict[str, BasePlugin] = {}
# plugin_dir = settings_service.settings.PLUGIN_DIR # plugin_dir = settings_service.settings.PLUGIN_DIR
self.plugin_dir = os.path.dirname(__file__) self.plugin_dir = os.path.dirname(__file__)

View file

@ -1,3 +1,5 @@
from __future__ import annotations
import os import os
import yaml import yaml
@ -17,7 +19,7 @@ class SettingsService(Service):
self.auth_settings = auth_settings self.auth_settings = auth_settings
@classmethod @classmethod
def load_settings_from_yaml(cls, file_path: str) -> "SettingsService": def load_settings_from_yaml(cls, file_path: str) -> SettingsService:
# Check if a string is a valid path or a file name # Check if a string is a valid path or a file name
if "/" not in file_path: if "/" not in file_path:
# Get current path # Get current path

View file

@ -1,3 +1,5 @@
from __future__ import annotations
from langflow.services.base import Service from langflow.services.base import Service
from langflow.services.settings.auth import AuthSettings from langflow.services.settings.auth import AuthSettings
from langflow.services.settings.base import Settings from langflow.services.settings.base import Settings
@ -12,7 +14,7 @@ class SettingsService(Service):
self.auth_settings: AuthSettings = auth_settings self.auth_settings: AuthSettings = auth_settings
@classmethod @classmethod
def initialize(cls) -> "SettingsService": def initialize(cls) -> SettingsService:
# Check if a string is a valid path or a file name # Check if a string is a valid path or a file name
settings = Settings() settings = Settings()

View file

@ -1,3 +1,5 @@
from __future__ import annotations
from abc import abstractmethod from abc import abstractmethod
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
@ -11,7 +13,7 @@ if TYPE_CHECKING:
class StorageService(Service): class StorageService(Service):
name = "storage_service" name = "storage_service"
def __init__(self, session_service: "SessionService", settings_service: "SettingsService"): def __init__(self, session_service: SessionService, settings_service: SettingsService):
self.settings_service = settings_service self.settings_service = settings_service
self.session_service = session_service self.session_service = session_service
self.set_ready() self.set_ready()

View file

@ -1,3 +1,5 @@
from __future__ import annotations
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from langflow.services.factory import ServiceFactory from langflow.services.factory import ServiceFactory
@ -11,5 +13,5 @@ class StoreServiceFactory(ServiceFactory):
def __init__(self): def __init__(self):
super().__init__(StoreService) super().__init__(StoreService)
def create(self, settings_service: "SettingsService"): def create(self, settings_service: SettingsService):
return StoreService(settings_service) return StoreService(settings_service)

View file

@ -1,3 +1,5 @@
from __future__ import annotations
import json import json
from typing import TYPE_CHECKING, Any from typing import TYPE_CHECKING, Any
from uuid import UUID from uuid import UUID
@ -31,7 +33,7 @@ user_data_var: ContextVar[dict[str, Any] | None] = ContextVar("user_data", defau
@asynccontextmanager @asynccontextmanager
async def user_data_context(store_service: "StoreService", api_key: str | None = None): async def user_data_context(store_service: StoreService, api_key: str | None = None):
# Fetch and set user data to the context variable # Fetch and set user data to the context variable
if api_key: if api_key:
try: try:
@ -78,7 +80,7 @@ class StoreService(Service):
name = "store_service" name = "store_service"
def __init__(self, settings_service: "SettingsService"): def __init__(self, settings_service: SettingsService):
self.settings_service = settings_service self.settings_service = settings_service
self.base_url = self.settings_service.settings.store_url self.base_url = self.settings_service.settings.store_url
self.download_webhook_url = self.settings_service.settings.download_webhook_url self.download_webhook_url = self.settings_service.settings.download_webhook_url

View file

@ -1,3 +1,5 @@
from __future__ import annotations
from collections.abc import Callable, Coroutine from collections.abc import Callable, Coroutine
from typing import TYPE_CHECKING, Any from typing import TYPE_CHECKING, Any
@ -27,7 +29,7 @@ def check_celery_availability():
class TaskService(Service): class TaskService(Service):
name = "task_service" name = "task_service"
def __init__(self, settings_service: "SettingsService"): def __init__(self, settings_service: SettingsService):
self.settings_service = settings_service self.settings_service = settings_service
try: try:
if self.settings_service.settings.celery_enabled: if self.settings_service.settings.celery_enabled:

View file

@ -1,3 +1,5 @@
from __future__ import annotations
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from langflow.services.factory import ServiceFactory from langflow.services.factory import ServiceFactory
@ -11,5 +13,5 @@ class TelemetryServiceFactory(ServiceFactory):
def __init__(self): def __init__(self):
super().__init__(TelemetryService) super().__init__(TelemetryService)
def create(self, settings_service: "SettingsService"): def create(self, settings_service: SettingsService):
return TelemetryService(settings_service) return TelemetryService(settings_service)

View file

@ -1,3 +1,5 @@
from __future__ import annotations
import asyncio import asyncio
import contextlib import contextlib
import os import os
@ -27,7 +29,7 @@ if TYPE_CHECKING:
class TelemetryService(Service): class TelemetryService(Service):
name = "telemetry_service" name = "telemetry_service"
def __init__(self, settings_service: "SettingsService"): def __init__(self, settings_service: SettingsService):
super().__init__() super().__init__()
self.settings_service = settings_service self.settings_service = settings_service
self.base_url = settings_service.settings.telemetry_base_url self.base_url = settings_service.settings.telemetry_base_url

View file

@ -1,5 +1,7 @@
from __future__ import annotations
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from typing import TYPE_CHECKING, Any, Optional from typing import TYPE_CHECKING, Any
from uuid import UUID from uuid import UUID
from langflow.services.tracing.schema import Log from langflow.services.tracing.schema import Log
@ -27,7 +29,7 @@ class BaseTracer(ABC):
trace_type: str, trace_type: str,
inputs: dict[str, Any], inputs: dict[str, Any],
metadata: dict[str, Any] | None = None, metadata: dict[str, Any] | None = None,
vertex: Optional["Vertex"] = None, vertex: Vertex | None = None,
): ):
raise NotImplementedError raise NotImplementedError
@ -53,5 +55,5 @@ class BaseTracer(ABC):
raise NotImplementedError raise NotImplementedError
@abstractmethod @abstractmethod
def get_langchain_callback(self) -> Optional["BaseCallbackHandler"]: def get_langchain_callback(self) -> BaseCallbackHandler | None:
raise NotImplementedError raise NotImplementedError

View file

@ -1,3 +1,5 @@
from __future__ import annotations
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from langflow.services.factory import ServiceFactory from langflow.services.factory import ServiceFactory
@ -11,5 +13,5 @@ class TracingServiceFactory(ServiceFactory):
def __init__(self): def __init__(self):
super().__init__(TracingService) super().__init__(TracingService)
def create(self, settings_service: "SettingsService"): def create(self, settings_service: SettingsService):
return TracingService(settings_service) return TracingService(settings_service)

View file

@ -1,8 +1,10 @@
from __future__ import annotations
import os import os
import traceback import traceback
import types import types
from datetime import datetime, timezone from datetime import datetime, timezone
from typing import TYPE_CHECKING, Any, Optional from typing import TYPE_CHECKING, Any
from uuid import UUID from uuid import UUID
from loguru import logger from loguru import logger
@ -65,7 +67,7 @@ class LangSmithTracer(BaseTracer):
trace_type: str, trace_type: str,
inputs: dict[str, Any], inputs: dict[str, Any],
metadata: dict[str, Any] | None = None, metadata: dict[str, Any] | None = None,
vertex: Optional["Vertex"] = None, vertex: Vertex | None = None,
): ):
if not self._ready: if not self._ready:
return return
@ -161,5 +163,5 @@ class LangSmithTracer(BaseTracer):
self._run_tree.post() self._run_tree.post()
self._run_link = self._run_tree.get_url() self._run_link = self._run_tree.get_url()
def get_langchain_callback(self) -> Optional["BaseCallbackHandler"]: def get_langchain_callback(self) -> BaseCallbackHandler | None:
return None return None

View file

@ -1,8 +1,10 @@
from __future__ import annotations
import asyncio import asyncio
import os import os
from collections import defaultdict from collections import defaultdict
from contextlib import asynccontextmanager from contextlib import asynccontextmanager
from typing import TYPE_CHECKING, Any, Optional from typing import TYPE_CHECKING, Any
from uuid import UUID from uuid import UUID
from loguru import logger from loguru import logger
@ -40,7 +42,7 @@ def _get_langfuse_tracer():
class TracingService(Service): class TracingService(Service):
name = "tracing_service" name = "tracing_service"
def __init__(self, settings_service: "SettingsService"): def __init__(self, settings_service: SettingsService):
self.settings_service = settings_service self.settings_service = settings_service
self.inputs: dict[str, dict] = defaultdict(dict) self.inputs: dict[str, dict] = defaultdict(dict)
self.inputs_metadata: dict[str, dict] = defaultdict(dict) self.inputs_metadata: dict[str, dict] = defaultdict(dict)
@ -155,7 +157,7 @@ class TracingService(Service):
trace_type: str, trace_type: str,
inputs: dict[str, Any], inputs: dict[str, Any],
metadata: dict[str, Any] | None = None, metadata: dict[str, Any] | None = None,
vertex: Optional["Vertex"] = None, vertex: Vertex | None = None,
): ):
inputs = self._cleanup_inputs(inputs) inputs = self._cleanup_inputs(inputs)
self.inputs[trace_name] = inputs self.inputs[trace_name] = inputs
@ -203,7 +205,7 @@ class TracingService(Service):
@asynccontextmanager @asynccontextmanager
async def trace_context( async def trace_context(
self, self,
component: "Component", component: Component,
trace_name: str, trace_name: str,
inputs: dict[str, Any], inputs: dict[str, Any],
metadata: dict[str, Any] | None = None, metadata: dict[str, Any] | None = None,
@ -248,7 +250,7 @@ class TracingService(Service):
inputs[key] = "*****" # avoid logging api_keys for security reasons inputs[key] = "*****" # avoid logging api_keys for security reasons
return inputs return inputs
def get_langchain_callbacks(self) -> list["BaseCallbackHandler"]: def get_langchain_callbacks(self) -> list[BaseCallbackHandler]:
callbacks = [] callbacks = []
for tracer in self._tracers.values(): for tracer in self._tracers.values():
if not tracer.ready: # type: ignore if not tracer.ready: # type: ignore

View file

@ -1,3 +1,5 @@
from __future__ import annotations
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from langflow.services.factory import ServiceFactory from langflow.services.factory import ServiceFactory
@ -11,7 +13,7 @@ class VariableServiceFactory(ServiceFactory):
def __init__(self): def __init__(self):
super().__init__(VariableService) super().__init__(VariableService)
def create(self, settings_service: "SettingsService"): def create(self, settings_service: SettingsService):
# here you would have logic to create and configure a VariableService # here you would have logic to create and configure a VariableService
# based on the settings_service # based on the settings_service

View file

@ -1,3 +1,5 @@
from __future__ import annotations
import os import os
from uuid import UUID from uuid import UUID
@ -14,7 +16,7 @@ from langflow.services.variable.kubernetes_secrets import KubernetesSecretManage
class KubernetesSecretService(VariableService, Service): class KubernetesSecretService(VariableService, Service):
def __init__(self, settings_service: "SettingsService"): def __init__(self, settings_service: SettingsService):
self.settings_service = settings_service self.settings_service = settings_service
# TODO: settings_service to set kubernetes namespace # TODO: settings_service to set kubernetes namespace
self.kubernetes_secrets = KubernetesSecretManager() self.kubernetes_secrets = KubernetesSecretManager()

View file

@ -1,3 +1,5 @@
from __future__ import annotations
import os import os
from datetime import datetime, timezone from datetime import datetime, timezone
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
@ -19,7 +21,7 @@ if TYPE_CHECKING:
class DatabaseVariableService(VariableService, Service): class DatabaseVariableService(VariableService, Service):
def __init__(self, settings_service: "SettingsService"): def __init__(self, settings_service: SettingsService):
self.settings_service = settings_service self.settings_service = settings_service
def initialize_user_variables(self, user_id: UUID | str, session: Session = Depends(get_session)): def initialize_user_variables(self, user_id: UUID | str, session: Session = Depends(get_session)):

View file

@ -1,3 +1,5 @@
from __future__ import annotations
from typing import TYPE_CHECKING, Any from typing import TYPE_CHECKING, Any
from asgiref.sync import async_to_sync from asgiref.sync import async_to_sync
@ -15,7 +17,7 @@ def test_celery(word: str) -> str:
@celery_app.task(bind=True, soft_time_limit=30, max_retries=3) @celery_app.task(bind=True, soft_time_limit=30, max_retries=3)
def build_vertex(self, vertex: "Vertex") -> "Vertex": def build_vertex(self, vertex: Vertex) -> Vertex:
""" """
Build a vertex Build a vertex
""" """

View file

@ -157,6 +157,7 @@ select = [
"E", "E",
"EM", "EM",
"F", "F",
"FA",
"FLY", "FLY",
"FURB", "FURB",
"I", "I",