From 5a807529a199d7630d12a2c0fc521e74a310829c Mon Sep 17 00:00:00 2001 From: Ashpreet Bedi Date: Thu, 4 Jan 2024 16:20:58 +0000 Subject: [PATCH] v2.1.4 --- phi/assistant/assistant.py | 1 + phi/assistant/file/file.py | 1 + phi/assistant/message.py | 1 + phi/assistant/run.py | 1 + phi/assistant/thread.py | 1 + phi/docker/app/whoami/__init__.py | 1 + phi/docker/app/whoami/whoami.py | 15 +++ phi/workspace/config.py | 147 +++++++++++++++++++++++++----- pyproject.toml | 9 +- 9 files changed, 152 insertions(+), 25 deletions(-) create mode 100644 phi/docker/app/whoami/__init__.py create mode 100644 phi/docker/app/whoami/whoami.py diff --git a/phi/assistant/assistant.py b/phi/assistant/assistant.py index b6b118ee3..467271dea 100644 --- a/phi/assistant/assistant.py +++ b/phi/assistant/assistant.py @@ -229,6 +229,7 @@ def update(self) -> "Assistant": self.load_from_openai(self.openai_assistant) logger.debug(f"Assistant updated: {self.id}") return self + raise ValueError("Assistant not available") except AssistantIdNotSet: logger.warning("Assistant not available") raise diff --git a/phi/assistant/file/file.py b/phi/assistant/file/file.py index 6a48152cb..871f7e0ce 100644 --- a/phi/assistant/file/file.py +++ b/phi/assistant/file/file.py @@ -130,6 +130,7 @@ def download(self, path: Optional[str] = None, suffix: Optional[str] = None) -> temp_file.write(response.content) temp_file_path = temp_file.name return temp_file_path + raise ValueError("File not available") except FileIdNotSet: logger.warning("File not available") raise diff --git a/phi/assistant/message.py b/phi/assistant/message.py index 83ec96e3f..b7293038c 100644 --- a/phi/assistant/message.py +++ b/phi/assistant/message.py @@ -157,6 +157,7 @@ def update(self, thread_id: Optional[str] = None) -> "Message": self.load_from_openai(self.openai_message) logger.debug(f"Message updated: {self.id}") return self + raise ValueError("Message not available") except (ThreadIdNotSet, MessageIdNotSet): logger.warning("Message not available") raise diff --git a/phi/assistant/run.py b/phi/assistant/run.py index 6c3d2870e..b37e77820 100644 --- a/phi/assistant/run.py +++ b/phi/assistant/run.py @@ -230,6 +230,7 @@ def update(self, thread_id: Optional[str] = None) -> "Run": self.load_from_openai(self.openai_run) logger.debug(f"Run updated: {self.id}") return self + raise ValueError("Run not available") except (ThreadIdNotSet, RunIdNotSet): logger.warning("Message not available") raise diff --git a/phi/assistant/thread.py b/phi/assistant/thread.py index d1a6e965f..da0a7ac8d 100644 --- a/phi/assistant/thread.py +++ b/phi/assistant/thread.py @@ -124,6 +124,7 @@ def update(self) -> "Thread": self.load_from_openai(self.openai_thread) logger.debug(f"Thead updated: {self.id}") return self + raise ValueError("Thread not available") except ThreadIdNotSet: logger.warning("Thread not available") raise diff --git a/phi/docker/app/whoami/__init__.py b/phi/docker/app/whoami/__init__.py new file mode 100644 index 000000000..aa64a20e8 --- /dev/null +++ b/phi/docker/app/whoami/__init__.py @@ -0,0 +1 @@ +from phi.docker.app.whoami.whoami import Whoami diff --git a/phi/docker/app/whoami/whoami.py b/phi/docker/app/whoami/whoami.py new file mode 100644 index 000000000..4bf912c4d --- /dev/null +++ b/phi/docker/app/whoami/whoami.py @@ -0,0 +1,15 @@ +from phi.docker.app.base import DockerApp, ContainerContext # noqa: F401 + + +class Whoami(DockerApp): + # -*- App Name + name: str = "whoami" + + # -*- Image Configuration + image_name: str = "traefik/whoami" + image_tag: str = "v1.10" + + # -*- App Ports + # Open a container port if open_port=True + open_port: bool = True + port_number: int = 80 diff --git a/phi/workspace/config.py b/phi/workspace/config.py index 3b1b65776..629d5d5e5 100644 --- a/phi/workspace/config.py +++ b/phi/workspace/config.py @@ -7,12 +7,135 @@ from phi.infra.resources import InfraResources from phi.api.schemas.workspace import WorkspaceSchema from phi.workspace.settings import WorkspaceSettings +from phi.utils.py_io import get_python_objects_from_module from phi.utils.log import logger # List of directories to ignore when loading the workspace ignored_dirs = ["ignore", "test", "tests", "config"] +def get_workspace_objects_from_file(resource_file: Path) -> dict: + """Returns workspace objects from the resource file""" + try: + python_objects = get_python_objects_from_module(resource_file) + # logger.debug(f"python_objects: {python_objects}") + + workspace_objects = {} + docker_resources_available = False + create_default_docker_resources = False + k8s_resources_available = False + create_default_k8s_resources = False + aws_resources_available = False + create_default_aws_resources = False + for obj_name, obj in python_objects.items(): + _type_name = obj.__class__.__name__ + if _type_name in [ + "WorkspaceSettings", + "DockerResources", + "K8sResources", + "AwsResources", + ]: + workspace_objects[obj_name] = obj + if _type_name == "DockerResources": + docker_resources_available = True + elif _type_name == "K8sResources": + k8s_resources_available = True + elif _type_name == "AwsResources": + aws_resources_available = True + + try: + if not docker_resources_available: + if obj.__class__.__module__.startswith("phi.docker"): + create_default_docker_resources = True + if not k8s_resources_available: + if obj.__class__.__module__.startswith("phi.k8s"): + create_default_k8s_resources = True + if not aws_resources_available: + if obj.__class__.__module__.startswith("phi.aws"): + create_default_aws_resources = True + except Exception: + pass + + if not docker_resources_available and create_default_docker_resources: + from phi.docker.resources import DockerResources, DockerResource, DockerApp + + logger.debug("Creating default docker resources") + default_docker_resources = DockerResources() + add_default_docker_resources = False + for obj_name, obj in python_objects.items(): + _obj_class = obj.__class__ + if issubclass(_obj_class, DockerResource): + if default_docker_resources.resources is None: + default_docker_resources.resources = [] + default_docker_resources.resources.append(obj) + add_default_docker_resources = True + logger.debug(f"Added DockerResource: {obj_name}") + elif issubclass(_obj_class, DockerApp): + if default_docker_resources.apps is None: + default_docker_resources.apps = [] + default_docker_resources.apps.append(obj) + add_default_docker_resources = True + logger.debug(f"Added DockerApp: {obj_name}") + + if add_default_docker_resources: + workspace_objects["default_docker_resources"] = default_docker_resources + + if not k8s_resources_available and create_default_k8s_resources: + from phi.k8s.resources import K8sResources, K8sResource, K8sApp, CreateK8sResource + + logger.debug("Creating default k8s resources") + default_k8s_resources = K8sResources() + add_default_k8s_resources = False + for obj_name, obj in python_objects.items(): + _obj_class = obj.__class__ + # logger.debug(f"Checking {_obj_class}: {obj_name}") + if issubclass(_obj_class, K8sResource) or issubclass(_obj_class, CreateK8sResource): + if default_k8s_resources.resources is None: + default_k8s_resources.resources = [] + default_k8s_resources.resources.append(obj) + add_default_k8s_resources = True + logger.debug(f"Added K8sResource: {obj_name}") + elif issubclass(_obj_class, K8sApp): + if default_k8s_resources.apps is None: + default_k8s_resources.apps = [] + default_k8s_resources.apps.append(obj) + add_default_k8s_resources = True + logger.debug(f"Added K8sApp: {obj_name}") + + if add_default_k8s_resources: + workspace_objects["default_k8s_resources"] = default_k8s_resources + + if not aws_resources_available and create_default_aws_resources: + from phi.aws.resources import AwsResources, AwsResource, AwsApp + + logger.debug("Creating default aws resources") + default_aws_resources = AwsResources() + add_default_aws_resources = False + for obj_name, obj in python_objects.items(): + _obj_class = obj.__class__ + # logger.debug(f"Checking {_obj_class}: {obj_name}") + if issubclass(_obj_class, AwsResource): + if default_aws_resources.resources is None: + default_aws_resources.resources = [] + default_aws_resources.resources.append(obj) + add_default_aws_resources = True + logger.debug(f"Added AwsResource: {obj_name}") + elif issubclass(_obj_class, AwsApp): + if default_aws_resources.apps is None: + default_aws_resources.apps = [] + default_aws_resources.apps.append(obj) + add_default_aws_resources = True + logger.debug(f"Added AwsApp: {obj_name}") + + if add_default_aws_resources: + workspace_objects["default_aws_resources"] = default_aws_resources + + return workspace_objects + except Exception: + logger.error(f"Error reading: {resource_file}") + raise + + class WorkspaceConfig(BaseModel): """The WorkspaceConfig stores data for a phidata workspace.""" @@ -91,9 +214,6 @@ def workspace_settings(self) -> Optional[WorkspaceSettings]: return None logger.debug(f"Loading workspace_settings from {ws_settings_file}") - - from phi.utils.py_io import get_python_objects_from_module - try: python_objects = get_python_objects_from_module(ws_settings_file) for obj_name, obj in python_objects.items(): @@ -164,7 +284,6 @@ def get_resources( from sys import path as sys_path from phi.utils.load_env import load_env - from phi.utils.py_io import get_python_objects_from_module # Objects to read from the files in the workspace_dir_path docker_resource_groups: Optional[List[Any]] = None @@ -311,7 +430,6 @@ def get_resources_from_file( from sys import path as sys_path from phi.utils.load_env import load_env - from phi.utils.py_io import get_python_objects_from_module # Objects to read from the file docker_resource_groups: Optional[List[Any]] = None @@ -331,24 +449,9 @@ def get_resources_from_file( logger.debug(f"Adding {resource_file_parent_dir} to path") sys_path.insert(0, str(resource_file_parent_dir)) - # Create a dict of objects in the workspace directory - workspace_objects = {} logger.debug(f"**--> Loading resources from {resource_file}") - try: - python_objects = get_python_objects_from_module(resource_file) - # logger.debug(f"python_objects: {python_objects}") - for obj_name, obj in python_objects.items(): - _type_name = obj.__class__.__name__ - if _type_name in [ - "WorkspaceSettings", - "DockerResources", - "K8sResources", - "AwsResources", - ]: - workspace_objects[obj_name] = obj - except Exception: - logger.warning(f"Error in {resource_file}") - raise + # Create a dict of objects from the file + workspace_objects = get_workspace_objects_from_file(resource_file) # logger.debug(f"workspace_objects: {workspace_objects}") for obj_name, obj in workspace_objects.items(): diff --git a/pyproject.toml b/pyproject.toml index d5205c3fe..6d09802c7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "phidata" -version = "2.1.3" +version = "2.1.4" description = "AI Toolkit for Engineers" requires-python = ">=3.7" readme = "README.md" @@ -80,15 +80,18 @@ module = [ "docker.*", "duckdb.*", "kubernetes.*", + "langchain.*", + "langchain_core.*", "openai.*", + "pandas.*", "pgvector.*", "psycopg.*", "pypdf.*", - "pandas.*", + "qdrant_client.*", "setuptools.*", "sqlalchemy.*", + "streamlit.*", "textract.*", "wikipedia.*", - "qdrant_client.*", ] ignore_missing_imports = true