Przeglądaj źródła

Add hot_topic_client and use in dialogue_manager

StrayWarrior 20 godzin temu
rodzic
commit
a902a0f97d

+ 22 - 0
pqai_agent/clients/hot_topic_client.py

@@ -0,0 +1,22 @@
+import time
+from pqai_agent.toolkit.hot_topic_toolkit import HotTopicToolkit
+
+class HotTopicClient:
+    def __init__(self):
+        self._cache = None
+        self._cache_time = 0
+        self._cache_ttl = 300  # 5分钟
+        self._toolkit = HotTopicToolkit()
+
+    def get_hot_topics(self):
+        now = time.time()
+        if self._cache and (now - self._cache_time) < self._cache_ttl:
+            return self._cache
+        topics = self._toolkit.get_hot_topics()
+        self._cache = topics
+        self._cache_time = now
+        return topics
+
+# 全局可用实例
+hot_topic_client = HotTopicClient()
+

+ 3 - 0
pqai_agent/dialogue_manager.py

@@ -1,6 +1,7 @@
 #! /usr/bin/env python
 # -*- coding: utf-8 -*-
 # vim:fenc=utf-8
+import json
 import random
 from enum import Enum
 from typing import Dict, List, Optional, Tuple, Any
@@ -14,6 +15,7 @@ import cozepy
 from sqlalchemy.orm import sessionmaker, Session
 
 from pqai_agent import configs
+from pqai_agent.clients.hot_topic_client import hot_topic_client
 from pqai_agent.clients.relation_stage_client import RelationStageClient
 from pqai_agent.data_models.agent_push_record import AgentPushRecord
 from pqai_agent.logging import logger
@@ -572,6 +574,7 @@ class DialogueManager:
             "relation_stage": self.relation_stage,
             "formatted_staff_profile": prompt_utils.format_agent_profile_v2(self.staff_profile),
             "formatted_user_profile": prompt_utils.format_user_profile(self.user_profile),
+            "hot_topics": json.dumps(hot_topic_client.get_hot_topics(), indent=2, ensure_ascii=False),
             **self.user_profile,
             **legacy_staff_profile
         }