From 0302b8fd70cf6ded39b8329c3816af0398c8f72f Mon Sep 17 00:00:00 2001 From: 666ghj <670939375@qq.com> Date: Thu, 4 Dec 2025 15:00:08 +0800 Subject: [PATCH] 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. --- backend/scripts/run_parallel_simulation.py | 16 ++++++++++++++++ backend/scripts/run_reddit_simulation.py | 15 +++++++++++++++ backend/scripts/run_twitter_simulation.py | 15 +++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/backend/scripts/run_parallel_simulation.py b/backend/scripts/run_parallel_simulation.py index e479b9d..35fb47c 100644 --- a/backend/scripts/run_parallel_simulation.py +++ b/backend/scripts/run_parallel_simulation.py @@ -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() diff --git a/backend/scripts/run_reddit_simulation.py b/backend/scripts/run_reddit_simulation.py index 2081bd2..8b12d55 100644 --- a/backend/scripts/run_reddit_simulation.py +++ b/backend/scripts/run_reddit_simulation.py @@ -54,6 +54,20 @@ class UnicodeFormatter(logging.Formatter): return self.UNICODE_ESCAPE_PATTERN.sub(replace_unicode, result) +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 setup_oasis_logging(log_dir: str): """配置 OASIS 的日志,使用固定名称的日志文件""" os.makedirs(log_dir, exist_ok=True) @@ -281,6 +295,7 @@ class RedditSimulationRunner: agent_graph=agent_graph, platform=oasis.DefaultPlatformType.REDDIT, database_path=db_path, + semaphore=30, # 限制最大并发 LLM 请求数,防止 API 过载 ) await env.reset() diff --git a/backend/scripts/run_twitter_simulation.py b/backend/scripts/run_twitter_simulation.py index 470ad6c..158492b 100644 --- a/backend/scripts/run_twitter_simulation.py +++ b/backend/scripts/run_twitter_simulation.py @@ -54,6 +54,20 @@ class UnicodeFormatter(logging.Formatter): return self.UNICODE_ESCAPE_PATTERN.sub(replace_unicode, result) +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 setup_oasis_logging(log_dir: str): """配置 OASIS 的日志,使用固定名称的日志文件""" os.makedirs(log_dir, exist_ok=True) @@ -296,6 +310,7 @@ class TwitterSimulationRunner: agent_graph=agent_graph, platform=oasis.DefaultPlatformType.TWITTER, database_path=db_path, + semaphore=30, # 限制最大并发 LLM 请求数,防止 API 过载 ) await env.reset()