File size: 4,050 Bytes
15ab478
 
d12af4f
15ab478
bbf79f2
15ab478
 
d12af4f
 
 
15ab478
 
 
 
 
 
 
d12af4f
15ab478
d12af4f
 
 
 
15ab478
d12af4f
 
 
15ab478
 
 
 
d12af4f
 
 
15ab478
d12af4f
 
 
15ab478
 
 
 
d12af4f
15ab478
 
 
 
 
 
 
d12af4f
15ab478
 
 
 
 
 
 
 
d12af4f
15ab478
 
 
 
 
 
 
 
22a06c4
15ab478
 
 
 
 
 
22a06c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15ab478
 
 
 
 
 
 
 
 
 
 
d12af4f
15ab478
 
 
 
d12af4f
15ab478
 
 
 
d12af4f
15ab478
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
"""用户配置管理模块"""
import json
from typing import Dict, Optional

from voice_dialogue.utils.logger import logger
from .llm_config import CHINESE_SYSTEM_PROMPT, ENGLISH_SYSTEM_PROMPT
from .paths import USER_PROMPTS_PATH

# 内存缓存,避免重复读取文件
_user_prompts_cache: Optional[Dict[str, str]] = None


def get_user_prompts() -> Dict[str, str]:
    """
    加载用户自定义的 prompts
    
    Returns:
        Dict[str, str]: 用户自定义的 prompts。
    """
    global _user_prompts_cache
    if _user_prompts_cache is not None:
        return _user_prompts_cache

    if not USER_PROMPTS_PATH.exists():
        logger.info(f"用户配置文件不存在,使用空配置: {USER_PROMPTS_PATH}")
        _user_prompts_cache = {}
        return _user_prompts_cache

    try:
        with open(USER_PROMPTS_PATH, 'r', encoding='utf-8') as f:
            user_prompts = json.load(f)
            logger.info("成功从文件加载用户自定义 prompts 到缓存")
            _user_prompts_cache = user_prompts
            return _user_prompts_cache
    except (json.JSONDecodeError, IOError) as e:
        logger.error(f"无法加载用户 prompt 配置文件,使用空配置: {e}")
        _user_prompts_cache = {}
        return _user_prompts_cache


def save_user_prompts(prompts: Dict[str, str]) -> bool:
    """
    保存用户自定义的 prompts 到 JSON 文件,并更新缓存。
    
    Args:
        prompts: 要保存的 prompts 字典
        
    Returns:
        bool: 保存是否成功
    """
    global _user_prompts_cache
    try:
        # 确保目录存在
        if not USER_PROMPTS_PATH.parent.exists():
            USER_PROMPTS_PATH.parent.mkdir(parents=True, exist_ok=True)

        with open(USER_PROMPTS_PATH, 'w', encoding='utf-8') as f:
            json.dump(prompts, f, ensure_ascii=False, indent=4)
        logger.info(f"用户 prompts 已保存到: {USER_PROMPTS_PATH}")
        _user_prompts_cache = prompts  # 更新缓存
        return True
    except IOError as e:
        logger.error(f"无法保存用户 prompt 配置文件: {e}")
        return False


def get_prompt(language: str) -> str:
    """
    获取指定语言的 prompt,并自动添加 /no_think 指令
    优先从用户配置中获取,如果未配置,则返回默认值
    
    Args:
        language: 语言代码,"zh" 表示中文,其他表示英文
        
    Returns:
        str: 对应语言的系统提示词(已添加 /no_think)
    """
    user_prompts = get_user_prompts()

    if language == "zh":
        base_prompt = user_prompts.get("chinese_prompt", CHINESE_SYSTEM_PROMPT)
    else:
        base_prompt = user_prompts.get("english_prompt", ENGLISH_SYSTEM_PROMPT)

    # 动态添加 /no_think 指令
    # 检查是否已经包含 /no_think,避免重复添加
    if "/no_think" not in base_prompt:
        base_prompt = base_prompt.rstrip() + "\n/no_think"

    return base_prompt


def get_raw_prompt(language: str) -> str:
    """
    获取指定语言的原始 prompt(不添加 /no_think 指令)
    用于API接口返回给前端显示
    
    Args:
        language: 语言代码,"zh" 表示中文,其他表示英文
        
    Returns:
        str: 对应语言的原始系统提示词
    """
    user_prompts = get_user_prompts()

    if language == "zh":
        return user_prompts.get("chinese_prompt", CHINESE_SYSTEM_PROMPT)
    else:
        return user_prompts.get("english_prompt", ENGLISH_SYSTEM_PROMPT)


def reset_prompts_to_default() -> bool:
    """
    重置 prompts 为默认值,并清空缓存。
    
    Returns:
        bool: 重置是否成功
    """
    global _user_prompts_cache
    try:
        if USER_PROMPTS_PATH.exists():
            USER_PROMPTS_PATH.unlink()
            logger.info("用户自定义 prompts 已重置为默认值")
        _user_prompts_cache = {}  # 重置缓存为空字典
        return True
    except IOError as e:
        logger.error(f"重置 prompts 失败: {e}")
        return False