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:
parent
2df3104840
commit
0302b8fd70
3 changed files with 46 additions and 0 deletions
|
|
@ -48,6 +48,20 @@ else:
|
||||||
print(f"已加载环境配置: {_backend_env}")
|
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():
|
def disable_oasis_logging():
|
||||||
"""
|
"""
|
||||||
禁用 OASIS 库的详细日志输出
|
禁用 OASIS 库的详细日志输出
|
||||||
|
|
@ -385,6 +399,7 @@ async def run_twitter_simulation(
|
||||||
agent_graph=agent_graph,
|
agent_graph=agent_graph,
|
||||||
platform=oasis.DefaultPlatformType.TWITTER,
|
platform=oasis.DefaultPlatformType.TWITTER,
|
||||||
database_path=db_path,
|
database_path=db_path,
|
||||||
|
semaphore=30, # 限制最大并发 LLM 请求数,防止 API 过载
|
||||||
)
|
)
|
||||||
|
|
||||||
await env.reset()
|
await env.reset()
|
||||||
|
|
@ -528,6 +543,7 @@ async def run_reddit_simulation(
|
||||||
agent_graph=agent_graph,
|
agent_graph=agent_graph,
|
||||||
platform=oasis.DefaultPlatformType.REDDIT,
|
platform=oasis.DefaultPlatformType.REDDIT,
|
||||||
database_path=db_path,
|
database_path=db_path,
|
||||||
|
semaphore=30, # 限制最大并发 LLM 请求数,防止 API 过载
|
||||||
)
|
)
|
||||||
|
|
||||||
await env.reset()
|
await env.reset()
|
||||||
|
|
|
||||||
|
|
@ -54,6 +54,20 @@ class UnicodeFormatter(logging.Formatter):
|
||||||
return self.UNICODE_ESCAPE_PATTERN.sub(replace_unicode, result)
|
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):
|
def setup_oasis_logging(log_dir: str):
|
||||||
"""配置 OASIS 的日志,使用固定名称的日志文件"""
|
"""配置 OASIS 的日志,使用固定名称的日志文件"""
|
||||||
os.makedirs(log_dir, exist_ok=True)
|
os.makedirs(log_dir, exist_ok=True)
|
||||||
|
|
@ -281,6 +295,7 @@ class RedditSimulationRunner:
|
||||||
agent_graph=agent_graph,
|
agent_graph=agent_graph,
|
||||||
platform=oasis.DefaultPlatformType.REDDIT,
|
platform=oasis.DefaultPlatformType.REDDIT,
|
||||||
database_path=db_path,
|
database_path=db_path,
|
||||||
|
semaphore=30, # 限制最大并发 LLM 请求数,防止 API 过载
|
||||||
)
|
)
|
||||||
|
|
||||||
await env.reset()
|
await env.reset()
|
||||||
|
|
|
||||||
|
|
@ -54,6 +54,20 @@ class UnicodeFormatter(logging.Formatter):
|
||||||
return self.UNICODE_ESCAPE_PATTERN.sub(replace_unicode, result)
|
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):
|
def setup_oasis_logging(log_dir: str):
|
||||||
"""配置 OASIS 的日志,使用固定名称的日志文件"""
|
"""配置 OASIS 的日志,使用固定名称的日志文件"""
|
||||||
os.makedirs(log_dir, exist_ok=True)
|
os.makedirs(log_dir, exist_ok=True)
|
||||||
|
|
@ -296,6 +310,7 @@ class TwitterSimulationRunner:
|
||||||
agent_graph=agent_graph,
|
agent_graph=agent_graph,
|
||||||
platform=oasis.DefaultPlatformType.TWITTER,
|
platform=oasis.DefaultPlatformType.TWITTER,
|
||||||
database_path=db_path,
|
database_path=db_path,
|
||||||
|
semaphore=30, # 限制最大并发 LLM 请求数,防止 API 过载
|
||||||
)
|
)
|
||||||
|
|
||||||
await env.reset()
|
await env.reset()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue