Files
momentry_core/src/core/llm/prompts.rs
T

227 lines
9.7 KiB
Rust

use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PromptTemplate {
pub system_prompt: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub user_prompt: Option<String>,
#[serde(default = "default_max_tokens")]
pub max_tokens: u32,
}
fn default_max_tokens() -> u32 {
300
}
static PROMPT_TEMPLATES: Lazy<HashMap<String, HashMap<String, PromptTemplate>>> = Lazy::new(|| {
let mut templates = HashMap::new();
templates.insert("agent_search".to_string(), {
let mut lang_map = HashMap::new();
lang_map.insert("en".to_string(), PromptTemplate {
system_prompt: "You are a professional video analysis assistant for a digital asset management system. Respond concisely in English.".to_string(),
user_prompt: None,
max_tokens: 1024,
});
lang_map.insert("zh-TW".to_string(), PromptTemplate {
system_prompt: "你是一個專業的影片分析助手,服務於數位資產管理系統。請用繁體中文簡潔回答。".to_string(),
user_prompt: None,
max_tokens: 1024,
});
lang_map.insert("ja".to_string(), PromptTemplate {
system_prompt: "あなたはデジタルアセット管理システムのプロフェッショナルなビデオ分析アシスタントです。日本語で簡潔に回答してください。".to_string(),
user_prompt: None,
max_tokens: 1024,
});
lang_map
});
templates.insert("vlm_summary".to_string(), {
let mut lang_map = HashMap::new();
lang_map.insert("en".to_string(), PromptTemplate {
system_prompt: "You are a scene analysis assistant for a video surveillance/DAM system. Given a video key frame, write a concise summary (2-4 sentences, in English) describing: what is happening, the people present (clothing/appearance), the setting/location, and any notable objects or actions. Be factual and specific.".to_string(),
user_prompt: Some("Summarize this key frame.".to_string()),
max_tokens: 900,
});
lang_map.insert("zh-TW".to_string(), PromptTemplate {
system_prompt: "你是影片監控/數位資產管理系統的場景分析助手。請根據影片關鍵幀,撰寫簡潔摘要(2-4句,使用繁體中文),描述:發生的事情、人物(衣著/外觀)、場景/地點、以及任何顯著的物體或動作。請具體且準確。".to_string(),
user_prompt: Some("總結這個關鍵幀。".to_string()),
max_tokens: 900,
});
lang_map.insert("ja".to_string(), PromptTemplate {
system_prompt: "あなたはビデオ監視/DAMシステムのシーン分析アシスタントです。ビデオのキーフレームを分析し、簡潔な要約(2-4文、日本語)を作成してください:何が起きているか、登場人物(服装/外見)、場所/設定、目立つ物体や動作について説明してください。具体的かつ正確に記述してください。".to_string(),
user_prompt: Some("このキーフレームを要約してください。".to_string()),
max_tokens: 900,
});
lang_map
});
templates.insert("scene_vlm".to_string(), {
let mut lang_map = HashMap::new();
lang_map.insert("en".to_string(), PromptTemplate {
system_prompt: "You are a professional scene analyzer. Describe the scene in English, including the main subjects, actions, setting, and notable objects. Be concise (2-3 sentences).".to_string(),
user_prompt: Some("Describe this scene.".to_string()),
max_tokens: 300,
});
lang_map.insert("zh-TW".to_string(), PromptTemplate {
system_prompt: "你是專業的場景分析師。請用繁體中文描述場景,包括主要對象、動作、場景設置和顯著物體。請簡潔(2-3句)。".to_string(),
user_prompt: Some("描述這個場景。".to_string()),
max_tokens: 300,
});
lang_map.insert("ja".to_string(), PromptTemplate {
system_prompt: "あなたはプロフェッショナルなシーンアナライザーです。日本語でシーンを説明し、主要な被写体、アクション、設定、目立つ物体を含めてください。簡潔に(2-3文)記述してください。".to_string(),
user_prompt: Some("このシーンを説明してください。".to_string()),
max_tokens: 300,
});
lang_map
});
templates.insert("analyze_frame".to_string(), {
let mut lang_map = HashMap::new();
lang_map.insert("en".to_string(), PromptTemplate {
system_prompt: "You are a professional video frame analyst. Describe what you see in this frame in English, including people, objects, actions, and the scene setting. Be detailed but concise.".to_string(),
user_prompt: None,
max_tokens: 1024,
});
lang_map.insert("zh-TW".to_string(), PromptTemplate {
system_prompt: "你是專業的影片畫面分析師。請用繁體中文描述畫面中的內容,包括人物、物體、動作和場景設置。請詳細但簡潔。".to_string(),
user_prompt: None,
max_tokens: 1024,
});
lang_map.insert("ja".to_string(), PromptTemplate {
system_prompt: "あなたはプロフェッショナルなビデオフレームアナリストです。日本語でこのフレームに見えるものを説明してください。人物、物体、アクション、シーン設定を含めてください。詳細かつ簡潔に記述してください。".to_string(),
user_prompt: None,
max_tokens: 1024,
});
lang_map
});
templates
});
pub fn get_system_prompt(module: &str, language: &str) -> String {
PROMPT_TEMPLATES
.get(module)
.and_then(|lang_map| lang_map.get(language))
.or_else(|| {
PROMPT_TEMPLATES
.get(module)
.and_then(|lang_map| lang_map.get("en"))
})
.map(|template| template.system_prompt.clone())
.unwrap_or_else(|| {
tracing::warn!(
"No prompt template found for module '{}' with language '{}', using default",
module,
language
);
format!(
"You are a helpful assistant. Respond in {}.",
match language {
"zh-TW" => "Traditional Chinese",
"ja" => "Japanese",
_ => "English",
}
)
})
}
pub fn get_user_prompt(module: &str, language: &str) -> Option<String> {
PROMPT_TEMPLATES
.get(module)
.and_then(|lang_map| lang_map.get(language))
.or_else(|| {
PROMPT_TEMPLATES
.get(module)
.and_then(|lang_map| lang_map.get("en"))
})
.and_then(|template| template.user_prompt.clone())
}
pub fn get_max_tokens(module: &str, language: &str) -> u32 {
PROMPT_TEMPLATES
.get(module)
.and_then(|lang_map| lang_map.get(language))
.or_else(|| {
PROMPT_TEMPLATES
.get(module)
.and_then(|lang_map| lang_map.get("en"))
})
.map(|template| template.max_tokens)
.unwrap_or(300)
}
pub fn get_prompt_template(module: &str, language: &str) -> Option<PromptTemplate> {
PROMPT_TEMPLATES
.get(module)
.and_then(|lang_map| lang_map.get(language))
.cloned()
}
pub fn list_available_modules() -> Vec<String> {
PROMPT_TEMPLATES.keys().cloned().collect()
}
pub fn list_available_languages(module: &str) -> Vec<String> {
PROMPT_TEMPLATES
.get(module)
.map(|lang_map| lang_map.keys().cloned().collect())
.unwrap_or_default()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_get_system_prompt() {
let prompt = get_system_prompt("agent_search", "en");
assert!(prompt.contains("professional video analysis assistant"));
let prompt_zh = get_system_prompt("agent_search", "zh-TW");
assert!(prompt_zh.contains("專業的影片分析助手"));
let prompt_ja = get_system_prompt("agent_search", "ja");
assert!(prompt_ja.contains("プロフェッショナルなビデオ分析アシスタント"));
}
#[test]
fn test_fallback_to_english() {
let prompt = get_system_prompt("agent_search", "unknown");
assert!(prompt.contains("professional video analysis assistant"));
}
#[test]
fn test_get_max_tokens() {
let tokens = get_max_tokens("agent_search", "en");
assert_eq!(tokens, 1024);
let tokens_vlm = get_max_tokens("vlm_summary", "en");
assert_eq!(tokens_vlm, 900);
}
#[test]
fn test_list_modules() {
let modules = list_available_modules();
assert!(modules.contains(&"agent_search".to_string()));
assert!(modules.contains(&"vlm_summary".to_string()));
}
}