Spaces:
Sleeping
Sleeping
| from __future__ import annotations | |
| from pathlib import Path | |
| from importlib import metadata | |
| from pydantic import field_validator, Field | |
| from pydantic_settings import BaseSettings, SettingsConfigDict | |
| from pydantic_core.core_schema import ValidationInfo | |
| from phi.utils.log import logger | |
| PHI_CLI_DIR: Path = Path.home().resolve().joinpath(".phi") | |
| class PhiCliSettings(BaseSettings): | |
| app_name: str = "phi" | |
| app_version: str = metadata.version("phidata") | |
| tmp_token_path: Path = PHI_CLI_DIR.joinpath("tmp_token") | |
| config_file_path: Path = PHI_CLI_DIR.joinpath("config.json") | |
| credentials_path: Path = PHI_CLI_DIR.joinpath("credentials.json") | |
| ai_conversations_path: Path = PHI_CLI_DIR.joinpath("ai_conversations.json") | |
| auth_token_cookie: str = "__phi_session" | |
| auth_token_header: str = "X-PHIDATA-AUTH-TOKEN" | |
| api_runtime: str = "prd" | |
| api_enabled: bool = True | |
| alpha_features: bool = False | |
| api_url: str = Field("https://api.phidata.com", validate_default=True) | |
| signin_url: str = Field("https://phidata.app/login", validate_default=True) | |
| playground_url: str = Field("https://phidata.app/playground", validate_default=True) | |
| model_config = SettingsConfigDict(env_prefix="PHI_") | |
| def validate_runtime_env(cls, v): | |
| """Validate api_runtime.""" | |
| valid_api_runtimes = ["dev", "stg", "prd"] | |
| if v not in valid_api_runtimes: | |
| raise ValueError(f"Invalid api_runtime: {v}") | |
| return v | |
| def update_signin_url(cls, v, info: ValidationInfo): | |
| api_runtime = info.data["api_runtime"] | |
| if api_runtime == "dev": | |
| return "http://localhost:3000/login" | |
| elif api_runtime == "stg": | |
| return "https://stgphi.com/login" | |
| else: | |
| return "https://phidata.app/login" | |
| def update_playground_url(cls, v, info: ValidationInfo): | |
| api_runtime = info.data["api_runtime"] | |
| if api_runtime == "dev": | |
| return "http://localhost:3000/playground" | |
| elif api_runtime == "stg": | |
| return "https://stgphi.com/playground" | |
| else: | |
| return "https://phidata.app/playground" | |
| def update_api_url(cls, v, info: ValidationInfo): | |
| api_runtime = info.data["api_runtime"] | |
| if api_runtime == "dev": | |
| from os import getenv | |
| if getenv("PHI_RUNTIME") == "docker": | |
| return "http://host.docker.internal:7070" | |
| return "http://localhost:7070" | |
| elif api_runtime == "stg": | |
| return "https://api.stgphi.com" | |
| else: | |
| return "https://api.phidata.com" | |
| def gate_alpha_feature(self): | |
| if not self.alpha_features: | |
| logger.error("This is an Alpha feature not for general use.\nPlease message the phidata team for access.") | |
| exit(1) | |
| phi_cli_settings = PhiCliSettings() | |