Upload agentframe/config.py with huggingface_hub
Browse files- agentframe/config.py +92 -0
agentframe/config.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""AgentFrame 配置系统: 环境变量 + JSON 配置文件 + 默认值"""
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
from dataclasses import dataclass, field, asdict
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
@dataclass
|
| 8 |
+
class LLMConfig:
|
| 9 |
+
"""LLM Provider 配置"""
|
| 10 |
+
provider: str = "deepseek" # deepseek | openai | mock
|
| 11 |
+
api_key: str = "" # 从环境变量 AGENTFRAME_API_KEY 或 DEEPSEEK_API_KEY
|
| 12 |
+
base_url: str = "https://api.deepseek.com/v1"
|
| 13 |
+
model: str = "deepseek-v4-pro" # 主模型
|
| 14 |
+
fast_model: str = "deepseek-v4-flash" # 快速模型 (检测/摘要)
|
| 15 |
+
max_tokens: int = 2000
|
| 16 |
+
temperature: float = 0.8
|
| 17 |
+
timeout: int = 120
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
@dataclass
|
| 21 |
+
class MemoryConfig:
|
| 22 |
+
"""上下文保持核心配置"""
|
| 23 |
+
n_layers: int = 27 # 模型层数 (DeepSeek-V2 27 层)
|
| 24 |
+
quant_bits: int = 4 # 量化位宽 (4 = INT4, 35.6x)
|
| 25 |
+
top_k: int = 32 # 路由检索 top-k
|
| 26 |
+
vram_limit_mb: int = 10240 # 显存层上限
|
| 27 |
+
ram_limit_mb: int = 32768 # 内存层上限
|
| 28 |
+
seed: int = 42
|
| 29 |
+
reversible: bool = False # 可逆量化开关
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
@dataclass
|
| 33 |
+
class APIConfig:
|
| 34 |
+
"""API 服务配置"""
|
| 35 |
+
host: str = "0.0.0.0"
|
| 36 |
+
port: int = 8090
|
| 37 |
+
debug: bool = False
|
| 38 |
+
max_history_turns: int = 12 # 对话历史保留轮数
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
@dataclass
|
| 42 |
+
class AgentFrameConfig:
|
| 43 |
+
"""总配置"""
|
| 44 |
+
llm: LLMConfig = field(default_factory=LLMConfig)
|
| 45 |
+
memory: MemoryConfig = field(default_factory=MemoryConfig)
|
| 46 |
+
api: APIConfig = field(default_factory=APIConfig)
|
| 47 |
+
|
| 48 |
+
@classmethod
|
| 49 |
+
def from_env(cls) -> "AgentFrameConfig":
|
| 50 |
+
"""从环境变量加载 (最高优先级)"""
|
| 51 |
+
cfg = cls()
|
| 52 |
+
cfg.llm.api_key = (
|
| 53 |
+
os.environ.get("AGENTFRAME_API_KEY")
|
| 54 |
+
or os.environ.get("DEEPSEEK_API_KEY")
|
| 55 |
+
or cfg.llm.api_key
|
| 56 |
+
)
|
| 57 |
+
cfg.llm.base_url = os.environ.get("AGENTFRAME_BASE_URL", cfg.llm.base_url)
|
| 58 |
+
cfg.llm.model = os.environ.get("AGENTFRAME_MODEL", cfg.llm.model)
|
| 59 |
+
cfg.llm.fast_model = os.environ.get("AGENTFRAME_FAST_MODEL", cfg.llm.fast_model)
|
| 60 |
+
cfg.api.port = int(os.environ.get("AGENTFRAME_PORT", cfg.api.port))
|
| 61 |
+
return cfg
|
| 62 |
+
|
| 63 |
+
@classmethod
|
| 64 |
+
def from_file(cls, path: str) -> "AgentFrameConfig":
|
| 65 |
+
"""从 JSON 配置文件加载"""
|
| 66 |
+
with open(path) as f:
|
| 67 |
+
data = json.load(f)
|
| 68 |
+
cfg = cls.from_env()
|
| 69 |
+
# 覆盖: 文件 < 环境变量
|
| 70 |
+
if "llm" in data:
|
| 71 |
+
for k, v in data["llm"].items():
|
| 72 |
+
setattr(cfg.llm, k, v)
|
| 73 |
+
if "memory" in data:
|
| 74 |
+
for k, v in data["memory"].items():
|
| 75 |
+
setattr(cfg.memory, k, v)
|
| 76 |
+
if "api" in data:
|
| 77 |
+
for k, v in data["api"].items():
|
| 78 |
+
setattr(cfg.api, k, v)
|
| 79 |
+
return cfg
|
| 80 |
+
|
| 81 |
+
def to_dict(self) -> dict:
|
| 82 |
+
"""导出为 dict (序列化用)"""
|
| 83 |
+
return {
|
| 84 |
+
"llm": asdict(self.llm),
|
| 85 |
+
"memory": asdict(self.memory),
|
| 86 |
+
"api": asdict(self.api),
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
def save(self, path: str):
|
| 90 |
+
"""保存配置到 JSON"""
|
| 91 |
+
with open(path, "w") as f:
|
| 92 |
+
json.dump(self.to_dict(), f, indent=2, ensure_ascii=False)
|