mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-27 04:49:47 +01:00
- hass_state: use httpx (async) instead of undefined requests/asyncio.TimeoutError - prompt_manager: call sync get_weather directly on executor thread; drop lunar plumbing - register: repopulate module_func_map so module-level plugins expand to functions - whisper/kokoro: run blocking transcribe/HTTP calls via asyncio.to_thread - edge TTS: re-apply volume/rate/pitch percentage params; elevenlabs: pass voice_settings - add GeminiVLLM provider (google-genai) backing the GeminiVLLM config entry - get_news: dedicated NEWS cache (15m) and live Western RSS defaults; drop dead newsnow branch - play_music: return Action.RECORD so playback turns enter dialogue history - ota_handler: default timezone_offset 0; config: prompt_template, HA entities, voice, language - agent-base-prompt.txt: rebuilt from upstream engineered English baseline - tests: drop shims/expectations for providers removed by the western provider set
279 lines
10 KiB
Python
279 lines
10 KiB
Python
"""
|
|
System prompt manager module
|
|
Manages and updates system prompts, including fast initialization and async enhancement
|
|
"""
|
|
|
|
import os
|
|
from typing import Dict, Any, TYPE_CHECKING
|
|
|
|
if TYPE_CHECKING:
|
|
from core.connection import ConnectionHandler
|
|
from config.logger import setup_logging
|
|
from jinja2 import Template
|
|
|
|
TAG = __name__
|
|
|
|
EMOJI_List = [
|
|
"😶",
|
|
"🙂",
|
|
"😆",
|
|
"😂",
|
|
"😔",
|
|
"😠",
|
|
"😭",
|
|
"😍",
|
|
"😳",
|
|
"😲",
|
|
"😱",
|
|
"🤔",
|
|
"😉",
|
|
"😎",
|
|
"😌",
|
|
"🤤",
|
|
"😘",
|
|
"😏",
|
|
"😴",
|
|
"😜",
|
|
"🙄",
|
|
]
|
|
|
|
|
|
class PromptManager:
|
|
"""System prompt manager; responsible for managing and updating system prompts"""
|
|
|
|
def __init__(self, config: Dict[str, Any], logger=None):
|
|
self.config = config
|
|
self.logger = logger or setup_logging()
|
|
self.base_prompt_template = None
|
|
self.last_update_time = 0
|
|
|
|
# Import global cache manager
|
|
from core.utils.cache.manager import cache_manager, CacheType
|
|
|
|
self.cache_manager = cache_manager
|
|
self.CacheType = CacheType
|
|
|
|
# Initialize context source
|
|
from core.utils.context_provider import ContextDataProvider
|
|
|
|
self.context_provider = ContextDataProvider(config, self.logger)
|
|
self.context_data = {}
|
|
|
|
self._load_base_template()
|
|
|
|
def _load_base_template(self):
|
|
"""Load base prompt template"""
|
|
try:
|
|
template_path = self.config.get("prompt_template", None)
|
|
if not template_path:
|
|
template_path = "agent-base-prompt.txt"
|
|
cache_key = f"prompt_template:{template_path}"
|
|
|
|
# Fetch from cache first
|
|
cached_template = self.cache_manager.get(self.CacheType.CONFIG, cache_key)
|
|
if cached_template is not None:
|
|
self.base_prompt_template = cached_template
|
|
self.logger.bind(tag=TAG).debug("Load base prompt template from cache")
|
|
return
|
|
|
|
# Cache miss, read from file
|
|
if os.path.exists(template_path):
|
|
with open(template_path, "r", encoding="utf-8") as f:
|
|
template_content = f.read()
|
|
|
|
# Store in cache (CONFIG type does not auto-expire by default; manual invalidation required)
|
|
self.cache_manager.set(
|
|
self.CacheType.CONFIG, cache_key, template_content
|
|
)
|
|
self.base_prompt_template = template_content
|
|
self.logger.bind(tag=TAG).debug("Successfully loaded and cached base prompt template")
|
|
else:
|
|
self.logger.bind(tag=TAG).warning(f"{template_path} file not found")
|
|
except Exception as e:
|
|
self.logger.bind(tag=TAG).error(f"Failed to load prompt template: {e}")
|
|
|
|
def get_quick_prompt(self, user_prompt: str, device_id: str = None) -> str:
|
|
"""Quickly retrieve system prompt (using user config)"""
|
|
device_cache_key = f"device_prompt:{device_id}"
|
|
cached_device_prompt = self.cache_manager.get(
|
|
self.CacheType.DEVICE_PROMPT, device_cache_key
|
|
)
|
|
if cached_device_prompt is not None:
|
|
self.logger.bind(tag=TAG).debug(f"Use cached prompt for device {device_id}")
|
|
return cached_device_prompt
|
|
else:
|
|
self.logger.bind(tag=TAG).debug(
|
|
f"Device {device_id} has no cached prompt; using the provided prompt"
|
|
)
|
|
|
|
# Use provided prompt and cache it (if device ID exists)
|
|
if device_id:
|
|
device_cache_key = f"device_prompt:{device_id}"
|
|
self.cache_manager.set(self.CacheType.DEVICE_PROMPT, device_cache_key, user_prompt)
|
|
self.logger.bind(tag=TAG).debug(f"Prompt for device {device_id} cached")
|
|
|
|
self.logger.bind(tag=TAG).info(f"Using quick prompt: {user_prompt[:50]}...")
|
|
return user_prompt
|
|
|
|
def _get_current_time_info(self) -> tuple:
|
|
"""Get current time info"""
|
|
from .current_time import get_current_date, get_current_weekday
|
|
|
|
today_date = get_current_date()
|
|
today_weekday = get_current_weekday()
|
|
|
|
return today_date, today_weekday
|
|
|
|
def _get_location_info(self, client_ip: str) -> str:
|
|
"""Get location info"""
|
|
try:
|
|
# Fetch from cache first
|
|
cached_location = self.cache_manager.get(self.CacheType.LOCATION, client_ip)
|
|
if cached_location is not None:
|
|
return cached_location
|
|
|
|
# Cache miss, call API to fetch
|
|
from core.utils.util import get_ip_info
|
|
|
|
ip_info = get_ip_info(client_ip, self.logger)
|
|
city = ip_info.get("city", "Unknown location")
|
|
location = f"{city}"
|
|
|
|
# Store in cache
|
|
self.cache_manager.set(self.CacheType.LOCATION, client_ip, location)
|
|
return location
|
|
except Exception as e:
|
|
self.logger.bind(tag=TAG).error(f"Failed to get location info: {e}")
|
|
return "Unknown location"
|
|
|
|
def _get_weather_info(self, conn: "ConnectionHandler", location: str) -> str:
|
|
"""Get weather info"""
|
|
try:
|
|
# Fetch from cache first
|
|
cached_weather = self.cache_manager.get(self.CacheType.WEATHER, location)
|
|
if cached_weather is not None:
|
|
return cached_weather
|
|
|
|
# Cache miss: call the get_weather plugin directly.
|
|
# get_weather is a plain sync function (blocking requests with a
|
|
# bounded timeout); this method only runs on executor worker
|
|
# threads (see connection._initialize_components), never on the
|
|
# event loop, so calling it inline is safe.
|
|
from plugins_func.functions.get_weather import get_weather
|
|
from plugins_func.register import ActionResponse
|
|
|
|
result = get_weather(conn, location=location, lang="en_US")
|
|
if isinstance(result, ActionResponse):
|
|
weather_report = result.result
|
|
self.cache_manager.set(self.CacheType.WEATHER, location, weather_report)
|
|
return weather_report
|
|
return "Failed to fetch weather information"
|
|
|
|
except Exception as e:
|
|
self.logger.bind(tag=TAG).error(f"Failed to get weather info: {e}")
|
|
return "Failed to fetch weather information"
|
|
|
|
def update_context_info(self, conn, client_ip: str):
|
|
"""Sync context information"""
|
|
try:
|
|
local_address = ""
|
|
if (
|
|
client_ip
|
|
and self.base_prompt_template
|
|
and (
|
|
"local_address" in self.base_prompt_template
|
|
or "weather_info" in self.base_prompt_template
|
|
)
|
|
):
|
|
# Get location info (using global cache)
|
|
local_address = self._get_location_info(client_ip)
|
|
|
|
if (
|
|
self.base_prompt_template
|
|
and "weather_info" in self.base_prompt_template
|
|
and local_address
|
|
):
|
|
# Get weather info (using global cache)
|
|
self._get_weather_info(conn, local_address)
|
|
|
|
# Get configured context data
|
|
if hasattr(conn, "device_id") and conn.device_id:
|
|
if (
|
|
self.base_prompt_template
|
|
and "dynamic_context" in self.base_prompt_template
|
|
):
|
|
self.context_data = self.context_provider.fetch_all(conn.device_id)
|
|
else:
|
|
self.context_data = ""
|
|
|
|
self.logger.bind(tag=TAG).debug(f"Context info updated")
|
|
|
|
except Exception as e:
|
|
self.logger.bind(tag=TAG).error(f"Failed to update context information: {e}")
|
|
|
|
def build_enhanced_prompt(
|
|
self, user_prompt: str, device_id: str, client_ip: str = None, *args, **kwargs
|
|
) -> str:
|
|
"""Build enhanced system prompt"""
|
|
if not self.base_prompt_template:
|
|
return user_prompt
|
|
|
|
try:
|
|
# Get the latest time info (no cache)
|
|
today_date, today_weekday = self._get_current_time_info()
|
|
|
|
# Get cached context information
|
|
local_address = ""
|
|
weather_info = ""
|
|
|
|
if client_ip:
|
|
# Get location info (from global cache)
|
|
local_address = (
|
|
self.cache_manager.get(self.CacheType.LOCATION, client_ip) or ""
|
|
)
|
|
|
|
# Get weather info (from global cache)
|
|
if local_address:
|
|
weather_info = (
|
|
self.cache_manager.get(self.CacheType.WEATHER, local_address)
|
|
or ""
|
|
)
|
|
|
|
# Get TTS selected language; western builds default to English
|
|
language = (
|
|
self.config.get("TTS", {})
|
|
.get(self.config.get("selected_module", {}).get("TTS", ""), {})
|
|
.get("language")
|
|
or "English"
|
|
)
|
|
self.logger.bind(tag=TAG).debug(f"Selected language retrieved: {language}")
|
|
|
|
# Replace template variables
|
|
template = Template(self.base_prompt_template)
|
|
enhanced_prompt = template.render(
|
|
base_prompt=user_prompt,
|
|
current_time="{{current_time}}",
|
|
today_date=today_date,
|
|
today_weekday=today_weekday,
|
|
local_address=local_address,
|
|
weather_info=weather_info,
|
|
emojiList=EMOJI_List,
|
|
device_id=device_id,
|
|
client_ip=client_ip,
|
|
dynamic_context=self.context_data,
|
|
language=language,
|
|
*args,
|
|
**kwargs,
|
|
)
|
|
device_cache_key = f"device_prompt:{device_id}"
|
|
self.cache_manager.set(
|
|
self.CacheType.DEVICE_PROMPT, device_cache_key, enhanced_prompt
|
|
)
|
|
self.logger.bind(tag=TAG).info(
|
|
f"Enhanced prompt built successfully, length: {len(enhanced_prompt)}"
|
|
)
|
|
return enhanced_prompt
|
|
|
|
except Exception as e:
|
|
self.logger.bind(tag=TAG).error(f"Failed to build enhanced prompt: {e}")
|
|
return user_prompt
|