Refactor code for initializing services and socketio server

This commit is contained in:
Gabriel Luiz Freitas Almeida 2024-03-25 10:27:27 -03:00
commit 2316beea93
5 changed files with 10 additions and 32 deletions

View file

@ -20,9 +20,7 @@ from langflow.utils.logger import configure
def get_lifespan(fix_migration=False, socketio_server=None):
@asynccontextmanager
async def lifespan(app: FastAPI):
initialize_services(
fix_migration=fix_migration, socketio_server=socketio_server
)
initialize_services(fix_migration=fix_migration, socketio_server=socketio_server)
setup_llm_caching()
LangfuseInstance.update()
create_or_update_starter_projects()
@ -36,9 +34,7 @@ def create_app():
"""Create the FastAPI app and include the router."""
configure()
socketio_server = socketio.AsyncServer(
async_mode="asgi", cors_allowed_origins="*", logger=True
)
socketio_server = socketio.AsyncServer(async_mode="asgi", cors_allowed_origins="*", logger=True)
lifespan = get_lifespan(socketio_server=socketio_server)
app = FastAPI(lifespan=lifespan)
origins = ["*"]
@ -105,9 +101,7 @@ def get_static_files_dir():
return frontend_path / "frontend"
def setup_app(
static_files_dir: Optional[Path] = None, backend_only: bool = False
) -> FastAPI:
def setup_app(static_files_dir: Optional[Path] = None, backend_only: bool = False) -> FastAPI:
"""Setup the FastAPI app."""
# get the directory of the current file
if not static_files_dir:

View file

@ -31,18 +31,14 @@ def hash_dict(d: dict) -> str:
return str(d)
def hash_infer_service_types_args(
factory_class: Type[ServiceFactory], available_services=None
) -> str:
def hash_infer_service_types_args(factory_class: Type[ServiceFactory], available_services=None) -> str:
factory_hash = hash_factory(factory_class)
services_hash = hash_dict(available_services)
return f"{factory_hash}_{services_hash}"
@cached(cache=LRUCache(maxsize=10), key=hash_infer_service_types_args)
def infer_service_types(
factory_class: Type[ServiceFactory], available_services=None
) -> "ServiceType":
def infer_service_types(factory_class: Type[ServiceFactory], available_services=None) -> "ServiceType":
create_method = factory_class.create
type_hints = get_type_hints(create_method, globalns=available_services)
service_types = []
@ -59,9 +55,7 @@ def infer_service_types(
service_type = ServiceType[type_name]
service_types.append(service_type)
except KeyError:
raise ValueError(
f"No matching ServiceType for parameter type: {param_type.__name__}"
)
raise ValueError(f"No matching ServiceType for parameter type: {param_type.__name__}")
return service_types
@ -85,7 +79,5 @@ def import_all_services_into_a_dict():
break
except Exception as exc:
logger.exception(exc)
raise RuntimeError(
f"Could not initialize services. Please check your settings."
) from exc
raise RuntimeError("Could not initialize services. Please check your settings.") from exc
return services

View file

@ -51,14 +51,10 @@ class ServiceManager:
self._create_service(dependency)
# Collect the dependent services
dependent_services = {
dep.value: self.services[dep] for dep in factory.dependencies
}
dependent_services = {dep.value: self.services[dep] for dep in factory.dependencies}
# Create the actual service
self.services[service_name] = self.factories[service_name].create(
**dependent_services
)
self.services[service_name] = self.factories[service_name].create(**dependent_services)
self.services[service_name].set_ready()
def _validate_service_creation(self, service_name: "ServiceType"):
@ -66,9 +62,7 @@ class ServiceManager:
Validate whether the service can be created.
"""
if service_name not in self.factories:
raise ValueError(
f"No factory registered for the service class '{service_name.name}'"
)
raise ValueError(f"No factory registered for the service class '{service_name.name}'")
def update(self, service_name: "ServiceType"):
"""

View file

@ -1,6 +1,5 @@
from langflow.services.factory import ServiceFactory
from langflow.services.monitor.service import MonitorService
from langflow.services.schema import ServiceType
class MonitorServiceFactory(ServiceFactory):

View file

@ -1,7 +1,6 @@
from typing import TYPE_CHECKING
from langflow.services.factory import ServiceFactory
from langflow.services.schema import ServiceType
from langflow.services.socket.service import SocketIOService
if TYPE_CHECKING: