Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] Unified remote APIs - Prompt studio changes #817

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions backend/file_management/file_management_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
from file_management.file_management_dto import FileInformation
from fsspec import AbstractFileSystem
from pydrive2.files import ApiRequestError
from utils.constants import FileStorageType
from utils.file_storage_helper import FileStorageUtil

from unstract.connectors.filesystems import connectors as fs_connectors
from unstract.connectors.filesystems.unstract_file_system import UnstractFileSystem
Expand All @@ -31,7 +33,6 @@


class FileManagerHelper:

@staticmethod
def get_file_system(connector: ConnectorInstance) -> UnstractFileSystem:
"""Creates the `UnstractFileSystem` for the corresponding connector."""
Expand Down Expand Up @@ -141,15 +142,15 @@ def upload_file(
remote_file.write(file.read())

@staticmethod
def fetch_file_contents(file_system: UnstractFileSystem, file_path: str) -> Any:
fs = file_system.get_fsspec_fs()
try:
file_info = fs.info(file_path)
except FileNotFoundError:
def fetch_file_contents(file_path: str) -> Any:
file_storage = FileStorageUtil.initialize_file_storage(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I need to understand this better.

FileStorageType.PERMANENT
)
if not file_storage.exists(file_path):
raise FileNotFound

file_content_type = file_info.get("ContentType")
file_type = file_info.get("type")
# file_content_type = file_info.get("ContentType")
# file_type = file_info.get("type")
if file_type != "file":
raise InvalidFileType
try:
Expand Down
18 changes: 18 additions & 0 deletions backend/utils/common_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import os
from enum import Enum


Expand Down Expand Up @@ -31,6 +32,23 @@ def is_json(string: str) -> bool:
return False
return True

@staticmethod
def get_env_or_die(env_key: str) -> str:
"""Returns the value of an env variable.

If its empty or None, raises an error and exits

Args:
env_key (str): Key to retrieve

Returns:
str: Value of the env
"""
env_value = os.environ.get(env_key)
if env_value is None or env_value == "":
raise ValueError(f"Env variable '{env_key}' is required")
return env_value


class ModelEnum(Enum):
@classmethod
Expand Down
12 changes: 12 additions & 0 deletions backend/utils/constants.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from enum import Enum

from django.conf import settings
from utils.common_utils import CommonUtils
Expand Down Expand Up @@ -78,3 +79,14 @@ class ExecutionLogConstants:
PERIODIC_TASK_NAME_V2 = "workflow_log_history_v2"
TASK = "workflow_manager.workflow.execution_log_utils.consume_log_history"
TASK_V2 = "consume_log_history"


class FileStorageKeys:
FILE_STORAGE_PROVIDER = "FILE_STORAGE_PROVIDER"
FILE_STORAGE_CREDENTIALS = "FILE_STORAGE_CREDENTIALS"


class FileStorageType(Enum):
PERMANENT = "permanent"
DEFAULT = "default"
TEMPORARY = "temporary"
34 changes: 34 additions & 0 deletions backend/utils/file_storage_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from typing import Any

from unstract.sdk.file_storage.fs_impl import FileStorage
from unstract.sdk.file_storage.fs_permanent import PermanentFileStorage
from utils.common_utils import CommonUtils
from utils.constants import FileStorageKeys, FileStorageType


class FileStorageUtil:
@staticmethod
def initialize_file_storage(type: FileStorageType) -> FileStorage:
provider_data = FileStorageUtil.load_file_storage_envs()
provider = provider_data[FileStorageKeys.FILE_STORAGE_PROVIDER]
credentials = provider_data[FileStorageKeys.FILE_STORAGE_CREDENTIALS]
if type.value == FileStorageType.PERMANENT.value:
file_storage = PermanentFileStorage(
provider=provider, credentials=credentials
)
if type.value == FileStorageType.DEFAULT.value:
file_storage = FileStorage(provider=provider, credentials=credentials)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If provider is not present, we can default it to Local

return file_storage

@staticmethod
def load_file_storage_envs() -> dict[str, Any]:
provider: str = CommonUtils.get_env_or_die(
env_key=FileStorageKeys.FILE_STORAGE_PROVIDER
)
credentials: str = CommonUtils.get_env_or_die(
env_key=FileStorageKeys.FILE_STORAGE_CREDENTIALS
)
provider_data: dict[str, Any] = {}
provider_data[FileStorageKeys.FILE_STORAGE_PROVIDER] = provider
provider_data[FileStorageKeys.FILE_STORAGE_CREDENTIALS] = credentials
return provider_data