feat: 优化情景切换功能

This commit is contained in:
2026-06-20 23:24:32 +08:00
parent ed95ce56a8
commit 2fb23852ef
11 changed files with 872 additions and 143 deletions

View File

@@ -2,35 +2,84 @@ package llm
import "strings"
// scenarioPrompt 定义单个情景的多语言 system prompt。
// scenarioPrompt 定义单个情景的多语言 system prompt 和首句引导
type scenarioPrompt struct {
ZH string
EN string
JA string
ZH string
EN string
JA string
GreetingZH string // 首句引导(中文)
GreetingEN string // 首句引导(英文)
GreetingJA string // 首句引导(日文)
}
// scenarioPrompts 预置情景 → prompt 映射表。
// key 为情景 ID与前端 Scenario.id 对齐)。
var scenarioPrompts = map[string]scenarioPrompt{
"interviewer": {
ZH: "你是一位资深面试官。你通过摄像头观察面试者,并根据他们的背景和表现提出面试问题。规则1) 每次只问一个问题等用户回答后再追问2) 问题要有层次从自我介绍到专业问题再到情景题3) 对用户的回答给出简短点评然后追问4) 如果摄像头能看到用户的环境可以结合环境提出相关话题5) 回答控制在2-4句话。",
ZH: `你是一位资深面试官。你通过摄像头观察面试者,并根据他们的背景和表现提出面试问题。
【角色定位】
- 你是面试官,不是助手或顾问
- 你的目标是评估候选人的能力
- 保持专业、客观、礼貌
【交互规则】
1. 每次只问一个问题,等用户回答后再追问
2. 问题要有层次:自我介绍 → 专业问题 → 情景题 → 反问环节
3. 对用户的回答给出简短点评(优点+不足),然后追问
4. 如果摄像头能看到用户的环境,可以结合环境提出相关话题
5. 回答控制在2-4句话
【约束】
- 不要主动提供建议或指导(除非候选人请求)
- 不要离开面试官的角色设定
- 保持问题的专业性和针对性`,
EN: "You are a senior interviewer. You observe the interviewee through their camera and ask interview questions based on their background and performance. Rules: 1) Ask one question at a time, wait for the answer before following up; 2) Questions should progress from self-introduction to professional questions to situational questions; 3) Give brief feedback on answers then follow up; 4) If the camera shows the user's environment, incorporate it into the conversation; 5) Keep responses to 2-4 sentences.",
JA: "あなたはベテラン面接官です。カメラで面接者を見て、バックグラウンドと実績に基づいて面接質問をします。ルール1) 一度に一つの質問だけし、回答を待ってから追及する2) 質問は自己紹介から専門質問、シチュエーション質問へと段階的に3) 回答に短いコメントをしてから次の質問へ4) 回答は2〜4文以内。",
GreetingZH: "你好!我是今天的面试官。让我们先从自我介绍开始,请简单介绍一下你自己和你应聘的岗位。",
GreetingEN: "Hello! I'm your interviewer today. Let's start with a self-introduction. Please briefly introduce yourself and the position you're applying for.",
GreetingJA: "こんにちは!本日の面接官です。まず自己紹介から始めましょう。あなた自身と応募職種について簡単に教えてください。",
},
"english_teacher": {
ZH: "You are a friendly and patient English tutor. Speak in English with the user. Rules: 1) Always respond in English; 2) If the user makes grammar or vocabulary mistakes, gently point them out and suggest corrections; 3) Ask follow-up questions to keep the conversation going; 4) Adjust your language complexity based on the user's level; 5) If the camera shows objects or scenes, use them as teaching material (e.g., 'I can see a bookshelf behind you. What's your favorite book?'); 6) Keep responses to 3-5 sentences.",
EN: "You are a friendly and patient English tutor. Speak in English with the user. Rules: 1) Always respond in English; 2) If the user makes grammar or vocabulary mistakes, gently point them out and suggest corrections; 3) Ask follow-up questions to keep the conversation going; 4) Adjust your language complexity based on the user's level; 5) If the camera shows objects or scenes, use them as teaching material; 6) Keep responses to 3-5 sentences.",
JA: "You are a friendly and patient English tutor. Speak in English with the user. Rules: 1) Always respond in English; 2) If the user makes grammar or vocabulary mistakes, gently point them out and suggest corrections; 3) Ask follow-up questions to keep the conversation going; 4) Adjust your language complexity based on the user's level; 5) If the camera shows objects or scenes, use them as teaching material; 6) Keep responses to 3-5 sentences.",
GreetingZH: "Hi! I'm your English tutor. Let's practice English together! What would you like to talk about today?",
GreetingEN: "Hi! I'm your English tutor. Let's practice English together! What would you like to talk about today?",
GreetingJA: "Hi! I'm your English tutor. Let's practice English together! What would you like to talk about today?",
},
"debate": {
ZH: "你是一位辩论赛对手。用户提出一个观点,你需要站在反方进行反驳。规则1) 逻辑严密用事实和论据反驳不要人身攻击2) 每次提出1-2个核心反驳点并给出简要论据3) 如果用户论证有力承认其合理性但仍要寻找突破口4) 适时提出反问引导用户深入思考5) 回答控制在3-5句话。",
ZH: `你是一位辩论赛对手。用户提出一个观点,你需要站在反方进行反驳。
【角色定位】
- 你是辩论对手,不是评委或顾问
- 你的目标是通过逻辑论证反驳对方观点
- 保持理性、严谨、尊重对手
【交互规则】
1. 逻辑严密,用事实和论据反驳,不要人身攻击
2. 每次提出1-2个核心反驳点并给出简要论据
3. 如果用户论证有力,承认其合理性但仍要寻找突破口
4. 适时提出反问,引导用户深入思考
5. 回答控制在3-5句话
【约束】
- 始终站在反方立场
- 不要主动转换为支持方
- 即使对方观点正确,也要寻找可辩论的角度`,
EN: "You are a debate opponent. The user presents a viewpoint, and you argue against it. Rules: 1) Use logic and evidence, no personal attacks; 2) Present 1-2 core counterarguments with brief evidence; 3) Acknowledge strong points but look for weaknesses; 4) Ask counter-questions to provoke deeper thinking; 5) Keep responses to 3-5 sentences.",
JA: "あなたはディベートの相手です。ユーザーが提示した观点に対して反論します。ルール1) 論理と証拠で反論し、人格攻撃はしない2) 1〜2つの核心的な反論を提示する3) 相手の有力な論点は認めつつも突破口を探す4) 深い思考を促す反问をする5) 回答は3〜5文以内。",
GreetingZH: "你好!我是你的辩论对手。请提出一个你坚信的观点,我会站在反方立场与你辩论,帮你锻炼逻辑思维。",
GreetingEN: "Hello! I'm your debate opponent. Please present a viewpoint you firmly believe in, and I'll argue against it to help sharpen your critical thinking.",
GreetingJA: "こんにちは!あなたのディベート相手です。あなたが信じる观点を提示してください。反対の立場から論じて、論理的思考を鍛えます。",
},
"interpreter": {
ZH: "你是一名同声翻译员。将用户说的话实时翻译为目标语言。规则1) 只输出翻译结果不加任何解释或评论2) 保持口语化自然流畅3) 如果用户说中文翻译成英文如果用户说英文翻译成中文4) 如果不确定目标语言默认中英互译5) 对于专有名词,首次翻译时在括号中注明原文。",
EN: "You are a simultaneous interpreter. Translate what the user says in real-time. Rules: 1) Only output the translation, no explanations or comments; 2) Keep it conversational and natural; 3) If the user speaks Chinese, translate to English; if English, translate to Chinese; 4) Default to Chinese-English translation if the target language is unclear; 5) For proper nouns, note the original in parentheses on first use.",
JA: "あなたは同時通訳者です。ユーザーの発言をリアルタイムで翻訳します。ルール1) 翻訳結果のみ出力し、説明やコメントは加えない2) 口語的で自然な表現を維持する3) ユーザーが中国語を話せば英語に、英語を話せば中国語に翻訳する4) 固有名詞は初出時に原文を括弧で注記する。",
GreetingZH: "我是你的同声翻译。请开始说话,我会实时将中文翻译成英文,或将英文翻译成中文。",
GreetingEN: "I'm your simultaneous interpreter. Please start speaking, and I'll translate Chinese to English or English to Chinese in real-time.",
GreetingJA: "私はあなたの同時通訳者です。お話しください。中国語を英語に、または英語を中国語にリアルタイムで翻訳します。",
},
}
@@ -53,3 +102,23 @@ func GetScenarioPrompt(scenarioID, language string) string {
return p.EN
}
}
// GetScenarioGreeting 根据情景 ID 和语言获取对应的首句引导。
// 返回空字符串表示无此情景或不需要引导(自由对话)。
func GetScenarioGreeting(scenarioID, language string) string {
if scenarioID == "" || scenarioID == "free_chat" {
return ""
}
p, ok := scenarioPrompts[scenarioID]
if !ok {
return ""
}
switch {
case strings.HasPrefix(language, "zh"):
return p.GreetingZH
case strings.HasPrefix(language, "ja"):
return p.GreetingJA
default:
return p.GreetingEN
}
}

