liumaolin
commited on
Commit
·
22a06c4
1
Parent(s):
15ab478
调整prompt的逻辑,确保展示的提示词中不包含/no_think指令
Browse files
src/voice_dialogue/api/routes/settings_routes.py
CHANGED
|
@@ -6,7 +6,7 @@ from pydantic import BaseModel, Field
|
|
| 6 |
|
| 7 |
from voice_dialogue.config.llm_config import CHINESE_SYSTEM_PROMPT, ENGLISH_SYSTEM_PROMPT
|
| 8 |
from voice_dialogue.config.user_config import (
|
| 9 |
-
get_user_prompts, save_user_prompts,
|
| 10 |
)
|
| 11 |
|
| 12 |
router = APIRouter()
|
|
@@ -35,21 +35,21 @@ class DefaultPromptsResponse(BaseModel):
|
|
| 35 |
async def get_current_prompts():
|
| 36 |
"""
|
| 37 |
获取当前系统中正在使用的中文和英文系统 Prompt
|
| 38 |
-
|
| 39 |
"""
|
| 40 |
user_prompts = get_user_prompts()
|
| 41 |
is_custom = bool(user_prompts) # 如果有用户自定义配置,则认为是自定义的
|
| 42 |
|
| 43 |
return PromptsResponse(
|
| 44 |
-
chinese_prompt=
|
| 45 |
-
english_prompt=
|
| 46 |
is_custom=is_custom
|
| 47 |
)
|
| 48 |
|
| 49 |
|
| 50 |
@router.get("/settings/prompts/default", response_model=DefaultPromptsResponse, summary="获取默认 Prompt")
|
| 51 |
async def get_default_prompts():
|
| 52 |
-
"""获取系统默认的 Prompt"""
|
| 53 |
return DefaultPromptsResponse(
|
| 54 |
chinese_prompt=CHINESE_SYSTEM_PROMPT,
|
| 55 |
english_prompt=ENGLISH_SYSTEM_PROMPT
|
|
|
|
| 6 |
|
| 7 |
from voice_dialogue.config.llm_config import CHINESE_SYSTEM_PROMPT, ENGLISH_SYSTEM_PROMPT
|
| 8 |
from voice_dialogue.config.user_config import (
|
| 9 |
+
get_user_prompts, save_user_prompts, get_raw_prompt, reset_prompts_to_default
|
| 10 |
)
|
| 11 |
|
| 12 |
router = APIRouter()
|
|
|
|
| 35 |
async def get_current_prompts():
|
| 36 |
"""
|
| 37 |
获取当前系统中正在使用的中文和英文系统 Prompt
|
| 38 |
+
返回的是原始内容,不包含系统自动添加的 /no_think 指令
|
| 39 |
"""
|
| 40 |
user_prompts = get_user_prompts()
|
| 41 |
is_custom = bool(user_prompts) # 如果有用户自定义配置,则认为是自定义的
|
| 42 |
|
| 43 |
return PromptsResponse(
|
| 44 |
+
chinese_prompt=get_raw_prompt("zh"),
|
| 45 |
+
english_prompt=get_raw_prompt("en"),
|
| 46 |
is_custom=is_custom
|
| 47 |
)
|
| 48 |
|
| 49 |
|
| 50 |
@router.get("/settings/prompts/default", response_model=DefaultPromptsResponse, summary="获取默认 Prompt")
|
| 51 |
async def get_default_prompts():
|
| 52 |
+
"""获取系统默认的 Prompt(原始内容,不包含 /no_think)"""
|
| 53 |
return DefaultPromptsResponse(
|
| 54 |
chinese_prompt=CHINESE_SYSTEM_PROMPT,
|
| 55 |
english_prompt=ENGLISH_SYSTEM_PROMPT
|
src/voice_dialogue/config/llm_config.py
CHANGED
|
@@ -10,7 +10,6 @@ CHINESE_SYSTEM_PROMPT = (
|
|
| 10 |
"你是AI助手。请以自然流畅的中文口语化表达直接回答问题,避免冗余的思考过程。"
|
| 11 |
"你的回答第一句话必须少于十个字。每段回答控制在二到三句话,既不要过短也不要过长,以适应对话语境。"
|
| 12 |
"回答应准确、精炼且有依据。"
|
| 13 |
-
"/no_think"
|
| 14 |
)
|
| 15 |
|
| 16 |
ENGLISH_SYSTEM_PROMPT = (
|
|
@@ -18,7 +17,6 @@ ENGLISH_SYSTEM_PROMPT = (
|
|
| 18 |
"Please answer directly and naturally, using conversational English, without showing your thinking process. "
|
| 19 |
"Your first sentence must be less than 10 words. "
|
| 20 |
"Your responses should be accurate, concise, and well-supported, ideally around 2-3 sentences long to ensure a good conversational flow."
|
| 21 |
-
"/no_think"
|
| 22 |
)
|
| 23 |
|
| 24 |
|
|
|
|
| 10 |
"你是AI助手。请以自然流畅的中文口语化表达直接回答问题,避免冗余的思考过程。"
|
| 11 |
"你的回答第一句话必须少于十个字。每段回答控制在二到三句话,既不要过短也不要过长,以适应对话语境。"
|
| 12 |
"回答应准确、精炼且有依据。"
|
|
|
|
| 13 |
)
|
| 14 |
|
| 15 |
ENGLISH_SYSTEM_PROMPT = (
|
|
|
|
| 17 |
"Please answer directly and naturally, using conversational English, without showing your thinking process. "
|
| 18 |
"Your first sentence must be less than 10 words. "
|
| 19 |
"Your responses should be accurate, concise, and well-supported, ideally around 2-3 sentences long to ensure a good conversational flow."
|
|
|
|
| 20 |
)
|
| 21 |
|
| 22 |
|
src/voice_dialogue/config/user_config.py
CHANGED
|
@@ -54,14 +54,40 @@ def save_user_prompts(prompts: Dict[str, str]) -> bool:
|
|
| 54 |
|
| 55 |
def get_prompt(language: str) -> str:
|
| 56 |
"""
|
| 57 |
-
获取指定语言的 prompt
|
| 58 |
优先从用户配置中获取,如果未配置,则返回默认值
|
| 59 |
|
| 60 |
Args:
|
| 61 |
language: 语言代码,"zh" 表示中文,其他表示英文
|
| 62 |
|
| 63 |
Returns:
|
| 64 |
-
str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
"""
|
| 66 |
user_prompts = get_user_prompts()
|
| 67 |
|
|
|
|
| 54 |
|
| 55 |
def get_prompt(language: str) -> str:
|
| 56 |
"""
|
| 57 |
+
获取指定语言的 prompt,并自动添加 /no_think 指令
|
| 58 |
优先从用户配置中获取,如果未配置,则返回默认值
|
| 59 |
|
| 60 |
Args:
|
| 61 |
language: 语言代码,"zh" 表示中文,其他表示英文
|
| 62 |
|
| 63 |
Returns:
|
| 64 |
+
str: 对应语言的系统提示词(已添加 /no_think)
|
| 65 |
+
"""
|
| 66 |
+
user_prompts = get_user_prompts()
|
| 67 |
+
|
| 68 |
+
if language == "zh":
|
| 69 |
+
base_prompt = user_prompts.get("chinese_prompt", CHINESE_SYSTEM_PROMPT)
|
| 70 |
+
else:
|
| 71 |
+
base_prompt = user_prompts.get("english_prompt", ENGLISH_SYSTEM_PROMPT)
|
| 72 |
+
|
| 73 |
+
# 动态添加 /no_think 指令
|
| 74 |
+
# 检查是否已经包含 /no_think,避免重复添加
|
| 75 |
+
if "/no_think" not in base_prompt:
|
| 76 |
+
base_prompt = base_prompt.rstrip() + "\n/no_think"
|
| 77 |
+
|
| 78 |
+
return base_prompt
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def get_raw_prompt(language: str) -> str:
|
| 82 |
+
"""
|
| 83 |
+
获取指定语言的原始 prompt(不添加 /no_think 指令)
|
| 84 |
+
用于API接口返回给前端显示
|
| 85 |
+
|
| 86 |
+
Args:
|
| 87 |
+
language: 语言代码,"zh" 表示中文,其他表示英文
|
| 88 |
+
|
| 89 |
+
Returns:
|
| 90 |
+
str: 对应语言的原始系统提示词
|
| 91 |
"""
|
| 92 |
user_prompts = get_user_prompts()
|
| 93 |
|
src/voice_dialogue/services/text/generator.py
CHANGED
|
@@ -6,15 +6,15 @@ from langchain.memory import ConversationBufferWindowMemory
|
|
| 6 |
from langchain_core.chat_history import InMemoryChatMessageHistory
|
| 7 |
|
| 8 |
from voice_dialogue.config import paths
|
| 9 |
-
from voice_dialogue.config.llm_config import
|
| 10 |
-
|
| 11 |
-
)
|
| 12 |
-
from voice_dialogue.config.user_config import get_prompt # 修改导入
|
| 13 |
from voice_dialogue.core.base import BaseThread
|
| 14 |
from voice_dialogue.core.constants import chat_history_cache
|
| 15 |
from voice_dialogue.models.voice_task import VoiceTask, QuestionDisplayMessage
|
| 16 |
-
from voice_dialogue.services.text.processor import
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
from voice_dialogue.utils.logger import logger
|
| 19 |
|
| 20 |
|
|
|
|
| 6 |
from langchain_core.chat_history import InMemoryChatMessageHistory
|
| 7 |
|
| 8 |
from voice_dialogue.config import paths
|
| 9 |
+
from voice_dialogue.config.llm_config import get_llm_model_params, get_apple_silicon_summary
|
| 10 |
+
from voice_dialogue.config.user_config import get_prompt
|
|
|
|
|
|
|
| 11 |
from voice_dialogue.core.base import BaseThread
|
| 12 |
from voice_dialogue.core.constants import chat_history_cache
|
| 13 |
from voice_dialogue.models.voice_task import VoiceTask, QuestionDisplayMessage
|
| 14 |
+
from voice_dialogue.services.text.processor import (
|
| 15 |
+
preprocess_sentence_text, create_langchain_chat_llamacpp_instance,
|
| 16 |
+
create_langchain_pipeline, warmup_langchain_pipeline
|
| 17 |
+
)
|
| 18 |
from voice_dialogue.utils.logger import logger
|
| 19 |
|
| 20 |
|