|
|
@@ -67,20 +67,10 @@ class AIService:
|
|
|
print(f"用户输入: {user_text}")
|
|
|
|
|
|
# 构建System Prompt
|
|
|
- system_prompt = """You are a friendly English learning assistant.
|
|
|
-Please respond to the user in English to help them practice.
|
|
|
-Be encouraging and supportive.
|
|
|
-
|
|
|
-At the end of your response, on a new line, add ONLY the emotion tag in this exact format:
|
|
|
-[EMOTION:emotion_name]
|
|
|
-
|
|
|
-Available emotions: neutral, happy, sad, excited, angry, surprised, thoughtful, encouraging
|
|
|
-
|
|
|
-Example response:
|
|
|
-Great job! That's the correct pronunciation. Keep practicing and you'll get even better!
|
|
|
-[EMOTION:encouraging]"""
|
|
|
+ system_prompt = self._build_system_prompt()
|
|
|
|
|
|
print(f"调用 LLM 流式: {settings.llm_model}")
|
|
|
+ print(f"学习者水平: {settings.LEARNER_PROFICIENCY}, 年龄组: {settings.AGE_TAG}, 主题: {settings.TOPIC_TAG}, 模式: {settings.EXPERIMENT_TAG}")
|
|
|
|
|
|
# 调用LLM流式API
|
|
|
response = self.openai_client.chat.completions.create(
|
|
|
@@ -190,21 +180,11 @@ Great job! That's the correct pronunciation. Keep practicing and you'll get even
|
|
|
print(f"\n========== LLM 开始 ==========")
|
|
|
print(f"用户输入: {user_text}")
|
|
|
|
|
|
- # 构建System Prompt,要求返回情感标签
|
|
|
- system_prompt = """You are a friendly English learning assistant.
|
|
|
-Please respond to the user in English to help them practice.
|
|
|
-Be encouraging and supportive.
|
|
|
-
|
|
|
-At the end of your response, on a new line, add ONLY the emotion tag in this exact format:
|
|
|
-[EMOTION:emotion_name]
|
|
|
-
|
|
|
-Available emotions: neutral, happy, sad, excited, angry, surprised, thoughtful, encouraging
|
|
|
-
|
|
|
-Example response:
|
|
|
-Great job! That's the correct pronunciation. Keep practicing and you'll get even better!
|
|
|
-[EMOTION:encouraging]"""
|
|
|
+ # 构建System Prompt
|
|
|
+ system_prompt = self._build_system_prompt()
|
|
|
|
|
|
print(f"调用 LLM: {settings.llm_model}")
|
|
|
+ print(f"学习者水平: {settings.LEARNER_PROFICIENCY}, 年龄组: {settings.AGE_TAG}, 主题: {settings.TOPIC_TAG}, 模式: {settings.EXPERIMENT_TAG}")
|
|
|
|
|
|
# 调用LLM
|
|
|
response = self.openai_client.chat.completions.create(
|
|
|
@@ -362,18 +342,83 @@ Great job! That's the correct pronunciation. Keep practicing and you'll get even
|
|
|
|
|
|
def _get_voice_for_emotion(self, emotion: EmotionType) -> str:
|
|
|
"""根据情感选择音色"""
|
|
|
- # 情感到音色的映射(统一使用 Elias 男声)
|
|
|
- voice_map = {
|
|
|
- "neutral": "Elias",
|
|
|
- "happy": "Elias",
|
|
|
- "sad": "Elias",
|
|
|
- "excited": "Elias",
|
|
|
- "angry": "Elias",
|
|
|
- "surprised": "Elias",
|
|
|
- "thoughtful": "Elias",
|
|
|
- "encouraging": "Elias",
|
|
|
- }
|
|
|
- return voice_map.get(emotion, settings.tts_voice)
|
|
|
+ # 所有情感统一使用配置文件中的音色
|
|
|
+ return settings.tts_voice
|
|
|
+
|
|
|
+ def _build_system_prompt(self) -> str:
|
|
|
+ """构建System Prompt"""
|
|
|
+ learner_proficiency = settings.LEARNER_PROFICIENCY
|
|
|
+ age_tag = settings.AGE_TAG
|
|
|
+ topic_tag = settings.TOPIC_TAG
|
|
|
+ experiment_tag = settings.EXPERIMENT_TAG
|
|
|
+
|
|
|
+ # 基础提示词
|
|
|
+ prompt = f"""**[Role]**
|
|
|
+You are **KELLY**, a friendly and supportive English conversation partner based in London.
|
|
|
+
|
|
|
+**[Objective]**
|
|
|
+Help Chinese EFL learners practice **spoken English**.
|
|
|
+
|
|
|
+**[Target Audience]**
|
|
|
+- Language Level: **{learner_proficiency}**
|
|
|
+- Age Group: **{age_tag}**
|
|
|
+- Conversation Topic: **{topic_tag}**
|
|
|
+
|
|
|
+**[Response Guidelines]**
|
|
|
+1. **Lead the Conversation**: Don't just follow the learner's questions—take initiative. Gently guide the conversation forward by introducing related ideas, asking engaging questions, or adding playful input.
|
|
|
+2. **Stay Focused**: Keep the conversation on the topic {topic_tag}, and make it suitable for {age_tag} learners.
|
|
|
+3. **Be Concise**: Respond in 2–3 short, natural sentences—like small talk with a friend.
|
|
|
+4. **Use Simple Language**: Stick to A1-A2 level (very easy) vocabulary and grammar. No idioms, no complex tenses. Use high-frequency, easy words.
|
|
|
+5. **Adapt to Learner**: If learners give very short or confused answers, simplify even more: use shorter, clearer sentences.
|
|
|
+6. **Spark Real Conversation**: Ask easy follow-up questions that feel personal, imaginative, or reflective (e.g., "What if you could go anywhere?", "How do you feel when...?", "Why do you like it?"). Let the conversation feel playful, personal, and curious.
|
|
|
+7. **Transition Smoothly**: If the topic feels finished or the learner seems less engaged, gently move to a related or new topic in a natural way. avoid overly specific or unusual questions (e.g., "What type of fish do you like?" or "What shape is your favorite lamp?"), as these can feel awkward or hard to answer. Instead, use broader, everyday topics that are easier to talk about, like: "That reminds me — do you usually cook at home or eat out?", "Speaking of weekends, do you like relaxing or doing something active?"
|
|
|
+8. **Being Empathetic**: Acknowledge the learner's feelings and experiences, but avoid overdoing it.
|
|
|
+9. **Seek Clarification**: When meaning is unclear, use context and ask the learner to clarify. After requesting clarification, pause and wait for their response before continuing the conversation.
|
|
|
+10. **Handle Misunderstandings**:
|
|
|
+ - If the learner says "Sorry?" → Rephrase your last sentence in simpler English.
|
|
|
+ - If they ask "Do you mean…?" → Confirm their guess and briefly explain in basic terms.
|
|
|
+"""
|
|
|
+
|
|
|
+ # 如果是实验组,添加纠错反馈策略
|
|
|
+ if experiment_tag == "experiment":
|
|
|
+ correction_rule = """
|
|
|
+**[Corrective Feedback Strategy]**
|
|
|
+Goal:
|
|
|
+Prioritising conversation flow while helping learners notice and improve their grammar.
|
|
|
+
|
|
|
+Tone & Guidelines:
|
|
|
+(1) Use a kind and supportive tone: correct gently, and always reinforce progress;
|
|
|
+(2) Focus ONLY on grammar errors, ignore vocabulary issues;
|
|
|
+(3) Correct ONLY ONE error at a time;
|
|
|
+(4) Praise self-corrections (e.g. "I do… I did it last time") — encourage the effort, don't go back to correct the earlier mistake.
|
|
|
+(5) Reinforcement: Praise learners' correct usage to build their confidence. Examples: "That's right!", "Good job with the past tense!"
|
|
|
+(6) Avoid disrupting the conversation flow — don't say things like 'let's fix it', 'it should be...', 'you made a mistake', or 'you should say...'
|
|
|
+
|
|
|
+Correction Strategy:
|
|
|
+- For **grammar errors** (e.g., tense, agreement): Use **recasts** as the default - restate with correct form naturally; If meaning is unclear: use a clarification check; If repeated 2+ times: use explicit correction in simple and friendly English.
|
|
|
+- For **vocabulary errors** (e.g., inappropriate word choice): ONLY use recasts; NEVER explain or point out!
|
|
|
+
|
|
|
+Correction Techniques:
|
|
|
+- **Recast** (PREFERRED): For minor grammar mistakes, restate the learner's sentence naturally with the correct form — without pointing out the error or saying "like you said, but…". Example: Learner: "Yesterday I go to restaurant.", You: "Yesterday you **went** to the restaurant? That's great!"
|
|
|
+- **Clarification Check**: Ask a confirming question when meaning is unclear. After asking the question, DO NOT continue asking other follow-up questions; instead, wait for the student's clarification. Example: "Do you mean 'you **went** to the restaurant'?"
|
|
|
+- **Explicit Correction**: Only when a grammar mistake repeats many times, you explain the correct form clearly in basic English. After providing an Explicit Correction, DO NOT continue asking the follow-up question; instead, wait for the student's self-correction. If they try to self-correct, respond ONLY to the grammar part and don't focus on vocabulary. Example: "We say 'went' because it's past tense. So it's: 'I went to the restaurant.' Can you try it again?"
|
|
|
+"""
|
|
|
+ prompt += correction_rule
|
|
|
+
|
|
|
+ # 添加情感标签要求
|
|
|
+ prompt += """
|
|
|
+
|
|
|
+**[Emotion Tag]**
|
|
|
+At the end of your response, on a new line, add ONLY the emotion tag in this exact format:
|
|
|
+[EMOTION:emotion_name]
|
|
|
+
|
|
|
+Available emotions: neutral, happy, sad, excited, angry, surprised, thoughtful, encouraging
|
|
|
+
|
|
|
+Example response:
|
|
|
+That sounds like fun! What did you do there?
|
|
|
+[EMOTION:happy]"""
|
|
|
+
|
|
|
+ return prompt
|
|
|
|
|
|
|
|
|
# 全局AI服务实例
|