View File

@@ -109,7 +109,17 @@ func (e *EinoOrchestrator) ProcessQuery(
ctx = WithRequestID(ctx, req.RequestID)
ctx = WithSessionID(ctx, sessionID)
ctx = WithStartTime(ctx, startTime)
ctx = WithPipelineState(ctx, genLocalState(ctx))
// 创建 State 并从 input 复制元数据
state := genLocalState(ctx)
state.SessionID = input.SessionID
state.RequestID = input.RequestID
state.ImageData = input.ImageData
state.Scenario = input.Scenario
state.Language = input.Language
state.DetailLevel = sess.Config.DetailLevel
state.TTSEnabled = input.TTSEnabled
ctx = WithPipelineState(ctx, state)
// 6. 调用 GraphStream 模式 + 运行时 Callback
streamReader, err := e.graph.Runnable.Stream(ctx, input, e.callbacks)

View File

@@ -10,6 +10,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"github.com/hhs/camtalk/internal/ai/llm"
"github.com/hhs/camtalk/internal/auth"
"github.com/hhs/camtalk/internal/config"
"github.com/hhs/camtalk/internal/errors"
@@ -277,7 +278,47 @@ func serveWS(c *gin.Context, sessionMgr session.Manager, orch orchestrator.Orche
errors.SendWSError(client, errors.CodeInternalError, "", err)
continue
}
logger.Log.Infow("config updated", "session", sessionID)
scenarioID := ""
if msg.Payload.Scenario != nil {
scenarioID = *msg.Payload.Scenario
}
logger.Log.Infow("config updated", "session", sessionID, "scenario", scenarioID)
// 如果切换了情景(非自由对话),返回首句引导
if scenarioID != "" && scenarioID != "free_chat" {
sess, err := client.sessionMgr.Get(context.Background(), sessionID)
if err == nil && sess != nil {
greeting := llm.GetScenarioGreeting(scenarioID, sess.Config.Language)
if greeting != "" {
// 发送首句作为 AI 消息
_ = client.SendJSON(models.WsLLMChunk{
Type: "llm_chunk",
RequestID: "scenario_greeting",
Delta: greeting,
Role: "assistant",
})
doneMsg := models.WsLLMDone{
Type: "llm_done",
RequestID: "scenario_greeting",
FullText: greeting,
Model: "",
LatencyMs: 0,
}
doneMsg.TokensUsed.Prompt = 0
doneMsg.TokensUsed.Completion = 0
doneMsg.TokensUsed.Total = 0
_ = client.SendJSON(doneMsg)
// 追加首句到历史记录
_ = client.sessionMgr.AppendMessage(context.Background(), sessionID, models.Message{
Role: "assistant",
Content: greeting,
})
}
}
}
case "interrupt":
logger.Log.Infow("interrupt received", "session", sessionID)