config.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. """
  2. config.py — loads .env and exposes typed settings for the entire project.
  3. No LLM calls. No side effects. Just config.
  4. """
  5. import os
  6. from dataclasses import dataclass
  7. from dotenv import load_dotenv
  8. load_dotenv()
  9. @dataclass(frozen=True)
  10. class Config:
  11. # mem0 server
  12. mem0_base_url: str
  13. mem0_agent_id: str
  14. # Groq
  15. groq_api_key: str
  16. groq_model: str
  17. # Folder paths
  18. books_inbox: str
  19. books_processing: str
  20. books_done: str
  21. books_manifests: str
  22. # Chunking
  23. chunk_size_tokens: int
  24. # Safety cap — docs with more sections than this are treated as flat
  25. # Prevents token burn on crappy OCR'd PDFs with hundreds of fake chapters
  26. max_sections: int
  27. # Throttling — delay between POSTs to spare the embedder/GPU
  28. ingest_delay: float
  29. # Logging
  30. log_level: str
  31. def load_config() -> Config:
  32. def require(key: str) -> str:
  33. val = os.getenv(key)
  34. if not val:
  35. raise EnvironmentError(f"Missing required env var: {key}")
  36. return val
  37. return Config(
  38. mem0_base_url=require("MEM0_BASE_URL").rstrip("/"),
  39. mem0_agent_id=os.getenv("MEM0_AGENT_ID", "knowledge_base"),
  40. groq_api_key=require("GROQ_API_KEY"),
  41. groq_model=os.getenv("GROQ_MODEL", "meta-llama/llama-4-scout-17b-16e-instruct"),
  42. books_inbox=os.getenv("BOOKS_INBOX", "./books/inbox"),
  43. books_processing=os.getenv("BOOKS_PROCESSING", "./books/processing"),
  44. books_done=os.getenv("BOOKS_DONE", "./books/done"),
  45. books_manifests=os.getenv("BOOKS_MANIFESTS", "./books/manifests"),
  46. chunk_size_tokens=int(os.getenv("CHUNK_SIZE_TOKENS", "350")),
  47. max_sections=int(os.getenv("MAX_SECTIONS", "60")),
  48. ingest_delay=float(os.getenv("INGEST_DELAY", "0.5")),
  49. log_level=os.getenv("LOG_LEVEL", "INFO"),
  50. )
  51. # Singleton — import this everywhere
  52. cfg = load_config()