Skip to content

LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows

fix: Refactored platform service to use blueprints (#583)

Chandrasekharan M836849c58cb8
diff --git a/docker/dockerfiles/platform.Dockerfile b/docker/dockerfiles/platform.Dockerfile
index 8e9349b01..2ba3aba4a 100644
--- a/docker/dockerfiles/platform.Dockerfile
+++ b/docker/dockerfiles/platform.Dockerfile
@@ -42,4 +42,4 @@ RUN set -e; \
EXPOSE 3001
# During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debug
-CMD [".venv/bin/gunicorn", "--bind", "0.0.0.0:3001", "--timeout", "300", "unstract.platform_service.main:app"]
+CMD [".venv/bin/gunicorn", "--bind", "0.0.0.0:3001", "--timeout", "300", "unstract.platform_service.run:app"]
diff --git a/platform-service/pdm.lock b/platform-service/pdm.lock
index 48d2498c0..5d487008f 100644
--- a/platform-service/pdm.lock
+++ b/platform-service/pdm.lock
@@ -5,7 +5,7 @@
groups = ["default", "deploy", "test"]
strategy = ["cross_platform", "inherit_metadata"]
lock_version = "4.4.2"
-content_hash = "sha256:a650076acef87a75ba22e8a034cb07502e3592e76fff246f3d70673ff15bc457"
+content_hash = "sha256:d0e87ecffff8ccef00cdaa3fa34c2665d8b52cf8c92e2da2c95feb3dd2045d6b"
[[package]]
name = "async-timeout"
@@ -614,6 +614,15 @@ files = [
{file = "setuptools-71.1.0.tar.gz", hash = "sha256:032d42ee9fb536e33087fb66cac5f840eb9391ed05637b3f2a76a7c8fb477936"},
]
+[[package]]
+name = "singleton-decorator"
+version = "1.0.0"
+summary = "A testable singleton decorator"
+groups = ["default"]
+files = [
+ {file = "singleton-decorator-1.0.0.tar.gz", hash = "sha256:1a90ad8a8a738be591c9c167fdd677c5d4a43d1bc6b1c128227be1c5e03bee07"},
+]
+
[[package]]
name = "tomli"
version = "2.0.1"
diff --git a/platform-service/pyproject.toml b/platform-service/pyproject.toml
index f98ed36c0..ad6ee2452 100644
--- a/platform-service/pyproject.toml
+++ b/platform-service/pyproject.toml
@@ -14,6 +14,7 @@ dependencies = [
"cryptography>=41.0.7",
"unstract-flags @ file:///${PROJECT_ROOT}/../unstract/flags",
"requests>=2.31.0",
+ "singleton-decorator~=1.0.0",
]
requires-python = ">=3.9,<3.11.1"
readme = "README.md"
@@ -37,6 +38,6 @@ deploy = [
]
[tool.pdm.scripts]
-platform_service.cmd = "flask --app src/unstract/platform_service/main.py run --port 3001"
+platform_service.cmd = "flask --app src/unstract/platform_service/run.py run --port 3001"
platform_service.env_file = ".env"
platform_service.help = "Runs the Unstract platform service"
diff --git a/platform-service/src/unstract/platform_service/controller/__init__.py b/platform-service/src/unstract/platform_service/controller/__init__.py
new file mode 100644
index 000000000..adfbe6331
--- /dev/null
+++ b/platform-service/src/unstract/platform_service/controller/__init__.py
@@ -0,0 +1,18 @@
+from typing import Any
+
+from flask import Blueprint, Response, jsonify
+from unstract.platform_service.exceptions import CustomException
+
+from .health import health_bp
+from .platform import platform_bp
+
+api = Blueprint("api", __name__)
+api.register_blueprint(platform_bp)
+api.register_blueprint(health_bp)
+
+
[email protected](CustomException)
+def handle_custom_exception(error: Any) -> tuple[Response, Any]:
+ response = jsonify({"error": error.message})
+ response.status_code = error.code # You can customize the HTTP status code
+ return jsonify(response), error.code
diff --git a/platform-service/src/unstract/platform_service/controller/health.py b/platform-service/src/unstract/platform_service/controller/health.py
new file mode 100644
index 000000000..c23d11ad7
--- /dev/null
+++ b/platform-service/src/unstract/platform_service/controller/health.py
@@ -0,0 +1,15 @@
+from typing import Literal
+
+from flask import Blueprint
+
+health_bp = Blueprint("health", __name__)
+
+
+@health_bp.route("/health", methods=["GET"], endpoint="health_check")
+def health_check() -> str:
+ return "OK"
+
+
+@health_bp.route("/ping", methods=["GET"])
+def ping() -> Literal["Pong"]:
+ return "Pong"
diff --git a/platform-service/src/unstract/platform_service/main.py b/platform-service/src/unstract/platform_service/controller/platform.py
similarity index 79%
rename from platform-service/src/unstract/platform_service/main.py
rename to platform-service/src/unstract/platform_service/controller/platform.py
index c2d26d71a..5cd3a4f4d 100644
--- a/platform-service/src/unstract/platform_service/main.py
+++ b/platform-service/src/unstract/platform_service/controller/platform.py
@@ -1,82 +1,35 @@
-import logging
-import os
import uuid
from datetime import datetime
-from typing import Any, Literal, Optional
+from typing import Any, Optional
import peewee
import redis
from cryptography.fernet import Fernet
-from dotenv import load_dotenv
-from flask import Flask, Request, Response, json, jsonify, make_response, request
+from flask import Blueprint, Request
+from flask import current_app as app
+from flask import json, jsonify, make_response, request
from unstract.platform_service.constants import DBTable, DBTableV2, FeatureFlag
-from unstract.platform_service.exceptions import CustomException
-from unstract.platform_service.helper import (
+from unstract.platform_service.env import Env
+from unstract.platform_service.helper.adapter_instance import (
AdapterInstanceRequestHelper,
- CostCalculationHelper,
- PromptStudioRequestHelper,
)
-from unstract.platform_service.utils import EnvManager
+from unstract.platform_service.helper.cost_calculation import CostCalculationHelper
+from unstract.platform_service.helper.prompt_studio import PromptStudioRequestHelper
from unstract.flags.feature_flag import check_feature_flag_status
-load_dotenv()
-
-logging.basicConfig(
- level=logging.INFO,
- format="%(asctime)s %(levelname)s %(name)s : %(message)s",
-)
-# Configuring envs
-MAX_FILE_SIZE = 100 * 1024 * 1024
-INVALID_ORGANIZATOIN = "Invalid organization"
-INVALID_PAYLOAD = "Bad Request / No payload"
-BAD_REQUEST = "Bad Request"
-REDIS_HOST = EnvManager.get_required_setting("REDIS_HOST")
-REDIS_PORT = int(EnvManager.get_required_setting("REDIS_PORT", 6379))
-REDIS_USERNAME = os.environ.get("REDIS_USERNAME")
-REDIS_PASSWORD = os.environ.get("REDIS_PASSWORD")
-PG_BE_HOST = os.environ.get("PG_BE_HOST")
-PG_BE_PORT = int(os.environ.get("PG_BE_PORT", 5432))
-PG_BE_USERNAME = os.environ.get("PG_BE_USERNAME")
-PG_BE_PASSWORD = os.environ.get("PG_BE_PASSWORD")
-PG_BE_DATABASE = os.environ.get("PG_BE_DATABASE")
-ENCRYPTION_KEY = EnvManager.get_required_setting("ENCRYPTION_KEY")
-MODEL_PRICES_URL = EnvManager.get_required_setting("MODEL_PRICES_URL")
-MODEL_PRICES_TTL_IN_DAYS = EnvManager.get_required_setting("MODEL_PRICES_TTL_IN_DAYS")
-MODEL_PRICES_FILE_PATH = EnvManager.get_required_setting("MODEL_PRICES_FILE_PATH")
-EnvManager.raise_for_missing_envs()
-MODEL_PRICES_TTL_IN_DAYS = int(MODEL_PRICES_TTL_IN_DAYS)
-
-# TODO: Follow Flask best practices and refactor accordingly
-app = Flask("platform_service")
-
be_db = peewee.PostgresqlDatabase(
- PG_BE_DATABASE,
- user=PG_BE_USERNAME,
- password=PG_BE_PASSWORD,
- host=PG_BE_HOST,
- port=PG_BE_PORT,
+ Env.PG_BE_DATABASE,
+ user=Env.PG_BE_USERNAME,
+ password=Env.PG_BE_PASSWORD,
+ host=Env.PG_BE_HOST,
+ port=Env.PG_BE_PORT,
)
-be_db.init(PG_BE_DATABASE)
+be_db.init(Env.PG_BE_DATABASE)
be_db.connect()
-cost_calculation_helper = CostCalculationHelper(
- url=MODEL_PRICES_URL,
- ttl_days=MODEL_PRICES_TTL_IN_DAYS,
- file_path=MODEL_PRICES_FILE_PATH,
- logger=app.logger,
-)
-
-
-def drop_unstract_usage_table() -> None:
- query = "DROP TABLE IF EXISTS unstract_usage"
- try:
- be_db.execute_sql(query)
- except Exception as e:
- app.logger.error(f"Error dropping 'unstract_usage' table: {e}")
-
-drop_unstract_usage_table()
+platform_bp = Blueprint("platform", __name__)
def get_token_from_auth_header(request: Request) -> Any:
@@ -179,12 +132,7 @@ def validate_bearer_token(token: Optional[str]) -> bool:
return True
[email protected]("/health", methods=["GET"], endpoint="health_check")
-def health_check() -> str:
- return "OK"
-
-
[email protected]("/page-usage", methods=["POST"], endpoint="page_usage")
+@platform_bp.route("/page-usage", methods=["POST"], endpoint="page_usage")
@authentication_middleware
def page_usage() -> Any:
"""Usage endpoint."""
@@ -195,7 +143,7 @@ def page_usage() -> Any:
}
payload: Optional[dict[Any, Any]] = request.json
if not payload:
- result["error"] = INVALID_PAYLOAD
+ result["error"] = Env.INVALID_PAYLOAD
return make_response(result, 400)
bearer_token = get_token_from_auth_header(request)
@@ -239,7 +187,7 @@ def page_usage() -> Any:
return make_response(result, 500)
[email protected]("/usage", methods=["POST"])
+@platform_bp.route("/usage", methods=["POST"])
@authentication_middleware
def usage() -> Any:
"""Usage endpoint.
@@ -260,7 +208,7 @@ def usage() -> Any:
}
payload: Optional[dict[Any, Any]] = request.json
if not payload:
- result["error"] = INVALID_PAYLOAD
+ result["error"] = Env.INVALID_PAYLOAD
return make_response(result, 400)
bearer_token = get_token_from_auth_header(request)
organization_uid, org_id = get_organization_from_bearer_token(bearer_token)
@@ -281,6 +229,7 @@ def usage() -> Any:
input_tokens = embedding_tokens
cost_in_dollars = 0.0
if provider:
+ cost_calculation_helper = CostCalculationHelper()
cost_in_dollars = cost_calculation_helper.calculate_cost(
model_name=model_name,
provider=provider,
@@ -358,7 +307,7 @@ def usage() -> Any:
return make_response(result, 500)
+@platform_bp.route(
"/platform_details",
methods=["GET"],
endpoint="platform_details",
@@ -377,19 +326,14 @@ def platform_details() -> Any:
bearer_token = get_token_from_auth_header(request)
_, organization_id = get_organization_from_bearer_token(bearer_token)
if not organization_id:
- result["error"] = INVALID_ORGANIZATOIN
+ result["error"] = Env.INVALID_ORGANIZATOIN
return result, 403
platform_details = {"organization_id": organization_id}
result = {"status": "OK", "details": platform_details}
return result, 200
[email protected]("/ping", methods=["GET"])
-def ping() -> Literal["Pong"]:
- return "Pong"
-
-
[email protected]("/cache", methods=["POST", "GET", "DELETE"], endpoint="cache")
+@platform_bp.route("/cache", methods=["POST", "GET", "DELETE"], endpoint="cache")
@authentication_middleware
def cache() -> Any:
"""Cache endpoint.
@@ -406,23 +350,23 @@ def cache() -> Any:
"""
bearer_token = get_token_from_auth_header(request)
_, account_id = get_organization_from_bearer_token(bearer_token)
- if not REDIS_HOST:
- app.logger.error("REDIS_HOST not set")
+ if not Env.REDIS_HOST:
+ app.logger.error("Env.REDIS_HOST not set")
return "Internal Server Error", 500
if request.method == "POST":
payload: Optional[dict[Any, Any]] = request.json
if not payload:
- return BAD_REQUEST, 400
+ return Env.BAD_REQUEST, 400
key = payload.get("key")
value = payload.get("value")
if key is None or value is None:
- return BAD_REQUEST, 400
+ return Env.BAD_REQUEST, 400
try:
r = redis.Redis(
- host=REDIS_HOST,
- port=REDIS_PORT,
- username=REDIS_USERNAME,
- password=REDIS_PASSWORD,
+ host=Env.REDIS_HOST,
+ port=Env.REDIS_PORT,
+ username=Env.REDIS_USERNAME,
+ password=Env.REDIS_PASSWORD,
)
redis_key = f"{account_id}:{key}"
r.set(redis_key, value)
@@ -434,10 +378,10 @@ def cache() -> Any:
key = request.args.get("key")
try:
r = redis.Redis(
- host=REDIS_HOST,
- port=REDIS_PORT,
- username=REDIS_USERNAME,
- password=REDIS_PASSWORD,
+ host=Env.REDIS_HOST,
+ port=Env.REDIS_PORT,
+ username=Env.REDIS_USERNAME,
+ password=Env.REDIS_PASSWORD,
)
redis_key = f"{account_id}:{key}"
app.logger.info(f"Getting cached data for key: {redis_key}")
@@ -454,10 +398,10 @@ def cache() -> Any:
key = request.args.get("key")
try:
r = redis.Redis(
- host=REDIS_HOST,
- port=REDIS_PORT,
- username=REDIS_USERNAME,
- password=REDIS_PASSWORD,
+ host=Env.REDIS_HOST,
+ port=Env.REDIS_PORT,
+ username=Env.REDIS_USERNAME,
+ password=Env.REDIS_PASSWORD,
)
redis_key = f"{account_id}:{key}"
app.logger.info(f"Deleting cached data for key: {redis_key}")
@@ -471,7 +415,7 @@ def cache() -> Any:
return "OK", 200
+@platform_bp.route(
"/adapter_instance",
methods=["GET"],
endpoint="adapter_instance",
@@ -488,7 +432,7 @@ def adapter_instance() -> Any:
bearer_token = get_token_from_auth_header(request)
organization_uid, organization_id = get_organization_from_bearer_token(bearer_token)
if not organization_id:
- return INVALID_ORGANIZATOIN, 403
+ return Env.INVALID_ORGANIZATOIN, 403
if request.method == "GET":
adapter_instance_id = request.args.get("adapter_instance_id")
@@ -501,7 +445,7 @@ def adapter_instance() -> Any:
organization_uid=organization_uid,
)
- f: Fernet = Fernet(ENCRYPTION_KEY.encode("utf-8"))
+ f: Fernet = Fernet(Env.ENCRYPTION_KEY.encode("utf-8"))
data_dict["adapter_metadata"] = json.loads(
f.decrypt(bytes(data_dict.pop("adapter_metadata_b")).decode("utf-8"))
@@ -518,7 +462,7 @@ def adapter_instance() -> Any:
return "Method Not Allowed", 405
+@platform_bp.route(
"/custom_tool_instance",
methods=["GET"],
endpoint="custom_tool_instance",
@@ -535,7 +479,7 @@ def custom_tool_instance() -> Any:
bearer_token = get_token_from_auth_header(request)
_, organization_id = get_organization_from_bearer_token(bearer_token)
if not organization_id:
- return INVALID_ORGANIZATOIN, 403
+ return Env.INVALID_ORGANIZATOIN, 403
if request.method == "GET":
prompt_registry_id = request.args.get("prompt_registry_id")
@@ -555,15 +499,3 @@ def custom_tool_instance() -> Any:
)
return "Internal Server Error", 500
return "Method Not Allowed", 405
-
-
[email protected](CustomException)
-def handle_custom_exception(error: Any) -> tuple[Response, Any]:
- response = jsonify({"error": error.message})
- response.status_code = error.code # You can customize the HTTP status code
- return jsonify(response), error.code
-
-
-if __name__ == "__main__":
- # Start the server
- app.run(host="0.0.0.0", port="3001")
diff --git a/platform-service/src/unstract/platform_service/env.py b/platform-service/src/unstract/platform_service/env.py
new file mode 100644
index 000000000..a938c4a44
--- /dev/null
+++ b/platform-service/src/unstract/platform_service/env.py
@@ -0,0 +1,27 @@
+import os
+
+from unstract.platform_service.utils import EnvManager
+
+
+class Env:
+ INVALID_ORGANIZATOIN = "Invalid organization"
+ INVALID_PAYLOAD = "Bad Request / No payload"
+ BAD_REQUEST = "Bad Request"
+ REDIS_HOST = EnvManager.get_required_setting("REDIS_HOST")
+ REDIS_PORT = int(EnvManager.get_required_setting("REDIS_PORT", 6379))
+ REDIS_USERNAME = os.environ.get("REDIS_USERNAME")
+ REDIS_PASSWORD = os.environ.get("REDIS_PASSWORD")
+ PG_BE_HOST = os.environ.get("PG_BE_HOST")
+ PG_BE_PORT = int(os.environ.get("PG_BE_PORT", 5432))
+ PG_BE_USERNAME = os.environ.get("PG_BE_USERNAME")
+ PG_BE_PASSWORD = os.environ.get("PG_BE_PASSWORD")
+ PG_BE_DATABASE = os.environ.get("PG_BE_DATABASE")
+ ENCRYPTION_KEY = EnvManager.get_required_setting("ENCRYPTION_KEY")
+ MODEL_PRICES_URL = EnvManager.get_required_setting("MODEL_PRICES_URL")
+ MODEL_PRICES_TTL_IN_DAYS = int(
+ EnvManager.get_required_setting("MODEL_PRICES_TTL_IN_DAYS")
+ )
+ MODEL_PRICES_FILE_PATH = EnvManager.get_required_setting("MODEL_PRICES_FILE_PATH")
+
+
+EnvManager.raise_for_missing_envs()
diff --git a/platform-service/src/unstract/platform_service/helper.py b/platform-service/src/unstract/platform_service/helper.py
deleted file mode 100644
index 2fa739dbe..000000000
--- a/platform-service/src/unstract/platform_service/helper.py
+++ /dev/null
@@ -1,185 +0,0 @@
-import json
-import os
-from datetime import datetime, timedelta
-from logging import Logger
-from typing import Any, Optional
-
-import peewee
-import requests
-from unstract.platform_service.constants import DBTableV2, FeatureFlag
-from unstract.platform_service.exceptions import CustomException
-
-from unstract.flags.feature_flag import check_feature_flag_status
-
-
-class AdapterInstanceRequestHelper:
- @staticmethod
- def get_adapter_instance_from_db(
- db_instance: peewee.PostgresqlDatabase,
- organization_id: str,
- adapter_instance_id: str,
- organization_uid: Optional[int] = None,
- ) -> dict[str, Any]:
- """Get adapter instance from Backend Database.
-
- Args:
- db_instance (peewee.PostgresqlDatabase): Backend DB
- organization_id (str): organization schema id
- adapter_instance_id (str): adapter instance id
-
- Returns:
- _type_: _description_
- """
- if check_feature_flag_status(FeatureFlag.MULTI_TENANCY_V2):
- query = (
- "SELECT id, adapter_id, adapter_metadata_b FROM "
- f"{DBTableV2.ADAPTER_INSTANCE} x "
- f"WHERE id='{adapter_instance_id}' and "
- f"organization_id='{organization_uid}'"
- )
- else:
- query = (
- f"SELECT id, adapter_id, adapter_metadata_b FROM "
- f'"{organization_id}".adapter_adapterinstance x '
- f"WHERE id='{adapter_instance_id}'"
- )
- cursor = db_instance.execute_sql(query)
- result_row = cursor.fetchone()
- if not result_row:
- raise CustomException(message="Adapter not found", code=404)
- columns = [desc[0] for desc in cursor.description]
- data_dict: dict[str, Any] = dict(zip(columns, result_row))
- cursor.close()
- db_instance.close()
- return data_dict
-
-
-class PromptStudioRequestHelper:
- @staticmethod
- def get_prompt_instance_from_db(
- db_instance: peewee.PostgresqlDatabase,
- organization_id: str,
- prompt_registry_id: str,
- ) -> dict[str, Any]:
- """Get prompt studio registry from Backend Database.
-
- Args:
- db_instance (peewee.PostgresqlDatabase): Backend DB
- organization_id (str): organization schema id
- prompt_registry_id (str): prompt_registry_id
-
- Returns:
- _type_: _description_
- """
- if check_feature_flag_status(FeatureFlag.MULTI_TENANCY_V2):
- query = (
- "SELECT prompt_registry_id, tool_spec, "
- "tool_metadata, tool_property FROM "
- f"{DBTableV2.PROMPT_STUDIO_REGISTRY} x "
- f"WHERE prompt_registry_id='{prompt_registry_id}'"
- )
- else:
- query = (
- f"SELECT prompt_registry_id, tool_spec, "
- f"tool_metadata, tool_property FROM "
- f'"{organization_id}".prompt_studio_registry_promptstudioregistry x'
- f" WHERE prompt_registry_id='{prompt_registry_id}'"
- )
- cursor = db_instance.execute_sql(query)
- result_row = cursor.fetchone()
- if not result_row:
- raise CustomException(message="Custom Tool not found", code=404)
- columns = [desc[0] for desc in cursor.description]
- data_dict: dict[str, Any] = dict(zip(columns, result_row))
- cursor.close()
- db_instance.close()
- return data_dict
-
-
-class CostCalculationHelper:
- def __init__(
- self,
- url: str,
- ttl_days: int,
- file_path: str,
- logger: Logger,
- ):
- self.ttl_days = ttl_days
- self.url = url
- self.file_path = file_path
- self.logger = logger
-
- def calculate_cost(self, model_name, provider, input_tokens, output_tokens):
- cost = 0.0
- item = None
- model_prices = self._get_model_prices()
- # Filter the model objects by model name
- filtered_models = {
- k: v for k, v in model_prices.items() if k.endswith(model_name)
- }
- # Check if the lite llm provider starts with the given provider
- for _, model_info in filtered_models.items():
- if provider in model_info.get("litellm_provider", ""):
- item = model_info
- break
- if item:
- input_cost_per_token = item.get("input_cost_per_token", 0)
- output_cost_per_token = item.get("output_cost_per_token", 0)
- cost += input_cost_per_token * input_tokens
- cost += output_cost_per_token * output_tokens
- return _format_float_positional(cost)
-
- def _get_model_prices(self):
- try:
- if os.path.exists(self.file_path):
- file_mtime = os.path.getmtime(self.file_path)
- file_expiry_date = datetime.fromtimestamp(file_mtime) + timedelta(
- days=self.ttl_days
- )
- if datetime.now() < file_expiry_date:
- # File exists and TTL has not expired, read and return content
- with open(self.file_path, encoding="utf-8") as f:
- return json.load(f)
- else:
- # TTL expired, fetch updated JSON data from API
- return self._fetch_and_save_json()
- else:
- # File does not exist, fetch JSON data from API
- return self._fetch_and_save_json()
- except Exception as e:
- self.logger.error("Error in calculate_cost: %s", e)
- return None
-
- def _fetch_and_save_json(self):
- try:
- # Fetch updated JSON data from API
- response = requests.get(self.url, timeout=10)
- if response.status_code == 200:
- json_data = response.json()
- # Save JSON data to file
- with open(self.file_path, "w", encoding="utf-8") as f:
- json.dump(json_data, f, ensure_ascii=False, indent=4)
- # Set the file's modification time to indicate TTL
- expiry_date = datetime.now() + timedelta(days=self.ttl_days)
- expiry_timestamp = expiry_date.timestamp()
- os.utime(self.file_path, (expiry_timestamp, expiry_timestamp))
- self.logger.info(
- "File '%s' updated successfully with TTL set to %d days.",
- self.file_path,
- self.ttl_days,
- )
- return json_data
- else:
- self.logger.error(
- "Failed to fetch data from API. Status code: %d",
- response.status_code,
- )
- return None
- except Exception as e:
- self.logger.error("Error fetching data from API: %s", e)
- return None
-
-
-def _format_float_positional(value: float, precision: int = 10) -> str:
- formatted: str = f"{value:.{precision}f}"
- return formatted.rstrip("0").rstrip(".") if "." in formatted else formatted
diff --git a/platform-service/src/unstract/platform_service/helper/adapter_instance.py b/platform-service/src/unstract/platform_service/helper/adapter_instance.py
new file mode 100644
index 000000000..3a4c6aa25
--- /dev/null
+++ b/platform-service/src/unstract/platform_service/helper/adapter_instance.py
@@ -0,0 +1,49 @@
+from typing import Any, Optional
+
+import peewee
+from unstract.platform_service.constants import DBTableV2, FeatureFlag
+from unstract.platform_service.exceptions import CustomException
+
+from unstract.flags.feature_flag import check_feature_flag_status
+
+
+class AdapterInstanceRequestHelper:
+ @staticmethod
+ def get_adapter_instance_from_db(
+ db_instance: peewee.PostgresqlDatabase,
+ organization_id: str,
+ adapter_instance_id: str,
+ organization_uid: Optional[int] = None,
+ ) -> dict[str, Any]:
+ """Get adapter instance from Backend Database.
+
+ Args:
+ db_instance (peewee.PostgresqlDatabase): Backend DB
+ organization_id (str): organization schema id
+ adapter_instance_id (str): adapter instance id
+
+ Returns:
+ _type_: _description_
+ """
+ if check_feature_flag_status(FeatureFlag.MULTI_TENANCY_V2):
+ query = (
+ "SELECT id, adapter_id, adapter_metadata_b FROM "
+ f"{DBTableV2.ADAPTER_INSTANCE} x "
+ f"WHERE id='{adapter_instance_id}' and "
+ f"organization_id='{organization_uid}'"
+ )
+ else:
+ query = (
+ f"SELECT id, adapter_id, adapter_metadata_b FROM "
+ f'"{organization_id}".adapter_adapterinstance x '
+ f"WHERE id='{adapter_instance_id}'"
+ )
+ cursor = db_instance.execute_sql(query)
+ result_row = cursor.fetchone()
+ if not result_row:
+ raise CustomException(message="Adapter not found", code=404)
+ columns = [desc[0] for desc in cursor.description]
+ data_dict: dict[str, Any] = dict(zip(columns, result_row))
+ cursor.close()
+ db_instance.close()
+ return data_dict
diff --git a/platform-service/src/unstract/platform_service/helper/cost_calculation.py b/platform-service/src/unstract/platform_service/helper/cost_calculation.py
new file mode 100644
index 000000000..24ca0051e
--- /dev/null
+++ b/platform-service/src/unstract/platform_service/helper/cost_calculation.py
@@ -0,0 +1,100 @@
+import json
+import os
+from datetime import datetime, timedelta
+from typing import Any, Optional
+
+import requests
+from flask import current_app as app
+from singleton_decorator import singleton
+from unstract.platform_service.env import Env
+from unstract.platform_service.utils import format_float_positional
+
+
+@singleton
+class CostCalculationHelper:
+ def __init__(
+ self,
+ url: str = Env.MODEL_PRICES_URL,
+ ttl_days: int = Env.MODEL_PRICES_TTL_IN_DAYS,
+ file_path: str = Env.MODEL_PRICES_FILE_PATH,
+ ):
+ self.ttl_days = ttl_days
+ self.url = url
+ self.file_path = file_path
+ self.model_token_data = self._get_model_token_data()
+
+ def calculate_cost(
+ self, model_name: str, provider: str, input_tokens: int, output_tokens: int
+ ) -> str:
+ cost = 0.0
+ item = None
+
+ if not self.model_token_data:
+ return format_float_positional(cost)
+ # Filter the model objects by model name
+ filtered_models = {
+ k: v for k, v in self.model_token_data.items() if k.endswith(model_name)
+ }
+ # Check if the lite llm provider starts with the given provider
+ for _, model_info in filtered_models.items():
+ if provider in model_info.get("litellm_provider", ""):
+ item = model_info
+ break
+ if item:
+ input_cost_per_token = item.get("input_cost_per_token", 0)
+ output_cost_per_token = item.get("output_cost_per_token", 0)
+ cost += input_cost_per_token * input_tokens
+ cost += output_cost_per_token * output_tokens
+ return format_float_positional(cost)
+
+ def _get_model_token_data(self) -> Optional[dict[str, Any]]:
+ try:
+ # File does not exist, fetch JSON data from API
+ if not os.path.exists(self.file_path):
+ return self._fetch_and_save_json()
+
+ file_mtime = os.path.getmtime(self.file_path)
+ file_expiry_date = datetime.fromtimestamp(file_mtime) + timedelta(
+ days=self.ttl_days
+ )
+ if datetime.now() < file_expiry_date:
+ app.logger.info(f"Reading model token data from {self.file_path}")
+ # File exists and TTL has not expired, read and return content
+ with open(self.file_path, encoding="utf-8") as f:
+ return json.load(f)
+ else:
+ # TTL expired, fetch updated JSON data from API
+ return self._fetch_and_save_json()
+ except Exception as e:
+ app.logger.error("Error in calculate_cost: %s", e)
+ return None
+
+ def _fetch_and_save_json(self) -> Optional[dict[str, Any]]:
+ """Fetch model's price and token data from the URL.
+
+ Caches it in a file with the mentioned TTL
+
+ Returns:
+ Optional[dict[str, Any]]: JSON of model and price / token data
+ """
+ try:
+ # Fetch updated JSON data from API
+ response = requests.get(self.url, timeout=10)
+ response.raise_for_status()
+ json_data = response.json()
+ # Save JSON data to file
+ with open(self.file_path, "w", encoding="utf-8") as f:
+ json.dump(json_data, f, ensure_ascii=False, indent=4)
+ # Set the file's modification time to indicate TTL
+ expiry_date = datetime.now() + timedelta(days=self.ttl_days)
+ expiry_timestamp = expiry_date.timestamp()
+ os.utime(self.file_path, (expiry_timestamp, expiry_timestamp))
+ app.logger.info(
+ "File '%s' updated successfully with TTL set to %d days.",
+ self.file_path,
+ self.ttl_days,
+ )
+ return json_data
+ except Exception as e:
+ app.logger.error("Error fetching data from API: %s", e)
+ return None
diff --git a/platform-service/src/unstract/platform_service/helper/prompt_studio.py b/platform-service/src/unstract/platform_service/helper/prompt_studio.py
new file mode 100644
index 000000000..1a05f1468
--- /dev/null
+++ b/platform-service/src/unstract/platform_service/helper/prompt_studio.py
@@ -0,0 +1,49 @@
+from typing import Any
+
+import peewee
+from unstract.platform_service.constants import DBTableV2, FeatureFlag
+from unstract.platform_service.exceptions import CustomException
+
+from unstract.flags.feature_flag import check_feature_flag_status
+
+
+class PromptStudioRequestHelper:
+ @staticmethod
+ def get_prompt_instance_from_db(
+ db_instance: peewee.PostgresqlDatabase,
+ organization_id: str,
+ prompt_registry_id: str,
+ ) -> dict[str, Any]:
+ """Get prompt studio registry from Backend Database.
+
+ Args:
+ db_instance (peewee.PostgresqlDatabase): Backend DB
+ organization_id (str): organization schema id
+ prompt_registry_id (str): prompt_registry_id
+
+ Returns:
+ _type_: _description_
+ """
+ if check_feature_flag_status(FeatureFlag.MULTI_TENANCY_V2):
+ query = (
+ "SELECT prompt_registry_id, tool_spec, "
+ "tool_metadata, tool_property FROM "
+ f"{DBTableV2.PROMPT_STUDIO_REGISTRY} x "
+ f"WHERE prompt_registry_id='{prompt_registry_id}'"
+ )
+ else:
+ query = (
+ f"SELECT prompt_registry_id, tool_spec, "
+ f"tool_metadata, tool_property FROM "
+ f'"{organization_id}".prompt_studio_registry_promptstudioregistry x'
+ f" WHERE prompt_registry_id='{prompt_registry_id}'"
+ )
+ cursor = db_instance.execute_sql(query)
+ result_row = cursor.fetchone()
+ if not result_row:
+ raise CustomException(message="Custom Tool not found", code=404)
+ columns = [desc[0] for desc in cursor.description]
+ data_dict: dict[str, Any] = dict(zip(columns, result_row))
+ cursor.close()
+ db_instance.close()
+ return data_dict
diff --git a/platform-service/src/unstract/platform_service/run.py b/platform-service/src/unstract/platform_service/run.py
new file mode 100644
index 000000000..ed81aff97
--- /dev/null
+++ b/platform-service/src/unstract/platform_service/run.py
@@ -0,0 +1,40 @@
+from logging.config import dictConfig
+
+from dotenv import load_dotenv
+from flask import Flask
+from unstract.platform_service.controller import api
+
+load_dotenv()
+
+dictConfig(
+ {
+ "version": 1,
+ "formatters": {
+ "default": {
+ "format": "[%(asctime)s] %(levelname)s in %(module)s: %(message)s",
+ }
+ },
+ "handlers": {
+ "wsgi": {
+ "class": "logging.StreamHandler",
+ "stream": "ext://flask.logging.wsgi_errors_stream",
+ "formatter": "default",
+ }
+ },
+ "root": {"level": "INFO", "handlers": ["wsgi"]},
+ }
+)
+
+
+def create_app() -> Flask:
+ app = Flask("platform_service")
+
+ app.register_blueprint(api)
+ return app
+
+
+app = create_app()
+
+if __name__ == "__main__":
+ # Start the server
+ app.run(host="0.0.0.0", port=3001, load_dotenv=True)
diff --git a/platform-service/src/unstract/platform_service/utils.py b/platform-service/src/unstract/platform_service/utils.py
index aa57b46bd..9f657dc98 100644
--- a/platform-service/src/unstract/platform_service/utils.py
+++ b/platform-service/src/unstract/platform_service/utils.py
@@ -39,3 +39,23 @@ class EnvManager:
cls.missing_settings
)
raise ValueError(ERROR_MESSAGE)
+
+
+def format_float_positional(value: float, precision: int = 10) -> str:
+ """Format floats to a string.
+
+ Formats a floating-point number to a string with the specified precision,
+ removing trailing zeros and the decimal point if not needed.
+
+ Args:
+ value (float): The floating-point number to format.
+ precision (int, optional): The number of decimal places to
+ include in the formatted output. Defaults to 10.
+
+ Returns:
+ str: The formatted string representation of the float,
+ with unnecessary trailing zeros and the decimal point
+ removed if the float is an integer.
+ """
+ formatted: str = f"{value:.{precision}f}"
+ return formatted.rstrip("0").rstrip(".") if "." in formatted else formatted