54 lines
1.7 KiB
Python
54 lines
1.7 KiB
Python
![]() |
import logging
|
||
|
import yaml
|
||
|
from datetime import datetime
|
||
|
from enum import Enum
|
||
|
from pathlib import Path
|
||
|
from fastapi import Request
|
||
|
from typing import Dict, Optional
|
||
|
|
||
|
class TaskStatus(str, Enum):
|
||
|
PROCESSING = "processing"
|
||
|
FAILED = "failed"
|
||
|
COMPLETED = "completed"
|
||
|
|
||
|
class FilterType(str, Enum):
|
||
|
RAW = "raw"
|
||
|
FIT = "fit"
|
||
|
BM25 = "bm25"
|
||
|
LLM = "llm"
|
||
|
|
||
|
def load_config() -> Dict:
|
||
|
"""Load and return application configuration."""
|
||
|
config_path = Path(__file__).parent / "config.yml"
|
||
|
with open(config_path, "r") as config_file:
|
||
|
return yaml.safe_load(config_file)
|
||
|
|
||
|
def setup_logging(config: Dict) -> None:
|
||
|
"""Configure application logging."""
|
||
|
logging.basicConfig(
|
||
|
level=config["logging"]["level"],
|
||
|
format=config["logging"]["format"]
|
||
|
)
|
||
|
|
||
|
def get_base_url(request: Request) -> str:
|
||
|
"""Get base URL including scheme and host."""
|
||
|
return f"{request.url.scheme}://{request.url.netloc}"
|
||
|
|
||
|
def is_task_id(value: str) -> bool:
|
||
|
"""Check if the value matches task ID pattern."""
|
||
|
return value.startswith("llm_") and "_" in value
|
||
|
|
||
|
def datetime_handler(obj: any) -> Optional[str]:
|
||
|
"""Handle datetime serialization for JSON."""
|
||
|
if hasattr(obj, 'isoformat'):
|
||
|
return obj.isoformat()
|
||
|
raise TypeError(f"Object of type {type(obj)} is not JSON serializable")
|
||
|
|
||
|
def should_cleanup_task(created_at: str) -> bool:
|
||
|
"""Check if task should be cleaned up based on creation time."""
|
||
|
created = datetime.fromisoformat(created_at)
|
||
|
return (datetime.now() - created).total_seconds() > 3600
|
||
|
|
||
|
def decode_redis_hash(hash_data: Dict[bytes, bytes]) -> Dict[str, str]:
|
||
|
"""Decode Redis hash data from bytes to strings."""
|
||
|
return {k.decode('utf-8'): v.decode('utf-8') for k, v in hash_data.items()}
|