Update .env.example and enhance create_model function for dual LLM configuration

- Updated .env.example to include new keys for dual LLM configuration, allowing for both general and boost settings.
- Modified create_model function to support an optional use_boost parameter, enabling the selection of either general or boost LLM configurations based on availability.
- Improved logging to indicate which LLM configuration is being used during model creation, enhancing clarity for users.
This commit is contained in:
666ghj 2025-12-04 15:51:25 +08:00
parent 0302b8fd70
commit 39253b3213
2 changed files with 45 additions and 21 deletions

View file

@ -1,8 +1,12 @@
# Zep Cloud API Key
# 从 https://app.getzep.com 获取
ZEP_API_KEY=
# ===== ZEP记忆图谱配置 =====
ZEP_API_KEY=your_zep_api_key_here
# LLM API Key
LLM_API_KEY=
LLM_BASE_URL=
LLM_MODEL_NAME=
# ===== 通用 LLM 配置 =====
LLM_API_KEY=your_api_key_here
LLM_BASE_URL=https://api.openai.com/v1
LLM_MODEL_NAME=gpt-4o-mini
# ===== 加速 LLM 配置(可选)=====
LLM_BOOST_API_KEY=your_boost_api_key_here
LLM_BOOST_BASE_URL=https://another-api-provider.com/v1
LLM_BOOST_MODEL_NAME=gpt-4o-mini

View file

@ -267,23 +267,41 @@ def fetch_new_actions_from_db(
return actions, new_last_rowid
def create_model(config: Dict[str, Any]):
def create_model(config: Dict[str, Any], use_boost: bool = False):
"""
创建LLM模型
统一使用项目根目录 .env 文件中的配置优先级最高
- LLM_API_KEY: API密钥
- LLM_BASE_URL: API基础URL
- LLM_MODEL_NAME: 模型名称
支持双 LLM 配置用于并行模拟时提速
- 通用配置LLM_API_KEY, LLM_BASE_URL, LLM_MODEL_NAME
- 加速配置可选LLM_BOOST_API_KEY, LLM_BOOST_BASE_URL, LLM_BOOST_MODEL_NAME
OASIS使用camel-ai的ModelFactory需要设置 OPENAI_API_KEY OPENAI_API_BASE_URL 环境变量
如果配置了加速 LLM并行模拟时可以让不同平台使用不同的 API 服务商提高并发能力
Args:
config: 模拟配置字典
use_boost: 是否使用加速 LLM 配置如果可用
"""
# 优先从 .env 读取配置
llm_api_key = os.environ.get("LLM_API_KEY", "")
llm_base_url = os.environ.get("LLM_BASE_URL", "")
llm_model = os.environ.get("LLM_MODEL_NAME", "")
# 检查是否有加速配置
boost_api_key = os.environ.get("LLM_BOOST_API_KEY", "")
boost_base_url = os.environ.get("LLM_BOOST_BASE_URL", "")
boost_model = os.environ.get("LLM_BOOST_MODEL_NAME", "")
has_boost_config = bool(boost_api_key)
# 如果 .env 中没有,则使用 config 作为备用
# 根据参数和配置情况选择使用哪个 LLM
if use_boost and has_boost_config:
# 使用加速配置
llm_api_key = boost_api_key
llm_base_url = boost_base_url
llm_model = boost_model or os.environ.get("LLM_MODEL_NAME", "")
config_label = "[加速LLM]"
else:
# 使用通用配置
llm_api_key = os.environ.get("LLM_API_KEY", "")
llm_base_url = os.environ.get("LLM_BASE_URL", "")
llm_model = os.environ.get("LLM_MODEL_NAME", "")
config_label = "[通用LLM]"
# 如果 .env 中没有模型名,则使用 config 作为备用
if not llm_model:
llm_model = config.get("llm_model", "gpt-4o-mini")
@ -297,7 +315,7 @@ def create_model(config: Dict[str, Any]):
if llm_base_url:
os.environ["OPENAI_API_BASE_URL"] = llm_base_url
print(f"LLM配置: model={llm_model}, base_url={llm_base_url[:40] if llm_base_url else '默认'}...")
print(f"{config_label} model={llm_model}, base_url={llm_base_url[:40] if llm_base_url else '默认'}...")
return ModelFactory.create(
model_platform=ModelPlatformType.OPENAI,
@ -372,7 +390,8 @@ async def run_twitter_simulation(
log_info("初始化...")
model = create_model(config)
# Twitter 使用通用 LLM 配置
model = create_model(config, use_boost=False)
# OASIS Twitter使用CSV格式
profile_path = os.path.join(simulation_dir, "twitter_profiles.csv")
@ -517,7 +536,8 @@ async def run_reddit_simulation(
log_info("初始化...")
model = create_model(config)
# Reddit 使用加速 LLM 配置(如果有的话,否则回退到通用配置)
model = create_model(config, use_boost=True)
profile_path = os.path.join(simulation_dir, "reddit_profiles.json")
if not os.path.exists(profile_path):