Skip to content

A Simple and Universal Swarm Intelligence Engine, Predicting Anything. 简洁通用的群体智能引擎,预测万物

Add MaxTokensWarningFilter to logging and set semaphore for LLM requests

666ghj0302b8fd70cf
diff --git a/backend/scripts/run_parallel_simulation.py b/backend/scripts/run_parallel_simulation.py
index e479b9dd..35fb47c4 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 2081bd2f..8b12d55c 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 470ad6c3..158492b3 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()