fix 修复了点小bug
This commit is contained in:
@@ -27,7 +27,7 @@ class ScheduleGenerator:
|
||||
def __init__(self, ):
|
||||
# 使用离线LLM模型
|
||||
self.llm_scheduler_all = LLM_request(
|
||||
model= global_config.llm_reasoning, temperature=0.9, max_tokens=2048,request_type="schedule")
|
||||
model= global_config.llm_reasoning, temperature=0.9, max_tokens=7000,request_type="schedule")
|
||||
self.llm_scheduler_doing = LLM_request(
|
||||
model= global_config.llm_normal, temperature=0.9, max_tokens=2048,request_type="schedule")
|
||||
|
||||
@@ -43,7 +43,7 @@ class ScheduleGenerator:
|
||||
|
||||
self.start_time = datetime.datetime.now()
|
||||
|
||||
self.schedule_doing_update_interval = 60 #最好大于60
|
||||
self.schedule_doing_update_interval = 300 #最好大于60
|
||||
|
||||
def initialize(self,name: str = "bot_name", personality: str = "你是一个爱国爱党的新时代青年", behavior: str = "你非常外向,喜欢尝试新事物和人交流",interval: int = 60):
|
||||
"""初始化日程系统"""
|
||||
|
||||
@@ -20,6 +20,13 @@ class LLMStatistics:
|
||||
self.output_file = output_file
|
||||
self.running = False
|
||||
self.stats_thread = None
|
||||
self._init_database()
|
||||
|
||||
def _init_database(self):
|
||||
"""初始化数据库集合"""
|
||||
if "online_time" not in db.list_collection_names():
|
||||
db.create_collection("online_time")
|
||||
db.online_time.create_index([("timestamp", 1)])
|
||||
|
||||
def start(self):
|
||||
"""启动统计线程"""
|
||||
@@ -35,6 +42,16 @@ class LLMStatistics:
|
||||
if self.stats_thread:
|
||||
self.stats_thread.join()
|
||||
|
||||
def _record_online_time(self):
|
||||
"""记录在线时间"""
|
||||
try:
|
||||
db.online_time.insert_one({
|
||||
"timestamp": datetime.now(),
|
||||
"duration": 5 # 5分钟
|
||||
})
|
||||
except Exception:
|
||||
logger.exception("记录在线时间失败")
|
||||
|
||||
def _collect_statistics_for_period(self, start_time: datetime) -> Dict[str, Any]:
|
||||
"""收集指定时间段的LLM请求统计数据
|
||||
|
||||
@@ -56,10 +73,11 @@ class LLMStatistics:
|
||||
"tokens_by_type": defaultdict(int),
|
||||
"tokens_by_user": defaultdict(int),
|
||||
"tokens_by_model": defaultdict(int),
|
||||
# 新增在线时间统计
|
||||
"online_time_minutes": 0,
|
||||
}
|
||||
|
||||
cursor = db.llm_usage.find({"timestamp": {"$gte": start_time}})
|
||||
|
||||
total_requests = 0
|
||||
|
||||
for doc in cursor:
|
||||
@@ -74,7 +92,7 @@ class LLMStatistics:
|
||||
|
||||
prompt_tokens = doc.get("prompt_tokens", 0)
|
||||
completion_tokens = doc.get("completion_tokens", 0)
|
||||
total_tokens = prompt_tokens + completion_tokens # 根据数据库字段调整
|
||||
total_tokens = prompt_tokens + completion_tokens
|
||||
stats["tokens_by_type"][request_type] += total_tokens
|
||||
stats["tokens_by_user"][user_id] += total_tokens
|
||||
stats["tokens_by_model"][model_name] += total_tokens
|
||||
@@ -91,6 +109,11 @@ class LLMStatistics:
|
||||
if total_requests > 0:
|
||||
stats["average_tokens"] = stats["total_tokens"] / total_requests
|
||||
|
||||
# 统计在线时间
|
||||
online_time_cursor = db.online_time.find({"timestamp": {"$gte": start_time}})
|
||||
for doc in online_time_cursor:
|
||||
stats["online_time_minutes"] += doc.get("duration", 0)
|
||||
|
||||
return stats
|
||||
|
||||
def _collect_all_statistics(self) -> Dict[str, Dict[str, Any]]:
|
||||
@@ -115,7 +138,8 @@ class LLMStatistics:
|
||||
output.append(f"总请求数: {stats['total_requests']}")
|
||||
if stats["total_requests"] > 0:
|
||||
output.append(f"总Token数: {stats['total_tokens']}")
|
||||
output.append(f"总花费: {stats['total_cost']:.4f}¥\n")
|
||||
output.append(f"总花费: {stats['total_cost']:.4f}¥")
|
||||
output.append(f"在线时间: {stats['online_time_minutes']}分钟\n")
|
||||
|
||||
data_fmt = "{:<32} {:>10} {:>14} {:>13.4f} ¥"
|
||||
|
||||
@@ -184,13 +208,16 @@ class LLMStatistics:
|
||||
"""统计循环,每1分钟运行一次"""
|
||||
while self.running:
|
||||
try:
|
||||
# 记录在线时间
|
||||
self._record_online_time()
|
||||
# 收集并保存统计数据
|
||||
all_stats = self._collect_all_statistics()
|
||||
self._save_statistics(all_stats)
|
||||
except Exception:
|
||||
logger.exception("统计数据处理失败")
|
||||
|
||||
# 等待1分钟
|
||||
for _ in range(60):
|
||||
# 等待5分钟
|
||||
for _ in range(300): # 5分钟 = 300秒
|
||||
if not self.running:
|
||||
break
|
||||
time.sleep(1)
|
||||
|
||||
@@ -46,11 +46,11 @@ class SubHeartflow:
|
||||
current_time = time.time()
|
||||
if current_time - self.last_reply_time > 180: # 3分钟 = 180秒
|
||||
# print(f"{self.observe_chat_id}麦麦已经3分钟没有回复了,暂时停止思考")
|
||||
await asyncio.sleep(25) # 每30秒检查一次
|
||||
await asyncio.sleep(60) # 每30秒检查一次
|
||||
else:
|
||||
await self.do_a_thinking()
|
||||
await self.judge_willing()
|
||||
await asyncio.sleep(25)
|
||||
await asyncio.sleep(60)
|
||||
|
||||
async def do_a_thinking(self):
|
||||
print("麦麦小脑袋转起来了")
|
||||
|
||||
Reference in New Issue
Block a user