Add MaxTokensWarningFilter to logging and set semaphore for LLM requests

- Introduced MaxTokensWarningFilter to suppress specific warnings related to max_tokens in the logging output across simulation scripts.
- Added a semaphore parameter to limit the maximum concurrent LLM requests in Twitter and Reddit simulation functions, preventing API overload.
- Ensured the filter is applied immediately upon module loading for effective logging management.
This commit is contained in:
666ghj
2025-12-04 15:00:08 +08:00
parent 2df3104840
commit 0302b8fd70
3 changed files with 46 additions and 0 deletions

View File

@@ -48,6 +48,20 @@ else:
print(f"已加载环境配置: {_backend_env}")
class MaxTokensWarningFilter(logging.Filter):
"""过滤掉 camel-ai 关于 max_tokens 的警告(我们故意不设置 max_tokens让模型自行决定"""
def filter(self, record):
# 过滤掉包含 max_tokens 警告的日志
if "max_tokens" in record.getMessage() and "Invalid or missing" in record.getMessage():
return False
return True
# 在模块加载时立即添加过滤器,确保在 camel 代码执行前生效
logging.getLogger().addFilter(MaxTokensWarningFilter())
def disable_oasis_logging():
"""
禁用 OASIS 库的详细日志输出
@@ -385,6 +399,7 @@ async def run_twitter_simulation(
agent_graph=agent_graph,
platform=oasis.DefaultPlatformType.TWITTER,
database_path=db_path,
semaphore=30, # 限制最大并发 LLM 请求数,防止 API 过载
)
await env.reset()
@@ -528,6 +543,7 @@ async def run_reddit_simulation(
agent_graph=agent_graph,
platform=oasis.DefaultPlatformType.REDDIT,
database_path=db_path,
semaphore=30, # 限制最大并发 LLM 请求数,防止 API 过载
)
await env.reset()