-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconfig.py
More file actions
129 lines (108 loc) · 4.81 KB
/
config.py
File metadata and controls
129 lines (108 loc) · 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# =======================================================================
# i3T4AN (Ethan Blair)
# Project: Vector Knowledge Base
# File: Backend configuration settings
# =======================================================================
import os
from typing import List, Set
from pydantic_settings import BaseSettings
from pydantic import Field
import logging
logger = logging.getLogger(__name__)
def detect_device() -> str:
"""
Auto-detect the best available compute device.
Priority: MPS (Apple Silicon) > CUDA (NVIDIA) > CPU
"""
try:
import torch
# Check for Apple Silicon MPS
if hasattr(torch.backends, 'mps') and torch.backends.mps.is_available():
logger.info("GPU detected: Apple MPS (Metal Performance Shaders)")
return "mps"
# Check for NVIDIA CUDA
if torch.cuda.is_available():
device_name = torch.cuda.get_device_name(0)
logger.info(f"GPU detected: CUDA ({device_name})")
return "cuda"
logger.info("No GPU detected, using CPU")
return "cpu"
except ImportError:
logger.warning("PyTorch not installed, defaulting to CPU")
return "cpu"
except Exception as e:
logger.warning(f"Device detection failed: {e}, defaulting to CPU")
return "cpu"
class Settings(BaseSettings):
# Qdrant Configuration
QDRANT_HOST: str = Field(default="localhost", description="Qdrant server host")
QDRANT_PORT: int = Field(default=6333, description="Qdrant server port")
QDRANT_COLLECTION: str = Field(default="vector_db", description="Name of the Qdrant collection")
# Upload Configuration
UPLOAD_DIR: str = Field(default="uploads", description="Directory to store uploaded files")
MAX_FILE_SIZE: int = Field(default=50 * 1024 * 1024, description="Maximum file size in bytes (default 50MB)")
ALLOWED_EXTENSIONS: Set[str] = Field(default={
".pdf", ".docx", ".pptx", ".ppt", ".xlsx", ".csv",
".jpg", ".jpeg", ".png", ".webp",
".txt", ".md",
".py", ".js", ".java", ".cpp", ".html", ".css", ".json", ".xml", ".yaml", ".yml", ".cs"
}, description="Allowed file extensions")
# Embedding Model Configuration
EMBEDDING_MODEL: str = Field(default="all-mpnet-base-v2", description="SentenceTransformer model name")
CHUNK_SIZE: int = Field(default=500, description="Text chunk size for embeddings")
CHUNK_OVERLAP: int = Field(default=50, description="Overlap between text chunks")
# Compute Device Configuration
DEVICE: str = Field(default="auto", description="Compute device: 'auto', 'cpu', 'cuda', or 'mps'")
# CORS Configuration (comma-separated list of allowed origins)
CORS_ORIGINS: List[str] = Field(
default=["http://localhost:8001", "http://127.0.0.1:8001"],
description="Allowed CORS origins. Use ['*'] to allow all (not recommended for production)"
)
# Admin Key for destructive operations
ADMIN_KEY: str = Field(
default="",
description="Admin key for destructive operations (reset). Leave empty to disable protection."
)
# Rate Limiting (absurdly high defaults for personal use - won't affect normal usage)
RATE_LIMIT_UPLOAD: str = Field(
default="1000/minute",
description="Rate limit for upload endpoints (e.g., '10/minute', '100/hour')"
)
RATE_LIMIT_SEARCH: str = Field(
default="1000/minute",
description="Rate limit for search endpoint"
)
RATE_LIMIT_RESET: str = Field(
default="60/minute",
description="Rate limit for reset endpoint (stricter for safety)"
)
# MCP Server Configuration (Model Context Protocol for AI agents)
MCP_ENABLED: bool = Field(
default=True,
description="Enable/disable the MCP server endpoint"
)
MCP_PATH: str = Field(
default="/mcp",
description="URL path for the MCP server endpoint"
)
MCP_NAME: str = Field(
default="Vector Knowledge Base",
description="Display name for the MCP server"
)
MCP_AUTH_ENABLED: bool = Field(
default=False,
description="Enable OAuth authentication for MCP (recommended for production)"
)
class Config:
env_file = "../.env" # Look in project root, not backend/
env_file_encoding = "utf-8"
case_sensitive = True
# Create global settings instance
settings = Settings()
# Resolve device if set to auto
if settings.DEVICE == "auto":
settings.DEVICE = detect_device()
else:
logger.info(f"Using manually configured device: {settings.DEVICE}")
# Ensure upload directory exists
os.makedirs(settings.UPLOAD_DIR, exist_ok=True)