config.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. """当下供需 gap 脚本主驱流程配置。"""
  2. from __future__ import annotations
  3. import os
  4. from app.core.config import settings
  5. from app.gap_script_demand.exceptions import GapScriptDemandError
  6. def _get_env(name: str, default: str = "") -> str:
  7. value = os.getenv(name)
  8. if value is None or value == "":
  9. return default
  10. return value
  11. def _get_env_int(name: str, default: int) -> int:
  12. raw = os.getenv(name)
  13. if raw is None or raw == "":
  14. return default
  15. try:
  16. return int(raw)
  17. except ValueError as exc:
  18. raise GapScriptDemandError(f"invalid integer env {name}={raw!r}") from exc
  19. def _get_env_float(name: str, default: float) -> float:
  20. raw = os.getenv(name)
  21. if raw is None or raw == "":
  22. return default
  23. try:
  24. return float(raw)
  25. except ValueError as exc:
  26. raise GapScriptDemandError(f"invalid float env {name}={raw!r}") from exc
  27. def _parse_cron_hours(value: str) -> str:
  28. hours = [item.strip() for item in value.split(",") if item.strip()]
  29. if not hours:
  30. raise GapScriptDemandError("gap script demand cron hours cannot be empty")
  31. normalized: list[str] = []
  32. for hour in hours:
  33. try:
  34. hour_num = int(hour)
  35. except ValueError as exc:
  36. raise GapScriptDemandError(f"invalid gap script demand cron hour: {hour!r}") from exc
  37. if not 0 <= hour_num <= 23:
  38. raise GapScriptDemandError(f"gap script demand cron hour out of range: {hour_num}")
  39. normalized.append(str(hour_num))
  40. return ",".join(normalized)
  41. def load_gap_script_demand_config() -> "GapScriptDemandConfig":
  42. from app.gap_script_demand.types import GapScriptDemandConfig
  43. llm_model = _get_env("GAP_SCRIPT_DEMAND_LLM_MODEL", settings.gap_script_demand_llm_model)
  44. config = GapScriptDemandConfig(
  45. cron_hours=_parse_cron_hours(
  46. _get_env("GAP_SCRIPT_DEMAND_CRON_HOURS", settings.gap_script_demand_cron_hours)
  47. ),
  48. cron_minute=_get_env_int(
  49. "GAP_SCRIPT_DEMAND_CRON_MINUTE",
  50. settings.gap_script_demand_cron_minute,
  51. ),
  52. llm_model=llm_model.strip() or settings.open_router_default_model,
  53. llm_max_attempts=_get_env_int(
  54. "GAP_SCRIPT_DEMAND_LLM_MAX_ATTEMPTS",
  55. settings.gap_script_demand_llm_max_attempts,
  56. ),
  57. llm_retry_sleep_seconds=_get_env_float(
  58. "GAP_SCRIPT_DEMAND_LLM_RETRY_SLEEP_SECONDS",
  59. settings.gap_script_demand_llm_retry_sleep_seconds,
  60. ),
  61. llm_max_tokens=_get_env_int(
  62. "GAP_SCRIPT_DEMAND_LLM_MAX_TOKENS",
  63. settings.gap_script_demand_llm_max_tokens,
  64. ),
  65. llm_temperature=_get_env_float(
  66. "GAP_SCRIPT_DEMAND_LLM_TEMPERATURE",
  67. settings.gap_script_demand_llm_temperature,
  68. ),
  69. demand_pool_source_table=_get_env(
  70. "DEMAND_POOL_SOURCE_TABLE",
  71. settings.demand_pool_source_table,
  72. ).strip(),
  73. demand_pool_strategy=_get_env(
  74. "GAP_SCRIPT_DEMAND_POOL_STRATEGY",
  75. settings.gap_script_demand_pool_strategy,
  76. ).strip(),
  77. output_strategy=_get_env(
  78. "GAP_SCRIPT_DEMAND_OUTPUT_STRATEGY",
  79. settings.gap_script_demand_output_strategy,
  80. ).strip(),
  81. judge_batch_size=_get_env_int(
  82. "GAP_SCRIPT_DEMAND_JUDGE_BATCH_SIZE",
  83. settings.gap_script_demand_judge_batch_size,
  84. ),
  85. )
  86. if config.judge_batch_size <= 0:
  87. raise GapScriptDemandError("GAP_SCRIPT_DEMAND_JUDGE_BATCH_SIZE must be positive")
  88. return config