liumaolin
commited on
Commit
·
d12af4f
1
Parent(s):
22a06c4
缓存user_prompts以提高性能
Browse files
src/voice_dialogue/config/user_config.py
CHANGED
|
@@ -1,10 +1,13 @@
|
|
| 1 |
"""用户配置管理模块"""
|
| 2 |
import json
|
| 3 |
-
from typing import Dict
|
| 4 |
|
| 5 |
-
from voice_dialogue.utils.logger import logger
|
| 6 |
from .llm_config import CHINESE_SYSTEM_PROMPT, ENGLISH_SYSTEM_PROMPT
|
| 7 |
from .paths import USER_PROMPTS_PATH
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
|
| 10 |
def get_user_prompts() -> Dict[str, str]:
|
|
@@ -12,25 +15,32 @@ def get_user_prompts() -> Dict[str, str]:
|
|
| 12 |
加载用户自定义的 prompts
|
| 13 |
|
| 14 |
Returns:
|
| 15 |
-
Dict[str, str]: 用户自定义的 prompts
|
| 16 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
if not USER_PROMPTS_PATH.exists():
|
| 18 |
-
logger.info(f"
|
| 19 |
-
|
|
|
|
| 20 |
|
| 21 |
try:
|
| 22 |
with open(USER_PROMPTS_PATH, 'r', encoding='utf-8') as f:
|
| 23 |
user_prompts = json.load(f)
|
| 24 |
-
logger.info("
|
| 25 |
-
|
|
|
|
| 26 |
except (json.JSONDecodeError, IOError) as e:
|
| 27 |
-
logger.error(f"无法加载用户 prompt
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
|
| 31 |
def save_user_prompts(prompts: Dict[str, str]) -> bool:
|
| 32 |
"""
|
| 33 |
-
保存用户自定义的 prompts 到 JSON
|
| 34 |
|
| 35 |
Args:
|
| 36 |
prompts: 要保存的 prompts 字典
|
|
@@ -38,6 +48,7 @@ def save_user_prompts(prompts: Dict[str, str]) -> bool:
|
|
| 38 |
Returns:
|
| 39 |
bool: 保存是否成功
|
| 40 |
"""
|
|
|
|
| 41 |
try:
|
| 42 |
# 确保目录存在
|
| 43 |
if not USER_PROMPTS_PATH.parent.exists():
|
|
@@ -46,6 +57,7 @@ def save_user_prompts(prompts: Dict[str, str]) -> bool:
|
|
| 46 |
with open(USER_PROMPTS_PATH, 'w', encoding='utf-8') as f:
|
| 47 |
json.dump(prompts, f, ensure_ascii=False, indent=4)
|
| 48 |
logger.info(f"用户 prompts 已保存到: {USER_PROMPTS_PATH}")
|
|
|
|
| 49 |
return True
|
| 50 |
except IOError as e:
|
| 51 |
logger.error(f"无法保存用户 prompt 配置文件: {e}")
|
|
@@ -99,15 +111,17 @@ def get_raw_prompt(language: str) -> str:
|
|
| 99 |
|
| 100 |
def reset_prompts_to_default() -> bool:
|
| 101 |
"""
|
| 102 |
-
重置 prompts
|
| 103 |
|
| 104 |
Returns:
|
| 105 |
bool: 重置是否成功
|
| 106 |
"""
|
|
|
|
| 107 |
try:
|
| 108 |
if USER_PROMPTS_PATH.exists():
|
| 109 |
USER_PROMPTS_PATH.unlink()
|
| 110 |
logger.info("用户自定义 prompts 已重置为默认值")
|
|
|
|
| 111 |
return True
|
| 112 |
except IOError as e:
|
| 113 |
logger.error(f"重置 prompts 失败: {e}")
|
|
|
|
| 1 |
"""用户配置管理模块"""
|
| 2 |
import json
|
| 3 |
+
from typing import Dict, Optional
|
| 4 |
|
|
|
|
| 5 |
from .llm_config import CHINESE_SYSTEM_PROMPT, ENGLISH_SYSTEM_PROMPT
|
| 6 |
from .paths import USER_PROMPTS_PATH
|
| 7 |
+
from ..utils.logger import logger
|
| 8 |
+
|
| 9 |
+
# 内存缓存,避免重复读取文件
|
| 10 |
+
_user_prompts_cache: Optional[Dict[str, str]] = None
|
| 11 |
|
| 12 |
|
| 13 |
def get_user_prompts() -> Dict[str, str]:
|
|
|
|
| 15 |
加载用户自定义的 prompts
|
| 16 |
|
| 17 |
Returns:
|
| 18 |
+
Dict[str, str]: 用户自定义的 prompts。
|
| 19 |
"""
|
| 20 |
+
global _user_prompts_cache
|
| 21 |
+
if _user_prompts_cache is not None:
|
| 22 |
+
return _user_prompts_cache
|
| 23 |
+
|
| 24 |
if not USER_PROMPTS_PATH.exists():
|
| 25 |
+
logger.info(f"用户配置文件不存在,使用空配置: {USER_PROMPTS_PATH}")
|
| 26 |
+
_user_prompts_cache = {}
|
| 27 |
+
return _user_prompts_cache
|
| 28 |
|
| 29 |
try:
|
| 30 |
with open(USER_PROMPTS_PATH, 'r', encoding='utf-8') as f:
|
| 31 |
user_prompts = json.load(f)
|
| 32 |
+
logger.info("成功从文件加载用户自定义 prompts 到缓存")
|
| 33 |
+
_user_prompts_cache = user_prompts
|
| 34 |
+
return _user_prompts_cache
|
| 35 |
except (json.JSONDecodeError, IOError) as e:
|
| 36 |
+
logger.error(f"无法加载用户 prompt 配置文件,使用空配置: {e}")
|
| 37 |
+
_user_prompts_cache = {}
|
| 38 |
+
return _user_prompts_cache
|
| 39 |
|
| 40 |
|
| 41 |
def save_user_prompts(prompts: Dict[str, str]) -> bool:
|
| 42 |
"""
|
| 43 |
+
保存用户自定义的 prompts 到 JSON 文件,并更新缓存。
|
| 44 |
|
| 45 |
Args:
|
| 46 |
prompts: 要保存的 prompts 字典
|
|
|
|
| 48 |
Returns:
|
| 49 |
bool: 保存是否成功
|
| 50 |
"""
|
| 51 |
+
global _user_prompts_cache
|
| 52 |
try:
|
| 53 |
# 确保目录存在
|
| 54 |
if not USER_PROMPTS_PATH.parent.exists():
|
|
|
|
| 57 |
with open(USER_PROMPTS_PATH, 'w', encoding='utf-8') as f:
|
| 58 |
json.dump(prompts, f, ensure_ascii=False, indent=4)
|
| 59 |
logger.info(f"用户 prompts 已保存到: {USER_PROMPTS_PATH}")
|
| 60 |
+
_user_prompts_cache = prompts # 更新缓存
|
| 61 |
return True
|
| 62 |
except IOError as e:
|
| 63 |
logger.error(f"无法保存用户 prompt 配置文件: {e}")
|
|
|
|
| 111 |
|
| 112 |
def reset_prompts_to_default() -> bool:
|
| 113 |
"""
|
| 114 |
+
重置 prompts 为默认值,并清空缓存。
|
| 115 |
|
| 116 |
Returns:
|
| 117 |
bool: 重置是否成功
|
| 118 |
"""
|
| 119 |
+
global _user_prompts_cache
|
| 120 |
try:
|
| 121 |
if USER_PROMPTS_PATH.exists():
|
| 122 |
USER_PROMPTS_PATH.unlink()
|
| 123 |
logger.info("用户自定义 prompts 已重置为默认值")
|
| 124 |
+
_user_prompts_cache = {} # 重置缓存为空字典
|
| 125 |
return True
|
| 126 |
except IOError as e:
|
| 127 |
logger.error(f"重置 prompts 失败: {e}")
|