config.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. """配置加载:从 .env 文件或环境变量读取,风格对齐 ContentFindAgentNew。
  2. 只读环境变量,不硬编码密钥。PG 是 M1 唯一必填项;其余环节配置随里程碑推进逐步使用。
  3. """
  4. from __future__ import annotations
  5. import os
  6. from dataclasses import dataclass
  7. from pathlib import Path
  8. # 我们自己的过程库 schema(与 OPEN_AIGC_PG_SCHEMA=public 区分开)
  9. DEFAULT_PG_SCHEMA = "creation_knowledge"
  10. def load_env_file(env_path: str | Path = ".env") -> dict[str, str]:
  11. """解析 .env 为 dict;文件不存在则返回空。与 crawapi_http._load_env_file 同款。"""
  12. path = Path(env_path)
  13. if not path.exists():
  14. return {}
  15. env: dict[str, str] = {}
  16. for line in path.read_text(encoding="utf-8").splitlines():
  17. stripped = line.strip()
  18. if not stripped or stripped.startswith("#") or "=" not in stripped:
  19. continue
  20. key, value = stripped.split("=", 1)
  21. env[key.strip()] = value.strip().strip('"').strip("'")
  22. return env
  23. def env_value(
  24. key: str,
  25. file_env: dict[str, str],
  26. default: str | None = None,
  27. required: bool = False,
  28. ) -> str:
  29. """取值优先级:os.environ > .env 文件 > default。required 缺失时报错。"""
  30. value = os.getenv(key) or file_env.get(key) or default
  31. if required and not value:
  32. raise RuntimeError(f"missing required env: {key}")
  33. return value or ""
  34. @dataclass
  35. class PgConfig:
  36. """过程库(Greenplum open_aigc,schema creation_knowledge)连接配置。"""
  37. host: str
  38. port: int
  39. user: str
  40. password: str
  41. database: str
  42. schema: str = DEFAULT_PG_SCHEMA
  43. timeout: int = 10
  44. @classmethod
  45. def from_env(cls, env_file: str | Path = ".env") -> "PgConfig":
  46. file_env = load_env_file(env_file)
  47. return cls(
  48. host=env_value("OPEN_AIGC_PG_HOST", file_env, required=True),
  49. port=int(env_value("OPEN_AIGC_PG_PORT", file_env, "5432")),
  50. user=env_value("OPEN_AIGC_PG_USER", file_env, required=True),
  51. password=env_value("OPEN_AIGC_PG_PASSWORD", file_env, required=True),
  52. database=env_value(
  53. "OPEN_AIGC_PG_DB_NAME", file_env, "open_aigc"
  54. ),
  55. schema=env_value("CK_PG_SCHEMA", file_env, DEFAULT_PG_SCHEMA),
  56. )
  57. @dataclass
  58. class Settings:
  59. """全流程配置聚合。M1 只用到 pg;其余字段供 M2+ 使用。"""
  60. pg: PgConfig
  61. # 爬虫(M2):小红书/微信等走 aiddit;抖音单独走 piaoquantv。
  62. aiddit_crawler_base_url: str
  63. crawler_timeout: int
  64. # OpenRouter / Gemini(遗留视频链路和兼容烟测)
  65. openrouter_model: str
  66. openrouter_timeout_seconds: float
  67. openrouter_base_url: str
  68. openrouter_api_key: str
  69. # 文本步 LLM(闸门/拆分/解构)
  70. llm_model: str
  71. # 卡片 / 抽帧
  72. max_cards: int
  73. frames_dir: str
  74. douyin_ratio: str # 视频下载偏好码率(控成本/体积),如 540p
  75. data_dir: str # 媒体本地落盘根目录;空字符串=关闭媒体保存(解耦开关)
  76. # query 搜索 + OSS 转存(默认值必填:tests/test_video_extract.py 用关键字构造 Settings 且不传这些)
  77. oss_upload_url: str = "http://crawler-upload-v2.aiddit.com/crawler/oss/upload_stream"
  78. oss_upload_timeout_seconds: int = 60
  79. search_default_limit: int = 5
  80. search_content_type: str = "图文"
  81. search_sort_type: str = "综合"
  82. # 抖音走独立后端(piaoquantv,绕开 aiddit 限流):host / account_id / cookie_batch 单独配
  83. piaoquantv_douyin_base_url: str = "http://crawapi.piaoquantv.com"
  84. piaoquantv_douyin_account_id: str = "7450041106378522636"
  85. piaoquantv_douyin_cookie_batch: str = "default"
  86. # Aliyun Bailian / Qwen(旧解构图文读懂 + 文本闸门/拆解)。
  87. # 带默认值,保护旧测试里直接 Settings(...) 的关键字构造。
  88. bailian_api_key: str = ""
  89. bailian_base_url: str = "https://dashscope.aliyuncs.com/compatible-mode/v1"
  90. bailian_vl_model: str = "qwen-vl-plus"
  91. bailian_text_model: str = "qwen-plus"
  92. bailian_timeout_seconds: float = 120.0
  93. @classmethod
  94. def from_env(cls, env_file: str | Path = ".env") -> "Settings":
  95. file_env = load_env_file(env_file)
  96. return cls(
  97. pg=PgConfig.from_env(env_file),
  98. aiddit_crawler_base_url=env_value(
  99. "AIDDIT_CRAWLER_BASE_URL", file_env,
  100. "http://crawler.aiddit.com",
  101. ),
  102. crawler_timeout=int(env_value(
  103. "AIDDIT_CRAWLER_TIMEOUT_SECONDS", file_env,
  104. "30",
  105. )),
  106. openrouter_model=env_value(
  107. "OPENROUTER_MODEL", file_env,
  108. "google/gemini-3-flash-preview",
  109. ),
  110. openrouter_timeout_seconds=float(env_value(
  111. "OPENROUTER_TIMEOUT_SECONDS", file_env,
  112. "90",
  113. )),
  114. openrouter_base_url=env_value(
  115. "OPENROUTER_BASE_URL", file_env, "https://openrouter.ai/api/v1"
  116. ),
  117. openrouter_api_key=env_value("OPENROUTER_API_KEY", file_env),
  118. bailian_api_key=env_value("ALIYUN_BAILIAN_API_KEY", file_env),
  119. bailian_base_url=env_value(
  120. "ALIYUN_BAILIAN_BASE_URL", file_env,
  121. "https://dashscope.aliyuncs.com/compatible-mode/v1",
  122. ),
  123. bailian_vl_model=env_value(
  124. "ALIYUN_BAILIAN_VL_MODEL", file_env,
  125. env_value("ALIYUN_BAILIAN_MODEL", file_env, "qwen-vl-plus"),
  126. ),
  127. bailian_text_model=env_value(
  128. "ALIYUN_BAILIAN_TEXT_MODEL", file_env,
  129. env_value("ALIYUN_BAILIAN_MODEL", file_env, "qwen-plus"),
  130. ),
  131. bailian_timeout_seconds=float(env_value(
  132. "ALIYUN_BAILIAN_TIMEOUT_SECONDS", file_env,
  133. "120",
  134. )),
  135. # 文本步(筛选/拆分/解构)默认走百炼 Qwen;可用 CK_STAGE_MODEL 覆盖
  136. llm_model=env_value(
  137. "CK_STAGE_MODEL", file_env,
  138. env_value("ALIYUN_BAILIAN_TEXT_MODEL", file_env,
  139. env_value("ALIYUN_BAILIAN_MODEL", file_env, "qwen-plus")),
  140. ),
  141. max_cards=int(env_value("CK_MAX_CARDS", file_env, "12")),
  142. frames_dir=env_value("CK_FRAMES_DIR", file_env, "runtime/frames"),
  143. douyin_ratio=env_value("CK_DOUYIN_RATIO", file_env, "540p"),
  144. data_dir=env_value("CK_DATA_DIR", file_env, "data"),
  145. oss_upload_url=env_value(
  146. "CRAWLER_OSS_UPLOAD_URL", file_env,
  147. "http://crawler-upload-v2.aiddit.com/crawler/oss/upload_stream",
  148. ),
  149. oss_upload_timeout_seconds=int(
  150. env_value(
  151. "CRAWLER_OSS_UPLOAD_TIMEOUT_SECONDS", file_env,
  152. "60",
  153. )
  154. ),
  155. search_default_limit=int(env_value(
  156. "CREATION_SEARCH_DEFAULT_LIMIT", file_env,
  157. "5",
  158. )),
  159. search_content_type=env_value(
  160. "CREATION_SEARCH_CONTENT_TYPE", file_env,
  161. "图文",
  162. ),
  163. search_sort_type=env_value(
  164. "CREATION_SEARCH_SORT_TYPE", file_env,
  165. "综合",
  166. ),
  167. piaoquantv_douyin_base_url=env_value(
  168. "PIAOQUANTV_DOUYIN_BASE_URL", file_env,
  169. "http://crawapi.piaoquantv.com",
  170. ),
  171. piaoquantv_douyin_account_id=env_value(
  172. "PIAOQUANTV_DOUYIN_ACCOUNT_ID", file_env,
  173. "7450041106378522636",
  174. ),
  175. piaoquantv_douyin_cookie_batch=env_value(
  176. "PIAOQUANTV_DOUYIN_COOKIE_BATCH", file_env,
  177. "default",
  178. ),
  179. )
  180. @property
  181. def crawler_base_url(self) -> str:
  182. return self.aiddit_crawler_base_url
  183. @property
  184. def douyin_base_url(self) -> str:
  185. return self.piaoquantv_douyin_base_url
  186. @property
  187. def douyin_account_id(self) -> str:
  188. return self.piaoquantv_douyin_account_id
  189. @property
  190. def douyin_cookie_batch(self) -> str:
  191. return self.piaoquantv_douyin_cookie_batch
  192. @property
  193. def video_model(self) -> str:
  194. return self.openrouter_model